HDOJ 题目3501 Calculation 2(欧拉函数)

来源:互联网 发布:2016淘宝seo搜索优化 编辑:程序博客网 时间:2024/05/16 02:41

Calculation 2

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


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
340
 

Sample Output
02
 

Author
GTmac
 

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

Recommend
zhouzeyong   |   We have carefully selected several similar problems for you:  3507 3506 3505 3504 3499 
 欧拉函数是算小于n的且与n互素的个数,当n与a互斥时,n必定与n-a互斥,所以总共会有eular(n)/2对,乘以n就是互素的数的和,前n-1项减去就行
ac代码
#include<stdio.h>#include<string.h>#define mod 1000000007__int64 eular(__int64 n){__int64 ans=n,i;for(i=2;i*i<=n;i++){if(n%i==0){ans-=ans/i;while(n%i==0)n/=i;}}if(n>1){ans-=ans/n;}return ans;}int main(){__int64 n;while(scanf("%I64d",&n)!=EOF,n){printf("%I64d\n",(n*(n-1)/2-eular(n)*n/2)%mod);}}


0 0
原创粉丝点击