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. GC执行finalize的过程以及对象的一次自我拯救

    参考资料:深入理解java虚拟机 /** * 此代码演示了两点: * 1.对象可以在被GC时自我拯救 * 2.这种自救的机会只有一次,因为一个对象的finalize()方法只会被系统自动调一次 */ ...

  2. cocos2dx观察者模式EventListenerCustom的使用(代替NotificationCenter)

    在cocos2dx 3.x版本已经被弃用,改用EventDispatcher代替. 观察者模式是MVC模式的一种,一个model可以对应很多个观察者view,当model收到事件通知时,对应的view ...

  3. PostgreSQL学习(1)-- 安装pageinspect extension

    1.源码编译 pageinspect的源码在postgre源码包的contrib目录下,解压postgre源码包后进入对应的目录. [root@localhost pageinspect]# pwd ...

  4. spring bean的介绍以及xml和注解的配置方法

    5.Bean 下边我们来了解一下Bean的: Bean的作用域Bean的生命周期Bean的自动装配Resources和ResourceLoader 5.1Bean容器的初始化 Bean容器的初始化 两 ...

  5. 第一本C语言笔记(上)

    1. 一般地源程序文件到可执行程序文件经过如下四步: 预处理 -- 头文件内容插入.宏替换.删注释(#include.#define) 编译 -- 把预处理过的源文件编程汇编文件 .c -> . ...

  6. 使用el-checkbox实现全选,点击失效没有反应

    最近在公司接收到了一个需求,给收藏夹的书籍添加批量.全选删除实现思路:点击全选改变item的checked,改变item的checked,重新便利一下所有item的checked来改变全选的selec ...

  7. leetcode-6-basic

    解题思路: 这道题真实地反映了我今晚有多脑残=.=只需要从根号N开始向前找,第一个能被N整除的数就是width,然后存到结果就 可以了.因为离根号N越近,width越大,与length的差越小. ve ...

  8. 使用html+javascriptt实现的简易四则运算(初学JavaScript笔记)

    今天第一天学javascript,做了个简易的四则运算,提供参考,效果图: html代码: <!DOCTYPE html> <html > <head > < ...

  9. python基础学习笔记——异常处理

    异常处理流程图 一,异常和错误 part1:程序中难免出现错误,而错误分成两种 1.语法错误(这种错误,根本过不了python解释器的语法检测,必须在程序执行前就改正) #语法错误示范一 if #语法 ...

  10. day02 Python 的模块,运算,数据类型以及方法

    初识pyhton的模块: 什么是模块: 我的理解就是实现一个功能的函数,把它封装起来,在你需要使用的时候直接调用即可,我的印象里类似于shell 的单独函数脚本. python 的模块分为标准的和第三 ...