Labs_test1 A

Labs_test1  A - Fedya and Maths  欧拉定理

A - Fedya and Maths
Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

Description

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

(1n + 2n + 3n + 4nmod 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.

Sample Input

Input
4
Output
4
Input
124356983594583453458888889
Output
0

Hint

Operation x mod y means taking remainder after division x by y.

Note to the first sample:

思路:由欧拉定理:a,n互质,则a^ph(n)=1 (mod n)。

         ∑ i^n mod 5 = ∑ i^(n/ph(5) *ph(5) +n mod ph(5)) mod n

                            = ∑ (i^(k*ph(5))*(i^n mod ph(5)) mod n

                            = ∑ 1 * (i^ (n mod ph(5))mod n

以及用同余模求大数n mod ph(5)

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<string>
#include<math.h>
#include<cctype>
#define ll long long
#define REP(i,a,b) for(int (i)=(a);(i)<=(b);(i)++)
#define REPP(i,a,b,t) for(int (i)=(a);(i)<=(b);(i)+=(t))
#define rep(i,a,b) for(int (i)=(a);(i)>=(b);(i)--)
#define repp(i,a,b,t) for(int (i)=(a);(i)>=(b);(i)-=(t))
#define PII pair<int,int>
#define fst first
#define snd second
#define MP make_pair
#define PB push_back
#define RI(x) scanf("%d",&(x))
#define RII(x,y) scanf("%d%d",&(x),&(y))
#define RIII(x,y,z) scanf("%d%d%d",&(x),&(y),&(z))
#define DRI(x) int (x);scanf("%d",&(x))
#define DRII(x,y) int (x),(y);scanf("%d%d",&(x),&(y))
#define DRIII(x,y,z) int (x),(y),(z);scanf("%d%d%d",&(x),&(y),&(z))
#define RS(x) scanf("%s",x)
#define RSS(x,y) scanf("%s%s",x,y)
#define DRS(x) char x[maxn];scanf("%s",x)
#define DRSS(x,y) char x[maxn],y[maxn];scanf("%s%s",x,y)
#define MS0(a) memset((a),0,sizeof((a)))
#define MS1(a) memset((a),-1,sizeof((a)))
#define MS(a,b) memset((a),(b),sizeof((a)))
#define ALL(v) v.begin(),v.end()
#define SZ(v) (int)(v).size()

using namespace std;

const int maxn=1000100;
const int INF=(1<<29);
const double EPS=0.0000000001;
const double Pi=acos(-1.0);

char s[maxn];
int a[maxn];

ll qpow(ll n,ll k,ll MOD)
{
    ll res=1;
    while(k){
        if(k&1) res=((res%MOD)*(n%MOD))%MOD;
        n=(n%MOD)*(n%MOD);
        k>>=1;
    }
    return res;
}

int main()
{
    //freopen("in.txt","r",stdin);
    while(~RS(s)){
        int len=strlen(s);
        REP(i,0,len-1) a[i]=s[len-1-i]-'0';
        int k=0;
        REP(i,0,len-1){
            k=((a[i]%4)*qpow(10,i,4)+k%4)%4;
        }
        ll ans=0;
        REP(i,1,4) ans=(ans%5+qpow(i,k,5)%5)%5;
        cout<<ans<<endl;
    }
    return 0;
}
View Code
没有AC不了的题,只有不努力的ACMER!
原文地址:https://www.cnblogs.com/--560/p/4688061.html