Visible Trees
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2209 Accepted Submission(s): 906
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#include<stdio.h> #include<string.h> #include<math.h> #define N 100010 #define ll long long int p[N]; int q[N]; int k; void getp(int n) { int i,j; k=0; for(i=2;i*i<=n;i++) { if(n%i==0) { p[k++]=i; while(n%i==0) n/=i; } } if(n>1) p[k++]=n; } int solve(int n) { int i,j,kk,t=0; ll sum=0; q[t++]=-1; for(i=0;i<k;i++) { kk=t; for(j=0;j<kk;j++) q[t++]=p[i]*q[j]*-1; } for(i=1;i<t;i++) sum+=n/q[i]; return sum; } int main() { int t,n,m,i,j; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&m); ll ans=n; for(i=2;i<=m;i++) { getp(i); ans+=n-solve(n); } printf("%lld\n",ans); } return 0; }