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之前与它互质的数的个数

设φ函数为欧拉函数,那么φ(x)=x(1-1/p1)(1-1/p2)(1-1/p3)(1-1/p4)…..(1-1/pn),当中p1, p2……pn为x的

全部质因数(这里说的质因子是种类,不是个数)(比方说12=2*2*3,它的因子都是质数)。x是不为0的整数

那么这道题目就好办了化简上述公式:假设p1....pn是x的质因数

φ(x)=x*((p1-1)/p1)((p2-1)/p2)((p3-1)/p3)((p4-1)/p4)…..((pn-1)/pn)(不用忽略前面另一个x要乘上)

如此以下的代码就是欧拉函数了接下来要说的是,假设我们知道了n之前与它互质的数的和

,那么不互质怎么求。非常easy,前n项和sum(n)=(n+1)*n/2高中的知识点;

接着(不互质的数的和)=sum(n)-(n之前与它互质的数的和)。

那怎样求n之前与它互质的数的和,首先我们应该知道一个定理就是

假设gcd(n,i)=1,那么gcd(n,n-i)=1,如此前n个数中假设i与n互质那么n-i与n也是互质的

如此来,n之前与它互质的数的和=(a1+n-a1)+(a2+n-a2)+(a3+n-a3)+(a4+n-a4)+......(an+n-an)

这不正是(有多少对i与n-i乘以n)就是结果了。而互质的个数我们

又知道了,那么有多少对就是φ(x)/2了,互质的数的和=φ(x)/2*n.

接下来(不互质的数的和)=sum(n)-(n之前与它互质的数的和)就能够求出结果

/*
Author: 2486
Memory: 1408 KB Time: 15 MS
Language: G++ Result: Accepted
VJ RunId: 4178187 Real RunId: 14209894
*/
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
typedef long long LL;
const LL mod=1000000007;
LL n;
LL getOL(LL x) {
LL res=x;
for(int i=2; i<=sqrt(x); i++) {
if(x%i==0) {
res=res/i*(i-1);
while(x%i==0) {
x/=i;
}
}
}
if(x>=2) {
res=res/x*(x-1);
}
return res;
}
int main() {
while(~scanf("%I64d",&n),n) {
LL ans1=n*(n+1)/2-n;//n前,所以n不包含在求和内
LL ans2=getOL(n)*n/2;
printf("%I64d\n",(ans1-ans2)%mod);
}
return 0;
}

Calculation 2-欧拉函数的运用的更多相关文章

  1. HDU3501 Calculation 2 [欧拉函数]

    题目传送门 Calculation 2 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

  2. hdu 3501 Calculation 2 (欧拉函数)

    题目 题意:求小于n并且 和n不互质的数的总和. 思路:求小于n并且与n互质的数的和为:n*phi[n]/2 . 若a和n互质,n-a必定也和n互质(a<n).也就是说num必定为偶数.其中互质 ...

  3. HDU 3501 Calculation 2(欧拉函数)

    Calculation 2 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submi ...

  4. HDU 3501 Calculation 2 (欧拉函数)

    题目链接 题意 : 求小于n的数中与n不互质的所有数字之和. 思路 : 欧拉函数求的是小于等于n的数中与n互质的数个数,这个题的话,先把所有的数字之和求出来,再减掉欧拉函数中所有质数之和(即为eula ...

  5. 欧拉函数 || Calculation 2 || HDU 3501

    题面: 题解:欧拉函数的基础应用,再套个很 easy 的等差数列前 n 项和就成了. 啊,最近在补作业+准备月考+学数论,题就没怎么写,感觉菜得一匹>_< CSL加油加油~! 代码: #i ...

  6. 欧拉函数:HDU3501-Calculation 2

    Calculation 2 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Probl ...

  7. hdu 3501 容斥原理或欧拉函数

    Calculation 2 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

  8. 杭电3501Calculation 2 欧拉函数

    Calculation 2 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) To ...

  9. hdu2588 GCD (欧拉函数)

    GCD 题意:输入N,M(2<=N<=1000000000, 1<=M<=N), 设1<=X<=N,求使gcd(X,N)>=M的X的个数.  (文末有题) 知 ...

  10. BZOJ 2705: [SDOI2012]Longge的问题 [欧拉函数]

    2705: [SDOI2012]Longge的问题 Time Limit: 3 Sec  Memory Limit: 128 MBSubmit: 2553  Solved: 1565[Submit][ ...

随机推荐

  1. mlock家族:锁定物理内存【转】

    转自:http://blog.csdn.net/fjt19900921/article/details/8074541 锁住内存是为了防止这段内存被操作系统swap掉.并且由于此操作风险高,仅超级用户 ...

  2. V++ MFC CEdit输出数组 UNICODE TO ASCII码

    MFC怎么在静态编辑框中输出数组 //字符转ASCII码void CUTF8Dlg::OnBnClickedButtonCharAscii(){ // TODO: 在此添加控件通知处理程序代码 Upd ...

  3. UVA 10746 Crime Wave - The Sequel

    最小费用最大流 源点->警察->bank->汇点 剩下的模板就可以 #include <map> #include <set> #include <li ...

  4. makefile函数集锦【转】

    转自:http://blog.csdn.net/turkeyzhou/article/details/8612841 Makefile  常用函数表一.字符串处理函数1.$(subst FROM,TO ...

  5. c++ poco StreamSocket tcpclient测试用例

    1.代码 #include <iostream> #include "Poco/Net/Socket.h" #include "Poco/Net/Stream ...

  6. Centos 6.3nginx安装

    1. 增加源: vi /etc/yum.repos.d/nginx.repo CentOS: [nginx] name=nginx repo baseurl=http://nginx.org/pack ...

  7. python接口自动化9-https请求(SSL)【转载】

    本篇转自博客:上海-悠悠 原文地址:http://www.cnblogs.com/yoyoketang/tag/python%E6%8E%A5%E5%8F%A3%E8%87%AA%E5%8A%A8%E ...

  8. poj 1106(半圆围绕圆心旋转能够覆盖平面内最多的点)

    Transmitters Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 4955   Accepted: 2624 Desc ...

  9. java中的BigInteger

    头文件 import java.io.*; import java.math.*; 读入 Scanner cin = Scann(System.in); while(cin.hasNext()) &l ...

  10. HttpClient 同时上传多个文件及参数, 同时利用 Web Api 接收

    using (System.Net.Http.HttpClient client = new System.Net.Http.HttpClient()) { client.BaseAddress = ...