题意:就是给定一个坐标(n,m),求(1,1)到(n,m)区间内x与y互质的坐标数。
思路:利用容斥从2到n,遍历与m互质的个数。
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2841
View Code
1 #include <iostream> 2 #include <cstdio> 3 #include <cmath> 4 #include <cstdlib> 5 #include <cstring> 6 #include <string> 7 #include <algorithm> 8 using namespace std; 9 #define LL __int64 10 #define N 100005 11 12 int t,n,m; 13 LL prime[N]; 14 15 int dfs(LL x,LL n){ 16 int ct=0; 17 for(int i=2;i*i<=x;i++) 18 if(x%i==0){ 19 prime[ct++]=i; 20 while(x%i==0) x/=i; 21 } 22 if(x>1) prime[ct++]=x; 23 LL sum=0; 24 for(int i=1;i<1<<ct;i++){ 25 LL val=1,k=0; 26 for(int j=0;j<ct;j++){ 27 if(i&1<<j){ 28 val*=prime[j]; 29 k++; 30 } 31 } 32 if(k&1) sum+=n/val; 33 else sum-=n/val; 34 } 35 return n-sum; 36 } 37 38 int main(){ 39 40 // freopen("data.in","r",stdin); 41 // freopen("data.out","w",stdout); 42 43 scanf("%d",&t); 44 while(t--){ 45 scanf("%d%d",&n,&m); 46 LL sum=m; 47 for(LL i=2;i<=n;i++) 48 sum+=dfs(i,m); 49 printf("%I64d\n",sum); 50 } 51 return 0; 52 }