Tanky WooRSS

HDOJ 2035 人见人爱A^B

12 Jul 2010
这篇博客是从旧博客 WordPress 迁移过来,内容可能存在转换异常。

汗,用了了HDOJ 1061一样的方法,居然老是WA。找不出错误,参考网上的,居然都是直接暴力的。当然,暴力还有个前提,还是和HDOJ 1061一样的思想,直接算后三位数就可以了,每次对1000求余。

// HDOJ 2035
// Author: Tanky Woo
#include 
#include 
using namespace std;

long A, B;

int main()
{
    while(scanf("%ld %ld", &A;, &B;) && A && B)
    {
        A %= 1000;
        int temp = 1;
        for(int i = 0; i < B; ++i)
        {
            temp *= A;
            temp %= 1000;
        }
        printf("%d\n", temp);
    }
    return 0;

}

下面是WA的。直接给main()函数部分了:

int main()
{
    int model[100];
    int nCases;
    while( scanf("%ld %ld", &A;, &B;) && A && B )
    {
        int t = A % 1000;
        int i = 0;
        int temp = t * t % 1000;
        model[0] = t;
        model[++i] = temp;
        while(1)
        {
            temp *= t;
            temp %= 1000;
            if(temp == t || i >= B)
                break;
            model[++i] = temp;
        }
        ++i;
        int k = ((B % i -1)+i) % i;   // 最后的再+i并对i求与是为了防止N%i=0的情况,比如N=4
        printf("%d\n", model[k]);


    }
    return 0;
}