当前位置:网站首页>[matrix multiplication] [noi 2012] [cogs963] random number generator
[matrix multiplication] [noi 2012] [cogs963] random number generator
2022-07-07 20:45:00 【Full stack programmer webmaster】
Hello everyone , I meet you again , I'm the king of the whole stack .
963. [NOI2012] Random number generator
** Input file :randoma.in The output file :randoma.out Simple comparison
The time limit :1 s Memory limit :128 MB**【 Problem description and narration 】
Dong Dong has been fascinated by random algorithms recently , And random number is the basis of generating random algorithm . Dong Dong is going to use the linear congruence method (Linear Congruential Method) To generate a random sequence of numbers . Such a method requires setting four non negative integer parameters m,a,c,X[0], A series of random numbers are generated according to the following formula {Xn}:
X[n+1]=(aX[n]+c) mod mamong mod m Represents the previous number divided by m The remainder of . From this formula, we can see , The next number in this sequence is always generated by the previous number .
The sequence generated by this method has the property of random sequence . So this method is widely used , Including frequently used C++ and Pascal The library function that generates random numbers uses the same method .
Dong Dong knows that the sequence generated in this way has good randomness , Just anxious, he still wants to know as soon as possible X[n] How much is the .
Because the random number needed by every building is 0,1,…,g-1 Between , He needs to X[n] Divide g Take the remainder and get the number he wants , namely X[n] mod g, You just have to tell Dong Dong the number he wants X[n] mod g How much is enough .
【 Input format 】
Input file randoma.in It includes 6 An integer with spaces m,a,c,X[0],n and g, among a,c,X[0] Is a nonnegative integer .m,n,g It's a positive integer. .
【 Output format 】
output to a file randoma.out in , Output a number , namely X[n] mod g
【 Example input 】
11 8 7 1 5 3【 Example output 】
2【 Example illustrate 】
Calculated X[n]=X[5]=8, so (X[n] mod g) = (8 mod 3) = 2【 Data scale 】
40% Data in m Prime number
30% Data in m And a-1 Coprime
50% Data in n<=10^6
100% Data in n<=10^18
40% The data of m,a,c,X[0]<=10^4
85% The data of m,a,c,X[0]<=10^9
100% Data in m,a,c,X[0]<=10^18
100% Data in g<=10^8
For all data ,n>=1,m>=1,a>=0,c>=0,X[0]>=0,g>=1.Answer key :
Simpler matrix multiplication , For two matrices :
A[a,c0,1]
B[X[n−1]1]
obviously ,X[n] It can be obtained by multiplying these two matrices : A∗B=C[X[n]1]
So for X[n], We can ask : An∗[X[0]1]
It is necessary to write about high-speed travel , Because ordinary passengers will explode ... (PS: High speed multiplication is almost the same as high speed exponentiation , Just put * Change to +)
Code:
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
long long A[2][2]={{0,0},{0,1}},B[2][2]={{0,0},{1,0}},C[2][2]={0};
long long n,g,m,nn;
long long kc(long long x,long long y){
long long z=0;
x%=m; y%=m;
if (x<y) swap(x,y);
while (y){
if (y&1) z=(z+x)%m;
x=(2*x)%m;
y>>=1;
}
return z;
}
int main(){
scanf("%lld%lld%lld%lld%lld%lld",&m,&A[0][0],&A[0][1],&B[0][0],&n,&g);
nn=n;
while (nn){
if (nn&1){
memset(C,0,sizeof(C));
for (int i=0; i<2; i++)
for (int j=0; j<2; j++)
for (int k=0; k<2; k++)
C[i][j]=(C[i][j]+kc(A[i][k],B[k][j]))%m;
for (int i=0; i<2; i++)
for (int j=0; j<2; j++)
B[i][j]=C[i][j];
}
nn>>=1;
memset(C,0,sizeof(C));
for (int i=0; i<2; i++)
for (int j=0; j<2; j++)
for (int k=0; k<2; k++)
C[i][j]=(C[i][j]+kc(A[i][k],A[k][j]))%m;
for (int i=0; i<2; i++)
for (int j=0; j<2; j++)
A[i][j]=C[i][j];
}
printf("%lld\n",B[0][0]%g);
return 0;
}Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/116386.html Link to the original text :https://javaforall.cn
边栏推荐
- H3C s7000/s7500e/10500 series post stack BFD detection configuration method
- Validutil, "Rethinking the setting of semi supervised learning on graphs"
- Introduction to referer and referer policy
- Codesonar enhances software reliability through innovative static analysis
- 解决使用uni-app MediaError MediaError ErrorCode -5
- 使用高斯Redis实现二级索引
- Numerical method for solving optimal control problem (0) -- Definition
- Airiot helps the urban pipe gallery project, and smart IOT guards the lifeline of the city
- 嵌入式系统真正安全了吗?[ OneSpin如何为开发团队全面解决IC完整性问题 ]
- 恶魔奶爸 B1 听力最后壁垒,一鼓作气突破
猜你喜欢

CodeSonar网络研讨会

Implement secondary index with Gaussian redis

How to meet the dual needs of security and confidentiality of medical devices?

Helix QAC 2020.2新版静态测试工具,最大限度扩展了标准合规性的覆盖范围

如何满足医疗设备对安全性和保密性的双重需求?

ISO 26262 - 基于需求测试以外的考虑因素

Onespin | solve the problems of hardware Trojan horse and security trust in IC Design
![[paper reading] maps: Multi-Agent Reinforcement Learning Based Portfolio Management System](/img/76/b725788272ba2dcdf866b28cbcc897.jpg)
[paper reading] maps: Multi-Agent Reinforcement Learning Based Portfolio Management System
![嵌入式系统真正安全了吗?[ OneSpin如何为开发团队全面解决IC完整性问题 ]](/img/af/61b384b1b6ba46aa1a6011f8a30085.png)
嵌入式系统真正安全了吗?[ OneSpin如何为开发团队全面解决IC完整性问题 ]

The latest version of codesonar has improved functional security and supports Misra, c++ parsing and visualization
随机推荐
Flask1.1.4 werkzeug1.0.1 source code analysis: Routing
机械臂速成小指南(十一):坐标系的标准命名
HDU4876ZCC loves cards(多校题)
权限不足
How to choose fund products? What fund is suitable to buy in July 2022?
使用 BR 恢复 Azure Blob Storage 上的备份数据
备份 TiDB 集群到持久卷
Implement secondary index with Gaussian redis
数值法求解最优控制问题(〇)——定义
Intelligent software analysis platform embold
Mysql子查询关键字的使用方式(exists)
Nebula Importer 数据导入实践
死锁的产生条件和预防处理[通俗易懂]
写一下跳表
Cantata9.0 | 全 新 功 能
4G设备接入EasyGBS平台出现流量消耗异常,是什么原因?
开发一个小程序商城需要多少钱?
You want to kill a port process, but you can't find it in the service list. You can find this process and kill it through the command line to reduce restarting the computer and find the root cause of
Meta Force原力元宇宙系统开发佛萨奇模式
【奖励公示】第22期 2022年6月奖励名单公示:社区明星评选 | 新人奖 | 博客同步 | 推荐奖