1 条题解
-
0
C++ :
#include<cstdio> #include<cstdlib> #include<cstring> #include<algorithm> #include<iostream> using namespace std; typedef long long LL; #define maxn 1010 const LL mod=1000000007; int n;char s[maxn]; struct mrtx { LL a[2][2];int l,r; mrtx(){memset(a,0,sizeof(a));} }A[maxn],f[maxn]; mrtx multt(mrtx x,mrtx y) { mrtx ret;ret.l=x.l;ret.r=y.r; for (int i=0;i<x.l;i++) for (int j=0;j<y.r;j++) { for (int k=0;k<x.r;k++) ret.a[i][j]+=x.a[i][k]*y.a[k][j]; ret.a[i][j]%=mod; } return ret; } mrtx pluss(mrtx x,mrtx y) { mrtx ret;ret.l=x.l;ret.r=x.r; for (int i=0;i<x.l;i++) for (int j=0;j<x.r;j++) ret.a[i][j]=(x.a[i][j]+y.a[i][j])%mod; return ret; } mrtx qpow(mrtx x,int tt) { mrtx ret;ret.l=ret.r=2; ret.a[0][0]=ret.a[1][1]=1; while (tt) { if (tt&1) ret=multt(ret,x); x=multt(x,x);tt>>=1; }return ret; } int main() { //freopen("a.in","r",stdin); //freopen("a.out","w",stdout); scanf("%d\n",&n);gets(s+1); A[0].l=A[0].r=2; A[0].a[0][0]=A[0].a[0][1]=A[0].a[1][0]=1; for (int i=1;i<=n;i++) A[i]=qpow(A[i-1],10); f[0].l=2;f[0].r=1;f[0].a[0][0]=1;f[0].a[1][0]=-1; for (int i=1;i<=n;i++) { mrtx now;now.l=now.r=2; now.a[0][0]=now.a[1][1]=1; for (int j=i;j>0;j--) { int x=s[j]-'0'; now=multt(now,qpow(A[i-j],x)); f[i]=pluss(multt(now,f[j-1]),f[i]); } } printf("%lld\n",(f[n].a[0][0]+mod)%mod); return 0; }
- 1
信息
- ID
- 982
- 时间
- 2000ms
- 内存
- 128MiB
- 难度
- (无)
- 标签
- 递交数
- 0
- 已通过
- 0
- 上传者