-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path024-noi-12-p1-random-number-generator.cpp
More file actions
87 lines (86 loc) · 1.76 KB
/
Copy path024-noi-12-p1-random-number-generator.cpp
File metadata and controls
87 lines (86 loc) · 1.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/*
* NOI '12 P1 - Random Number Generator [noi12p1]
* Problem: https://dmoj.ca/problem/noi12p1
* Verdict: ACCEPTED (15.0 pts) Solved: 2023-04-15
* Language: C++20
* Runtime: 0.104464231 s Memory: 3444.0 KB
* Source: https://dmoj.ca/src/5475156
*/
/*
ID: BidoTeima
LANG: C++11
TASK:
*/
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void moo(string filename);
void ACPLS(string str = "")
{
if(str=="NOF")return;
if(str.size() && str != "IIOT")
moo(str);
else if(str != "IIOT"){
#ifndef ONLINE_JUDGE
freopen("output.txt", "w", stdout);
freopen("input.txt", "r", stdin);
#endif
}
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
void moo(string fileName){
freopen((fileName+".in").c_str(),"r",stdin);
freopen((fileName+".out").c_str(),"w",stdout);
}
#define tcccc \
int tttttt/*,subtask*/; \
cin >> tttttt/* >> subtask*/; \
while (tttttt--)/*end
*/
ll mod=1e18;
ll mul(ll a, ll b){
a %= mod;
ll res = 0;
while(b > 0){
if(b & 1)
res = (res + a) % mod;
a = (2 * a) % mod;
b >>= 1;
}
return res;
}
ll binexpo(ll a, ll b){
a %= mod;
ll res = 1;
while (b > 0) {
if (b & 1)
res = mul(res,a);
a = mul(a,a);
b >>= 1;
}
return res;
}
ll calc(ll a, ll n){
if(n == 1)
return 1;
n>>=1;
return mul(calc(a, n), (binexpo(a, n) + 1));
}
int main()
{
ACPLS("");
ll m,a,c,x0,n,g;
cin>>m>>a>>c>>x0>>n>>g;
mod=m;
ll y = x0;
for(int i = 0; i < 60; i++){
if(!(n & (1ll << i)))
continue;
y = mul(y, binexpo(a, 1ll<<i)) + mul(calc(a, 1ll<<i), c);
y %= mod;
}
cout << y%g;
return 0;
}