vijos P1067 守望者的烦恼
2016-03-10 09:59
429 查看
预处理然后矩阵加速递推
就是预处理的时候还可以以自己作为起点所以还有个自加所以在初始化矩阵的时候我们还需要+1,忽略这点就只有30了= =
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#define LL long long
#define fo(i,a,b) for(int i=a;i<=b;i++)
#define down(i,a,b) for(int i=a;i>=b;i--)
using namespace std;
inline LL read()
{
LL d=0,f=1;char s=getchar();
while(s<'0'||s>'9'){if(s=='-')f=-1;s=getchar();}
while(s>='0'&&s<='9'){d=d*10+s-'0';s=getchar();}
return d*f;
}
#define N 10
#define inf 7777777
int n,m;
struct matrix
{
LL a
;
void clear()
{
memset(a,0,sizeof(a));
}
void check()
{
fo(i,0,m-1)
{
fo(j,0,m-1)
{
cout<<a[i][j];
}
cout<<endl;
}
cout<<endl;
}
matrix operator*(const matrix b)const
{
matrix anss;anss.clear();
fo(i,0,m-1)
fo(j,0,m-1)
{
fo(k,0,m-1)
{
anss.a[i][j]+=a[i][k]*(b.a[k][j]);
anss.a[i][j]%=inf;
}
}
return anss;
}
};
matrix I;
void getI()
{
fo(i,0,m-1)
fo(j,0,m-1)
if(i==j)I.a[i][j]=1;
}
matrix KSM(matrix a,LL b)
{
if(b==0)return I;
if(b==1)return a;
matrix ret=KSM(a,b/2);
ret=ret*ret;
if(b%2)ret=ret*a;
return ret;
}
int main()
{
m=read();n=read();
getI();
matrix A,ans,cnt;ans.clear();
ans.a[m-1][0]=1;ans.a[m-2][0]=2;
down(i,m-3,0)
{
LL anss=1;
fo(j,i+1,i+m)
if(j>=m)break;
else anss+=ans.a[j][0];
ans.a[i][0]=anss;
}
if(n<=m)
{
cout<<ans.a[m-n][0]<<endl;
return 0;
}
fo(i,0,m-1)A.a[0][i]=1;
fo(i,1,m-1)
fo(j,0,m-1)
if(i==j+1)A.a[i][j]=1;
else A.a[i][j]=0;
cnt=KSM(A,n-m);
// cnt.check();
ans=cnt*ans;
// ans.check();
cout<<ans.a[0][0]<<endl;
return 0;
}
就是预处理的时候还可以以自己作为起点所以还有个自加所以在初始化矩阵的时候我们还需要+1,忽略这点就只有30了= =
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#define LL long long
#define fo(i,a,b) for(int i=a;i<=b;i++)
#define down(i,a,b) for(int i=a;i>=b;i--)
using namespace std;
inline LL read()
{
LL d=0,f=1;char s=getchar();
while(s<'0'||s>'9'){if(s=='-')f=-1;s=getchar();}
while(s>='0'&&s<='9'){d=d*10+s-'0';s=getchar();}
return d*f;
}
#define N 10
#define inf 7777777
int n,m;
struct matrix
{
LL a
;
void clear()
{
memset(a,0,sizeof(a));
}
void check()
{
fo(i,0,m-1)
{
fo(j,0,m-1)
{
cout<<a[i][j];
}
cout<<endl;
}
cout<<endl;
}
matrix operator*(const matrix b)const
{
matrix anss;anss.clear();
fo(i,0,m-1)
fo(j,0,m-1)
{
fo(k,0,m-1)
{
anss.a[i][j]+=a[i][k]*(b.a[k][j]);
anss.a[i][j]%=inf;
}
}
return anss;
}
};
matrix I;
void getI()
{
fo(i,0,m-1)
fo(j,0,m-1)
if(i==j)I.a[i][j]=1;
}
matrix KSM(matrix a,LL b)
{
if(b==0)return I;
if(b==1)return a;
matrix ret=KSM(a,b/2);
ret=ret*ret;
if(b%2)ret=ret*a;
return ret;
}
int main()
{
m=read();n=read();
getI();
matrix A,ans,cnt;ans.clear();
ans.a[m-1][0]=1;ans.a[m-2][0]=2;
down(i,m-3,0)
{
LL anss=1;
fo(j,i+1,i+m)
if(j>=m)break;
else anss+=ans.a[j][0];
ans.a[i][0]=anss;
}
if(n<=m)
{
cout<<ans.a[m-n][0]<<endl;
return 0;
}
fo(i,0,m-1)A.a[0][i]=1;
fo(i,1,m-1)
fo(j,0,m-1)
if(i==j+1)A.a[i][j]=1;
else A.a[i][j]=0;
cnt=KSM(A,n-m);
// cnt.check();
ans=cnt*ans;
// ans.check();
cout<<ans.a[0][0]<<endl;
return 0;
}
相关文章推荐
- UITableView 去掉多余行数、Cell系统分割线顶头
- Java--让一个字符串翻转
- Java语音程序开发用户指南
- haahah
- Delphi:对TNotifyEvent的理解
- android四大组件之ContentProvider小结
- oracle忘记用户名和密码
- js遮罩层的应用
- 开发规范3:CSS
- 九个酒迷喝醉酒
- 自定义布局中判断是否可以下拉的逻辑
- iOS猜拳游戏源码
- 北邮OJ 84. Single Number
- Vim如何保存需要root权限的文件
- 使用Eclipse生成generated serial version ID的时候,Eclipse弹出出错信息
- 关于request.getServletPort()获取访问地址的说明
- 第一次上机实验2
- mysql中 show table status 获取表信息
- sublime的安装和相关插件的安装
- string拼接和StringBuilder性能对比