2019 GDUT 新生专题Ⅳ B题

本文介绍了一种解决数学作业中特定表达式计算问题的高效方法。通过观察和总结规律,发现当n为特定形式时,结果呈现周期性变化。利用这一特性,即使n的值极大,也能迅速得出结果,无需进行复杂计算。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值