Crimson Triples
Description:â
After summoning the next boss â The Brain of Cthulhu, you noticed that it surrounds itself with eyes, numbered from to . In one attack, The Brain of Cthulhu chooses a triple of eyes (not necessarily distinct) with numbers . The triple of eyes is called crimson if and only if the following property holds:
To defeat the boss, you want to know how many ways The Brain of Cthulhu can choose a crimson triple of eyes. The triples of eyes and are considered different if , or , or .
Input Each test contains multiple test cases. The first line contains the number of test cases (). The description of the test cases follows. The only line of each test case contains one integer () â the number of eyes of The Brain of Cthulhu. It is guaranteed that the sum of across all test cases does not exceed .
Output For each test case, output one integer â the number of ways to choose a crimson triple of eyes.
Approaches:â
1. Math and Number Theory Observationâ
To solve this problem efficiently, we need to simplify the condition .
- Prime Factorization Analysis: Let's look at the exponent of a specific prime in the prime factorization of and . Let these exponents be and respectively.
- The exponent of in is .
- The exponent of in is .
- The exponent of on the left-hand side (LHS) of our equation is .
- The exponent of on the right-hand side (RHS) of our equation is .
- Deriving the Requirement: We need for all primes .
- If you test the possible orderings of and , you will find this equality only holds if and .
- This means the exponent of any prime in must be less than or equal to its exponent in both and .
- In number theory terms, this simply means must divide and must divide .
- Counting the Triples: Now the problem is reduced to finding the number of pairs such that both are multiples of , for every possible from to .
- For a fixed , there are exactly multiples of in the range .
- Since and are chosen independently from these multiples, there are valid pairs for a given .
- We just need to sum for all from to .
Complexityâ
- Time Complexity: per testcase. Since the sum of across all test cases is , the overall time complexity is which easily passes within the time limit.
- Space Complexity: as we only need a few variables to store the sum.
Solutions:â
C++â
#include <iostream>
using namespace std;
void solve() {
int n;
cin >> n;
long long res = 0;
// Sum (n / b)^2 for all 1 <= b <= n
for (int b = 1; b <= n; b++) {
long long multiples = n / b;
res += multiples * multiples;
}
cout << res << "\n";
}
int main() {
// Fast I/O
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin >> t;
while (t--) {
solve();
}
return 0;
}
Javaâ
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
if (scanner.hasNextInt()) {
int t = scanner.nextInt();
while (t-- > 0) {
solve(scanner);
}
}
scanner.close();
}
private static void solve(Scanner scanner) {
int n = scanner.nextInt();
long res = 0;
// Sum (n / b)^2 for all 1 <= b <= n
for (int b = 1; b <= n; b++) {
long multiples = n / b;
res += multiples * multiples;
}
System.out.println(res);
}
}
Pythonâ
import sys
def solve():
input_data = sys.stdin.read().split()
if not input_data:
return
t = int(input_data[0])
for i in range(1, t + 1):
n = int(input_data[i])
res = 0
# Sum (n / b)^2 for all 1 <= b <= n
for b in range(1, n + 1):
multiples = n // b
res += multiples * multiples
print(res)
if __name__ == "__main__":
solve()
JavaScriptâ
const fs = require('fs');
function main() {
const input = fs.readFileSync(0, 'utf-8').trim().split(/\s+/);
if (input.length === 0 || input[0] === '') return;
let t = parseInt(input[0], 10);
let index = 1;
for (let i = 0; i < t; i++) {
let n = parseInt(input[index++], 10);
let res = 0; // Number max safe integer is ~9e15, max answer is ~3.2e10, so safe.
// Sum (n / b)^2 for all 1 <= b <= n
for (let b = 1; b <= n; b++) {
let multiples = Math.floor(n / b);
res += multiples * multiples;
}
console.log(res);
}
}
main();
Completed working through this block? Sync progress to workspace.