标签:prime clu which test font 代码 integer c++ cal
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.
For each test case, you should print the sum module 1000000007 in a line.
3 4 0
0 2
解题思路:求n内与n互质的所有数之和,公式:n*(n-1)/2-n*Euler(n)/2。
AC代码:
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long LL; 4 const int mod=1000000007; 5 int Euler(int x){ 6 int r=x; 7 for(int i=2;i*i<=x;i++){ 8 if(x%i==0){ 9 r=r/i*(i-1); 10 while(x%i==0)x/=i; 11 } 12 } 13 if(x>1)r=r/x*(x-1); 14 return r; 15 } 16 int main(){ 17 int n; 18 while(cin>>n&&n){ 19 LL sum=(LL)n*(n-1)/2-(LL)n*Euler(n)/2; 20 cout<<sum%mod<<endl; 21 } 22 return 0; 23 }
标签:prime clu which test font 代码 integer c++ cal
原文地址:https://www.cnblogs.com/acgoto/p/9424726.html