1. 程式人生 > >pku1365 Prime Land (數論,合數分解模板)

pku1365 Prime Land (數論,合數分解模板)

getchar() for bsp long 次數 spa main clu pre

題意:給你一個個數對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<cstdio>
#include<cstring>
#define ll long long
int
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]); } }

pku1365 Prime Land (數論,合數分解模板)