hdu 4983 Goffi and GCD(欧拉函数)

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

 发现自己欧拉函数都给忘记了,所有赶紧补题。。。

1、k!=1时情况很简单,记住将if(k==2 || n==1)这个特判放在if(k>2)的前面,因为这个WA了很久,各种原因自己思考。

2、下面讨论k=1时情况。x=gcd(n-a,n),则n/x=gcd(n-b,n),因为n-a可以取到0...n-1也就是1....n,所以完全可以去掉n-这个限制条件,即gcd(a,n)=x、gcd(b,n)=n/x时个数,因为a<=n,所以gcd(a,n)的个数=u[n/x],u是欧拉函数。所以原式等于sigma(u[n/x]*u[x])其中x是n的约数。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 using namespace std;
 5 #define MOD 1000000007
 6 #define ll long long
 7 ll eular(ll n)
 8 {
 9     ll res=1;
10     for(ll i=2;i*i<=n;i++)
11     {
12         if(n%i==0)
13         {
14             n/=i,res*=i-1;
15             while(n%i==0)
16             {
17                 n/=i;
18                 res*=i;
19             }
20         }
21     }
22     if(n>1) res*=n-1;
23     return res;
24 }
25 ll n,k;
26 int main()
27 {
28     while(scanf("%I64d%I64d",&n,&k)==2)
29     {
30         if(k==2 || n==1)
31         {
32             printf("1\n");
33             continue;
34         }
35         if(k>2)
36         {
37             printf("0\n");
38             continue;
39         }
40
41         ll ans=0;
42         for(ll i=1;i*i<=n;i++)
43         {
44             if(n%i==0)
45             {
46                 if(i*i!=n)
47                   ans=(ans+eular(n/i)*eular(i)*2)%MOD;
48                 else
49                     ans=(ans+eular(n/i)*eular(i))%MOD;
50             }
51         }
52         printf("%I64d\n",ans);
53
54     }
55     return 0;
56 }

时间: 2024-08-06 03:44:58

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

HDU 1695 GCD 欧拉函数+容斥原理+质因数分解

链接:http://acm.hdu.edu.cn/showproblem.php?pid=1695 题意:在[a,b]中的x,在[c,d]中的y,求x与y的最大公约数为k的组合有多少.(a=1, a <= b <= 100000, c=1, c <= d <= 100000, 0 <= k <= 100000) 思路:因为x与y的最大公约数为k,所以xx=x/k与yy=y/k一定互质.要从a/k和b/k之中选择互质的数,枚举1~b/k,当选择的yy小于等于a/k时,可以

HDU 2588 GCD (欧拉函数)

GCD Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1013    Accepted Submission(s): 457 Problem Description The greatest common divisor GCD(a,b) of two positive integers a and b,sometimes writt

hdu 4983 Goffi and GCD(数论)

题目链接:hdu 4983 Goffi and GCD 题目大意:求有多少对元组满足题目中的公式. 解题思路: n = 1或者k=2时:答案为1 k > 2时:答案为0(n≠1) k = 1时:需要计算,枚举n的因子,令因子k=gcd(n?a,n, 那么另一边的gcd(n?b,n)=nk才能满足相乘等n,满足k=gcd(n?a,n)的a的个数即为?(n/s),欧拉有o(n ̄ ̄√的算法 #include <cstdio> #include <cstring> #include

HDU 4983 Goffi and GCD(数论)

HDU 4983 Goffi and GCD 思路:数论题,如果k为2和n为1,那么只可能1种,其他的k > 2就是0种,那么其实只要考虑k = 1的情况了,k = 1的时候,枚举n的因子,然后等于求该因子满足的个数,那么gcd(x, n) = 该因子的个数为phi(n / 该因子),然后再利用乘法原理计算即可 代码: #include <cstdio> #include <cstring> #include <cmath> typedef long long l

HDU 4983 Goffi and GCD(欧拉函数模板)

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)=n^k. 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

hdu 1695 GCD(欧拉函数+容斥原理)

http://acm.hdu.edu.cn/showproblem.php? pid=1695 非常经典的题.同一时候感觉也非常难. 在区间[a,b]和[c,d]内分别随意取出一个数x,y,使得gcd(x,y) = k.问这种(x,y)有多少对.能够觉得a,c均为1,并且gcd(5,7)与gcd(7,5)是同一种. 由于gcd(x,y) = k,那么gcd(x/k,y/k) = 1.也就是求区间[1,b/k]和[1,d/k]内这种(x,y)对使得gcd(x,y) = 1. 为了防止计数反复,首先

hdu 1695 GCD 欧拉函数+容斥

题意:给定a,b,c,d,k x属于[1 , c],y属于[1 , d],求满足gcd(x,y)=k的对数.其中<x,y>和<y,x>算相同. 思路:不妨设c<d,x<=y.问题可以转化为x属于[1,c / k ],y属于[1,d/k ],x和y互质的对数. 那么假如y<=c/k,那么对数就是y从1到c/k欧拉函数的和.如果y>c/k,就只能从[ c/k+1 , d ]枚举,然后利用容斥.详见代码: /****************************

HDU 1695 GCD 欧拉函数+容斥定理

输入a b c d k求有多少对x y 使得x在a-b区间 y在c-d区间 gcd(x, y) = k 此外a和c一定是1 由于gcd(x, y) == k 将b和d都除以k 题目转化为1到b/k 和1到d/k 2个区间 如果第一个区间小于第二个区间 讲第二个区间分成2部分来做1-b/k 和 b/k+1-d/k 第一部分对于每一个数i 和他互质的数就是这个数的欧拉函数值 全部数的欧拉函数的和就是答案 第二部分能够用全部数减去不互质的数 对于一个数i 分解因子和他不互质的数包括他的若干个因子 这个

hdu2588 gcd 欧拉函数

GCD Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1567    Accepted Submission(s): 751 Problem Description The greatest common divisor GCD(a,b) of two positive integers a and b,sometimes writte