转自:http://www.cnblogs.com/kevince/p/3887827.html

首先声明一下,这里的规律指的是循环,即找到最小循环周期。

这么一说大家心里肯定有数了吧,“不就是next数组性质的应用嘛”,没错,正是如此。

在ACM的比赛中有些时候会遇到一些题目,可以或必须通过找出数据的规律来编写代码,这里我们专门来讨论下 如何运用KMP中next数组的性质 来寻找一个长数组中的最小循环周期。

先来看一道题

ZOJ 3785

What day is that day?


Time Limit: 2 Seconds      Memory Limit: 65536 KB

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.

题目的大意是知道今天是周六,让你求 f = 11 + 22 + 33 + ... + NN 这么多天之后是星期几。

也就是求f % 7对于每个输入的N的值。这题在网上一搜题解,都说是打表找规律,当然这题有两种找法,一是对于每个ii  % 7 的值都找规律。

这里我们打表可知 前100个值如下所示

1 4 6 4 3 1 0 1 1 4 2 1 6 0 1 2 5 1 5 1 0 1 4 1 4 4 6 0 1 1 3 2 6 1 0 1 2 2 1 2 6 0 1 4 6 4 3 1 0 1 1 4 2 1 6 0 1 2 5 1 5 1 0 1 4 1 4 4 6 0 1 1 3 2 6 1 0 1 2 2 1 2 6 0 1 4 6 4 3 1 0 1 1 4 2 1 6 0 1 2

有一种找规律的方法是当有数字等于第一个数的时候做个标记,再人工判断是否能够构成一个循环。

不可否认的,对于周期较短的一组数字这样找周期并不难,可是如果周期大到数百数千甚至数万时,靠这种方法找周期恐怕是杯水车薪。

当时我就迷茫在了这一长串的数字中不知所措,猛然想起前不久看过的KMP中next数组的性质,当即想到了用KMP求最小重复子串长度的方法,于是脑洞大开……

该性质为:令j=leni-next[i],如果i%j==0且i/j>1,j就是Pi的最小循环节( Pi表示文本串的前i个字符,leni表示该字符串的长度,一般表示为leni = i + 1)

还有一种找规律的方法是直接对 f % 7 的值进行打表找规律,按照上述方法找到的周期为294,下面要做的就很简单了~

 #include <iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<vector>
#define N 605
#define M 200
#define ll long long
using namespace std;
int i,j;
int T;
int l;
int a[N],sum[N];
char s[N];
int next[N];
int fun(int n)
{
int re = ;
for(int i=;i<=n;i++){
re*=n;
re%=;
}
return re;
}
void ini()
{
a[]=sum[]=;
a[]=sum[]=;
s[]='';
s[]='';
for(i=;i<=N-;i++){
a[i]=fun(i);
sum[i]+=a[i]+sum[i-];
sum[i]%=;
s[i]=sum[i]+'';
}
s[i]='\n';
//printf("%s\n",s);
// for(i=1;i<=M;i++){
// printf(" %d %d %d\n",i,a[i],sum[i]);
// printf(" %d",sum[i]);
//}
return;
} void ini2()
{
int j = -, i = ;
next[] = -;
while(i < N-)
{
if(j == - || s[i] == s[j])
{ i++;
j++;
next[i] = j;
}
else
{
j = next[j];
}
}
} void ini3()
{
for(int i = ; i <= N-; ++i)
{
int length = i - next[i]; //循环节的长度
if(i != length && i % length == ) //如果有多个循环
{
printf("%d %d\n", i, i / length);
break;
} }
} int main()
{
int x;
int ans;
ini();
//ini2();
// ini3();
//freopen("data.txt","r",stdin);
scanf("%d",&T);
//while(scanf("%d",&n)!=EOF)
while(T--)
{
scanf("%d",&x);
ans=sum[x%];
if(ans==) printf("Saturday\n");
else if(ans==) printf("Sunday\n");
else if(ans==) printf("Monday\n");
else if(ans==) printf("Tuesday\n");
else if(ans==) printf("Wednesday\n");
else if(ans==) printf("Thursday\n");
else if(ans==) printf("Friday\n");
}
return ;
}

后来又练了一次,用了map

4172110 2016-03-15 15:26:39 Accepted 3785 C++ 820 280 njczy2010
 #include <iostream>
#include <cstdio>
#include <map>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string> using namespace std; #define N 1005
#define ll long long
#define mod 7 int TT;
int n;
int s[N];
int next[N];
map<int,string> mp; void get_next()
{
int i,j;
i = ;
j = -;
next[] = -;
while(i<N)
{
if(j == - || s[i] == s[j]){
i++;j++;next[i] = j;
}
else{
j = next[j];
}
}
} int quickpow(int x,int n)
{
int re = ;
while(n)
{
if(n&){
re = (re * x) % mod;
}
n /= ;
x = (x * x) % mod;
}
return re;
} void fi()
{
int i;
for(i = ;i < N;i++){
int length = i - next[i]; //循环节的长度
if(i != length && i % length == ) //如果有多个循环
{
printf("%d %d %d\n", i, length, i / length);
break;
}
}
} void ini()
{
int i;
s[] = ;
for(i = ;i < N;i++){
s[i] = (s[i - ] + quickpow(i,i) )%mod;
}
mp[] = "Saturday";
mp[] = "Sunday";
mp[] = "Monday";
mp[] = "Tuesday";
mp[] = "Wednesday";
mp[] = "Thursday";
mp[] = "Friday";
//get_next();
//fi();
//
} int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
ini();
scanf("%d",&TT);
while(TT--){
//while(scanf("%d",&n)!=EOF){
scanf("%d",&n);
cout << mp[ s[ n% ] ] << endl;
}
return ;
}

