[NOI2012]随机数生成器 矩阵乘法
2024-09-03 18:35:06
Code:
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<string>
using namespace std;
void setIO(string a){ freopen((a+".in").c_str(),"r",stdin); }
#define ll long long
ll mod,g;
int nodes;
struct matrix{
long long mat[2][2];
}unit,solve,fin;
void init(matrix &a){
for(int i=0;i<=nodes;++i)
for(int j=0;j<=nodes;++j) a.mat[i][j]=0;
}
void get(matrix &a){
init(a);
for(int i=0;i<=nodes;++i) a.mat[i][i]=1;
}
ll mult(ll a,ll b){
ll res=0;
while(b>0){
if(b&1) res=(res+a)%mod;
a=(a+a)%mod;
b>>=1;
}
return res;
}
matrix operator*(matrix a,matrix b){
matrix c;
init(c);
for(int i=0;i<=nodes;++i)
for(int j=0;j<=nodes;++j)
for(int k=0;k<=nodes;++k)
c.mat[i][j]=(c.mat[i][j]+mult(a.mat[i][k],b.mat[k][j]))%mod;
return c;
}
matrix operator^(matrix a,ll p){
matrix res;
get(res);
while(p>0){
if(p&1) res=res*a;
a=a*a;
p>>=1;
}
return res;
}
int main(){
//setIO("input");
nodes=1;
ll a,c,x,n;
cin>>mod>>a>>c>>x>>n>>g;
init(unit),init(solve);
unit.mat[0][0]=a,unit.mat[1][1]=1,unit.mat[1][0]=c;
solve.mat[0][0]=x,solve.mat[0][1]=1;
fin=solve*(unit^n);
cout<<(fin.mat[0][0]%mod)%g;
return 0;
}
最新文章
- 学习C++.Primer.Plus 10 对象和类
- Spring中scope作用域
- 图像预处理第9步:存为.bmp文件
- Winsock 入门 判读主机字节序 示例
- <;转>;Oracle SQL性能优化
- mysql 修改字段类型
- jquery 监控文本框键盘事件(回车事件),附常用keycode值。
- Android之列表内容显示
- .NET使用QRCodeEncoder生成二维码
- git Bug分支
- SQL Server 版本号汇总
- 2017-3-10 SQL server 数据库 T--SQL语句
- seajs模块标识命名和解析规则
- jira7.3.6 linux安装
- centos 7部署openvpn easy-rsa 3.0部署方法
- QDialog对话框
- ./configure: error: the HTTP rewrite module requires the PCRE library解决
- Delphi: Class Static Methods
- CURLOPT_RETURNTRANSFER
- 用Copy命令合并文件