题意:给你一个个数对a, b 表示ab这样的每个数相乘的一个数n,求n-1的质数因子并且每个指数因子k所对应的次数 h.
先把合数分解模板乖乖放上:
for (int i = 2; ans != 1; ++i) { if (ans%i == 0) { num[cnt] = i; int k = 0; while (ans%i == 0){ ++k; ans /= i; } index[cnt++] = k; } if (i > 10000)break; } if (ans != 1){ num[cnt] = ans; index[cnt++] = 1; }
然后,我自己写了个快速幂
快速幂的模板:
ll pow(ll a, ll n){ ll res; for (res = 1; n;a=a*a, n>>=1) if (n & 1) res = res*a; return res;}
AC代码:
#include#include #define ll long longint num[1000];int index[1000];ll pow(ll a, ll n){ ll res; for (res = 1; n;a=a*a, n>>=1) if (n & 1) res = res*a; return res;}int main(){ while (1){ ll a, b, ans = 1; while (scanf("%lld", &a), a!=0){ scanf("%lld", &b); ans *= pow(a, b); char nn=getchar(); if (nn == '\n')break; } if (a == 0)break; ans--; int cnt = 0; for (int i = 2; ans != 1; ++i) { if (ans%i == 0) { num[cnt] = i; int k = 0; while (ans%i == 0){ ++k; ans /= i; } index[cnt++] = k; } if (i > 10000)break; } if (ans != 1){ num[cnt] = ans; index[cnt++] = 1; } for (int i = cnt-1; i >= 0; --i) printf("%d %d%c", num[i], index[i], " \n"[i == 0]); }}