hdu 4893 Goffi and GCD

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(na,n)×gcd(nb,n)=nk .

Goffi wants to know the number of ( a,b ) satisfy the equality, if n and k are given and 1a,bn .

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 ( 1n,k109 ).
 
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 1
Hint
For 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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值