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 ;
}

最新文章

  1. 随机生成UUID(GUID)的方法
  2. 使用html2canvas实现网页截图并嵌入到PDF
  3. 【BZOJ 2733】【HNOI 2012】永无乡 Splay启发式合并
  4. Linux phpbb论坛的安装(中文版)
  5. Servlet中Service方法
  6. php复制目录及文件
  7. Android——显示当前运行所有服务,判断服务是否运行
  8. XJOI网上同步训练DAY1 T1
  9. Python函数式编程:内置filter函数使用说明
  10. [Cocos2d-x]博客推荐
  11. java线程控制方法
  12. 201521123054《Java程序设计》第8周学习总结
  13. Bootstrap3 排版-页面主体
  14. apache中 sed 指定文件中某字符串增加行
  15. 浅析toString()和toLocaleString()的区别
  16. web前端(2)—— 前端技术介绍
  17. Codeforces.472F.Design Tutorial: Change the Goal(构造 线性基 高斯消元)
  18. jxl和POI的区别
  19. .NET Core开发日志——Global Tools
  20. layer使用

热门文章

  1. 【0624作业】使用Scanner类输入并显示会员卡号
  2. javaweb基础(6)_servlet配置参数
  3. Linux学习日记:第一天
  4. 掉坑日志:Windows Native API与DPI缩放
  5. 01_6_SERVLET如何从上一个页面取得参数
  6. Java第11次作业:什么是继承?继承的好处?什么是覆写?super()?构造代码块?子父类初始化顺序? 抽象类能用final声明吗?final关键字声明类 方法 变量以及全局常量?抽象类的构造方法?
  7. Java第7次作业:造人类(用private封装,用static关键字自己造重载输出方法)什么是面向对象程序设计?什么是类和对象?什么是无参有参构造方法 ?什么是封装?
  8. numpy学习(一)
  9. How to Install PhantomJS on Ubuntu 16.04
  10. centos7.4系统部署nodejs前端项目