先用模拟的方法找出规律,发现答案是以 0 0 0 4 为循环。
由于输入的数巨大,用字符串接收数据,然后转化为4进制,在判断末尾是否为零。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <math.h>
#include <map>
#include <algorithm>
using namespace std;
const int maxn = 100005;
char num[maxn];
int n;
int mpow(int x,int y){
int ans = 1;
for(;y;y>>=1){
if(y&1){
ans *= x;
ans %= 5;
}
x *= x;
x %= 5;
}
return ans;
}
int main(){
/*int n;//找规律 (以0 0 0 4循环)
for(n = 1;n < 500;n++){
int ans = 0;
for(int i = 1;i <= 4;i++){
ans += mpow(i,n);
ans %= 5;
}
printf("%d ",(ans%5+5)%5);
if(n%4 == 0)
printf("\n");
}*/
while(~scanf("%s",num)){//输入的数很大用字符串接收
int len = (int)strlen(num);
for(int i = 0;i < len-1;i++){//将其转化为4进制判断末尾,判断最后一位是否为零
int temp = (num[i] - '0')*10;
temp %= 4;
num[i+1] += temp;
}
//printf("%d",num[len-1]);
if(num[len-1]%4 == 0)
printf("4\n");
else
printf("0\n");
}
return 0;
}