您的位置:首页 > 其它

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

2016-04-11 13:09 344 查看
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: