Visible Trees
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3131 Accepted Submission(s): 1387
Problem Description
There are many trees forming a m * n grid, the grid starts from (1,1). Farmer Sherlock is standing at (0,0) point. He wonders how many trees he can see.
If two trees and Sherlock are in one line, Farmer Sherlock can only see the tree nearest to him.
If two trees and Sherlock are in one line, Farmer Sherlock can only see the tree nearest to him.
Input
The first line contains one integer t, represents the number of test cases. Then there are multiple test cases. For each test case there is one line containing two integers m and n(1 ≤ m, n ≤ 100000)
Output
For each test case output one line represents the number of trees Farmer Sherlock can see.
Sample Input
2
1 1
2 3
Sample Output
1
5
Source
Recommend
————————————————————————————————————
题目的 意思是从(1,1)开始有个m*n的矩阵每个整点有一颗树 问从(0,0)看过去可以看到多少克树
思路:说白了就是求[1,m],[1,n]有多少对数互质(不互质的肯定会被挡住) 可以枚举m,在n中找有多少互质的数,可以先把枚举的m质因数的分解,然后利用容斥定理计数
防止超时分解的质因数可以先打个表
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<string>
#include<queue>
#include<stack>
#include<map>
#include<set>
using namespace std;
#define LL long long
const int inf=0x3f3f3f3f; vector<LL>p[100010];
int pr[100010];
int a,b,x,y,k;
LL ans; void init()
{
memset(pr,0,sizeof pr);
for(int i=1;i<=100005;i++) p[i].clear();
for(int i=2;i<=100005;i+=2)
p[i].push_back(2);
for(int i=3;i<=100005;i+=2)
{
if(!pr[i])
{
for(int j=i;j<=100005;j+=i)
{
pr[j]=1;
p[j].push_back(i);
} }
}
} void dfs(int no,int pos,int xx,int cnt)
{
if(pos>=p[no].size())
{
if(cnt==0)
return;
if(cnt%2)
{
ans+=y/xx;
}
else
{
ans-=y/xx;
}
return;
}
dfs(no,pos+1,xx*p[no][pos],cnt+1);
dfs(no,pos+1,xx,cnt);
} int main()
{
init();
int T;
int q=1;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&x,&y);
if(x>y) swap(x,y);
LL ans2=0;
for(int i=1; i<=x; i++)
{
ans=0;
dfs(i,0,1,0);
ans2+=y-ans;
}
printf("%lld\n",ans2);
}
return 0;
}