妙用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 ...
随机推荐
- maven中用yuicompressor和closure-compiler对js、css文件进行压缩
转载自:http://matychen.iteye.com/blog/1477350 项目采用maven构建的时候,需要压缩js,css等,网上找了相关资料,自己综合了下- 直接放代码: <! ...
- map/reduce之间的shuffle,partition,combiner过程的详解
Shuffle的本意是洗牌.混乱的意思,类似于java中的Collections.shuffle(List)方法,它会随机地打乱参数list里的元素顺序.MapReduce中的Shuffle过程.所谓 ...
- iOS tag的使用
一.添加标记 (标记不能为0) UIButton *backBtn = [[UIButton alloc] initWithFrame:CGRectMake(,,,)]; backBtn.backgr ...
- ssh无密码登录认证失败
http://www.2cto.com/os/201401/271150.html https://unix.stackexchange.com/questions/36540/why-am-i-st ...
- HDFS不存在绝对路径,无法找到文件所在具体位置
This is set in the dfs.datanode.data.dir property, which defaults to file://${hadoop.tmp.dir}/dfs/da ...
- 基于 python imageai 对象检测 目标检测 识别 视频
1.视频连接如下: http://www.iqiyi.com/w_19s6vownit.html
- 关于this问题
对于关键字this,其实很好理解,谁调用我就指向谁.下面举个例子说明: 其实这也是在学习闭包中的一个案例: var name = "The window"; var obj = { ...
- 【Matlab】让Matlab程序发出声音
我有时候运行一段很长的代码,在等待的时候去做别的事,希望程序运行完可以有一个提示音. 这可以用matlab的一个函数sound实现,该函数的输入参量是音频数据向量.采样频率和转换位数. % 响一声 s ...
- python设计模式之单例模式(二)
上次我们简单了解了一下什么是单例模式,今天我们继续探究.上次的内容点这 python设计模式之单例模式(一) 上次们讨论的是GoF的单例设计模式,该模式是指:一个类有且只有一个对象.通常我们需要的是让 ...
- sicily 1009. Mersenne Composite N
Description One of the world-wide cooperative computing tasks is the "Grand Internet Mersenne P ...