妙用next数组打表求最小循环节len
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的更多相关文章
- HDU-3746 Cyclic Nacklace 字符串匹配 KMP算法 求最小循环节
题目链接:https://cn.vjudge.net/problem/HDU-3746 题意 给一串珠子,我们可以在珠子的最右端或最左端加一些珠子 问做一条包含循环珠子的项链,最少还需要多少珠子 思路 ...
- The Minimum Length - HUST 1010(求最小循环节)
题意:有个一字符串A(本身不是循环串),然后经过很多次自增变成AAAAA,然后呢从自增串里面切出来一部分串B,用这个串B求出来A的长度. 分析:其实就是求最小循环节.......串的长度 - 最大 ...
- hdu 3746 Cyclic Nacklace(next数组求最小循环节)
题意:给出一串字符串,可以在字符串的开头的结尾添加字符,求添加最少的字符,使这个字符串是循环的(例如:abcab 在结尾添加1个c变为 abcabc 既可). 思路:求出最小循环节,看总长能不能整除. ...
- poj 2185 Milking Grid(next数组求最小循环节)
题意:求最小的循环矩形 思路:分别求出行.列的最小循环节,乘积即可. #include<iostream> #include<stdio.h> #include<stri ...
- next数组求最小循环节
1.kmp产生的next数组: 最小循环节(长度)=len-next[len]; 证明: ----------------------- ----------------------- k m ...
- hdoj3746(kmp算法的nex数组求最小循环节)
题目链接:https://vjudge.net/problem/HDU-3746 题意:给定一个字符串,问最少在两端添加多少元素使得整个字符串是呈周期性的. 思路: 应用到kmp中nex数组的性质,数 ...
- poj2406--Power Strings(KMP求最小循环节)
Power Strings Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 33178 Accepted: 13792 D ...
- KMP 求最小循环节
转载自:https://www.cnblogs.com/chenxiwenruo/p/3546457.html KMP模板,最小循环节 下面是有关学习KMP的参考网站 http://blog.cs ...
- 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 ...
随机推荐
- Mantis 从Windows 迁移到Linux上
1. 导出windows manits的mysql数据库文件, 在cmd运行:mysqldump -uroot -p3edc$RFV bugtracker > C:/mantis.sql; 2. ...
- Robot Framework 常用关键字使用方法
1. Select From Listid=sourceConnoracle_source 从下拉框选取值. 2. Select Radio button name value 选择单选框.也可以点 ...
- 【vijos】P1083 小白逛公园
[算法]线段树 [题解] 学自:https://vijos.org/p/1083/solution(wang_yanheng的回答) 回溯时维护一段区间的以下域: sumL:从左端点起连续区间的最大和 ...
- java 连接MySQL的代码
1.java connect MySQL as conding. https://www.cnblogs.com/centor/p/6142775.html
- win10以前连接过的wifi密码怎么查看
右键点击开始,在菜单中选择打开命令提示符,以管理员的权限打开. 然后输入命令netsh wlan show profile显示以前此电脑连接过的所有WIFI记录配置信息. 确定要查看的WIFI ...
- webpack_配置和使用教程
webpack是一个模块打包的工具,它的作用是把互相依赖的模块处理成静态资源. webpack 可以使用 loader 来预处理文件.这允许你打包除 JavaScript 之外的任何静态资源.你可以使 ...
- windows+nexus+maven环境搭建(转)
windows nexus环境搭建 1.下载nexus 版本为 nexus-2.11.4-01-bundle 下载地址 这里写链接内容 2.将下载好的文件放到D盘进行解压 3.解压后目录结构 nexu ...
- 如何得知 kernel 或 android 已開機多久時間
adb shell cat /proc/uptime 中的第一個數字, adb shell cat "/proc/uptime" 210.79 312.76 或者是 kernel ...
- 【模板】BZOJ 1692:队列变换—后缀数组 Suffix Array
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1692 题意: 给出一个长度为N的字符串,每次可以从串头或串尾取一个字符,添加到新串中,使新串 ...
- codevs 3287 货车运输 NOIP2013提高组
题目链接:http://codevs.cn/problem/3287/ 题解: 和bzoj3732一毛一样,只不过是找最大生成树和最小值罢了,具体参见我的bzoj3732的博客 #include< ...