Calculation 2

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2181    Accepted Submission(s): 920

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
 
Author
GTmac
 
Source
 
题目大意:给一个数n求1到n-1之间与它不互质的数之和mod(1000000007)。
解题思路:我首先想到的是把n质因数分解成n=a1^p1*a2^p2....*an^pn的形式,那么把与他含有共同因子的数之和算出来,这里面有重复的,所以用容斥原理计算。
一个数的欧拉函数phi(n),根据gcd(n,i)==1,gcd(n,n-i)==1,所以与n互质的数都是成对出现的(他们的和等于n)。
 
 //欧拉函数
#include <stdio.h>
#include <math.h> typedef __int64 LL;
const int Mod=; LL euler_phi(LL n)
{
LL m=(LL)sqrt(n*1.0);
LL ans=n;
for(int i=;i<=m;i++) if(n%i==)
{
ans=ans/i*(i-);
while(n%i==) n/=i;
}
if(n>) ans=ans/n*(n-);
return ans;
} int main()
{
LL n;
while(~scanf("%I64d",&n),n)
{
LL phi=euler_phi(n);
LL t1=(n*phi/)%Mod;
LL t2=(n*(n+)/-n)%Mod;
LL ans=(t2-t1+Mod)%Mod;
printf("%I64d\n",ans);
}
return ;
}
 //容斥原理
#include <stdio.h>
#include <string.h>
#include <math.h> typedef __int64 LL;
const int Mod=;
const int maxn=;
bool flag[maxn];
int prime[maxn],num,n;
int factor[],cnt;
bool vis[];
LL ans,temp; void get_prime()
{
num=;memset(flag,true,sizeof(flag));
for(int i=;i<maxn;i++)
{
if(flag[i]) prime[num++]=i;
for(int j=;j<num&&prime[j]*i<maxn;j++)
{
flag[i*prime[j]]=false;
if(i%prime[j]==) break;
}
}
} void get_factor()
{
cnt=;
int i,top=(int)sqrt(n*1.0),t=n;
for(i=;i<num && prime[i]<=top;i++)
{
if(t%prime[i]==)
{
factor[cnt++]=prime[i];
while(t%prime[i]==)
t/=prime[i];
}
}
if(t>) factor[cnt++]=t;
} void dfs(int now,int top,int start,LL s)
{
if(now==top)
{
LL t=(n-)/s;
LL a=((t+)*t/)%Mod;
LL b=(a*s)%Mod;
temp=(temp+b)%Mod;
return ;
}
for(int j=start;j<cnt;j++)
{
if(!vis[j])
{
vis[j]=true;
dfs(now+,top,j+,s*factor[j]);
vis[j]=false;
}
}
return ;
} void solve()
{
ans=;
int c=;
for(int i=;i<=cnt;i++)
{
memset(vis,false,sizeof(vis));
temp=;dfs(,i,,);
ans=(((ans+c*temp)%Mod)+Mod)%Mod;
c=-c;
}
} int main()
{
get_prime();
while(scanf("%d",&n),n)
{
get_factor();
solve();
printf("%I64d\n",ans);
}
return ;
}

hdu 3501 容斥原理或欧拉函数的更多相关文章

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

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

  2. HDU 1695 GCD (欧拉函数+容斥原理)

    GCD Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  3. HDU 1695 GCD(欧拉函数+容斥原理)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1695 题意:x位于区间[a, b],y位于区间[c, d],求满足GCD(x, y) = k的(x, ...

  4. hdu 1695 GCD (欧拉函数、容斥原理)

    GCD Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submis ...

  5. HDU 5430 Reflect(欧拉函数)

    题目: http://acm.hdu.edu.cn/showproblem.php?pid=5430 从镜面材质的圆上一点发出一道光线反射NNN次后首次回到起点. 问本质不同的发射的方案数. 输入描述 ...

  6. hdu 5279 Reflect phi 欧拉函数

    Reflect Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://bestcoder.hdu.edu.cn/contests/contest_chi ...

  7. hdu 1695 GCD(欧拉函数+容斥)

    Problem Description Given 5 integers: a, b, c, d, k, you're to find x in a...b, y in c...d that GCD( ...

  8. 容斥原理、欧拉函数、phi

    容斥原理: 直接摘用百度词条: 也可表示为 设S为有限集, ,则 两个集合的容斥关系公式:A∪B = A+B - A∩B (∩:重合的部分) 三个集合的容斥关系公式:A∪B∪C = A+B+C - A ...

  9. HDU 1787 GCD Again(欧拉函数,水题)

    GCD Again Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

随机推荐

  1. tcp 高性能服务, netty,mqtt

    1. io 线程不要有比较长的服务. 全部异步化. [1] netty 权威指南上只是说业务复杂时派发到业务线程池种. 共用的线程池最好都轻量. 多层线程池后, 下层的可以进行隔离. 这个是 mqtt ...

  2. 利用sysbench工具测试MHA

    利用sysbench工具测试MHA 1. sysbench准备数据 2. sysbench开始压测 3. master模拟意外宕机 4. mysqldb2 上观察mha状态 5. 手工failover ...

  3. Python学习笔记:open函数和with临时运行环境(文件操作)

    open函数 1.open函数: file=open(filename, encoding='utf-8'),open()函数是Python内置的用于对文件的读写操作,返回的是文件的流对象(而不是文件 ...

  4. python模块之shutil和zipfile

    shutil 模块 高级的 文件.文件夹.压缩包 处理模块 shutil.copyfileobj(fsrc, fdst[, length])将文件内容拷贝到另一个文件中 import shutil s ...

  5. linux 安装elasticsearch

    一.检测是否已经安装的elasticsearch ps aux|grep elasticsearch. 二.下载elasticsearch.tar.gz并上传至服务器usr/local/文件夹下 三. ...

  6. debian 7 stable 不能编译android源码

    rebuilts/gcc/linux-x86/arm/arm-linux-androideabi-4.8-linaro/bin/arm-linux-androideabi-gcc: /lib/x86_ ...

  7. 强大的with语句

    上下文管理器对象存在的目的是管理 with 语句,就像迭代器的存在是为了管理 for 语句一样. with 语句的目的是简化 try/finally 模式.这种模式用于保证一段代码运行完毕后执行某项操 ...

  8. CRM知识点汇总(未完💩💩💩💩💩)

    一:项目中每个类的作用 StarkSite 对照admin中的AdminSite,相当于一个容器,用来存放类与类之间的关系. 先实例化对象,然后执行该对象的register方法.将注册类添加到_reg ...

  9. GloVe词分布式表示

    GloVe 模型介绍 下面的内容主要来自https://blog.csdn.net/u014665013/article/details/79642083 GloVe的推导 GloVe是基于共现信息来 ...

  10. Matlab 二值图像label regions

    本系列文章由 @yhl_leo 出品,转载请注明出处. 文章链接: http://blog.csdn.net/yhl_leo/article/details/52862719 Matlab提供了现成的 ...