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,要用快速乘. ...
随机推荐
- What is event bubbling and capturing?
What is event bubbling and capturing? 答案1 Event bubbling and capturing are two ways of event propaga ...
- 【Dalston】【第二章】客户端负载均衡(Ribbon)
对于大型应用系统负载均衡(LB:Load Balancing)是首要被解决一个问题.在微服务之前LB方案主要是集中式负载均衡方案,在服务消费者和服务提供者之间又一个独立的LB,LB通常是专门的硬件,如 ...
- IDEA旗舰版新建web项目
即在一个Project下(MyEclipse中为工作空间)新建一个Module. 点击,在弹出框上打一个勾,如下图: 点Next,输入项目名,如下图: 点Finish, 右键WEB-INF,新建2个D ...
- hihoCoder week13 最近公共祖先·一
用的dfs,自下往上搜索一个节点的所有祖先,然后在相应祖先 判断是否是另一个节点的祖先,如果是 就截止,否则继续往上搜索,直到搜索到,或者知道所有的祖先都被扫描完成 #include <bits ...
- 题解——Codeforces Round #507 (based on Olympiad of Metropolises) T2(模拟)
T2还是模拟 枚举一下第一个放哪里 然后贪心的反转即可 虽然我也不会证,但是这题肯定有解qwq #include <cstdio> #include <algorithm> # ...
- 题解——POJ 2234 Matches Game
这道题也是一个博弈论 根据一个性质 对于\( Nim \)游戏,即双方可以任取石子的游戏,\( SG(x) = x \) 所以直接读入后异或起来输出就好了 代码 #include <cstdio ...
- MPU6050
MPU6050: Pitch,Roll,Yaw旋转方向遵循右手定则 pith角 –绕Y轴(俯仰) 范围:±90° ,与旋转方向相反转是增大 -- 抬头为正,低头为负 roll角 –绕X轴( ...
- Docker save & load
docker save Estimated reading time: 1 minute Description Save one or more images to a tar archive (s ...
- 浅谈 Make 命令
代码变成可执行文件,叫做编译(compile):先编译这个,还是先编译那个(即编译的安排),叫做构建(build). Make是最常用的构建工具,诞生于1977年,主要用于C语言的项目.但是实际上 , ...
- Educational Codeforces Round 23 F. MEX Queries 离散化+线段树
F. MEX Queries time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...