1. 程式人生 > >uva13216 Problem with a ridiculously long name but with a ridiculously short description

uva13216 Problem with a ridiculously long name but with a ridiculously short description

 題意是說,輸入整數n,1<=n<=10^1000,求66^n mod 100的餘數。一看這類題目,肯定是找規律。計算了前12個數,規律就出現了:1,66,56,96,36,76,16,56,96,36,76,16……所以,計算這個餘數就是僅僅跟n有關了。

C++在處理n時還比較麻煩,在這點上,python真的是太方便(雖然效率不高)

python版本AC程式碼

ans = [76,16,56,96,36]
testcase = int(input())
while testcase > 0:
	testcase -= 1
	n = int(input())
	if n == 0:
		print('1')
	elif n == 1:
		print('66')
	else:
		print(ans[n%5])

C++版本AC程式碼

#include <iostream>
#include<cstdio>
#include<cstring>
using namespace std;

//#define ZANGFONG
char n[1010];
int ans[6] = {76,16,56,96,36};

int main()
{
    #ifdef ZANGFONG
    freopen("in.txt","r",stdin);
    freopen("out.txt","w",stdout);
    #endif // ZANGFONG
    int testcase,i,res,len;
    scanf("%d\n",&testcase);
    while(testcase--)
    {
        memset(n,0,sizeof(n));
        scanf("%s\n",n);
        len = strlen(n);
        res = n[0] - '0';
        if(len == 1)
        {
            if(res == 0) printf("1\n");
            else if(res == 1) printf("66\n");
            else printf("%d\n",ans[res%5]);
        }
        else{
            res = n[len-1] - '0';
            res %= 5;
            printf("%d\n",ans[res]);
        }
    }
    return 0;
}