hdu 3501 Calculation 2 (欧拉函数)
题意:求小于n并且 和n不互质的数的总和。
思路:求小于n并且与n互质的数的和为:n*phi[n]/2 .
若a和n互质,n-a必定也和n互质(a<n)。也就是说num必定为偶数。其中互质的数成对存在。其和为n。
公式证明:
反证法:
如果存在K!=1使gcd(n,n-i)=k,那么(n-i)%k==0
而n%k=0
那么必须保证i%k=0
k是n的因子,如果i%k=0那么gcd(n,i)=k,矛盾出现;
所以先求出1……n-1 的和, 再用这个和 减去 上面公式求出来的值。
欧拉函数phi(m):当m>1是,phi(m)表示比m小且与m互质的正整数个数
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std;
#define LL long long
const int mo = ; LL phi(LL n) //欧拉函数模板
{
LL i, m = (int)sqrt(n+0.5), ans = n;
for(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 res, n, i;
while(~scanf("%lld", &n) && n)
{
res = n*(n-)/;
res -= phi(n)*n/; //当n等于2的时候,phi为1,所以不能写成 phi(n)/2*n;
printf("%lld\n", res%mo);
}
return ;
}
再贴一个关于欧拉函数的讲解博客
hdu 3501 Calculation 2 (欧拉函数)的更多相关文章
- hdu 3501 容斥原理或欧拉函数
Calculation 2 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tot ...
- HDU3501 Calculation 2 [欧拉函数]
题目传送门 Calculation 2 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- HDU 1695 GCD (欧拉函数+容斥原理)
GCD Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- HDU 5430 Reflect(欧拉函数)
题目: http://acm.hdu.edu.cn/showproblem.php?pid=5430 从镜面材质的圆上一点发出一道光线反射NNN次后首次回到起点. 问本质不同的发射的方案数. 输入描述 ...
- hdu 5279 Reflect phi 欧拉函数
Reflect Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://bestcoder.hdu.edu.cn/contests/contest_chi ...
- HDU 1695 GCD(欧拉函数+容斥原理)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1695 题意:x位于区间[a, b],y位于区间[c, d],求满足GCD(x, y) = k的(x, ...
- HDU 1787 GCD Again(欧拉函数,水题)
GCD Again Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- 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( ...
- hdu 2814 快速求欧拉函数
/** 大意: 求[a,b] 之间 phi(a) + phi(a+1)...+ phi(b): 思路: 快速求欧拉函数 **/ #include <iostream> #include & ...
随机推荐
- JPA学习---第四节:JPA实例与JPA主键生成策略
1.编写实体类,代码如下: package learn.jpa.bean; import javax.persistence.Entity; import javax.persistence.Gene ...
- C# 获取属性字段上DescriptionAttribute的值
var ent = new Ent(); foreach (var item in ent.GetType().GetProperties()) { var v = (DescriptionAttri ...
- ComboTree使用
1.参考资料 1.http://www.jeasyui.com/documentation/combotree.php 2.http://blog.csdn.net/woshichunchun/a ...
- 《剑指Offer》- 面试题3
<剑指Offer——名企面试官精讲典型编程题> 面试题3: 二维数组元素从左到右.从上到下递增,输入一个二维数组和一个整数, 查找该整数. 自己的思路:有序条件下进行查找,当然最简单 ...
- Machine Learning Done Wrong
Machine Learning Done Wrong Statistical modeling is a lot like engineering. In engineering, there ar ...
- Codeforces Round #240 (Div. 2)->A. Mashmokh and Lights
A. Mashmokh and Lights time limit per test 1 second memory limit per test 256 megabytes input standa ...
- cf 359C
stl 里的map使用 然后就是快速幂取余 #include <cstdio> #include <cstring> #include <algorithm> ...
- android C/C++ source files 全局宏定义 .
\system\core\include\arch\linux-arm AndroidConfig.h * ============================================== ...
- 【二叉树遍历模版】前序遍历&&中序遍历&&后序遍历&&层次遍历&&Root->Right->Left遍历
[二叉树遍历模版]前序遍历 1.递归实现 test.cpp: 12345678910111213141516171819202122232425262728293031323334353637 ...
- prim求MST
PRIM==>>MST模板 #include <iostream> using namespace std; #define typec int #define V 3 con ...