-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path129-repunit-divisibility.js
More file actions
156 lines (136 loc) · 2.95 KB
/
129-repunit-divisibility.js
File metadata and controls
156 lines (136 loc) · 2.95 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/**
* Repunit Divisibility
* Time Complexity: O(T * n^0.25 * log n)
* Space Complexity: O(log n)
*/
const BigIntZero = 0n;
const BigIntOne = 1n;
const BigIntTwo = 2n;
function power(base, exp, mod) {
let res = 1n;
base %= mod;
while (exp > 0n) {
if (exp & 1n) res = (res * base) % mod;
base = (base * base) % mod;
exp >>= 1n;
}
return res;
};
function isPrimeMR(n) {
if (n < 2n) return false;
if (n === 2n || n === 3n) return true;
if (n % 2n === 0n) return false;
let d = n - 1n;
let s = 0n;
while ((d & 1n) === 0n) {
d >>= 1n;
s++;
}
const bases = [2n, 3n, 5n, 7n, 11n, 13n, 17n, 19n, 23n];
for (const a of bases) {
if (n <= a) break;
let x = power(a, d, n);
if (x === 1n || x === n - 1n) continue;
let composite = true;
for (let r = 1n; r < s; r++) {
x = (x * x) % n;
if (x === n - 1n) {
composite = false;
break;
}
}
if (composite) return false;
}
return true;
};
function gcd(a, b) {
while (b > 0n) {
let temp = b;
b = a % b;
a = temp;
}
return a;
};
function pollardRho(n) {
if (n === 1n) return 1n;
if (n % 2n === 0n) return 2n;
let x = 2n;
let y = 2n;
let d = 1n;
let c = 1n;
const f = (val) => (val * val + c) % n;
while (d === 1n) {
x = f(x);
y = f(f(y));
let diff = x > y ? x - y : y - x;
d = gcd(diff, n);
if (d === n) {
x = BigInt(Math.floor(Math.random() * Number(n - 2n)) + 2);
y = x;
c = BigInt(Math.floor(Math.random() * Number(n - 1n)) + 1);
d = 1n;
}
}
return d;
};
function getFactors(n, factors) {
if (n === 1n) return;
if (isPrimeMR(n)) {
factors.set(n, (factors.get(n) || 0n) + 1n);
return;
}
let divisor = pollardRho(n);
getFactors(divisor, factors);
getFactors(n / divisor, factors);
};
function getPhi(n) {
const factors = new Map();
getFactors(n, factors);
let result = n;
for (const [p, exponent] of factors) {
result = (result / p) * (p - 1n);
}
return result;
};
function getPrimeFactorsOnly(n) {
const factors = new Map();
getFactors(n, factors);
return factors;
};
function solve(nStr) {
const n = BigInt(nStr);
const M = 9n * n;
const phiM = getPhi(M);
let k = phiM;
const phiFactors = getPrimeFactorsOnly(phiM);
for (const [p, count] of phiFactors) {
while (k % p === 0n) {
const nextK = k / p;
if (power(10n, nextK, M) === 1n) {
k = nextK;
} else {
break;
}
}
}
return k.toString();
};
function processData(input) {
const lines = input.trim().split(/\s+/);
if (lines.length === 0) return;
const T = parseInt(lines[0], 10);
let ptr = 1;
for (let i = 0; i < T; i++) {
const nStr = lines[ptr++];
console.log(solve(nStr));
}
};
process.stdin.resume();
process.stdin.setEncoding("ascii");
let _input = "";
process.stdin.on("data", function (input) {
_input += input;
});
process.stdin.on("end", function () {
processData(_input);
});