ProjectEuler654
我,ycl:BM是什么早就忘了!
毕老爷:那你们可以做一做这道题练练BM板子啊。
//Achen
#include<bits/stdc++.h>
#define For(i,a,b) for(int i=(a);i<=(b);i++)
#define Rep(i,a,b) for(int i=(a);i>=(b);i--)
#define Formylove return 0
const int N=1e5+,p=1e9+;
typedef long long LL;
typedef double db;
using namespace std;
int n,m;
LL f[][]; template<typename T> void read(T &x) {
char ch=getchar(); T f=; x=;
while(ch!='-'&&(ch<''||ch>'')) ch=getchar();
if(ch=='-') f=-,ch=getchar();
for(;ch>=''&&ch<='';ch=getchar()) x=x*+ch-''; x*=f;
} int main() {
freopen("1.in","r",stdin);
//freopen("biao.in","w",stdout);
read(m); read(n);
For(i,,m) f[][i]=;
For(i,,n) {
if(i>) For(j,,m) f[i][j]=f[i-][m-j];
For(j,,m) f[i][j]=(f[i][j]+f[i][j-])%p;
}
printf("%lld\n",f[][m]);
//printf("%d\n",n);
//For(i,1,n) printf("%lld ",f[i][m]);
//printf("%lld\n",f[n][m]);
Formylove;
}
n^2暴力
//Achen
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<vector>
#include<cstdio>
#include<queue>
#include<cmath>
#include<set>
#include<map>
#define Formylove return 0
#define For(i,a,b) for(int i=(a);i<=(b);i++)
#define Rep(i,a,b) for(int i=(a);i>=(b);i--)
const int N=,p=1e9+;
typedef long long LL;
typedef double db;
using namespace std;
int n,cnt,fail[N];
vector<LL>f[N];
LL a[N],delta[N]; template<typename T>void read(T &x) {
char ch=getchar(); x=; T f=;
while(ch!='-'&&(ch<''||ch>'')) ch=getchar();
if(ch=='-') f=-,ch=getchar();
for(;ch>=''&&ch<='';ch=getchar()) x=x*+ch-''; x*=f;
} LL ksm(LL a,LL b) {
LL rs=,bs=a%p;
while(b) {
if(b&) rs=rs*bs%p;
bs=bs*bs%p;
b>>=;
}
return rs;
} #define ANS
int main() {
#ifdef ANS
freopen("biao.in","r",stdin);
freopen("shizi.out","w",stdout);
#endif
read(n);
For(i,,n) read(a[i]);
For(i,,n) {
LL tp=; int up=f[cnt].size();
For(j,,up-) tp=(tp+f[cnt][j]*a[i-j-]%p)%p;
if(tp==a[i]) continue;
delta[i]=(a[i]-tp+p)%p;
fail[cnt]=i;
++cnt;
if(cnt==) {
f[cnt].resize(i);
continue;
}
LL mul=delta[i]*ksm(delta[fail[cnt-]],p-)%p,fmul=(p-mul)%p;
f[cnt].resize(i-fail[cnt-]-);
f[cnt].push_back(mul);
up=f[cnt-].size();
For(j,,up-) f[cnt].push_back(fmul*f[cnt-][j]%p);
up=f[cnt-].size();
if(f[cnt].size()<f[cnt-].size()) f[cnt].resize(up);
For(j,,up-) f[cnt][j]=(f[cnt][j]+f[cnt-][j])%p;
}
int up=f[cnt].size();
printf("%d\n",up);
For(i,,up-) printf("%lld ",f[cnt][i]);
Formylove;
}
BM出递推
//Achen
#include<bits/stdc++.h>
#define For(i,a,b) for(int i=(a);i<=(b);i++)
#define Rep(i,a,b) for(int i=(a);i>=(b);i--)
#define Formylove return 0
const int N=1e5+,p=1e9+;
typedef long long LL;
typedef double db;
using namespace std;
int cnt;
LL A[N],f[N],n; template<typename T> void read(T &x) {
char ch=getchar(); T f=; x=;
while(ch!='-'&&(ch<''||ch>'')) ch=getchar();
if(ch=='-') f=-,ch=getchar();
for(;ch>=''&&ch<='';ch=getchar()) x=x*+ch-''; x*=f;
} LL rs[N],bs[N],tp[N];
void cheng(LL a[],LL b[]) {
For(i,,cnt*) tp[i]=;
For(i,,cnt-) For(j,,cnt-)
(tp[i+j]+=a[i]*b[j])%=p;
Rep(i,cnt*-,cnt) if(tp[i])
For(j,,cnt-)
(tp[i-j-]+=A[j]*tp[i]%p)%=p;
For(i,,cnt-) a[i]=tp[i];
} void ksm(LL b) {
while(b) {
if(b&) cheng(rs,bs);
cheng(bs,bs);
b>>=;
}
} int main() {
freopen("shizi.out","r",stdin);
//freopen("shizi.out","w",stdout);
read(cnt);
For(i,,cnt-) read(A[i]);
read(n);
rs[]=; bs[]=; ksm(n-);
For(i,,cnt) read(f[i]);
LL ans=;
For(i,,cnt-) ans=(ans+f[i+]*rs[i]%p)%p;
printf("%lld\n",ans);
Formylove;
}
多项式取模
ProjectEuler654的更多相关文章
随机推荐
- event driven model
http://www.jdon.com/eda.html http://blog.csdn.net/gykimo/article/details/9182287 事件代表过去发生的事件,事件既是技术架 ...
- 【LeetCode】【定制版排序】Sort Colors
之前转载过一篇STL的sort方法底层详解的博客:https://www.cnblogs.com/ygh1229/articles/9806398.html 但是我们在开发中会根据自己特定的应用,有新 ...
- 011_Eclipse中使用HDFSFileSystemAPI事例介绍
需求 1.文件操作 1)上传本地文件到HDFS 2)读取文件 3)在hadoopfs中新建文件,并写入 4)重命名文件 5)删除hadoopfs上的文件 2.目录操作 1)读取某个目录下的所有文件 2 ...
- awk的内置函数
常见awk内置数值函数
- Qt移植对USB鼠标键盘、触摸屏的支持
.USB键盘 经过一番搜索,发现对Qt键盘的支持主要关系到两个方面: 1. 键盘类型确定: 4.7以前的Qt版本,如果是PS2圆孔键盘,Qt编译时需加上选项:-qt-kbd-vr41xx(未测试):如 ...
- 明远imx6
http://pan.baidu.com/s/1ntsrQtF#path=%252FMY-I.MX6%2520V2.5 nkuc tftpboot 0x10800000 uImage.ramdisk ...
- P4340 [SHOI2016]随机序列
题目 P4340 [SHOI2016]随机序列 思维好题 做法 是否觉得水在于你是否发现加减是会抵消的,所以我们只用考虑乘的部分 一块乘只能前面无号(也就是前缀形式)才统计,所以用线段树维护区间前缀乘 ...
- winter 2018 02 01 关于模运算的一道题
题目:给出一个正整数n,问是否存在x,满足条件2^x mod n=1,如果存在,求出x的最小值. 分析:1.若给出的n是1,则肯定不存在这样的x; 2.若给出的是偶数,2的次幂取余一个偶数得到 ...
- recovery 差分升级包制作超时【转】
本文转载自:https://blog.csdn.net/csdn66_2016/article/details/73800349 我们在对android系统升级的时候,可以减少升级包的大小,只升级差异 ...
- git checkout cannot stat permission denied
https://stackoverflow.com/questions/5970879/git-rebase-error-cannot-stat-file-permission-denied 退出vi ...