****HDU
来源:互联网 发布:稀疏编码算法 详解 编辑:程序博客网 时间:2024/06/06 07:21
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.
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
Hint
题意
一条直线上的点不能被看到 求能看到的树的数目
题解:
可以发现gcd(x,y)!=1的点都不能被看到 所以用容斥定理来做
AC代码
#include <cstdio>#include <iostream>#include <map>#include <set>#include <vector>#include <cstring>#include <algorithm>using namespace std;typedef long long LL;const int N = 1e5+5;int prime[N],phi[N];vector<int > yinzi[N];int tot = 0;void init(){ phi[1] = 1; for (int i = 2; i < N; ++i){ if (!phi[i]){ phi[i]=i-1; prime[tot++]=i; } for (int j = 0; j < tot&&1ll*prime[j]*i<N; ++j){ if (i%prime[j]) phi[i*prime[j]] = phi[i]*(prime[j]-1); else { phi[i*prime[j]] = phi[i]*prime[j]; break; } } } for (int i = 2; i < N; ++i){ int tmp = i; for (int j = 0;(LL)prime[j]*prime[j]<=tmp; ++j){ if (tmp%prime[j]==0){ yinzi[i].push_back(prime[j]); tmp/=prime[j]; while(tmp%prime[j]==0) tmp/=prime[j]; } if (tmp == 1) break; } if (tmp>1) yinzi[i].push_back(tmp); }}LL dfs(int x,int n,int now){ LL res = 0; for (int i = x; i < yinzi[now].size(); ++i){ res += n/yinzi[now][i]-dfs(i+1,n/yinzi[now][i],now); } return res;}int main(){ init(); int t; scanf("%d",&t); while (t--){ int n,m; scanf("%d%d",&n,&m); if (n>m) swap(n,m); LL ans = 0; /*欧拉函数是小于n所有与n互质的数目 这个包括1 但这一题1 3互质与 3 1互质是两个点*/ for (int i = 1; i <= n; ++i){ if (i!=1) ans+=phi[i]*2; else ans+=phi[i]; }/*对于求1~n与1~n之间的互质的种数(与点对数不同 因为gcd(x,y)==1和gcd(y,x)==1算是一种) 所以要看清问的是什么 虽然欧拉函数是问的小于n所有与n互质的数目 但由于前面所说 所以1~n欧拉函数求和即可算出互质的种数*/ for (int j = n+1; j <= m; ++j){ ans += n-dfs(0,n,j); } printf("%lld\n",ans); } return 0;}/*比如对于phi[6] = 2 1 6互质 5 6互质 题目问题是求互质的点对数 那么除了1 6 实际还有6 1这个点 1 1点只有一对*/
纯容斥写法 之前一直wa在1e5这个边界上
#include <cstdio>#include <map>#include <cmath>#include <cstring>#include <vector>#include <algorithm>using namespace std;typedef long long LL;const int N = 1e5+10;int phi[N];int prime[N];vector<int> yinzi[N];int tot = 0;void init(){ phi[1] = 1; for (int i = 2; i < N; ++i){ if (!phi[i]){ prime[tot++] = i; phi[i] = i-1; } for (int j = 0;j < tot&&1ll*i*prime[j]<N; ++j){ if (i%prime[j]) phi[i*prime[j]] = phi[i]*(prime[j]-1); else { phi[i*prime[j]] = phi[i]*prime[j]; break; } } } for (int i = 1; i <= 1e5; ++i){ int tmp = i; for (int j = 0; (LL)prime[j]*prime[j]<=tmp; ++j ){ if (tmp%prime[j]==0){ yinzi[i].push_back(prime[j]); while (tmp%prime[j]==0) tmp/=prime[j]; } if (tmp==1) break; }if (tmp>1) yinzi[i].push_back(tmp); }}LL ronc(int a,int b,int now){ LL res = 0; for (int i = a; i < yinzi[now].size(); ++i){ res += b/yinzi[now][i]-ronc(i+1,b/yinzi[now][i],now); } return res;}int main(){ init(); int t; scanf("%d",&t); while (t--){ LL n,m; scanf("%lld%lld",&n,&m); if (n>m) swap(n,m); LL ans = 0; for (int i = 1;i <= m; ++i){ ans += n-ronc(0,n,i); } printf("%lld\n",ans); } return 0;}
??这题还可以莫比乌斯反演啊啊啊
阅读全文
0 0
- hdu
- hdu
- HDU
- hdu ()
- hdu
- hdu
- HDU
- HDU
- hdu
- hdu
- HDU
- Hdu
- hdu
- hdu-
- hdu
- hdu
- hdu
- HDU
- ubuntu 命令行卸载并清理软件
- python成员访问
- win7下无新建文件夹
- Angular(4)
- 错排
- ****HDU
- 异常:org.hibernate.hql.ast.QuerySyntaxException: EmpModel is not mapped [from EmpModel where name = ?
- FFMPEG学习【libavutil】:数据结构(四)
- 杭电problem1004- Let the Balloon Rise
- 只为成功找方法,不为失败找借口! javaweb学习总结(六)——Servlet开发(二)
- python抽象
- Install MySQL 5.7/8.0 on Fedora 26/25, CentOS/RHEL 7.3/6.9
- python学习(三)
- (2)初识看板