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,要用快速乘. ...
随机推荐
- 论文阅读:Learning Visual Question Answering by Bootstrapping Hard Attention
Learning Visual Question Answering by Bootstrapping Hard Attention Google DeepMind ECCV-2018 2018 ...
- Tomcat服务器环境变量配置及在Eclipse中启动和配置
本文原创,转载需注明出处: 如何配置在Eclipse中配置Tomcat服务器 1.在配置的时候要右击‘我的电脑‘看是否安装了jdk,配置了jdk的环境变量,看是否有classpath和path是否指向 ...
- js实例分析JavaScript中的事件委托和事件绑定
我们在学习JavaScript中,难免都会去网上查一些资料.也许偶尔就会遇到“事件委托”(也有的称我“事件代理”,这里不评论谁是谁非.以下全部称为“事件委托”),尤其是在查JavaScript的事件处 ...
- 在服务器端对sshd做白名单
1.添加用户 #useradd aaa #passwd aaa -->输入密码:123456 添加3个用户,bbb和ccc与aaa添加一样 2.添加白名单 #vim /etc/ssd/sshd_ ...
- Lintcode35-Reverse Linked List-Easy
35. Reverse Linked List Reverse a linked list. Example Example1:For linked list 1->2->3, the r ...
- python学习 day02打卡
今天主要学习的内容: 1.while 循环 : 语法: while 条件 : 循环体 #判断条件是否成立.如果成立执行循环体.然后再次判断条件...直到条件不成立石跳出循环 else : 当条件不成 ...
- SyncDictionary
using System; using System.Collections; using System.Collections.Generic; using System.Threading; us ...
- Linux let 命令
命令:let let 命令是 BASH 中用于计算的工具,用于执行一个或多个表达式,变量计算中不需要加上 $ 来表示变量.如果表达式中包含了空格或其他特殊字符,则必须引起来. 语法格式 let arg ...
- hdu 6170 Two strings dp
Two strings Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Prob ...
- JS绘制拓扑图示例 (JTopo)
目前在做的项目是渔政的监控,需要用到的设备包括雷达,光电,站点信息等,想要更直观的展现设备之间的连接关系和状态信息,这时候需要画一张拓扑图 在做拓扑图之前,首先要学习一下,html里面另一个比较常用的 ...