B - Fedya and Maths

B - Fedya and Maths

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

(1^n + 2^n + 3^n + 4^n) 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:

img

题目描述:

计算上述式子,但是n会很大,只能用字符串存。

分析:

如果是幂函数mod 5 或10,就可以发现一个规律。每次取模后都是4个数循环的。

如题

(1^n + 2^n + 3^n + 4^n) mod 5可以写成((1^n)mod 5+(2^n)mod 5+(3^n)mod 5+(4^n)mod 5)mod5,我们不妨举几个数试试。

(2^1)mod 5=2,(2^2)mod 5=4,(2^3)mod 5=3,(2^4)mod 5=1,

(2^6)mod 5=2,(2^6)mod 5=4。。。。。。

同理3是2,4,3,1 , 2,4,3,1。。。循环的

4是4,1,4,1, 4,1,4,1。。。循环的

再把式子加起来可以发现是10,10,10,4, 10,10,10,4......循环的。

对他mod 5就是0,0,0,4。。。。。。循环。

然后就是判断字符串代表的数是否能被4整除了。

因为4*25=100所以百位以上的数全能被4整除。所以只需要判断后面2位即可。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int main()
{
    char num[100000];
    scanf("%s",num);
    //取最后2位 
    int len=strlen(num);
    int a;
    if(len>=2)
    a=(num[len-1]-'0')+(num[len-2]-'0')*10;
    else
    a=num[0];
    if(a%4==0) cout<<'4';
    else cout<<'0';
    return 0;
} 
​
 

猜你喜欢

转载自www.cnblogs.com/studyshare777/p/12194089.html