hdu 4983(欧拉函数)

Goffi and GCD

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 992    Accepted Submission(s): 336

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
1

Hint

For the first case, (2, 1) and (1, 2) satisfy the equality.

Source

BestCoder Round #6

题意:求使得成立的(a,b)的个数.

首先,我们可以知道 gcd(n,a)<=n,所以当 k>2 的时候没有这样的(a,b)

然后当 k==2 的时候我们只有一个这样的组合 (a,b) = (n,n)

接下来考虑 k=1的情况:当 k = 1时 ,gcd(n-a,n) = gcd(a,n) (这里源自gcd的变换公式) 整个式子就转换成了 gcd(a,n)*gcd(b,n)=n

设 gcd(a,n) 为 x,那么 gcd(a/x,n/x)=1,a/x 与 n/x 互质,利用欧拉函数 phi(x) 可以得到 a的个数,同理可以得到b的个数.并且x也是 n 的因子,所以在求解n的因子的同时就可以将(a,b)求出来了。

这里总结一个公式:如果d是n的一个约数,那么1<=i<=n中 gcd(i,n) = d的个数是phi(n/d),即n/d的欧拉函数

#include <stdio.h>
#include <math.h>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <vector>
using namespace std;
typedef long long LL;
const LL mod = 1000000007;
LL phi(LL x)
{
    LL ans=x;
    for(LL i=2; i*i<=x; i++)
        if(x%i==0)
        {
            ans=ans/i*(i-1);
            while(x%i==0) x/=i;
        }
    if(x>1)
        ans=ans/x*(x-1);
    return ans;
}
LL n,k;
int main()
{
    while(scanf("%lld%lld",&n,&k)!=EOF)
    {
        if(n==1) {
            printf("1\n");
            continue;
        }
        if(k>1)
        {
            if(k==2)
                printf("1\n");
            else printf("0\n");
            continue;
        }
        LL sum = 0;
        for(LL i=1; i*i<=n; i++)
        {
            if(n%i==0)
            {
                if(i*i==n) sum = (sum+phi(i)*phi(i))%mod;
                else {
                    sum = (sum+2*phi(i)*phi(n/i))%mod;
                }
            }
        }
        printf("%lld\n",sum);
    }
    return 0;
}
时间: 2024-08-14 01:30:36

hdu 4983(欧拉函数)的相关文章

hdu 6390 欧拉函数+容斥(莫比乌斯函数) GuGuFishtion

http://acm.hdu.edu.cn/showproblem.php?pid=6390 题意:求一个式子 题解:看题解,写代码 第一行就看不出来,后面的sigma公式也不会化简.mobius也不会 就自己写了个容斥搞一下(才能维持现在的生活) //别人的题解https://blog.csdn.net/luyehao1/article/details/81672837 #include <iostream> #include <cstdio> #include <cstr

hdu 3307(欧拉函数+好题)

Description has only two Sentences Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 1071    Accepted Submission(s): 323 Problem Description an = X*an-1 + Y and Y mod (X-1) = 0.Your task is to cal

HDU 1286 欧拉函数。

[科普]什么是BestCoder?如何参加? 找新朋友 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 8077    Accepted Submission(s): 4250 Problem Description 新年快到了,"猪头帮协会"准备搞一个聚会,已经知道现有会员N人,把会员从1到N编号,其中会长的号码是N号,凡是

hdu 2824(欧拉函数)

The Euler function Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5235    Accepted Submission(s): 2225 Problem Description The Euler function phi is an important kind of function in number theo

hdu 2588 欧拉函数

两个数的gcd为d,其实就是将这两个数同除以d后互质.本题中n是固定的,x是小于等于n的数,很容易想到可以枚举n的约数求出(n除以约数)的欧拉函数的和即是答案. 1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 using namespace std; 5 6 typedef long long ll; 7 8 int euler_phi( int n ) 9 { 10 int ans =

hdu 3501 欧拉函数

容易想到容斥原理,但是结合欧拉函数的公式,我们得到: 小于n且与n互质的数的和为:n * phi(n) / 2 于是问题迎刃而解. 1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 using namespace std; 5 6 typedef long long ll; 7 const int MOD = 1000000007; 8 9 int euler_phi( int n ) 10

hdu 1787(欧拉函数)

GCD Again Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2874    Accepted Submission(s): 1240 Problem Description Do you have spent some time to think and try to solve those unsolved problem af

hdu 4002 欧拉函数

题意:求1-n内最大的x/phi(x) 通式:φ(x)=x*(1-1/p1)*(1-1/p2)*(1-1/p3)*(1-1/p4)…..(1-1/pn),其中p1, p2……pn为x的所有质因数,x是不为0的整数.φ(1)=1(唯一和1互质的数就是1本身). 因此含质因数最多的即为所求,打表求出前n个积,之后找到比自己小的最大积 大数打表 1 #include<cstdio> 2 #include<iostream> 3 #include<algorithm> 4 #i

hdu 4002 欧拉函数 2011大连赛区网络赛B

题意:求1-n内最大的x/phi(x) 通式:φ(x)=x*(1-1/p1)*(1-1/p2)*(1-1/p3)*(1-1/p4)…..(1-1/pn),其中p1, p2……pn为x的所有质因数,x是不为0的整数.φ(1)=1(唯一和1互质的数就是1本身). 因此含质因数最多的即为所求,打表求出前n个积,之后找到比自己小的最大积 大数打表 2015-07-27:到此一游 1 #include<cstdio> 2 #include<iostream> 3 #include<al