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 = n;
11     for ( int i = 2; i * i <= n; i++ )
12     {
13         if ( n % i == 0 )
14         {
15             ans = ans / i * ( i - 1 );
16             do
17             {
18                 n = n / i;
19             }
20             while ( n % i == 0 );
21         }
22     }
23     if ( n > 1 )
24     {
25         ans = ans / n * ( n - 1 );
26     }
27     return ans;
28 }
29
30 int main ()
31 {
32     int t;
33     scanf("%d", &t);
34     while ( t-- )
35     {
36         int a, m;
37         scanf("%d%d", &a, &m);
38         ll res = 0;
39         for ( int i = 1; i * i <= a; i++ )
40         {
41             if ( a % i ) continue;
42             if ( i >= m )
43             {
44                 res += euler_phi( a / i );
45             }
46             if ( i * i == a ) continue;
47             if ( a / i >= m )
48             {
49                 res += euler_phi( i );
50             }
51         }
52         printf("%I64d\n", res);
53     }
54     return 0;
55 }
时间: 2024-10-08 21:06:05

hdu 2588 欧拉函数的相关文章

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 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: g

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 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