当前位置:网站首页>[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 m
among 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
边栏推荐
- 神兵利器——敏感文件发现工具
- 如何满足医疗设备对安全性和保密性的双重需求?
- Spark judges that DF is empty
- FTP steps for downloading files from Huawei CE switches
- 部署、收回和删除解决方式—-STSADM和PowerShell「建议收藏」
- 华泰证券可以做到万一佣金吗,万一开户安全嘛
- 如何满足医疗设备对安全性和保密性的双重需求?
- 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
- Codesonar enhances software reliability through innovative static analysis
- 使用高斯Redis实现二级索引
猜你喜欢
Make this crmeb single merchant wechat mall system popular, so easy to use!
如何满足医疗设备对安全性和保密性的双重需求?
测量楼的高度
最新版本的CodeSonar改进了功能安全性,支持MISRA,C ++解析和可视化
机械臂速成小指南(十二):逆运动学分析
ISO 26262 - 基于需求测试以外的考虑因素
Small guide for rapid formation of manipulator (11): standard nomenclature of coordinate system
Optimization cases of complex factor calculation: deep imbalance, buying and selling pressure index, volatility calculation
Ubuntu安装mysql8遇到的问题以及详细安装过程
How to meet the dual needs of security and confidentiality of medical devices?
随机推荐
目标:不排斥 yaml 语法。争取快速上手
Micro service remote debug, nocalhost + rainbow micro service development second bullet
When easygbs cascades, how to solve the streaming failure and screen jam caused by the restart of the superior platform?
Apifox interface integrated management new artifact
POJ 1742 coins (monotone queue solution) [suggestions collection]
国家正规的股票交易app有哪些?使用安不安全
Onespin | solve the problems of hardware Trojan horse and security trust in IC Design
理财产品要怎么选?新手还什么都不懂
Introduction to referer and referer policy
CodeSonar通过创新型静态分析增强软件可靠性
智能交通焕发勃勃生机,未来会呈现哪些巨变?[通俗易懂]
【函数递归】简单递归的5个经典例子,你都会吗?
Apifox 接口一体化管理新神器
反诈困境,国有大行如何破局?
程序猿赚的那点钱算个P啊!
Airiot helps the urban pipe gallery project, and smart IOT guards the lifeline of the city
凌云出海记 | 易点天下&华为云:推动中国电商企业品牌全球化
如何满足医疗设备对安全性和保密性的双重需求?
How to choose financial products? Novice doesn't know anything
Update iteration summary of target detection based on deep learning (continuous update ing)