当前位置: 代码迷 >> 综合 >> ZOJ 3785 What day is that day? (指数循环定理+最小公倍数)
  详细解决方案

ZOJ 3785 What day is that day? (指数循环定理+最小公倍数)

热度:100   发布时间:2023-11-15 13:41:37.0

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3785

#include<bits/stdc++.h>
using namespace std;#define debug puts("YES");
#define rep(x,y,z) for(int (x)=(y);(x)<(z);(x)++)
#define ll long long#define lrt int l,int r,int rt
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define root l,r,rt
#define mst(a,b) memset((a),(b),sizeof(a))#define pii pair<ll,ll>
#define mk(x,y) make_pair(x,y)
#define fi first
#define se secondconst int  maxn =1e5+5;
const int mod=7;
const int ub=1e6;
ll powmod(ll x,ll y){ll t; for(t=1;y;y>>=1,x=x*x%mod) if(y&1) t=t*x%mod; return t;}
ll gcd(ll x,ll y){return y?gcd(y,x%y):x;}
/*
题目大意:求式子sigma i^i i=1~N,其中N给定模7后的答案。
不难发现7是质数,然后指数上对于所有数都会模6,
那么其中是有个循环节在里面的,如何找到呢?
我们是要发现一个x,这个x既可以被7整除,又可以被6整除,这样,
它下一个的数就会变成1^1了,明显这个循环节是42,最小公倍数。
打表前42个数即可。
时间复杂度:O(logn)。
*/ll res[43],tot=0,n;
char s[7][15]={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};int main(){for(int i=1;i<=42;i++) res[i]=(powmod(i,i)+res[i-1])%mod;int t;scanf("%d",&t);while(t--){scanf("%lld",&n);ll ans=(n/42*res[42]%mod+res[n%42])%mod;printf("%s\n",s[(ans+6)%mod]);}return 0;
}

 

  相关解决方案