zoj 3785 What day is that day?(找规律,KMP)

It's Saturday today, what day is it after 11 + 22 + 33 + ... + NN days?

Input

There are multiple test cases. The first line of input contains an integer T indicating the number of test cases. For each test case:

There is only one line containing one integer N (1 <= N <= 1000000000).

Output

For each test case, output one string indicating the day of week.

Sample Input
2
1
2
Sample Output
Sunday
Thursday
Hint

A week consists of Sunday, Monday, Tuesday, Wednesday, Thursday, Friday and Saturday.

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5272

这题很难找到规律(294太大了),但是用KMP里的next数组就可以很快找到。

#include<iostream>
#include<algorithm>
#include<string>
#include<map>
#include<cmath>
#include<string.h>
#include<stdlib.h>
#include<cstdio>
#define ll long long
using namespace std;
long mod(long a,long n,long b){    //a^b mod c
    long t;
    if(n==0) return 1%b; 
    if(n==1) return a%b;
    t=mod(a,n/2,b);
    t=t*t%b;
    if((n&1)==1) t=t*a%b;
    return t;}
int main(){
	string s[7]={"Saturday","Sunday","Monday","Tuesday","Wednesday","Thursday","Friday"};
	ll x[1005];
	x[0]=0;
	for(int i=1;i<=294;++i)
	x[i]=(x[i-1]%7+mod(i,i,7))%7;
//	ll x[1005];
//	x[0]=0;
//	for(int i=1;i<=1000;++i)
//		x[i]=(x[i-1]%7+mod(i,i,7))%7;
//	int next[1001];0
//	next[0] = 0;
//	int len=1000;
//	for(int i = 1, q = 0; i < len; i++){ //x[]里放的是要找规律的数组 
//   	while(q > 0 && x[i] != x[q])
//     		 q = next[q-1];
//   		 if (x[i] == x[q])
//    		    q++;
//   		 next[i] = q;
//   	 	 if (q != 0 && (i + 1) % (i + 1 - q) == 0){
//     		     printf("%d\n", i+1-q);  //输出的就是最大循环长度 
//    	  		 break;
//    	}
//    }
	int t;
	cin>>t;
	while(t--){
		int a;
		cin>>a;
		cout<<s[x[a%294]]<<endl;
    }
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值