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