HDOJ 3501 Calculation 2

来源:互联网 发布:stc单片机pwm输出频率 编辑:程序博客网 时间:2024/05/01 21:30

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3501

题意:给出一个数n,求出小于n的所有因数和。

一开始想用因数和打表的,但是数据实在是太大了,10^9数组都开不出来,直接就爆栈了,所以只能单个处理,单个处理的话我们直接求出其因数和就没有那么方便,所以我们就反过来,求出所有与之不互质的数的和,再用总和去减,有关不互质的数的和的求法,是用到了eular函数的引申性质(不会的同学欢迎观看小编另外一篇博客)。

#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>using namespace std;typedef long long LL;const int MOD = 1000000007;LL eular(LL n){LL ans = n;for(int i=2; i*i<=n; i++)if(n % i == 0){ans -= ans/i;while(n%i==0) n /= i;}if(n > 1) ans -= ans/n;return ans;}int main(){LL n;while(scanf("%I64d", &n) && n){LL ans = n*(n-1)/2;LL sum = eular(n);//printf("%I64d %I64d\n",ans, sum);printf("%I64d\n", (ans-n*sum/2)%MOD);}}


0 0
原创粉丝点击