您的位置:首页 > 其它

hdu 1215 七夕节(因子和)

2016-06-06 11:50 417 查看


七夕节

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 40232    Accepted Submission(s): 12657


Problem Description

七夕节那天,月老来到数字王国,他在城门上贴了一张告示,并且和数字王国的人们说:"你们想知道你们的另一半是谁吗?那就按照告示上的方法去找吧!"

人们纷纷来到告示前,都想知道谁才是自己的另一半.告示如下:



数字N的因子就是所有比N小又能被N整除的所有正整数,如12的因子有1,2,3,4,6.

你想知道你的另一半吗?

 

Input

输入数据的第一行是一个数字T(1<=T<=500000),它表明测试数据的组数.然后是T组测试数据,每组测试数据只有一个数字N(1<=N<=500000).

 

Output

对于每组测试数据,请输出一个代表输入数据N的另一半的编号.

 

Sample Input

3
2
10
20

 

Sample Output

1
8
22

题意:求n的因子之和

思路:因子和模板,S=g(p1,e1)*g(p2,e2)*...g(pk,ek)    g(pi,ei)=1+pi的1次方+pi的2次方+...pi的ei次方

代码:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
using namespace std;
#define LL long long
#define N 500010
LL e
[2];
int pn;
void Devide(LL n)
{
pn=0;
for(int i=2;i*i<=n;i++)
{
if(n%i==0)
{
int num=0;
while(n%i==0)
{
num++;
n/=i;
}
e[++pn][0]=i;
e[pn][1]=num;
}
}
if(n>1) e[++pn][0]=n,e[pn][1]=1;
}
LL pow_mod(LL a,LL n)
{
LL ans=1;
while(n)
{
if(n%2) ans=ans*a;
a=a*a;
n>>=1;
}
return ans;
}
LL bi()
{
LL ans=1;
for(int i=1;i<=pn;i++)
ans*=((1-pow_mod(e[i][0],e[i][1]+1))/(1-e[i][0]));
return ans;
}
int main()
{
LL T,n;
scanf("%lld",&T);
while(T--)
{
scanf("%lld",&n);
if(n==1) {
printf("0\n");
continue;
}
Devide(n);
LL ans=bi();
printf("%lld\n",ans-n);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: