-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcode.cpp
52 lines (45 loc) · 1.58 KB
/
code.cpp
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
// Copyright (c) 2020 Shivam Rathore. All rights reserved.
// Use of this source code is governed by MIT License that
// can be found in the LICENSE file.
// This file contains Solution to Challenge #045, run using
// g++ 001-050/045/c++/code.cpp -o bin/out
// ./bin/out < 001-050/045/c++/in.txt > 001-050/045/c++/out.txt
#include <bits/stdc++.h>
using namespace std;
#define ll long long
int rand5() {
// will return random number between `1 and 5 (inclusive)` with uniform
// probability of `1/5`
return 1 + rand() % 5;
}
int rand7() {
// As `7` if not one of the power of `5` hence it will not possible to
// generate random number between `1 and 7` uniformly using `rand5()` in
// finite time; and hence a different approach is to be considers:
// `val` will have a random number between `1 and 25 (inclusive)` with
// uniform probability of `1/25`
int val = 5 * (rand5() - 1) + rand5();
// if `val` is smaller than 22 i.e. between `1 and 21` we can directly
// return the `number module 7` as our random number otherwise we again
// recur for `rand7()`.
//
// Probability for `x` (`x` is in between `1 and 7`): `P(x)`
// ```
// P(x) = (3/25) + (4/25)*(3/25) + (4/25)*(4/25)*(3/25) +
// (4/25)*(4/25)*(4/25)*(3/25) + ...... upto infinity
// i.e.
// = (3/25) / (1 - 4/25)
// = 1/7 (uniform)
// ```
if (val <= 21) return 1 + val % 7;
// now, recur if greater than 21
return rand7();
}
int main() {
srand(time(NULL));
int t;
cin >> t;
while (t--) {
rand7();
}
}