Prime Land(poj 1365)

题意:这题题意难懂,看了题解才知道的。比如第二组sample,就是5^1*2^1=10, 求10-1即9的质因数分解,从大到小输出,即3^2.本来很简单的嘿,直接最快速幂+暴力最裸的就行了。

#include<cstdio>
#include<cstring>
#include<iostream>
#define M 100010
#define ll long long
using namespace std;
ll x[M],y[M],sum,ans1[M],ans2[M];
int cnt;
ll poww(ll a,ll b)
{
    ll r=1,base=a;
    while(b)
    {
        if(b&1)r*=base;
        base*=base;
        b/=2;
    }
    return r;
}
int main()
{
    while(1)
    {
        cnt=0;sum=1;
        memset(x,0,sizeof(x));
        memset(y,0,sizeof(y));
        while(1)
        {
            ++cnt;
            ll num=0;char c=getchar();
            while(c>='0'&&c<='9')
              num=num*10+c-'0',c=getchar();
            x[cnt]=num;
            if(num==0)return 0;
            num=0;c=getchar();
            while(c>='0'&&c<='9')
              num=num*10+c-'0',c=getchar();
            y[cnt]=num;
            if(c=='\n')break;
        }
        for(int i=1;i<=cnt;i++)
          sum*=poww(x[i],y[i]);
          
        cnt=0;sum--;
        for(ll i=2;i<=sum;i++)
        {
            ll tot=0;
            while(sum&&sum%i==0)
              sum/=i,tot++;
            if(tot)
            {
                ++cnt;ans1[cnt]=i;ans2[cnt]=tot;
            }
            if(sum==0)break;
        }
        for(ll i=cnt;i>=1;i--)
          cout<<ans1[i]<<" "<<ans2[i]<<" ";
        printf("\n");
    }
    return 0;
}
View Code

 

posted @ 2016-09-04 21:04  karles~  阅读(166)  评论(0编辑  收藏  举报