给定一数n,求n的因子数目和因子之和
上述求因子和式子等于
∏{(piei+1 - 1) / (pi - 1)} (∏就是连乘)
模板:
1 const int maxn = 1000000+10; 2 int prime[maxn]; 3 bool is_prime[maxn]; 4 int sieve(int n)//返回n以内素数的个数 5 { 6 int p = 0; 7 for(int i = 0; i <= n; i++)is_prime[i] = 1; 8 is_prime[0] = is_prime[1] = 0; 9 for(ll i = 2; i <= n; i++) 10 { 11 if(is_prime[i]) 12 { 13 prime[p++] = i; 14 for(ll j = i * i; j <= n; j += i)is_prime[j] = 0;//这里涉及i*i,必须使用long long 15 } 16 } 17 return p; 18 } 19 20 ll Divisors_num(ll n, int tot)//素数总数 21 { 22 ll ans = 1; 23 for(int i = 0; i < tot && prime[i] * prime[i] <= n; i++) 24 { 25 if(n % prime[i] == 0) 26 { 27 int cnt = 0; 28 while(n % prime[i] == 0) 29 { 30 cnt++; 31 n /= prime[i]; 32 } 33 ans *= (cnt + 1); 34 } 35 } 36 if(n > 1)ans *= 2; 37 return ans; 38 } 39 ll pow(ll a, ll b) 40 { 41 ll ans = 1; 42 while(b) 43 { 44 if(b & 1)ans = ans * a; 45 a *= a; 46 b /= 2; 47 } 48 return ans; 49 } 50 ll Divisors_sum(ll n, int tot) 51 { 52 ll ans = 1; 53 for(int i = 0; i < tot && prime[i] * prime[i] <= n; i++) 54 { 55 if(n % prime[i] == 0) 56 { 57 int cnt = 0; 58 while(n % prime[i] == 0) 59 { 60 cnt++; 61 n /= prime[i]; 62 } 63 ans = (pow(prime[i], cnt + 1) - 1) / (prime[i] - 1) * ans; 64 } 65 } 66 if(n > 1)ans *= (n + 1); 67 return ans; 68 }