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. UVA 753 A Plug for UNIX

    最大流解决 . 设置源点 0,连接所有设备(device) .设备-插头 -汇点 #include <map> #include <set> #include <list ...

  2. C++11中的小细节--字符串的原始字面量

    原始字面量很容易理解,即不进行转义的完整字符串. 最近看了看Python,其中讲到了原始字符串. Both string and bytes literals may optionally be pr ...

  3. 测试 System.SysUtils.TStringHelper

    来自:http://www.cnblogs.com/del/archive/2013/06/14/3135002.html -------------------------------------- ...

  4. 【模拟】Friday the Thirteenth

    题目描述 Is Friday the 13th really an unusual event?That is, does the 13th of the month land on a Friday ...

  5. 设计模式之不变模式(Immutable Pattern)分析

    http://www.iteye.com/topic/959751 最近老有人问我不变模式,我其实也理解得不深,于是花了一些时间进行学习总结,分析了一下不变模式(immutable pattern), ...

  6. POJ 2135 Farm Tour (费用流)

    [题目链接] http://poj.org/problem?id=2135 [题目大意] 有一张无向图,求从1到n然后又回来的最短路 同一条路只能走一次 [题解] 题目等价于求从1到n的两条路,使得两 ...

  7. 用swift开发自己的MacOS锁屏软件(二)

    上一篇中尝试写了hello world,这一篇中,开始尝试锁屏功能 1.尝试查找swift有没有相关的函数,可以控制系统锁屏之类的,结果并没有找到 2.尝试查找cocoa有没有相关的接口,结果仍然没有 ...

  8. Enum枚举类使用集合

    1.使用扩展方法使用枚举值对于的Description属性值 public static class EnumExtenstion { public static string GetDescript ...

  9. windows下搭建svn服务器

    转自:http://www.cnblogs.com/cloud2rain/archive/2013/04/11/3015080.html 这篇文档非常好,转来学习,有一点就是把subversion创建 ...

  10. memcache学习资料

    memcached是国外社区网站LiveJournal团队开发,通过缓存数据库查询结果,减少数据库访问次数,从而提高动态web站点性能.官方站点 http://memcached.org/memcac ...