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

W - Calculation 2 (欧拉函数的扩展)

时间:2018-08-05 11:49:38      阅读:127      评论:0      收藏:0      [点我收藏+]

标签:prime   clu   which   test   font   代码   integer   c++   cal   

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*(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 }

W - Calculation 2 (欧拉函数的扩展)

标签:prime   clu   which   test   font   代码   integer   c++   cal   

原文地址:https://www.cnblogs.com/acgoto/p/9424726.html

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