当前位置:网站首页>[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
边栏推荐
- C language helps you understand pointers from multiple perspectives (1. Character pointers 2. Array pointers and pointer arrays, array parameter passing and pointer parameter passing 3. Function point
- 开发那些事儿:Go加C.free释放内存,编译报错是什么原因?
- 字符串中数据排序
- 机械臂速成小指南(十二):逆运动学分析
- 恶魔奶爸 A3阶段 近常速语流初接触
- I Basic concepts
- Helix QAC 2020.2 new static test tool maximizes the coverage of standard compliance
- 写了个 Markdown 命令行小工具,希望能提高园友们发文的效率!
- 恶魔奶爸 A1 语音听力初挑战
- 恶魔奶爸 指南帖——简易版
猜你喜欢

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

Tensorflow2. How to run under x 1 Code of X

Measure the height of the building

Tensorflow2.x下如何运行1.x的代码

Airiot helps the urban pipe gallery project, and smart IOT guards the lifeline of the city

Network principle (1) - overview of basic principles

Onespin | solve the problems of hardware Trojan horse and security trust in IC Design
Klocwork code static analysis tool

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

最新版本的CodeSonar改进了功能安全性,支持MISRA,C ++解析和可视化
随机推荐
[award publicity] issue 22 publicity of the award list in June 2022: Community star selection | Newcomer Award | blog synchronization | recommendation Award
When easygbs cascades, how to solve the streaming failure and screen jam caused by the restart of the superior platform?
C language helps you understand pointers from multiple perspectives (1. Character pointers 2. Array pointers and pointer arrays, array parameter passing and pointer parameter passing 3. Function point
寫一下跳錶
Micro service remote debug, nocalhost + rainbow micro service development second bullet
Numerical method for solving optimal control problem (0) -- Definition
easyui 日期控件清空值
[function recursion] do you know all five classic examples of simple recursion?
如何满足医疗设备对安全性和保密性的双重需求?
使用高斯Redis实现二级索引
机械臂速成小指南(十一):坐标系的标准命名
Codesonar enhances software reliability through innovative static analysis
H3C S7000/S7500E/10500系列堆叠后BFD检测配置方法
取两个集合的交集
使用camunda做工作流设计,驳回操作
Spark judges that DF is empty
Tensorflow2.x下如何运行1.x的代码
Klocwork 代码静态分析工具
华为CE交换机下载文件FTP步骤
字符串中数据排序