【数论-莫比乌斯】hdu 2841 Visible Trees
2017-08-04 12:23
330 查看
Visible Trees
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3326 Accepted Submission(s): 1449
[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
题意:给定你一个n*m的矩阵,每棵树都在这个矩阵的格点上,问你在(0,0)点可以看到多少个树?
思路:如果横纵坐标的gcd为1的话,可以看到这棵树,莫比乌斯反演公式变形,求1~n,1~m这两个区间gcd为1的数对;
if(n>m) swap(n,m) ,
;
kuangbin的模板代码不错!
代码:
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; typedef long long ll; const int N=100100; bool check[N+10]; int prime[N+10]; int mu[N+10]; void Moblus() { memset(check,false,sizeof(check)); mu[1]=1; int tot=0; for(int i=2;i<=N;i++) { if(!check[i]) { prime[tot++]=i; mu[i]=-1; } for(int j=0;j<tot;j++) { if(i*prime[j]>N) break; check[i*prime[j]]=true; if(i%prime[j]==0) { mu[i*prime[j]]=0; break; } else { mu[i*prime[j]]=-mu[i]; } } } } int sum[N+10]; ll solve(int n,int m) { ll ans=0; if(n>m) swap(n,m); for(int i=1,la=0;i<=n;i=la+1) { la=min(n/(n/i),m/(m/i)); ans+=(ll)(sum[la]-sum[i-1])*(n/i)*(m/i); } return ans; } int main() { Moblus(); sum[0]=0; for(int i=1;i<N;i++) sum[i]=sum[i-1]+mu[i]; int t; scanf("%d",&t); while(t--) { int n,m; scanf("%d%d",&n,&m); ll ans=solve(n,m); printf("%I64d\n",ans); } return 0; }
相关文章推荐
- HDU 2841 Visible Trees 数论+容斥原理
- HDU 2841 Visible Trees (数论,容斥原理)
- HDU 2841 Visible Trees - 莫比乌斯 /容斥原理
- HDU 2841 Visible Trees(容斥原理,数论)
- HDU 2841 Visible Trees(数论)
- HDU 2841 Visible Trees(数论)
- HDU 2841 Visible Trees 数论+容斥原理
- hdu 2841 Visible Trees【容斥原理】
- [hdu 5072]Coprime 数论-莫比乌斯反演
- hdu 2841 Visible Trees (容斥原理)
- hdu 2841 Visible Trees&& hdu 1695 GCD(容斥定理+数论)
- [思维+容斥原理] hdu 2841 Visible Trees
- HDU 2841 Visible Trees 解题报告(筛 or 容斥原理)
- HDU 2841 Visible Trees (容斥原理+素因子分解)
- HDU 2841 Visible Trees(欧拉+容斥)
- hdu 2841 Visible Trees 容斥原理
- hdu 2841 Visible Trees(容斥原理)
- hdu 2841 Visible Trees (容斥原理)
- [数论][莫队][莫比乌斯反演] hdu 4676 Sum Of Gcd
- HDU 2841-Visible Trees(容斥原理)