Visible TreesTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2213 Accepted Submission(s): 908
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
其实感觉这个代码写搓了 但是确实是利用队列的一种方式 重点理解dfs容斥方式
传送门 http://www.cnblogs.com/hsd-/p/5008912.html
#include<bits/stdc++.h> using namespace std; __int64 n; __int64 a,b; __int64 slove( __int64 m,__int64 gg) { __int64 que[1000]; __int64 a[1000]; memset(que,0,sizeof(que)); memset(a,0,sizeof(a)); __int64 sum=0; __int64 t=0; __int64 ss=0; for(__int64 i=2;i*i<=m;i++) { if(m%i==0) { que[ss++]=i; while(m%i==0) m=m/i; } } if(m>1) que[ss++]=m; a[t++]=-1; for(__int64 i=0;i<ss;i++) { int k=t; for(__int64 j=0;j<k;j++) { a[t++]=que[i]*a[j]*(-1); } } for(__int64 i=1;i<t;i++) sum=sum+gg/a[i]; return sum; } int main() { while(scanf("%I64d",&n)!=EOF) { for(__int64 i=1;i<=n;i++) { __int64 re=0; scanf("%I64d %I64d",&a,&b); for(__int64 j=1;j<=a;j++) re+=slove(j,b); printf("%I64d\n",a*b-re); } } return 0; }