BIT 1057 SUM
来源:互联网 发布:mysql 子查询效率 编辑:程序博客网 时间:2024/06/02 16:29
题目大意:给出一个数字N(N<10^9),求所有小于等于n的数中与n不互质的数的和
考察点:数论
思路分析:与n不互质的数的和我们不太好求,反过来,求与n互质的数的和我们就可以利用欧拉函数来求了。
在数论,对正整数n,欧拉函数是少于或等于n的数中与n互质的数的数目。此函数以其首名研究者欧拉命名,它又称为Euler's totient function、φ函数、欧拉商数等。 例如φ(8)=4,因为1,3,5,7均和8互质。
那么欧拉函数我们该如何求呢,欧拉函数有几条性质:http://baike.baidu.com/view/107769.htm
利用欧拉函数的性质,我们可以参考这篇文章中介绍的方法http://blog.csdn.net/yuanchuanshun/article/details/6168342,就能在比较短的时间内出结果了
#include<stdio.h>long long oula(long long n){ long long ou; int i; ou=1; for (i=2;i*i<=n;i++) if (n%i==0) { ou=ou*(i-1);n=n/i; while (n%i==0) { n=n/i; ou=ou*i; } } if (n>1) ou=ou*(n-1); return ou;} int main(){ long long ans,n; scanf("%lld",&n); while (n!=0) { if (n==2) printf("0\n"); else{ ans=oula(n); ans=(n*(ans/2))%1000000007; ans=n*(n+1)/2-ans-n; ans=ans%1000000007; printf("%lld\n",ans);} scanf("%lld",&n); } return 0;}
- BIT 1057 SUM
- BIT 1010 Maximum Sum
- BIT 1010 Maximum Sum
- bit 1010 Maximum Sum
- BIT 1011 Maximum Sum II
- BIT寒假练习-2013__1010:Maximum Sum
- Compute 16-bit One's Complement Sum (memo)
- bit
- Bit++
- bit
- BIT
- Bit++
- Leetcode - Bit minipulation - 371. Sum of Two Integers(递归模拟位运算求和)
- 所有整数对之间的位差异数之和(Sum of bit differences among all pairs)
- Sum
- sum
- sum
- Sum
- Android之Activity与Service通信
- iOS中autorelease变量应注意的问题
- 【算法导论】之堆排序
- awk提取一串字符中的数字
- c++初级学习
- BIT 1057 SUM
- ServletContext,ActionContext,ServletActionContext
- Missing artifact com.sun:tools:jar 1.5.0 终极解决方法
- Oracle联机重做日志文件(online redo log file)相关操作
- 非阻塞的Socket
- hdu 2352 Verdis Quo
- hibernate学习笔记——hql查询
- Windows中字符与字符串
- 数字转字符串 基于8086汇编