HDU1215_因式分解

2014-11-24 08:49:30 · 作者: · 浏览: 1
因式分解模板
tn=n;
for(i=2;i*i<=n;i++)//试除2~sqrt(n)
if(tn%i==0){//如果能被i整除
p[++cnt]=i;//保存底数
e[cnt]=0;//保存指数
while(tn%i==0){//计算指数
e[cnt]++;
tn/=i;
}
}
}
if(tn>1){//存在大于sqrt(n)的素因子
p[++cnt]=tn;
e[cnt]=1;
}
HDU1215
题目链接: http://acm.hdu.edu.cn/showproblem.php pid=1215

[java]
package D0717;

import java.io.*;

public class HDU1215 {

public static void main(String[] args) throws IOException {
StreamTokenizer st = new StreamTokenizer(new BufferedReader(
new InputStreamReader(System.in)));
PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
st.nextToken();
int n = (int) st.nval;
while (n-- > 0) {
st.nextToken();
int a = (int) st.nval;
int sum = 1;
for (int i = 2; i*i <= a; i++) {
if (a % i == 0) {
sum += i;
if(a/i!=i){
sum+=a/i;
}
}
}
out.println(sum);
}
out.flush();

}

}
作者:lhfight