hdoj-3501-Calculation 2
2016-08-28 11:39
288 查看
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
求1~n里面比n小,但是与n不互素的数的总和
利用公式可求
利用欧拉函数即可求解,1~n比n小且与n互素的数的总和为 sum(n) = n * phi(n) / 2;那么可以先求出1~n-1的总和,然后 减去sum(n)即可。
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
求1~n里面比n小,但是与n不互素的数的总和
利用公式可求
利用欧拉函数即可求解,1~n比n小且与n互素的数的总和为 sum(n) = n * phi(n) / 2;那么可以先求出1~n-1的总和,然后 减去sum(n)即可。
#include<cstdio> #include<cstring> #include<algorithm> #include<iostream> #include<cmath> using namespace std; const int mod=1000000007; typedef long long ll; ll phi(int n) { int m=(int)sqrt(n+0.5); int ans=n; for(int i=2;i<=m;i++) if(n%i==0) { ans=ans/i*(i-1); while(n%i==0) n/=i; } if(n>1) ans=ans/n*(n-1); return ans; } int main() { ll n; while(scanf("%lld",&n)!=EOF) { if(n==0) break; ll sum=n*(n-1)/2; ll res=sum-(n*phi(n)/2); printf("%I64d\n",res%mod); } return 0; }
相关文章推荐
- hdoj 3501 Calculation 2
- HDOJ 题目3501 Calculation 2(欧拉函数)
- hdoj-3501-Calculation 2-欧拉函数
- hdoj-【3501 Calculation 2】
- 【欧拉函数】 HDOJ 3501 Calculation 2
- hdoj 3501 Calculation 2(欧拉函数)
- HDOJ 3501 Calculation 2
- hdoj 3501 Calculation 2(欧拉函数的延伸)
- HDOJ 3501 Calculation 2
- hdoj Calculation 2 3501 (欧拉函数)
- hdoj 3501 Calculation 2 【欧拉函数】
- hdu 3501 Calculation 2(<n且与n不互质的数之和)
- HDOJ 1202 The calculation of GPA
- HDU 3501 Calculation 2(容斥原理、求[1,n - 1]中和n不互素的数字之和)
- hdu 3501 Calculation 2(数论:欧拉定理)
- [HDU]3501 Calculation 2 [欧拉函数之求和]
- HDU 3501 Calculation 2 (欧拉函数||容斥原理)
- hdu 3501 Calculation 2(<n且与n不互质的数之和)
- HDU 3501 Calculation 2
- HDOJ 1202 The calculation of GPA