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. jsp页面之间传值 以及如何取出url的参数

    写项目时往往要写多个页面,而多个jsp之间传值有时是必要的,这时可以用到如下方法: 而在另一个页面取值可以用:${param.xxx}   此处的xxx就是要传递的值

  2. IDEA安装及破解

    一.下载(IDEA 2019.1.2) 1.下载地址:https://www.jetbrains.com/idea/download/#section=windows 2.选择版本,并选择最终版(.e ...

  3. VIM+ctags+cscope用法

    使用vim + cscope/ctags,就能够实现Source Insight的功能,可以很方便地查看分析源代码.   关键词: vim, cscope, ctags, tags   1. 查看vi ...

  4. 01_9_Struts用ModelDriven接收参数

    01_9_Struts用ModelDriven接收参数 1. 配置struts.xml文件 <package name="user" namespace="/use ...

  5. ssh整合思想 Spring与Hibernate和Struts2的action整合 调用action添加数据库 使用HibernateTemplate的save(entity)方法 update delete get 等方法crud操作

    UserAction类代码: package com.swift.action; import com.opensymphony.xwork2.ActionSupport; import com.sw ...

  6. 如何挂载一个镜像文件(how to mount an image file)

    如何挂载一个镜像文件(how to mount an image file) 08/16/2012master 4 Comments 在使用KVM或Xen虚拟化的情况下,经常需要使用镜像文件(imag ...

  7. Android驱动开发读书笔记五

    第五章 本章介绍了S3C6410开发板的功能,开发板的不同主要是在烧录嵌入式系统的方式不同,以及如何在此开发板上安装Android. 1.安装串口调试工具minicom 首先需要一根USB转串口线,由 ...

  8. C++ string头文件

    转载自https://blog.csdn.net/superna666/article/details/52809007/ 作者 zhenzhenjiajia888 标准c++中string类函数介绍 ...

  9. NOIP模拟赛 路面修整

    [题目描述] FJ打算好好修一下农场中某条凹凸不平的土路.按奶牛们的要求,修好后的路面高度应当单调上升或单调下降,也就是说,高度上升与高度下降的路段不能同时出现在修好的路中. 整条路被分成了N段,N个 ...

  10. 【Python学习之五】高级特性4(切片、迭代、列表生成器、生成器、迭代器)

    4.生成器(generator) 通过列表生成式,我们可以直接创建一个列表.但是,受到内存限制,列表容量肯定是有限的.如果列表元素可以按照某种算法推算出来,那我们是否可以在循环的过程中不断推算出后续的 ...