Goffi and GCD
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 412 Accepted Submission(s): 34
Problem Description
Goffi is doing his math homework and he finds an equality on his text book: gcd(n−a,n)×gcd(n−b,n)=nk .
Goffi wants to know the number of ( a,b ) satisfy the equality, if n and k are given and 1≤a,b≤n .
Note: gcd(a,b) means greatest common divisor of a and b .
Input
Input contains multiple test cases (less than 100). For each test case, there's one line containing two integers n and k ( 1≤n,k≤109 ).
Output
For each test case, output a single integer indicating the number of ( a,b ) modulo 109+7 .
Sample Input
2 1 3 2
Sample Output
2 1HintFor the first case, (2, 1) and (1, 2) satisfy the equality.
但是自己没有想到。。
用容斥搞的。。
/***********************************************\
|Author: YMC
|Created Time: 2014/8/24 19:43:10
|File Name: C.cpp
|Description:
\***********************************************/
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <string>
#include <cstring>
#include <algorithm>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#define L(rt) (rt<<1)
#define R(rt) (rt<<1|1)
#define mset(l,n) memset(l,n,sizeof(l))
#define rep(i,n) for(int i=0;i<n;++i)
#define maxx(a) memset(a, 0x3f, sizeof(a))
#define zero(a) memset(a, 0, sizeof(a))
#define srep(i,n) for(int i = 1;i <= n;i ++)
#define MP make_pair
const int inf=0x3f3f3f3f ;
const double eps=1e-8 ;
const double pi=acos (-1.0);
typedef long long ll;
#define mod 1000000007ll
using namespace std;
int da[100000];
map<int,int> ma;
int cnt,t,tt;
ll ans;
int phi(int n){
int ret = 1,i;
for(int i=2;i*i<=n;++i){
if(n%i==0){
n/=i;ret*=(i-1);
while(n%i == 0) n/=i,ret*=i;
}
}
if(n > 1) ret*=n-1;
return ret;
}
int main() {
int n,k;
//freopen("input.txt","r",stdin);
while(cin>>n>>k){
if(n == 1){
cout<<1<<endl;
continue;
}
if(k > 2){
if(n == 1){
cout<<1<<endl;
continue;
} else {
cout<<0<<endl;
continue;
}
}
if(k == 2){ //不一定
cout<<1<<endl;
continue;
}
if(k == 1){
cnt = 0;
ma.clear();
for(int i=1;i*i<=n;++i){
if(n % i == 0) {
da[cnt ++] = i;
da[cnt ++] = n/i;
}
if(i*i == n) cnt--;
}
sort(da,da+cnt);
for(int i=cnt-1;i>=0;--i){
t = da[i];
ma[t] = n/t;
for(int j=i+1;j<cnt;++j){
if(da[j] % da[i] == 0) ma[da[i]] -= ma[da[j]];
}
}
/*rep(i,cnt){
cout<<da[i]<<" "<<ma[da[i]]<<endl;
}cout<<endl;*/
ma[n] = 1;
ans = 0;
for(int i=0;i<cnt;++i){
t = da[i];
tt = n/da[i];
ans += ma[t]*ma[tt];
ans %= mod;
}
cout<<ans<<endl;
continue;
}
}
return 0;
}