https://www.cnblogs.com/njczy2010/p/3930688.html

https://blog.csdn.net/dominating413421391/article/details/44203019?locationNum=1&fps=1

https://blog.csdn.net/piekey1994/article/details/38436511 [数学]

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
int len;
int n = 1000;
int next[10000];
int f[50000];
char s[50000];
int Pow(int a, int b)
{
int res=1;
while(b)
{
if(b&1)
res=res*a%7;
a=a*a%7;
b>>=1;
}
return res;
}
void getnext()
{
int j=0,k=-1;
next[0]=-1;
while(j<n)
{
if(k==-1||s[j]==s[k])
{
next[++j]=++k;
}
else
{
k=next[k];
}
}
}
int main()
{
for(int i=1;i<=600;i++)
{
f[i] = f[i-1] + Pow(i,i);
f[i]%=7;
}
for(int i=0;i<=600;i++)
{
sprintf(&s[i],"%d",f[i]);
}
s[601]='\0';
cout<<s<<endl;
getnext();
for(int i=1;i<=600;i++) //由题意可知,长度为1的字符串不算
{
len=i-next[i]; //len代表最小循环节
if(i%len==0 && i/len>1) //周期大于1才是循环串
{
cout<<"ans = "<<len<<endl;
}
}
printf("\n");
}
#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;
ll x[1005];
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"}; x[0]=0;
for(int i=1;i<=1000;++i)
x[i]=(x[i-1]%7+mod(i,i,7))%7;
int next[1001];
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;
}
}
return 0;
}

妙用next数组打表求最小循环节len的更多相关文章

  1. HDU-3746 Cyclic Nacklace 字符串匹配 KMP算法 求最小循环节

    题目链接:https://cn.vjudge.net/problem/HDU-3746 题意 给一串珠子,我们可以在珠子的最右端或最左端加一些珠子 问做一条包含循环珠子的项链,最少还需要多少珠子 思路 ...

  2. The Minimum Length - HUST 1010(求最小循环节)

    题意:有个一字符串A(本身不是循环串),然后经过很多次自增变成AAAAA,然后呢从自增串里面切出来一部分串B,用这个串B求出来A的长度.   分析:其实就是求最小循环节.......串的长度 - 最大 ...

  3. hdu 3746 Cyclic Nacklace(next数组求最小循环节)

    题意:给出一串字符串,可以在字符串的开头的结尾添加字符,求添加最少的字符,使这个字符串是循环的(例如:abcab 在结尾添加1个c变为 abcabc 既可). 思路:求出最小循环节,看总长能不能整除. ...

  4. poj 2185 Milking Grid(next数组求最小循环节)

    题意:求最小的循环矩形 思路:分别求出行.列的最小循环节,乘积即可. #include<iostream> #include<stdio.h> #include<stri ...

  5. next数组求最小循环节

    1.kmp产生的next数组: 最小循环节(长度)=len-next[len]; 证明: ----------------------- ----------------------- k    m ...

  6. hdoj3746(kmp算法的nex数组求最小循环节)

    题目链接:https://vjudge.net/problem/HDU-3746 题意:给定一个字符串,问最少在两端添加多少元素使得整个字符串是呈周期性的. 思路: 应用到kmp中nex数组的性质,数 ...

  7. poj2406--Power Strings(KMP求最小循环节)

    Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 33178   Accepted: 13792 D ...

  8. KMP 求最小循环节

    转载自:https://www.cnblogs.com/chenxiwenruo/p/3546457.html KMP模板,最小循环节   下面是有关学习KMP的参考网站 http://blog.cs ...

  9. HDU 3746 - Cyclic Nacklace & HDU 1358 - Period - [KMP求最小循环节]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3746 Time Limit: 2000/1000 MS (Java/Others) Memory Li ...

随机推荐

  1. [技巧篇]06.关于防止SQL注入的方式,不使用预处理

    在一期,二期阶段,有一些同学,对于SQL语句总是使用字符串的拼接,这是一个比较坏的毛病,这样非常影响我们的程序的安全性,所以一般情况下我们都推荐预处理模式,针对这种模式希望不了解的同学去努力学习,下面 ...

  2. Http字段含义

    转载自:http://blog.csdn.net/sand_ant/article/details/10503579 一.request请求Header简介 Accept:--客户机支持的类型 Acc ...

  3. (译)理解python线程

    看到一篇老外写的线程文章,很赞,零基础都能看懂.先贴在这里,有时间再翻译出来. http://agiliq.com/blog/2013/09/understanding-threads-in-pyth ...

  4. MongoDB入门(3)- MongoDB备份与恢复

    1. 备份 MongoDB提供了备份工具,mongodump.exe,在bin目录下,其用法如下: mongodump.exe -h localhost -d database_name -o d:\ ...

  5. poi-对于word的操作(二)

    poi对于word文本的底纹和下划线的样式的展现 package poi.test; import java.io.FileOutputStream; import java.math.BigInte ...

  6. bzoj 1702: [Usaco2007 Mar]Gold Balanced Lineup 平衡的队列——map+hash+转换

    Description N(1<=N<=100000)头牛,一共K(1<=K<=30)种特色, 每头牛有多种特色,用二进制01表示它的特色ID.比如特色ID为13(1101), ...

  7. 【BZOJ】1299: [LLH邀请赛]巧克力棒

    [算法]博弈论 [题解]这道题不是典型的SG函数题了. 不把它当成游戏看待,那么这道题是在说n个石子堆,每次可以加入若干个或进行Nim游戏. 我们当前先手,则考虑构造必败态来获胜. 当前已加入的NIm ...

  8. iOS开发者两分钟学会用GitHub在Mac上托管代码的两种方法

        原文发布者:http://blog.csdn.net/duxinfeng2010 在Mac上使用Xcode进行iOS-Apple苹果iPhone手机开发过程中少不了使用GitHub在Mac上托 ...

  9. 【洛谷 P1452】 Beauty Contest (二维凸包,旋转卡壳)

    题目链接 旋转卡壳模板题把. 有时间再补总结吧. #include <cstdio> #include <cmath> #include <algorithm> u ...

  10. vue-cli proxyTable中跨域中pathRewrite 怎么用

    问:proxyTable 里面的pathRewrite里面的‘^/iclient’:'' 什么意思? 答:用代理, 首先你得有一个标识, 告诉他你这个连接要用代理. 不然的话, 可能你的 html, ...