B – Fedya and Maths
Fedya studies in a gymnasium. Fedya’s maths hometask is to calculate the following expression:
(1n + 2n + 3n + 4n) mod 5
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 ≤ 10105). 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
Note
Operation x mod y means taking remainder after division x by y.
Note to the first sample:
题意大概就是一个很大的数,然后让你注意输入,还特别说明,输入的范围The single line contains a single integer n (0 ≤ n ≤ 10105).大于任何一种数据类型,然后,输出下面这个公式的解.
正常的做法肯定是想法设法的将输入模仿大数的输入方式,中间尽可能的优化时间复杂度:
然而最后可能。。。不是很乐观啊
只时候有一种针对各种应试的方法,名曰,暴力解题法
我们不妨就先用long long输入;然后先输出十个八个解,找找规律,比如
1
0
2
0
3
0
4
4
5
0
6
0
7
0
8
4
9
0
10
0
1
0
11
0
12
4
444
4
444444
4
44444444
4
2145151371
0
然后找规律,发现。。只要能被4整除就没问题;
然后只需:这么几笔就足够解决问题了
#include<stdio.h>
int main()
{
long long n;
while(~scanf("%lld",&n))
{
if(n%4==0)
printf("4\n");
else
printf("0\n");
}
return 0;
}
当然开始用c++怎么提交都过不了,我想我这思路应该没问题啊,改为C语言就过了,当然还是靠人指点了一下。
附上正规的做法;
//易发现本题的循环节 [0,0,0,4],[0,0,0,4],...
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<functional>
#include<iostream>
#include<cmath>
#include<cctype>
#include<ctime>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define Lson (x<<1)
#define Rson ((x<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (100000007)
#define MAXN (100000+10)
long long mul(long long a,long long b){return (a*b)%F;}
long long add(long long a,long long b){return (a+b)%F;}
long long sub(long long a,long long b){return (a-b+(a-b)/F*F+F)%F;}
typedef long long ll;
char c[MAXN];
int main()
{
// freopen("b.in","r",stdin);
// freopen(".out","w",stdout);
scanf("%s",c+1);
int n=strlen(c+1);
int t;
if (n==1) t=c[1]-'0';
else t=c[n]-'0'+(c[n-1]-'0')*10;
if (t%4==0) cout<<"4\n";
else cout<<"0\n";
return 0;
}
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/114880.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...