【ZOJ】3785 What day is that day? ——KMP 暴力打表找规律的更多相关文章

  1. 【ZOJ】3785 What day is that day? ——浅谈KMP在ACM竞赛中的暴力打表找规律中的应用

    转载请声明出处:http://www.cnblogs.com/kevince/p/3887827.html    ——By Kevince 首先声明一下,这里的规律指的是循环,即找到最小循环周期. 这 ...

  2. ZOJ 3622 Magic Number 打表找规律

    A - Magic Number Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Subm ...

  3. zoj 3785 What day is that day? (打表找规律)

    题目 思路:比赛的时候有想过找循环节,但是,打表打错了. 后来,看着过了挺多人,就急了, 看了一下别人的时间 耗时都挺长的,就以为不是找规律, 没想到真是找规律,不过,这个题的数据可能挺大的. AC代 ...

  4. zoj 3629 Treasure Hunt IV 打表找规律

    H - Treasure Hunt IV Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu ...

  5. 【ARC077F】SS kmp+打表找规律

    Description ​ 如果某个串可以由两个一样的串前后连接得到,我们就称之为"偶串".比如说"xyzxyz"和"aaaaaa"是偶串, ...

  6. ZOJ 3785 What day is that day?(今天是星期几?)

    Description 题目描述 It's Saturday today, what day is it after 11 + 22 + 33 + ... + NN days? 今天是星期六,11 + ...

  7. ZOJ 3587 Marlon&#39;s String 扩展KMP

    链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3587 题意:给出两个字符串S和T.S,T<=100000.拿出 ...

  8. zoj 3785 What day is that day?

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5272 打表找规律. #include <cstdio> #incl ...

  9. ZOJ 3785 What day is that day?(数论:费马小定理)

    What day is that day? Time Limit: 2 Seconds      Memory Limit: 65536 KB It's Saturday today, what da ...

随机推荐

  1. Android开发工具常用快捷键大全

    Android开发中常用的开发工具有android studio和eclipse两种,下面小编整理了一些这两种开发工具中常用的快捷键,使用这些快捷键,你的android编程将事半功倍. android ...

  2. Socket常见错误代码与描述

    最近程序 出现 几次 Socket 错误, 为了便于 差错.. 搜了一些 贴在这里.. 出现网络联机错误Socket error #11001表示您的计算机无法连上服务器,请检查您的Proxy设定以及 ...

  3. Powershell使用真实的对象工作

    Powershell使用真实的对象工作 来源 https://www.pstips.net/powershell-work-with-reallife-objects.html 每一个Powershe ...

  4. SNMP协议介绍

    SNMP协议介绍 一.什么是SNMP SNMP:“简单网络管理协议”,用于网络管理的协议.SNMP用于网络设备的管理.SNMP的工作方式:管理员需要向设备获取数据,所以SNMP提供了 “读”操作:管理 ...

  5. BZOJ5323 [Jxoi2018]游戏 【数论/数学】

    题目链接 BZOJ5323 题解 有一些数是不能被别的数筛掉的 这些数出现最晚的位置就是该排列的\(t(p)\) 所以我们只需找出所有这些数,线性筛一下即可,设有\(m\)个 然后枚举最后的位置 \[ ...

  6. BZOJ1607 [Usaco2008 Dec]Patting Heads 轻拍牛头 【筛法】

    题目 今天是贝茜的生日,为了庆祝自己的生日,贝茜邀你来玩一个游戏. 贝茜让N(1≤N≤100000)头奶牛坐成一个圈.除了1号与N号奶牛外,i号奶牛与i-l号和i+l号奶牛相邻.N号奶牛与1号奶牛相邻 ...

  7. rman异机恢复,全部恢复和增量恢复

    1.首先准备工作:hostname 192.168.222.11 ol7.localdomain ol7建立相关目录:mkdir -p /u01/app/oracle/oradata/DB11G/mk ...

  8. 浏览器 连不上网 (3):DNS 服务器问题

    解决:设置一下DNS服务器的地址 步骤: 打开网络和共享中心(网络和 Internet设置)-> 更改适配器 -> 双击我们连接的 无线网(WiFi式) 或 以太网(网线式): 从出现的窗 ...

  9. php windows rename 中文出错

    php windows rename 中文出错 rename()函数可以重命名文件.目录等,但是要注意目的地和起始地址的编码. 比如:我的PHP文件编码是UTF-8,但是在WINDOW系统中中文默认编 ...

  10. 【CF1023F】Mobile Phone Network(dsu,MST)

    题意: 保证原边以边权单调非减的顺序读入 思路:先把未知边加入,再加入原始边做MST,考虑从大到小,用数据结构维护,每一条原始边相当两个链赋值操作,每一条未知边相当于一个询问,答案即为询问之和 LCT ...