-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path182-rsa-encryption.js
More file actions
157 lines (129 loc) · 3.33 KB
/
182-rsa-encryption.js
File metadata and controls
157 lines (129 loc) · 3.33 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
157
/**
* RSA Encryption
* Time Complexity: O(√φ + 2^t)
* Space Complexity: O(t)
*/
process.stdin.resume();
process.stdin.setEncoding("ascii");
const MOD = 1000000007n;
const INV2 = 500000004n;
let input_stdin = "";
process.stdin.on("data", function (data) {
input_stdin += data;
});
process.stdin.on("end", function () {
if (input_stdin.trim() === "") return;
const lines = input_stdin.trim().split("\n");
for (const line of lines) {
const trimmed = line.trim();
if (trimmed.length > 0) {
solve(trimmed);
}
}
});
function solve(line) {
const parts = line.split(/\s+/);
if (parts.length < 2) return;
const p = BigInt(parts[0]);
const q = BigInt(parts[1]);
const phi = (p - 1n) * (q - 1n);
const maxK = phi / 2n - 1n;
const factors = getDistinctPrimeFactors(phi);
const constraints = [];
for (const f of factors) {
if (f === 2n) {
if ((p & 3n) === 1n || (q & 3n) === 1n) {
constraints.push({ f: f, bad: [0n] });
}
} else {
constraints.push({ f: f, bad: [0n, (f - 1n) >> 1n] });
}
}
const result = getSumAndCountIEP(0, 1n, 0n, constraints, maxK);
const totalSumE = (2n * result.sum + result.count) % MOD;
console.log(totalSumE.toString());
};
function getSumAndCountIEP(idx, currentMod, currentRem, constraints, limit) {
if (idx === constraints.length) {
return calcArithmeticSum(currentMod, currentRem, limit);
}
const { f, bad } = constraints[idx];
let res = getSumAndCountIEP(
idx + 1,
currentMod,
currentRem,
constraints,
limit,
);
let totalSum = res.sum;
let totalCount = res.count;
for (const badRem of bad) {
const crt = combineCRT(currentMod, currentRem, f, badRem);
if (crt) {
const subRes = getSumAndCountIEP(
idx + 1,
crt.mod,
crt.rem,
constraints,
limit,
);
totalSum = (totalSum - subRes.sum + MOD) % MOD;
totalCount = (totalCount - subRes.count + MOD) % MOD;
}
}
return { sum: totalSum, count: totalCount };
};
function calcArithmeticSum(mod, rem, limit) {
if (rem > limit) return { sum: 0n, count: 0n };
const maxJ = (limit - rem) / mod;
const count = (maxJ + 1n) % MOD;
const n = maxJ % MOD;
const tri = (n * (n + 1n)) % MOD;
const triVal = (tri * INV2) % MOD;
const term1 = ((mod % MOD) * triVal) % MOD;
const term2 = ((rem % MOD) * count) % MOD;
const sum = (term1 + term2) % MOD;
return { sum, count };
};
function combineCRT(m1, r1, m2, r2) {
let diff = (((r2 - r1) % m2) + m2) % m2;
const inv = modInverse(m1, m2);
if (inv === null) return null;
const k = (diff * inv) % m2;
const newMod = m1 * m2;
const newRem = (m1 * k + r1) % newMod;
return { mod: newMod, rem: newRem };
};
function modInverse(a, m) {
let m0 = m,
x0 = 0n,
x1 = 1n;
if (m === 1n) return 0n;
a %= m;
while (a > 1n) {
if (m === 0n) return null;
const q = a / m;
let t = m;
m = a % m;
a = t;
t = x0;
x0 = x1 - q * x0;
x1 = t;
}
return x1 < 0n ? x1 + m0 : x1;
};
function getDistinctPrimeFactors(n) {
const factors = [];
if ((n & 1n) === 0n) {
factors.push(2n);
while ((n & 1n) === 0n) n >>= 1n;
}
for (let d = 3n; d * d <= n; d += 2n) {
if (n % d === 0n) {
factors.push(d);
while (n % d === 0n) n /= d;
}
}
if (n > 1n) factors.push(n);
return factors;
};