标签:style blog http color io os ar for sp
2 1 1 7 6
Case #1: 2 Case #2: 818503
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#define maxn 205
#define MAXN 200005
#define INF 0x3f3f3f3f
#define mod 20140518
#define eps 1e-6
const double pi=acos(-1.0);
typedef long long ll;
using namespace std;
ll n,k,ans;
struct Matrix
{
int row,col;
ll v[15][15];
Matrix operator*(Matrix &tt)
{
int i,j,k;
Matrix temp;
temp.row=row;
temp.col=tt.col;
for(i=1; i<=row; i++)
for(j=1; j<=tt.col; j++)
{
temp.v[i][j]=0;
for(k=1; k<=col; k++)
{
temp.v[i][j]+=v[i][k]*tt.v[k][j];
temp.v[i][j]%=mod;
}
}
return temp;
}
};
Matrix pow_mod(Matrix x,ll i) // x^i
{
Matrix tmp;
tmp.row=x.row; tmp.col=x.col;
memset(tmp.v,0,sizeof(tmp.v));
for(int j=1;j<=x.row;j++) tmp.v[j][j]=1;
while(i)
{
if(i&1) tmp=tmp*x;
x=x*x;
i>>=1;
}
return tmp;
}
void solve()
{
int i,j;
Matrix A,res,x;
x.row=1; x.col=k;
res.row=1; res.col=k;
memset(x.v,0,sizeof(x.v));
x.v[1][1]=k+1;
A.row=A.col=k;
memset(A.v,0,sizeof(A.v));
for(i=1;i<=k;i++)
{
A.v[i-1][i]=k-i+2;
for(j=i;j<=k;j++)
{
A.v[j][i]=1;
}
}
res=pow_mod(A,n-1);
res=x*res;
ans=0;
for(i=1;i<=k;i++)
{
ans+=res.v[1][i];
ans%=mod;
}
}
int main()
{
int i,j,test,ca=0;
scanf("%d",&test);
while(test--)
{
scanf("%lld%lld",&n,&k);
solve();
printf("Case #%d: %lld\n",++ca,ans);
}
return 0;
}bnu oj 34985 Elegant String (矩阵+dp)
标签:style blog http color io os ar for sp
原文地址:http://blog.csdn.net/tobewhatyouwanttobe/article/details/39697215