版权声明:xgc原创文章,未经允许不得转载。 https://blog.csdn.net/xgc_woker/article/details/82774362
Description
给定一个整数N的标准分解形式,求N要求多少次phi才会变成1。
Sample Input
1
2
2 2
3 1
Sample Output
3
因为你对于一个非2的质数,每一次取phi都会出现一个2,那也就是说你每次都肯定可以消掉一个2,那最后答案就是消掉2的个数,那你对于每一个质数求出它能搞出多少个2即可。
#include <cmath>
#include <cstdio>
#include <cstring>
using namespace std;
typedef long long LL;
inline int read() {
int s = 0, f = 1; char ch = getchar();
while(ch < '0' || ch > '9') {if(ch == '-') f = -1; ch = getchar();}
while(ch >= '0' && ch <= '9') s = s * 10 + ch - '0', ch = getchar();
return s * f;
}
int plen, p[110000], v[110000];
LL f[110000];
void get_prime() {
f[1] = 1;
for(int i = 2; i <= 100000; i++) {
if(!v[i]) p[++plen] = i, f[i] = f[i - 1];
for(int j = 1; j <= plen && (i * p[j]) <= 100000; j++) {
v[i * p[j]] = 1; f[i * p[j]] = f[i] + f[p[j]];
if(i % p[j] == 0) break;
}
}
}
int main() {
get_prime();
int tt = read();
while(tt--) {
int m = read();
LL ans = 1;
for(int i = 1; i <= m; i++) {
int x = read(), y = read();
ans += (LL)f[x] * y;
if(x == 2) ans--;
} printf("%lld\n", ans);
}
return 0;
}