bzoj2875
题意:\(x_{i+1}=(x_{i}*a+c)%m\)求,x_n%g
题解:\(x_n=(a^n*x_0+(a^{n-1}+a^{n-2}+...+a+1)*c)%m\),由于a-1和m不一定互质,所以没法逆元,只能矩阵快速幂求,乘法必须用快速乘,不然会爆ll
/**************************************************************
Problem: 2875
User: walfy
Language: C++
Result: Accepted
Time:52 ms
Memory:1292 kb
****************************************************************/
//#pragma GCC optimize(2)
//#pragma GCC optimize(3)
//#pragma GCC optimize(4)
//#pragma GCC optimize("unroll-loops")
//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast,no-stack-protector")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include<bits/stdc++.h>
#define fi first
#define se second
#define db double
#define mp make_pair
#define pb push_back
#define pi acos(-1.0)
#define ll long long
#define vi vector<int>
//#define mod 1000000007
#define ld long double
//#define C 0.5772156649
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1
#define pll pair<ll,ll>
#define pil pair<int,ll>
#define pli pair<ll,int>
#define pii pair<int,int>
#define ull unsigned long long
#define base 1000000000000000000
#define fin freopen("a.txt","r",stdin)
#define fout freopen("a.txt","w",stdout)
#define fio ios::sync_with_stdio(false);cin.tie(0)
inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
//inline void sub(ll &a,ll b){a-=b;if(a<0)a+=mod;}
//inline void add(ll &a,ll b){a+=b;if(a>=mod)a-=mod;}
template<typename T>inline T const& MAX(T const &a,T const &b){return a>b?a:b;}
template<typename T>inline T const& MIN(T const &a,T const &b){return a<b?a:b;}
//inline ll qp(ll a,ll b){ll ans=1;while(b){if(b&1)ans=ans*a%mod;a=a*a%mod,b>>=1;}return ans;}
inline ll qp(ll a,ll b,ll c){ll ans=1;while(b){if(b&1)ans=ans*a%c;a=a*a%c,b>>=1;}return ans;}
inline ll qm(ll a,ll b,ll c){ll ans=0;while(b){if(b&1)ans=(ans+a)%c;a=(a+a)%c,b>>=1;};return ans;}
using namespace std;
const ull ba=233;
const db eps=1e-8;
const ll INF=0x3f3f3f3f3f3f3f3f;
const int N=500000+10,maxn=100000+10,inf=0x3f3f3f3f;
struct Node{
ll row,col;
ll a[3][3];
};
Node mul(Node x,Node y,ll mod)
{
Node ans;
ans.row=x.row,ans.col=y.col;
memset(ans.a,0,sizeof ans.a);
for(int i=0;i<x.row;i++)
for(int j=0;j<x.col;j++)
for(int k=0;k<y.col;k++)
ans.a[i][k]=(ans.a[i][k]+qm(x.a[i][j],y.a[j][k],mod)+mod)%mod;
return ans;
}
Node quick_mul(Node x,ll n,ll mod)
{
Node ans;
ans.row=x.row,ans.col=x.col;
memset(ans.a,0,sizeof ans.a);
for(int i=0;i<ans.col;i++)ans.a[i][i]=1;
while(n){
if(n&1)ans=mul(ans,x,mod);
x=mul(x,x,mod);
n/=2;
}
return ans;
}
int main()
{
ll m,a,c,x,n,g;scanf("%lld%lld%lld%lld%lld%lld",&m,&a,&c,&x,&n,&g);
a%=m,c%=m;
Node A;A.row=A.col=3;
A.a[0][0]=a,A.a[0][1]=0,A.a[0][2]=1;
A.a[1][0]=0,A.a[1][1]=a,A.a[1][2]=0;
A.a[2][0]=0,A.a[2][1]=0,A.a[2][2]=1;
A=quick_mul(A,n-1,m);
ll t1=(A.a[0][0]+A.a[0][1]+A.a[0][2])%m,t2=(A.a[1][0]+A.a[1][1]+A.a[1][2])%m;
t2=qm(t2,a,m);t2=qm(t2,x,m);
t1=qm(t1,c,m);t1=(t1+t2)%m;
printf("%lld\n",t1%g);
return 0;
}
/********************
********************/
bzoj2875的更多相关文章
- 【BZOJ2875】随机数生成器(矩阵快速幂)
[BZOJ2875]随机数生成器(矩阵快速幂) 题面 Description 栋栋最近迷上了随机算法,而随机数是生成随机算法的基础.栋栋准备使用线性同余法(Linear Congruential Me ...
- 【BZOJ2875】【NOI2012】随机数生成器(矩阵快速幂)
[BZOJ2875]随机数生成器(矩阵快速幂) 题面 Description 栋栋最近迷上了随机算法,而随机数是生成随机算法的基础.栋栋准备使用线性同余法(Linear Congruential Me ...
- 【bzoj2875】 Noi2012—随机数生成器
http://www.lydsy.com/JudgeOnline/problem.php?id=2875 (题目链接) 题意 求${X_{n}}$. Solution 矩乘板子,这里主要讲下会爆lon ...
- BZOJ-2875 随机数生成器 矩阵乘法快速幂+快速乘
题目没给全,吃X了... 2875: [Noi2012]随机数生成器 Time Limit: 10 Sec Memory Limit: 512 MB Submit: 1479 Solved: 829 ...
- bzoj2875: [Noi2012]随机数生成器
矩阵乘法. x[n] = {x[0],1} * ( {a,0} ^ n ) {b,1} 写成这样谁能看懂.... noi里的大水题.我居然 #include<cstdio> #includ ...
- bzoj2875随机数生成器
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2875 矩阵乘裸题. 如果直接乘的话会爆long long,所以用加法代替乘,过程中不断取模. ...
- BZOJ2875 & 洛谷2044:[NOI2012]随机数生成器——题解
https://www.lydsy.com/JudgeOnline/problem.php?id=2875 https://www.luogu.org/problemnew/show/P2044 栋栋 ...
- BZOJ2875 [Noi2012]随机数生成器 【矩阵乘法 + 快速乘】
题目 栋栋最近迷上了随机算法,而随机数是生成随机算法的基础.栋栋准备使用线性同余法(Linear Congruential Me thod)来生成一个随机数列,这种方法需要设置四个非负整数参数m,a, ...
- bzoj2875随机数生成器——矩阵快速幂
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2875 矩阵快速幂,把x和c分开求,最后加上即可: 为防止爆long long,要用快速乘. ...
随机推荐
- 在Jenkins中集成Sonarqube
Analyzing with SonarQube Scanner for MSBuild Global Configuration This step is mandatory if you want ...
- Perceptual Losses for Real-Time Style Transfer and Super-Resolution and Super-Resolution 论文笔记
Perceptual Losses for Real-Time Style Transfer and Super-Resolution and Super-Resolution 论文笔记 ECCV 2 ...
- sql注入解析
sql注入解析 sql注入解析(一)基本语法 sql注入解析(二)执行注入 sql注入解析(三)数据库类型 sql注入解析(四)避开过滤
- 33 Python 详解命令解析 - argparse--更加详细--转载
https://blog.csdn.net/lis_12/article/details/54618868 Python 详解命令行解析 - argparse Python 详解命令行解析 - arg ...
- 提高R语言速度--转载
1. 参考<R语言编程艺术>(Norman Matloff) chapter 14 & chapter 15 2. 方法 (1)向量化 与非向量化-循环做个对比: ...
- 性能跃升50%!解密自主研发的金融级分布式关系数据库OceanBase 2.0
小蚂蚁说: 相信大家对蚂蚁金服自主研发的金融级分布式关系数据库OceanBase的故事不再陌生了.在刚刚过去的2018年天猫双11中,成交额2135亿再次创造了新纪录,而支撑今年双11的支付宝核心链路 ...
- tkinter 打包成exe可执行文件
1.安装pyinstaller pip install pyinstaller 2.打包 打开cmd,切换到需要打包的文件(demo.py)目录.执行 pyinstaller -F -w demo.p ...
- 扩展EF的Fluent API中的 OnModelCreating方法 实现全局数据过滤器
1.生成过滤的表达式目录树 protected virtual Expression<Func<TEntity, bool>> CreateFilterExpression&l ...
- cin 与 getchar 中的坑
今天在一道题上发现一个坑. 输入三个字符,按以下规则求其平均值. (1)如果是数字0~9,那么直接参与求值: (2)如果是其他字符,则其ASCII码参与求值. 输入 输入数据有多组.第一行是数据的组数 ...
- 2019热门JAVA面试问题
收到不少读者反馈,说自己在应聘一些中大型互联网公司的Java工程师岗位时遇到了不少困惑. 这些同学说自己也做了精心准备,网上搜集了不少Java面试题,然而实际去互联网公司面试才发现,人家问的,和你准备 ...