HDU——T3501 Calculation 2

http://acm.hdu.edu.cn/showproblem.php?pid=3501

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

Problem Description

Given a positive integer N, your task is to calculate the sum of the positive integers less than N which are not coprime to N. A is said to be coprime to B if A, B share no common positive divisors except 1.

Input

For each test case, there is a line containing a positive integer N(1 ≤ N ≤ 1000000000). A line containing a single 0 follows the last test case.

Output

For each test case, you should print the sum module 1000000007 in a line.

Sample Input

3
4
0

Sample Output

0
2

Author

GTmac

Source

2010 ACM-ICPC Multi-University Training Contest(7)——Host by HIT

欧拉函数得和==phi(n)*n/2;

md 还是见识少。。

 1 #include <algorithm>
 2 #include <cstdio>
 3
 4 using namespace std;
 5
 6 #define LL long long
 7 const LL mod(1000000007);
 8 const LL N(1000000000+5);
 9 LL x;
10
11 LL phi(LL x)
12 {
13     LL ret=1;
14     for(LL i=2;i*i<=x;i++)
15         if(x%i==0)
16         {
17             x/=i;
18             ret*=i-1;
19             for(;x%i==0;)
20                 ret*=i,x/=i;
21         }
22     if(x>1) ret*=x-1;
23     return ret;
24 }
25
26 int main()
27 {
28     for(;scanf("%lld",&x)&&x;)
29     {
30         LL zz=(x-1)*x>>1;
31         printf("%lld\n",(zz-(x*phi(x)>>1))%mod);
32     }
33     return 0;
34 }
时间: 2024-08-14 07:09:16

HDU——T3501 Calculation 2的相关文章

hdu 3501 Calculation 2 (欧拉函数)

题目 题意:求小于n并且 和n不互质的数的总和. 思路:求小于n并且与n互质的数的和为:n*phi[n]/2 . 若a和n互质,n-a必定也和n互质(a<n).也就是说num必定为偶数.其中互质的数成对存在.其和为n. 公式证明: 反证法:如果存在K!=1使gcd(n,n-i)=k,那么(n-i)%k==0而n%k=0那么必须保证i%k=0k是n的因子,如果i%k=0那么gcd(n,i)=k,矛盾出现; 所以先求出1--n-1 的和, 再用这个和 减去 上面公式求出来的值. 欧拉函数phi(m)

HDU 3501 Calculation 2(欧拉函数的应用)

HDU 3501 Calculation 2 大意:求1~n之间与n不互质的数的总和. 思路:欧拉函数的应用:先用欧拉函数求出与n互质的总数m,计算m个数的总和,用n的总和减去m的总和就是想要的结果. 1 #include <stdio.h> 2 #define LL __int64 3 4 int eular(int n){ 5 int ret = 1; 6 for(int i = 2; i*i <= n;i++) 7 if(n%i == 0){ 8 n /= i, ret *= i-

hdu 1202The calculation of GPA (简单题+坑)

The calculation of GPA Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 18748    Accepted Submission(s): 4331 Problem Description 每学期的期末,大家都会忙于计算自己的平均成绩,这个成绩对于评奖学金是直接有关的.国外大学都是计算GPA(grade point a

HDU 3501 Calculation 2 (欧拉函数应用)

Calculation 2 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2989    Accepted Submission(s): 1234 Problem Description Given a positive integer N, your task is to calculate the sum of the posit

HDU 3501 Calculation 2

传送门:http://acm.hdu.edu.cn/showproblem.php?pid=3501 解题思路: 小于n与n互质的数的和为Eular(n)×n/2 实现代码: #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int MOD=1000000007 ; /* 小于n与n互质的数的和为Eu

HDU 3501 Calculation 2(欧拉函数)

Calculation 2 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Description Given a positive integer N, your task is to calculate the sum of the positive integers less than N which are not coprime to N. A is s

hdu 2837 Calculation【欧拉函数,快速幂求指数循环节】

欢迎关注__Xiong的博客: http://blog.csdn.net/acmore_xiong?viewmode=list Calculation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1912    Accepted Submission(s): 413 链接:click me Problem Description A

HDU——3501 Calculation 2

题意: 给定正整数n,求所有小于n的且与n为非互质数的和. 输入 对于每一个测试用例,都有一个包含正整数n(1≤n≤1000000000)的行.包含单个0的行将遵循最后一个测试用例. 输出 对于每个测试用例,您应该在一行中打印求和模1000000007. 思路: 考虑互质的数的一个性质,n与m互质,那么m-n与m也一定互质. 好,我们再来考虑这个题. 让求与n为非互质数的数的和,那么我们是不是就可以先把所有数的和求出来然后再减去与n互质的数的和???!! 好,我们就这样干. 有人又要问了,为什么

【数论-欧拉函数】HDU 3501 Calculation 2 ( 与n不互质的数的和 )

[题目链接]click here~ [题目大意]给定整数n,求与n不互质的数的和,最后mod1e9+7 [解题思路]我们利用欧拉函数和欧几里德定理,if  gcd(n,i)==1 ,则有 gcd(n,n-i)==1 ,可以知道 其中一个若为i则存在一个为n-i 那么二者之和为n  ,这样的一共有eular(n)/2对  故与n互质的所有数的和为 n*eular(n)/2 那么与n不互质的 数就是(n)*(n-1)/2-n*eular(n)/2 [source]2010 ACM-ICPC Mult