Codeforces 453B. 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^nmod 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 ≤ 10^(10^5)). The number doesn't contain any leading zeroes.

Output

Print the value of the expression without leading zeros.

从输入就知道肯定得找规律

使用广义欧拉定理简化

  a^b≡a^(b%φ(m)+φ(m))(mod m)   (b > φ(m) )  
易得在此题中只有顶多φ(5)=4种情况,全部算出来后分别是4,0,0,0,其中4在n整除4时得到,所以问题便变成了判断该数是否整除4
也就是末尾两位数是否整除4
#include <cstdio>
#include <cctype>
#include <stdlib.h>
#include <iostream>
#include <cmath>
#include <iomanip>
#include <cstring>
#include <algorithm>
#include <string>
#include <vector>
#include <map>

using namespace std;
typedef long long LL;

char s[100005];
int main()
{
  // freopen("test.in","r",stdin);
  cin >> s;
  int len = strlen(s);
  int now = 0,base = 1;
  for (int i=len-1;i>=max(0,len-2);i--){
    now += base * (s[i] - '0');
    base *= 10;
  }
  if (now % 4){
    cout << 0;
  }
  else
    cout << 4;
  return  0;
}
View Code
原文地址:https://www.cnblogs.com/ToTOrz/p/6856548.html