Visible Trees
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 535 Accepted Submission(s): 197
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
题意:求可以看见多少个格子
题解:实质是求公约数为1对数(有顺序的),用容斥定理即可
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<stdio.h>
#include<string.h>
struct link{
int next,data;
}p[280008];
int head[100003],prime[100003],flag[100003];
int cou,all;
void add(int x,int y)
{
p[all].next=head[x];
p[all].data=y;
head[x]=all++;
}
void init()
{
int i,j,temp;
cou=all=0;
memset(head,-1,sizeof(head));
memset(flag,0,sizeof(flag));
for(i=2;i<=100000;i++)
{
if(!flag[i])
{
prime[cou++]=i;
for(j=i+i;j<=100000;j+=i) flag[j]=1;
}
}
for(i=1;i<=100000;i++)
{
temp=i;
for(j=0;prime[j]*prime[j]<=temp;j++)
{
if(temp%prime[j]==0)
{
add(i,prime[j]);
while(temp%prime[j]==0) temp/=prime[j];
}
}
if(temp>1) add(i,temp);
}
}
__int64 dfs(int now,int x)
{
__int64 res=0,i;
for(i=now;i!=-1;i=p[i].next)
res+=x/p[i].data-dfs(p[i].next,x/p[i].data);
return res;
}
int main()
{
int x,y,t,i;
__int64 res;
init();
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&x,&y);
if(x>y) x^=y^=x^=y;
for(res=y,i=2;i<=x;i++) res+=y-dfs(head[i],y);
printf("%I64d\n",res);
}
return 0;
}