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. Ubuntu编译Android源码过程中的空间不足解决方法

    Android源码一般几十G,就拿Android5.0来说,下载下来大概也有44G左右,和编译产生的文件以及Ubuntu系统占用的空间加起来,源码双倍的空间都不够有.编译源码前能分配足够的空间再好不过 ...

  2. 使用jquery-validate校验表单

    注意: 表单校验(validation校验[需要下载JQuery-validate插件,而且必须要在引入JQuery插件之后,再引入validate插件/*validate是建立在JQuery之上*/ ...

  3. tensorflow目标检测API之建立自己的数据集

    1 收集数据 为了方便,我找了11张月儿的照片做数据集,如图1,当然这在实际应用过程中是远远不够的 2 labelImg软件的安装 使用labelImg软件(下载地址:https://github.c ...

  4. 转 Spring Security 简介

    https://blog.csdn.net/xlecho/article/details/80026527 Spring Security 简介 2018年04月21日 09:53:02 阅读数:13 ...

  5. matplotlib绘图(四)

    控制文字属性的方法: 所有的方法都会返回一个matplotlib.text.Text对象  文本注释: annnotate() xy参数设置箭头指示的位置,xytext参数设置注释文字的位置 arro ...

  6. LeetCode之螺旋矩阵

    问题 螺旋矩阵 给定一个包含 m x n 个元素的矩阵(m 行, n 列),请按照顺时针螺旋顺序,返回矩阵中的所有元素. 示例 1: 输入: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ ...

  7. Beyond Compare4.x 破解方案

    如果过了30天的评估期或报“Beyond Compare 许可证密钥被撤销” 而导致不能再打开Beyond Compare,可以用下面的方法来解决: 1.找到“C:\Users\[Your User ...

  8. RSA非对称加密算法实现过程

    RSA非对称加密算法实现过程 非对称加密算法有很多,RSA算法就是其中比较出名的算法之一,下面是具体实现过程 <?php /** */ class Rsa { /** * private key ...

  9. type和object

    一.定义 1.object是所有新式类的父类 2.type是所有类的类    二.解析   下面通过代码来比较一下object和type的关系(__class__获取所属的类,__bases__获取父 ...

  10. bash数组操作-定义/初始化/赋值…

    数组:     连续的多个独立内存空间,每个内存空间相当于一个变量     数组元素:数组名+索引         索引:从0开始编号             声明数组:         declar ...