hdu 2841 Visible Trees 容斥原理
2016-05-21 20:11
337 查看
Visible Trees
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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)
[align=left]Output[/align]
For each test case output one line represents the number of trees Farmer Sherlock can see.
[align=left]Sample Input[/align]
2
1 1
2 3
[align=left]Sample Output[/align]
1
5
[align=left]Source[/align]
2009 Multi-University Training Contest 3 - Host by WHU
题意:一个人站在(0,0)的位置,如果到两个点那个人的位置的斜率相等则无法看到远处的那个点;
思路:找出(1-m)与i互质的个数 1<=i<=n;复杂度o(nsqrt(n));
#include<bits/stdc++.h> using namespace std; #define ll __int64 #define inf 1e18 ll p[100],flag,x,ans; ll getp(ll x) { flag=0; for(ll i=2;i*i<=x;i++) { if(x%i==0) { p[flag++]=i; while(x%i==0) x/=i; } } if(x!=1) p[flag++]=x; } ll gcd(ll x,ll y) { return y==0?x:gcd(y,x%y); } void dfs(ll lcm,ll step,ll pos) { if(lcm>x)return; if(pos==flag) { if(step&1) ans+=x/lcm; else ans-=x/lcm; return; } dfs(lcm,step,pos+1); dfs(lcm/gcd(lcm,p[pos])*p[pos],step+1,pos+1); } int main() { ll y,z,i,t; int T; scanf("%d",&T); while(T--) { scanf("%I64d%I64d",&x,&y); ans=0; for(i=1;i<=y;i++) { getp(i); dfs(1,1,0); } printf("%I64d\n",ans); } return 0; }
相关文章推荐
- ARM Linux S3C2440之ADC驱动实现
- 九度 OJ 1046:求最大值
- 【玩转常州app】上线啦
- Java-JSSE-SSL/TLS编程代码实例-双向认证
- 控件篇
- 屏幕截图实现
- 多重部分和问题
- 【bzoj 1119】 [POI2009] SLO(置换群)
- Java不同类型密钥库之PKCS12和JCEKS
- 输出双螺旋矩阵
- 准确率、召回率、F-score——信息检索、分类、推荐系统评估标准
- Bestcoder 大数求余
- python str.title( )和str.istitle( )
- 高效计算余数
- Markdown 使用简单介绍
- 51nod B君的圆锥(数学)
- mysql5.7.12 zip免安装版本配置
- BZOJ 2330 [SCOI 2011] 糖果 差分约束
- BSG白山极客挑战赛题解 E 【二分+树状数组】
- 纯JS实现slideToggle动画,慢慢下拉打开