Hillan and the girl
Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)Total Submission(s): 598 Accepted Submission(s): 286
Problem Description
“WTF! While everyone has his girl(gay) friend, I only have my keyboard!” Tired of watching others' affair, Hillan burst into scream, which made him decide not to hold it back.
“All right, I am giving you a question. If you answer correctly, I will be your girl friend.” After listening to Hillan, Girl replied, “What is the value of ∑ni=1∑mj=1f(i,j), where f(i,j)=0 if gcd(i,j) is a square number and f(i,j)=1 if gcd(i,j) is not a square number( gcd(i,j) means the greatest common divisor of x and y)?”
But Hillan didn't have enough Intelligence Quotient to give the right answer. So he turn to you for help.
“All right, I am giving you a question. If you answer correctly, I will be your girl friend.” After listening to Hillan, Girl replied, “What is the value of ∑ni=1∑mj=1f(i,j), where f(i,j)=0 if gcd(i,j) is a square number and f(i,j)=1 if gcd(i,j) is not a square number( gcd(i,j) means the greatest common divisor of x and y)?”
But Hillan didn't have enough Intelligence Quotient to give the right answer. So he turn to you for help.
Input
The first line contains an integer
T(1≤T≤10,000)——The number of the test cases.
For each test case, the only line contains two integers n,m(1≤n,m≤10,000,000) with a white space separated.
For each test case, the only line contains two integers n,m(1≤n,m≤10,000,000) with a white space separated.
Output
For each test case, the only line contains a integer that is the answer.
Sample Input
2 1 2333333 10 10
Sample Output
0 33
Hint
In the first test case, obviously $f\left(i,j\right)$ always equals to 0, because $i$ always equals to 1 and $\gcd\left(i,j\right)$ is always a square number(always equals to 1).
Source
Recommend
wange2014
思路:莫比乌斯反演基本公式,G(n)=sigma(n|d)F(d) F(n)=sigma(n|d)u(d/n)G(d)
其中F(x)为gcd(i,j)==x的i,j的对数 G(x)为gcd(i,j)%n==0 的i,j的对数,符合莫比乌斯反演定律
G(x)为(m/i)*(n/i)
ans=n*m -
,
代码
#include<iostream> #include<cmath> using namespace std; #define N 10000007 int prime[N]; int primesize; int flag[N]; int u[N]; inline long long minn(long long a,long long b) { return a<b?a:b; } void get_prime(int x) { u[1]=1; for(int i=2;i<=x;++i) { if(flag[i]==0) { u[i]=-1; prime[primesize++]=i; } for(int j=0;j<primesize;++j) { if(i*prime[j]>x) break; flag[i*prime[j]]=1; if(i%prime[j]==0) break; u[i*prime[j]]=-u[i]; } } } int main() { get_prime(10000000); int t; scanf("%d",&t); int m,n; long long ans; while(t--) { scanf("%d %d",&n,&m); ans=n*m; int d=minn(n,m); for(int i=1;i*i<=d;++i) { for(int cnt=1;cnt<=d/(i*i);++cnt) { ans-=u[cnt]*(n/(i*i*cnt))*(m/(i*i*cnt)); } } printf("%lld\n",ans); } return 0; }