2019 GDUT 新生专题Ⅳ B题

B - Fedya and Maths

题目
Fedya studies in a gymnasium. Fedya’s maths hometask is to calculate the following expression:

在这里插入图片描述
for given value of n. Fedya managed to complete the task. Can you? Note that given number n can be extremely large (e.g. it can exceed any integer type of your programming language).

Input
The single line contains a single integer n (0 ≤ n ≤ 10^100000). The number doesn’t contain any leading zeroes.

Output
Print the value of the expression without leading zeros.

Examples
Input
4
Output
4
Input
124356983594583453458888889
Output
0
思路:本来第一眼看了,n很大,要用快速幂来做,但是在纸上写了几个例子之后,发现n=0时,结果是4,n=1,2,3,结果是0,n=4时结果是4,n=5,6,7,结果是0,n=8时结果时4,依次类推,可以知道这是有规律的,n%4=0时,结果是4,其余结果0.
代码

#include <cstdio>
#include <cstring>
char n[100005]; 
int func(){
	int tmp=0,res;
	for(int i=0;i<strlen(n);i++){
		res=(tmp*10+n[i]-48)%4;
		tmp=res;
	}
	return res;
}
int main(){
	scanf("%s",n);
	int flag=func();
	if(flag==0) puts("4");
	else puts("0");
	return 0;
}
发布了32 篇原创文章 · 获赞 0 · 访问量 644

猜你喜欢

转载自blog.csdn.net/weixin_45794203/article/details/103985808