码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 3501 Calculation 2(欧拉函数)

时间:2015-07-06 17:19:33      阅读:108      评论:0      收藏:0      [点我收藏+]

标签:

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 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
简单翻译:
输入一个n,求小于n,并且不与n互质的数的和模1000000007的值。
 
解题思路:
欧拉函数,先求出小于n且与n互质的数的和,用总数减去互质的数的和,就得到了不互质的数的和。
 
代码:
技术分享
 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 using namespace std;
 5 const int mod=1000000007;
 6 int main()
 7 {
 8     int n;
 9     while(scanf("%d",&n)!=EOF&&n)
10     {
11         long long temp=n,eular=1;
12         long long ans=(temp*(temp+1)/2)%mod;
13         for(int i=2;i*i<=temp;i++)
14             if(temp%i==0)
15             {
16                 temp/=i;
17                 eular*=i-1;
18                 while(temp%i==0)
19                 {
20                     temp/=i;
21                     eular*=i;
22                 }
23             }
24         if(temp>1) eular*=temp-1;
25         eular%=mod;
26         temp=n;
27         long long w=(eular*temp/2)%mod;
28         ans-=w;
29         ans-=n;
30         while(ans<0) ans+=mod;
31         printf("%lld\n",ans);
32     }
33     return 0;
34 }
Calculation 2

 

HDU 3501 Calculation 2(欧拉函数)

标签:

原文地址:http://www.cnblogs.com/I-love-HLD/p/4624723.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!