AcWing 871. 约数之和 - JAVA
原题链接
简单
作者:
acw_weian
,
2020-10-21 22:34:04
,
所有人可见
,
阅读 1137
import java.io.*;
import java.util.*;
/**
* 基本思想:
* 如果 N=a1^k1 * a2 ^ k2 * ... an ^ kn
* 约数个数: (k1+1) * (k2+1) * ... (kn+1)
* 约数之和: (a1^0 + a1^1 + ... + a1^k1) * ... * (an^0 + an^1 + ... + an^kn)
*
*/
class Main{
static BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
public static void main(String[] args) throws Exception{
int t = Integer.valueOf(read.readLine());
Map<Integer, Integer> map = new HashMap();
while(t -- > 0){
Integer a = Integer.valueOf(read.readLine());
for(int i = 2; i <= a / i; i++){
if(a % i == 0){
int cnt = 0;
while(a % i == 0){
cnt++;
a /= i;
}
map.put(i, map.getOrDefault(i, 0) + cnt);
}
}
if(a > 1) map.put(a, map.getOrDefault(a, 0) + 1);
}
long res = 1;
int mod = (int)1e9 + 7;
for(Integer a: map.keySet()){
long sum = 0;
int k = map.get(a);
// long sum = 1;
// while(k-- > 0) sum = (sum * a + 1) % mod; //求出a^k + a^(k-1) + ... + a^0
for(int i = 0; i <= k; i++){
long pow = 1; int tmp = i;
while(tmp-- > 0) pow = pow * a % mod;
sum = (sum + pow) % mod;
}
res = res * sum % mod;
}
System.out.println(res);
}
}
老哥 为什么输出那里这样写过不了
long res=1;
for(int i:map.keySet()){
long s=0;
for(int j=0;j<=map.get(i);j++){
s=(s+(long)Math.pow(i,j))%mod;
}
res=(res*s)%mod;
}
System.out.print(res);
}
老哥,你解决了吗
棒