天天看点

ZOJ 3785-What day is that day?-数论(费马小定理) / 打表找规律

http://www.icpc.moe/onlinejudge/showProblem.do?problemCode=3785

求 【1^1 +2^2 +3^3 +....+n^n】%7的答案

因为7是素数,根据费马小定理,可以把指数降幂为0~5

根据取模的性质也可以把底数降幂为0~6

因此 指数循环节为6,底数循环节为7,最小公倍数为42,所以表的循环节为42,因此直接暴力算n%42的部分即可

当然你可以打表找规律咯

注意求i^i用pow有精度丢失,可以用快速幂代替

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<set>
#include<map>
#include<vector>
#include<string.h>
using namespace std;
string mp[]={"Saturday","Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday"};

long long pow_m(long long a,long long b,long long c)
{
    long long ans=1,tmp=a;
    while(b)
    {
        if (b&1)
        ans=ans*tmp%c;
        tmp=tmp*tmp%c;
        b>>=1;
    }
    return ans%c;
}

int main()
{


   //freopen("xl_in.txt","r",stdin);

   int t;
   scanf("%d",&t);
   while(t--)
   {
        int i=1,j=1;
          long long ans=0;
          int n;
          cin>>n; 
        ans+=(n/42*6)%7;
          n=n%42; 
          for(int k=0;k<n;k++)
          {

              ans+= pow_m((i++)%7,(j++)%6,7);
          }
         // printf("%lld\n",ans%7);
          cout<<mp[ans%7]<<endl;

  }

    return 0;
}