├── Euler-001.java ├── Euler-002.js ├── Euler-003.js ├── Euler-004.js ├── Euler-005.js ├── Euler-006.js ├── Euler-007.js ├── Euler-008.js ├── Euler-009.js ├── Euler-010.js ├── Euler-011.js ├── Euler-012.js ├── Euler-013.js ├── Euler-014.cpp ├── Euler-015.js ├── Euler-016.js ├── Euler-017.cpp ├── Euler-018.js ├── Euler-019.cpp ├── Euler-020.js ├── Euler-021.js ├── Euler-022.js ├── Euler-023.js ├── Euler-024.js ├── Euler-025.cpp ├── Euler-026.cpp ├── Euler-027.cpp ├── Euler-028.cpp ├── Euler-029.cpp ├── Euler-030.cpp ├── Euler-031.cpp ├── Euler-032.cpp ├── Euler-033.cpp ├── Euler-034.cpp ├── Euler-035.cpp ├── Euler-036.js ├── Euler-037.cpp ├── Euler-038.cpp ├── Euler-039.cpp └── Euler-040.cpp /Euler-001.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | import java.text.*; 4 | import java.math.*; 5 | import java.util.regex.*; 6 | 7 | public class Solution { 8 | 9 | public static long ans(long n){ 10 | //n * (n - 1)/2 11 | long three = 0, first = 0, five = 0, second = 0, extra = 0, third = 0; 12 | if(n%3 == 0){ 13 | three = n/3; 14 | first = 3*((three * (three - 1))/2); 15 | }else{ 16 | three = n/3; 17 | first = 3*((three * (three + 1))/2); 18 | } 19 | 20 | if(n%5 == 0){ 21 | five = n/5; 22 | second = 5*((five * (five - 1))/2); 23 | }else{ 24 | five = n/5; 25 | second = 5*((five * (five + 1))/2); 26 | } 27 | 28 | if(n%15 == 0){ 29 | extra = n/15; 30 | third = 15*((extra * (extra - 1))/2); 31 | }else{ 32 | extra = n/15; 33 | third = 15*((extra * (extra + 1))/2); 34 | } 35 | 36 | 37 | 38 | return first + second - third; 39 | } 40 | 41 | public static void main(String[] args) { 42 | Scanner in = new Scanner(System.in); 43 | int t = in.nextInt(); 44 | for(int a0 = 0; a0 < t; a0++){ 45 | long n = in.nextLong(); 46 | System.out.println(ans(n)); 47 | } 48 | } 49 | } -------------------------------------------------------------------------------- /Euler-002.js: -------------------------------------------------------------------------------- 1 | process.stdin.resume(); 2 | process.stdin.setEncoding('ascii'); 3 | 4 | var input_stdin = ""; 5 | var input_stdin_array = ""; 6 | var input_currentline = 0; 7 | 8 | process.stdin.on('data', function (data) { 9 | input_stdin += data; 10 | }); 11 | 12 | process.stdin.on('end', function () { 13 | input_stdin_array = input_stdin.split("\n"); 14 | main(); 15 | }); 16 | 17 | function readLine() { 18 | return input_stdin_array[input_currentline++]; 19 | } 20 | 21 | function fibonacci_sum_even(n) { 22 | // initialize the variables 23 | let sum_even = 0; 24 | let a = 1, b = 2; 25 | 26 | while (b <= n) { 27 | if (b % 2 === 0) { 28 | sum_even += b; 29 | } 30 | [a, b] = [b, a + b]; 31 | } 32 | 33 | return sum_even; 34 | } 35 | 36 | function main() { 37 | const t = parseInt(readLine().trim(), 10); 38 | 39 | for (let i = 0; i < t; i++) { 40 | const n = BigInt(readLine().trim()); 41 | 42 | const result = fibonacci_sum_even(n); 43 | console.log(result.toString()); 44 | } 45 | } -------------------------------------------------------------------------------- /Euler-003.js: -------------------------------------------------------------------------------- 1 | process.stdin.resume(); 2 | process.stdin.setEncoding('ascii'); 3 | 4 | let input_stdin = ""; 5 | let input_stdin_array = ""; 6 | let input_currentline = 0; 7 | 8 | process.stdin.on('data', function (data) { 9 | input_stdin += data; 10 | }); 11 | process.stdin.on('end', function () { 12 | input_stdin_array = input_stdin.split("\n"); 13 | main(); 14 | }); 15 | 16 | function readLine() { 17 | return input_stdin_array[input_currentline++]; 18 | } 19 | 20 | function largestPrimeFactor(n) { 21 | let maxPrime = -1; 22 | while (n % 2 === 0) { 23 | maxPrime = 2; 24 | n /= 2; 25 | } 26 | for (let i = 3; i <= Math.sqrt(n); i += 2) { 27 | while (n % i === 0) { 28 | maxPrime = i; 29 | n /= i; 30 | } 31 | } 32 | if (n > 2) { 33 | maxPrime = n; 34 | } 35 | return maxPrime; 36 | } 37 | 38 | function main() { 39 | let t = parseInt(readLine()); 40 | for (let a0 = 0; a0 < t; a0++) { 41 | let n = parseInt(readLine()); 42 | let largestPrime = largestPrimeFactor(n); 43 | console.log(largestPrime); 44 | } 45 | } -------------------------------------------------------------------------------- /Euler-004.js: -------------------------------------------------------------------------------- 1 | process.stdin.resume(); 2 | process.stdin.setEncoding('ascii'); 3 | 4 | let input_stdin = ""; 5 | let input_stdin_array = ""; 6 | let input_currentline = 0; 7 | 8 | process.stdin.on('data', function (data) { 9 | input_stdin += data; 10 | }); 11 | 12 | process.stdin.on('end', function () { 13 | input_stdin_array = input_stdin.split("\n"); 14 | main(); 15 | }); 16 | 17 | function readLine() { 18 | return input_stdin_array[input_currentline++]; 19 | } 20 | 21 | function isPalindrome(n) { 22 | n = n.toString(); 23 | let len = n.length; 24 | for (let i = 0; i < Math.floor(len / 2); i++) { 25 | if (n[i] !== n[len - 1 - i]) { 26 | return false; 27 | } 28 | } 29 | return true; 30 | } 31 | 32 | function largestPalindrome(n) { 33 | let maxPalindrome = 0; 34 | let a, b, product; 35 | for (a = 999; a >= 100; a--) { 36 | for (b = a; b >= 100; b--) { 37 | product = a * b; 38 | if (product < maxPalindrome) { 39 | break; 40 | } 41 | if (product < n && isPalindrome(product)) { 42 | maxPalindrome = product; 43 | } 44 | } 45 | } 46 | return maxPalindrome; 47 | } 48 | 49 | function main() { 50 | let t = parseInt(readLine()); 51 | for (let a0 = 0; a0 < t; a0++) { 52 | let n = parseInt(readLine()); 53 | console.log(largestPalindrome(n)); 54 | } 55 | } -------------------------------------------------------------------------------- /Euler-005.js: -------------------------------------------------------------------------------- 1 | process.stdin.resume(); 2 | process.stdin.setEncoding('ascii'); 3 | 4 | let input_stdin = ""; 5 | let input_stdin_array = ""; 6 | let input_currentline = 0; 7 | 8 | process.stdin.on('data', function (data) { 9 | input_stdin += data; 10 | }); 11 | 12 | process.stdin.on('end', function () { 13 | input_stdin_array = input_stdin.split("\n"); 14 | main(); 15 | }); 16 | 17 | function readLine() { 18 | return input_stdin_array[input_currentline++]; 19 | } 20 | 21 | /////////////// ignore above this line //////////////////// 22 | 23 | function main() { 24 | let t = parseInt(readLine()); 25 | for (let a0 = 0; a0 < t; a0++) { 26 | let n = parseInt(readLine()); 27 | let lcm = 1; 28 | for (let i = 2; i <= n; i++) { 29 | lcm = (lcm * i) / gcd(lcm, i); 30 | } 31 | console.log(parseInt(lcm)); 32 | } 33 | } 34 | 35 | function gcd(a, b) { 36 | if (b == 0) { 37 | return a; 38 | } 39 | return gcd(b, a % b); 40 | } -------------------------------------------------------------------------------- /Euler-006.js: -------------------------------------------------------------------------------- 1 | process.stdin.resume(); 2 | process.stdin.setEncoding('ascii'); 3 | 4 | let input_stdin = ""; 5 | let input_stdin_array = ""; 6 | let input_currentline = 0; 7 | 8 | process.stdin.on('data', function (data) { 9 | input_stdin += data; 10 | }); 11 | 12 | process.stdin.on('end', function () { 13 | input_stdin_array = input_stdin.split("\n"); 14 | main(); 15 | }); 16 | 17 | function readLine() { 18 | return input_stdin_array[input_currentline++]; 19 | } 20 | 21 | function getAbsoluteDifference(n) { 22 | let sum_of_squares = (n * (n + 1) * (2 * n + 1)) / 6; 23 | let square_of_sum = Math.pow((n * (n + 1)) / 2, 2); 24 | return Math.abs(sum_of_squares - square_of_sum); 25 | } 26 | 27 | function main() { 28 | let t = parseInt(readLine()); 29 | for (let a0 = 0; a0 < t; a0++) { 30 | let n = parseInt(readLine()); 31 | let result = getAbsoluteDifference(n); 32 | console.log(result); 33 | } 34 | } -------------------------------------------------------------------------------- /Euler-007.js: -------------------------------------------------------------------------------- 1 | process.stdin.resume(); 2 | process.stdin.setEncoding('ascii'); 3 | 4 | var input_stdin = ""; 5 | var input_stdin_array = ""; 6 | var input_currentline = 0; 7 | 8 | process.stdin.on('data', function (data) { 9 | input_stdin += data; 10 | }); 11 | 12 | process.stdin.on('end', function () { 13 | input_stdin_array = input_stdin.split("\n"); 14 | main(); 15 | }); 16 | 17 | function readLine() { 18 | return input_stdin_array[input_currentline++]; 19 | } 20 | 21 | function main() { 22 | var t = parseInt(readLine()); 23 | var maxN = 10000; 24 | var primes = generatePrimes(maxN); 25 | for (var a0 = 0; a0 < t; a0++) { 26 | var n = parseInt(readLine()); 27 | console.log(primes[n - 1]); 28 | } 29 | } 30 | 31 | function generatePrimes(maxN) { 32 | var primes = []; 33 | var isPrime = new Array(maxN + 1).fill(true); 34 | isPrime[0] = false; isPrime[1] = false; 35 | for (var p = 2; p <= maxN; p++) { 36 | if (isPrime[p]) { 37 | primes.push(p); 38 | for (var i = p * p; i <= maxN; i += p) { 39 | isPrime[i] = false; 40 | } 41 | } 42 | } 43 | 44 | return primes; 45 | } -------------------------------------------------------------------------------- /Euler-008.js: -------------------------------------------------------------------------------- 1 | process.stdin.resume(); 2 | process.stdin.setEncoding('ascii'); 3 | 4 | let input_stdin = ""; 5 | let input_stdin_array = ""; 6 | let input_currentline = 0; 7 | 8 | process.stdin.on('data', function (data) { 9 | input_stdin += data; 10 | }); 11 | 12 | process.stdin.on('end', function () { 13 | input_stdin_array = input_stdin.split("\n"); 14 | main(); 15 | }); 16 | 17 | function readLine() { 18 | return input_stdin_array[input_currentline++]; 19 | } 20 | 21 | function main() { 22 | let t = parseInt(readLine()); 23 | for (let a0 = 0; a0 < t; a0++) { 24 | let n_temp = readLine().split(' '); 25 | let n = parseInt(n_temp[0]); 26 | let k = parseInt(n_temp[1]); 27 | let num = readLine(); 28 | let maxProduct = 0; 29 | for (let i = 0; i <= n - k; i++) { 30 | let product = 1; 31 | for (let j = 0; j < k; j++) { 32 | product *= parseInt(num.charAt(i + j)); 33 | } 34 | if (product > maxProduct) { 35 | maxProduct = product; 36 | } 37 | } 38 | console.log(maxProduct); 39 | } 40 | } -------------------------------------------------------------------------------- /Euler-009.js: -------------------------------------------------------------------------------- 1 | process.stdin.resume(); 2 | process.stdin.setEncoding('ascii'); 3 | 4 | let input_stdin = ""; 5 | let input_stdin_array = ""; 6 | let input_currentline = 0; 7 | 8 | process.stdin.on('data', function (data) { 9 | input_stdin += data; 10 | }); 11 | 12 | process.stdin.on('end', function () { 13 | input_stdin_array = input_stdin.split("\n"); 14 | main(); 15 | }); 16 | 17 | function readLine() { 18 | return input_stdin_array[input_currentline++]; 19 | } 20 | 21 | function main() { 22 | let t = parseInt(readLine()); 23 | for (let a0 = 0; a0 < t; a0++) { 24 | let n = parseInt(readLine()); 25 | let maxProduct = -1; 26 | for (let a = 1; a <= n / 3; a++) { 27 | for (let b = a + 1; b <= n / 2; b++) { 28 | let c = n - a - b; 29 | if (a * a + b * b == c * c) { 30 | let product = a * b * c; 31 | if (product > maxProduct) { 32 | maxProduct = product; 33 | } 34 | } 35 | } 36 | } 37 | 38 | console.log(maxProduct); 39 | } 40 | } -------------------------------------------------------------------------------- /Euler-010.js: -------------------------------------------------------------------------------- 1 | process.stdin.resume(); 2 | process.stdin.setEncoding('ascii'); 3 | 4 | let input_stdin = ""; 5 | let input_stdin_array = ""; 6 | let input_currentline = 0; 7 | 8 | process.stdin.on('data', function (data) { 9 | input_stdin += data; 10 | }); 11 | 12 | process.stdin.on('end', function () { 13 | input_stdin_array = input_stdin.split("\n"); 14 | main(); 15 | }); 16 | 17 | function readLine() { 18 | return input_stdin_array[input_currentline++]; 19 | } 20 | function sumOfPrimes(n) { 21 | const isPrime = new Array(n + 1).fill(true); 22 | isPrime[0] = false; 23 | isPrime[1] = false; 24 | 25 | for (let i = 2; i <= Math.sqrt(n); i++) { 26 | if (isPrime[i]) { 27 | for (let j = i * i; j <= n; j += i) { 28 | isPrime[j] = false; 29 | } 30 | } 31 | } 32 | 33 | let sum = 0; 34 | for (let i = 2; i <= n; i++) { 35 | if (isPrime[i]) { 36 | sum += i; 37 | } 38 | } 39 | 40 | return sum; 41 | } 42 | 43 | function main() { 44 | const t = parseInt(readLine()); 45 | for (let i = 0; i < t; i++) { 46 | const n = parseInt(readLine()); 47 | const result = sumOfPrimes(n); 48 | console.log(result); 49 | } 50 | } -------------------------------------------------------------------------------- /Euler-011.js: -------------------------------------------------------------------------------- 1 | process.stdin.resume(); 2 | process.stdin.setEncoding('ascii'); 3 | 4 | let input_stdin = ""; 5 | let input_stdin_array = ""; 6 | let input_currentline = 0; 7 | 8 | process.stdin.on('data', function (data) { 9 | input_stdin += data; 10 | }); 11 | 12 | process.stdin.on('end', function () { 13 | input_stdin_array = input_stdin.split("\n"); 14 | main(); 15 | }); 16 | 17 | function readLine() { 18 | return input_stdin_array[input_currentline++]; 19 | } 20 | 21 | /////////////// ignore above this line //////////////////// 22 | 23 | function main() { 24 | let grid = []; 25 | for (grid_i = 0; grid_i < 20; grid_i++) { 26 | grid[grid_i] = readLine().split(' '); 27 | grid[grid_i] = grid[grid_i].map(Number); 28 | } 29 | 30 | let maxProduct = 0; 31 | 32 | // Find the maximum product in the horizontal direction 33 | for (let i = 0; i < 20; i++) { 34 | for (let j = 0; j < 17; j++) { 35 | let product = grid[i][j] * grid[i][j + 1] * grid[i][j + 2] * grid[i][j + 3]; 36 | if (product > maxProduct) { 37 | maxProduct = product; 38 | } 39 | } 40 | } 41 | 42 | for (let i = 0; i < 17; i++) { 43 | for (let j = 0; j < 20; j++) { 44 | let product = grid[i][j] * grid[i + 1][j] * grid[i + 2][j] * grid[i + 3][j]; 45 | if (product > maxProduct) { 46 | maxProduct = product; 47 | } 48 | } 49 | } 50 | 51 | for (let i = 0; i < 17; i++) { 52 | for (let j = 0; j < 17; j++) { 53 | let product = grid[i][j] * grid[i + 1][j + 1] * grid[i + 2][j + 2] * grid[i + 3][j + 3]; 54 | if (product > maxProduct) { 55 | maxProduct = product; 56 | } 57 | } 58 | } 59 | 60 | for (let i = 0; i < 17; i++) { 61 | for (let j = 3; j < 20; j++) { 62 | let product = grid[i][j] * grid[i + 1][j - 1] * grid[i + 2][j - 2] * grid[i + 3][j - 3]; 63 | if (product > maxProduct) { 64 | maxProduct = product; 65 | } 66 | } 67 | } 68 | 69 | console.log(maxProduct); 70 | } -------------------------------------------------------------------------------- /Euler-012.js: -------------------------------------------------------------------------------- 1 | function countDivisors(n) { 2 | let count = 0; 3 | for (let i = 1; i <= Math.sqrt(n); i++) { 4 | if (n % i === 0) { 5 | if (n / i === i) { 6 | count++; 7 | } else { 8 | count += 2; 9 | } 10 | } 11 | } 12 | return count; 13 | } 14 | 15 | function findTriangleNumberWithDivisors(divisors) { 16 | let n = 1; 17 | let triangleNumber = 1; 18 | while (countDivisors(triangleNumber) <= divisors) { 19 | n++; 20 | triangleNumber = n * (n + 1) / 2; 21 | } 22 | return triangleNumber; 23 | } 24 | 25 | function processData(input) { 26 | const lines = input.split("\n"); 27 | const testCases = Number(lines[0]); 28 | for (let i = 1; i <= testCases; i++) { 29 | const divisors = Number(lines[i]); 30 | console.log(findTriangleNumberWithDivisors(divisors)); 31 | } 32 | } 33 | 34 | process.stdin.resume(); 35 | process.stdin.setEncoding("ascii"); 36 | let _input = ""; 37 | process.stdin.on("data", function (input) { 38 | _input += input; 39 | }); 40 | 41 | process.stdin.on("end", function () { 42 | processData(_input); 43 | }); -------------------------------------------------------------------------------- /Euler-013.js: -------------------------------------------------------------------------------- 1 | function processData(input) { 2 | let lines = input.trim().split('\n'); 3 | let sum = BigInt(0); 4 | for (let i = 1; i < lines.length; i++) { 5 | sum += BigInt(lines[i]); 6 | } 7 | let firstTenDigits = sum.toString().substring(0, 10); 8 | console.log(firstTenDigits); 9 | } 10 | 11 | process.stdin.resume(); 12 | process.stdin.setEncoding("ascii"); 13 | _input = ""; 14 | process.stdin.on("data", function (input) { 15 | _input += input; 16 | }); 17 | 18 | process.stdin.on("end", function () { 19 | processData(_input); 20 | }); -------------------------------------------------------------------------------- /Euler-014.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | class Solution{ 8 | 9 | private: 10 | int* record; 11 | int* maxLength; 12 | int* startNumber; 13 | int maxNum; 14 | 15 | public: 16 | Solution(int maxNum){ 17 | 18 | this->maxNum = maxNum; 19 | 20 | record = new int[maxNum + 1]; 21 | maxLength = new int[maxNum + 1]; 22 | startNumber = new int[maxNum + 1]; 23 | for(int i = 0 ; i <= maxNum ; i ++) { 24 | record[i] = 0; 25 | maxLength[i] = 0; 26 | startNumber[i] = 0; 27 | } 28 | 29 | for(int i = 1 ; i <= maxNum ; i ++){ 30 | if(record[i] == 0) 31 | record[i] = search((long long)i); 32 | // cout << record[i] << endl; 33 | 34 | if(record[i] >= maxLength[i-1]){ 35 | maxLength[i] = record[i]; 36 | startNumber[i] = i; 37 | } 38 | else{ 39 | maxLength[i] = maxLength[i-1]; 40 | startNumber[i] = startNumber[i-1]; 41 | } 42 | } 43 | 44 | } 45 | 46 | ~Solution(){ 47 | delete[] record; 48 | delete[] maxLength; 49 | delete[] startNumber; 50 | } 51 | 52 | int longestChain(int N){ 53 | return startNumber[N]; 54 | } 55 | 56 | private: 57 | int search(long long num){ 58 | if(num <= (long long)maxNum && record[num] != 0) 59 | return record[num]; 60 | 61 | if(num == (long long)1) 62 | return 1; 63 | 64 | int res; 65 | if(num % (long long)2 == (long long)0) 66 | res = 1 + search(num / (long long)2); 67 | else 68 | res = 1 + search((long long)3 * num + (long long)1); 69 | 70 | if(num <= (long long)maxNum) 71 | record[num] = res; 72 | 73 | return res; 74 | } 75 | }; 76 | 77 | int main() { 78 | 79 | int maxN = 5000000; 80 | Solution solution(maxN); 81 | 82 | int T; 83 | cin >> T; 84 | while(T --){ 85 | int N; 86 | cin >> N; 87 | cout << solution.longestChain(N) << endl; 88 | } 89 | return 0; 90 | } -------------------------------------------------------------------------------- /Euler-015.js: -------------------------------------------------------------------------------- 1 | function processData(input) { 2 | input = input.trim().split('\n'); 3 | const mod = 1e9 + 7; 4 | let index = 0; 5 | const t = Number(input[index++]); 6 | for (let i = 0; i < t; i++) { 7 | const [n, m] = input[index++].trim().split(' ').map(Number); 8 | const v = Array.from({ length: n + 1 }, () => Array(m + 1).fill(1)); 9 | for (let i = 1; i <= n; i++) { 10 | for (let j = 1; j <= m; j++) { 11 | v[i][j] = (v[i - 1][j] + v[i][j - 1]) % mod; 12 | } 13 | } 14 | console.log(v[n][m]); 15 | } 16 | } 17 | process.stdin.resume(); 18 | process.stdin.setEncoding("ascii"); 19 | let _input = ""; 20 | process.stdin.on("data", function (input) { 21 | _input += input; 22 | }); 23 | process.stdin.on("end", function () { 24 | processData(_input); 25 | }); -------------------------------------------------------------------------------- /Euler-016.js: -------------------------------------------------------------------------------- 1 | function processData(input) { 2 | input = input.trim().split('\n'); 3 | const v = []; 4 | const two = [1]; 5 | let power = 0; 6 | v.push(1); 7 | for (; power <= 1e4;) { 8 | let digSum = 0; 9 | let carry = 0; 10 | for (let i = 0; i < two.length; i++) { 11 | carry += two[i] * 2; 12 | two[i] = carry % 10; 13 | digSum += two[i]; 14 | carry = Math.floor(carry / 10); 15 | } 16 | while (carry !== 0) { 17 | two.push(carry % 10); 18 | digSum += carry % 10; 19 | carry = Math.floor(carry / 10); 20 | } 21 | v.push(digSum); 22 | power++; 23 | } 24 | const t = Number(input[0]); 25 | for (let i = 1; i <= t; i++) { 26 | const n = Number(input[i]); 27 | console.log(v[n]); 28 | } 29 | } 30 | 31 | process.stdin.resume(); 32 | process.stdin.setEncoding("ascii"); 33 | let _input = ""; 34 | process.stdin.on("data", function (input) { 35 | _input += input; 36 | }); 37 | 38 | process.stdin.on("end", function () { 39 | processData(_input); 40 | }); -------------------------------------------------------------------------------- /Euler-017.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | typedef long long ll; 9 | 10 | string units[] = {"", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen", "Twenty"}; 11 | string decim[] = {"", "", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"}; 12 | string hun = "Hundred"; 13 | string thous = "Thousand"; 14 | string mill = "Million"; 15 | string bill = "Billion"; 16 | 17 | vector ans; 18 | void solve(ll n){ 19 | if(n <= 20){ 20 | ans.push_back(units[n]); 21 | }else if(n < 100){ // > 20 22 | ans.push_back(decim[n/10]); 23 | solve(n%10); 24 | }else if(n < 1000){// >= 100 25 | solve(n/100); 26 | ans.push_back(hun); 27 | solve(n%100); 28 | }else if(n < 1e6){ // >= 1000 29 | solve(n/1000); 30 | ans.push_back(thous); 31 | solve(n%1000); 32 | }else if(n < 1e9){ 33 | ll h = 1e6; 34 | solve(n/h); 35 | ans.push_back(mill); 36 | solve(n%h); 37 | }else{ 38 | ll h = 1e9; 39 | solve(n/h); 40 | ans.push_back(bill); 41 | solve(n%h); 42 | } 43 | 44 | } 45 | int main() { 46 | int t; scanf("%d", &t); 47 | while(t--){ 48 | ll n; scanf("%lld", &n); 49 | if(n == 0){ 50 | cout << "Zero\n"; 51 | } 52 | else{ 53 | ans.clear(); 54 | solve(n); 55 | cout << ans[0]; 56 | for(int i=1;i= 0; i--) { 14 | for (let j = 0; j <= i; j++) { 15 | if (i == n - 1) { 16 | dp[i][j] = v[i][j]; 17 | } else { 18 | dp[i][j] = v[i][j] + Math.max(dp[i + 1][j], dp[i + 1][j + 1]); 19 | } 20 | } 21 | } 22 | console.log(dp[0][0]); 23 | } 24 | } 25 | 26 | process.stdin.resume(); 27 | process.stdin.setEncoding("ascii"); 28 | let _input = ""; 29 | process.stdin.on("data", function (input) { 30 | _input += input; 31 | }); 32 | 33 | process.stdin.on("end", function () { 34 | processData(_input); 35 | }); -------------------------------------------------------------------------------- /Euler-019.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | typedef long long ll; 9 | 10 | int leap(ll y){ 11 | if(y%400 == 0){ 12 | return true; 13 | }else if(y%100 == 0){ 14 | return false; 15 | }else if(y%4 == 0){ 16 | return true; 17 | }else { 18 | return false; 19 | } 20 | } 21 | int ms[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; 22 | int days(ll y, int m){ 23 | if(m == 2 && leap(y)) return 29; 24 | else return ms[m]; 25 | } 26 | int getDays(ll y, int m){ 27 | 28 | 29 | ll rem = y-1; 30 | ll yls = rem/400 + rem/4 - rem/100; 31 | ll ans = yls+rem; 32 | 33 | int p = 1; 34 | while(p < m){ 35 | ans += days(y, p); 36 | p++; 37 | } 38 | return ans%7; 39 | 40 | } 41 | int get(ll y, int m, ll offset, ll ys, int ms){ 42 | ll ans = (offset%7 == 6)?1:0; 43 | int tmp = ms; 44 | while(ys < y || tmp < m){ 45 | offset = (offset+days(ys, tmp))%7; 46 | if(offset%7 == 6) ans++; 47 | tmp++; 48 | if(tmp == 13){ 49 | tmp = 1; 50 | ys++; 51 | } 52 | } 53 | return ans; 54 | } 55 | int main() { 56 | int t;cin>>t; 57 | while(t--){ 58 | ll y1, y2; 59 | int d1, d2, m1, m2; 60 | scanf("%lld %d %d %lld %d %d", &y1, &m1, &d1, &y2, &m2, &d2); 61 | 62 | if(d1 != 1){ 63 | m1++; 64 | d1 = 1; 65 | if(m1 == 13){ 66 | y1++; 67 | m1 = 1; 68 | } 69 | } 70 | ll before = (getDays(y1, m1)-getDays(1900, 1)+7)%7; 71 | ll b = get(y2, m2, before, y1, m1); 72 | printf("%lld\n", b); 73 | } 74 | return 0; 75 | } -------------------------------------------------------------------------------- /Euler-020.js: -------------------------------------------------------------------------------- 1 | function processData(input) { 2 | input = input.trim().split('\n').slice(1); 3 | for (let i = 0; i < input.length; i++) { 4 | let n = parseInt(input[i]); 5 | let v = [1]; 6 | let sc = 1; 7 | while (sc <= n) { 8 | mul(v, sc); 9 | sc++; 10 | } 11 | let sum = 0; 12 | for (let j = 0; j < v.length; j++) { 13 | sum += v[j]; 14 | } 15 | console.log(sum); 16 | } 17 | } 18 | function mul(v, x) { 19 | let carry = 0; 20 | for (let i = 0; i < v.length; i++) { 21 | carry += v[i] * x; 22 | v[i] = carry % 10; 23 | carry = Math.floor(carry / 10); 24 | } 25 | while (carry !== 0) { 26 | v.push(carry % 10); 27 | carry = Math.floor(carry / 10); 28 | } 29 | } 30 | 31 | process.stdin.resume(); 32 | process.stdin.setEncoding("ascii"); 33 | _input = ""; 34 | process.stdin.on("data", function (input) { 35 | _input += input; 36 | }); 37 | 38 | process.stdin.on("end", function () { 39 | processData(_input); 40 | }); -------------------------------------------------------------------------------- /Euler-021.js: -------------------------------------------------------------------------------- 1 | function processData(input) { 2 | let amicable = [ 3 | 220, 284, 1184, 1210, 2620, 2924, 5020, 5564, 6232, 6368, 10744, 10856, 12285, 14595, 4 | 17296, 18416, 63020, 66928, 66992, 67095, 69615, 71145, 76084, 79750, 87633, 88730 5 | ]; 6 | 7 | input = input.trim().split('\n'); 8 | let t = parseInt(input[0]); 9 | let curLine = 1; 10 | 11 | while (t--) { 12 | let n = parseInt(input[curLine++]); 13 | let total = 0; 14 | 15 | for (let it of amicable) { 16 | if (it >= n) break; 17 | 18 | total += it; 19 | } 20 | console.log(total); 21 | } 22 | } 23 | 24 | process.stdin.resume(); 25 | process.stdin.setEncoding("ascii"); 26 | let _input = ""; 27 | process.stdin.on("data", function (input) { 28 | _input += input; 29 | }); 30 | 31 | process.stdin.on("end", function () { 32 | processData(_input); 33 | }); -------------------------------------------------------------------------------- /Euler-022.js: -------------------------------------------------------------------------------- 1 | function processData(input) { 2 | let nameVals = new Map(); 3 | let namePos = new Map(); 4 | 5 | input = input.trim().split('\n'); 6 | let n = parseInt(input[0]); 7 | let names = input.slice(1, n + 1); 8 | 9 | for (let i = 0; i < n; i++) { 10 | let name = names[i]; 11 | let worth = 0; 12 | for (let c of name) { 13 | worth += c.charCodeAt(0) - 64; 14 | } 15 | nameVals.set(name, worth); 16 | } 17 | 18 | names.sort(); 19 | for (let i = 0; i < n; i++) { 20 | namePos.set(names[i], i + 1); 21 | } 22 | 23 | let q = parseInt(input[n + 1]); 24 | let queries = input.slice(n + 2); 25 | 26 | for (let i = 0; i < q; i++) { 27 | let query = queries[i]; 28 | console.log(nameVals.get(query) * namePos.get(query)); 29 | } 30 | } 31 | 32 | process.stdin.resume(); 33 | process.stdin.setEncoding("ascii"); 34 | let _input = ""; 35 | process.stdin.on("data", function (input) { 36 | _input += input; 37 | }); 38 | 39 | process.stdin.on("end", function () { 40 | processData(_input); 41 | }); -------------------------------------------------------------------------------- /Euler-023.js: -------------------------------------------------------------------------------- 1 | function processData(input) { 2 | input = input.trim().split('\n'); 3 | let abundant = []; 4 | let divisorSums = new Array(100010).fill(1); 5 | let abundantSums = new Array(28130).fill(false); 6 | for (let i = 2; i <= Math.sqrt(28123); i++) { 7 | for (let j = i; i * j <= 28123; j++) { 8 | let prod = i * j; 9 | divisorSums[prod] += (i == j) ? i : i + j; 10 | 11 | if (divisorSums[prod] > prod) { 12 | abundant.push(prod); 13 | divisorSums[prod] = -999999; 14 | } 15 | } 16 | } 17 | abundant.sort((a, b) => a - b); 18 | 19 | for (let i = 0; i < abundant.length; i++) { 20 | if (abundant[i] >= 14062) break; 21 | 22 | for (let j = i; j < abundant.length; j++) { 23 | let sum = abundant[i] + abundant[j]; 24 | 25 | if (sum > 28123) break; 26 | abundantSums[sum] = true; 27 | } 28 | } 29 | 30 | let t = Number(input.shift()); 31 | 32 | while (t--) { 33 | let n = Number(input.shift()); 34 | 35 | if (n > 28123 || abundantSums[n] == true) console.log("YES"); 36 | else console.log("NO"); 37 | } 38 | } 39 | 40 | process.stdin.resume(); 41 | process.stdin.setEncoding("ascii"); 42 | _input = ""; 43 | process.stdin.on("data", function (input) { 44 | _input += input; 45 | }); 46 | 47 | process.stdin.on("end", function () { 48 | processData(_input); 49 | }); -------------------------------------------------------------------------------- /Euler-024.js: -------------------------------------------------------------------------------- 1 | function processData(input) { 2 | input = input.trim().split('\n'); 3 | let t = parseInt(input.shift()); 4 | const GetFactoradic = (n, size) => { 5 | let fact = []; 6 | let index = 1; 7 | 8 | while (size--) { 9 | let rem = n % index; 10 | 11 | fact.push(rem); 12 | n = Math.floor(n / index); 13 | index++; 14 | } 15 | fact.reverse(); 16 | return fact; 17 | } 18 | 19 | while (t--) { 20 | let n = parseInt(input.shift()); 21 | 22 | let s = "abcdefghijklm"; 23 | let ans = ""; 24 | 25 | if (n == 1) { 26 | console.log(s); 27 | continue; 28 | } 29 | --n; 30 | let order = GetFactoradic(n, s.length); 31 | 32 | for (let i = 0; i < order.length; i++) { 33 | let c = s[order[i]]; 34 | s = s.slice(0, order[i]) + s.slice(order[i] + 1); 35 | ans += c; 36 | } 37 | console.log(ans); 38 | } 39 | } 40 | 41 | process.stdin.resume(); 42 | process.stdin.setEncoding("ascii"); 43 | _input = ""; 44 | process.stdin.on("data", function (input) { 45 | _input += input; 46 | }); 47 | 48 | process.stdin.on("end", function () { 49 | processData(_input); 50 | }); -------------------------------------------------------------------------------- /Euler-025.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | map memo; 5 | 6 | void NextTerm(vector &num, vector prev) 7 | { 8 | int carry = 0; 9 | int index = 4999; 10 | 11 | while(index >= 0) 12 | { 13 | int sum = num[index] + prev[index] + carry; 14 | 15 | if(sum >= 10) 16 | { 17 | carry = sum/10; 18 | num[index] = sum%10; 19 | } 20 | else 21 | { 22 | carry = 0; 23 | num[index] = sum; 24 | } 25 | index--; 26 | } 27 | } 28 | 29 | int main() 30 | { 31 | vector num(5000, 0); 32 | vector prev = num; 33 | num[4999] = 1; 34 | 35 | int digits = 0, index = 1; 36 | 37 | while(digits < 5000) 38 | { 39 | vector temp = num; 40 | NextTerm(num, prev); 41 | 42 | for(int i=0; i<5000; i++) 43 | { 44 | if(num[i] != 0) 45 | { 46 | digits = 5000 - i; 47 | 48 | if(memo.count(digits) == 0) 49 | { 50 | memo[digits] = index + 1; 51 | } 52 | break; 53 | } 54 | } 55 | prev = temp; 56 | index++; 57 | } 58 | 59 | int t; 60 | cin >> t; 61 | 62 | while(t--) 63 | { 64 | int n; 65 | cin >> n; 66 | 67 | cout << memo[n] << endl; 68 | } 69 | return 0; 70 | } -------------------------------------------------------------------------------- /Euler-026.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | map memo; 5 | 6 | string Divide(int num) 7 | { 8 | string ans = ""; 9 | int one = 1; 10 | int rem = -1; 11 | 12 | map remainders; 13 | int i = 0; 14 | 15 | while(rem != 0) 16 | { 17 | one *= 10; 18 | int count = 0; 19 | 20 | int next = (int)floor(one / num); 21 | int prod = num * next; 22 | one -= prod; 23 | rem = one % num; 24 | 25 | if(remainders.count(rem) != 0) 26 | { 27 | return ans.substr(remainders[rem]); 28 | } 29 | ans += to_string(next); 30 | remainders[rem] = i; 31 | i++; 32 | } 33 | return ""; 34 | } 35 | 36 | void ComputeRepeatLengths() 37 | { 38 | string longest = "3"; 39 | int index = 3; 40 | 41 | for(int i=3; i<10000; i++) 42 | { 43 | if(i < 7) 44 | { 45 | memo[i+1] = 3; 46 | continue; 47 | } 48 | memo[i] = index; 49 | 50 | if(i % 2 == 1) 51 | { 52 | string s = Divide(i); 53 | 54 | if(s.size() > longest.size()) 55 | { 56 | longest = s; 57 | index = i; 58 | } 59 | } 60 | } 61 | } 62 | 63 | 64 | int main() 65 | { 66 | ComputeRepeatLengths(); 67 | 68 | int t; 69 | cin >> t; 70 | 71 | while(t--) 72 | { 73 | int n; 74 | cin >> n; 75 | 76 | cout << memo[n] << endl; 77 | } 78 | return 0; 79 | } -------------------------------------------------------------------------------- /Euler-027.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | set primes = {1, 2}; 5 | 6 | void Sieve(int n) 7 | { 8 | vector sieve(100000000, false); 9 | sieve[2] = true; 10 | int p = 2; 11 | 12 | while(primes.size() < n) 13 | { 14 | for(int i=2; p*i < sieve.size(); i++) 15 | { 16 | sieve[p*i] = true; 17 | } 18 | for(int i=p+1; i> N; 51 | 52 | Sieve(2000); 53 | 54 | int a, b; 55 | int index = N; 56 | int mostPrimes = 0; 57 | 58 | pair ans; 59 | 60 | while(index >= 2) 61 | { 62 | for(int i=index; i>=2; i--) 63 | { 64 | if(primes.count(i)) 65 | { 66 | b = i; 67 | break; 68 | } 69 | } 70 | 71 | for(int i=1; i<=b; i++) 72 | { 73 | a = -i; 74 | int count = Formula(a, b); 75 | 76 | if(count > mostPrimes) 77 | { 78 | mostPrimes = count; 79 | ans = make_pair(a, b); 80 | } 81 | } 82 | index--; 83 | } 84 | cout << ans.first << ' ' << ans.second << endl; 85 | return 0; 86 | } -------------------------------------------------------------------------------- /Euler-028.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | using namespace std; 12 | typedef long long ll; 13 | typedef pair ii; 14 | typedef pair dd; 15 | typedef vector vi; 16 | typedef vector vii; 17 | typedef vector vvi; 18 | typedef vector vvii; 19 | const int mod = 1e9 + 7; 20 | int main(){ 21 | int T; scanf("%d", &T); 22 | while(T--){ 23 | ll n; scanf("%lld", &n); 24 | ll k = (n-1)/2; 25 | k--; 26 | ll ans = 1+24*(k%mod) + 18*(((k%mod)*((k+1)%mod))%mod); 27 | assert(ans > 0); 28 | ans %= mod; 29 | if((k+1)%3 == 0){ 30 | ans += ((8*(((k+1)/3)%mod))%mod) * (((k%mod) * ((2*k+1)%mod) )%mod); 31 | } 32 | else if(k%3 == 0){ 33 | ans += (((8*((k+1)%mod))%mod)%mod) * ((((k/3)%mod)*((2*k+1)%mod))%mod); 34 | }else{ 35 | assert((2*k+1)%3 == 0); 36 | ans += (((8*((k+1)%mod))%mod)%mod) * (((k%mod)*(((2*k+1)/3)%mod))%mod); 37 | } 38 | ans %= mod; 39 | ans += 24; 40 | assert(ans > 0); 41 | printf("%lld\n", ans%mod); 42 | } 43 | 44 | } -------------------------------------------------------------------------------- /Euler-029.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | typedef long long ll; 9 | 10 | int main() { 11 | int n; cin >> n; 12 | 13 | vector base(n+1, 1); 14 | //find base numbers; 15 | for(int i=2;i*i<=n;i++){ 16 | if(!base[i]) continue; 17 | for(ll k = i*i; k <= n; k *= i){ 18 | base[k] = false; 19 | } 20 | } 21 | set pars; 22 | vector num_pow; 23 | num_pow.push_back(0); //we don't consider 0th power 24 | for(int i=1;i<20;i++){ 25 | if((1 << i) > n) break; //base number greater than n (when 2^i > N then surely X^i > N for X > 2) 26 | for(int j=2;j<=n;j++){ 27 | //(X^i)^j == X^(i*j) ... count unique i*j 28 | pars.insert(i*j); 29 | } 30 | num_pow.push_back(pars.size()); 31 | } 32 | ll ans = 0; 33 | for(int a=2;a<=n;a++){ 34 | if(base[a]){ 35 | ll k =a; 36 | int pw = 1; 37 | while(k <= n){ 38 | k *= a; 39 | pw++; 40 | }pw--; 41 | // there are pw powers of this number ("a"s with the same base) 42 | ans += num_pow[pw]; 43 | } 44 | } 45 | cout << ans < 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | int main() 9 | { 10 | int n; 11 | cin >> n; 12 | 13 | int powers[10]; 14 | 15 | for(int i=0; i<=9; i++) 16 | { 17 | powers[i] = pow(i, n); 18 | } 19 | 20 | int i = 10; 21 | int total = 0; 22 | 23 | for(int i = 10; i < pow(10, n+1); ++i) 24 | { 25 | string s = to_string(i); 26 | int sum = 0; 27 | 28 | for(int j=0; j 2 | using namespace std; 3 | 4 | const long long MOD = 1000000007; 5 | 6 | vector coins = { 1, 2, 5, 10, 20, 50, 100, 200 }; 7 | 8 | int main() 9 | { 10 | long long DP[100010] = {0}; 11 | DP[0] = 1; 12 | 13 | for(int i=0; i<8; i++) 14 | { 15 | for(int j = coins[i]; j <= 100000; j++) 16 | { 17 | DP[j] = (DP[j] + DP[j - coins[i]]) % MOD; 18 | } 19 | } 20 | 21 | int t; 22 | cin >> t; 23 | 24 | while(t--) 25 | { 26 | int n; 27 | cin >> n; 28 | 29 | cout << DP[n] % MOD << endl; 30 | } 31 | } -------------------------------------------------------------------------------- /Euler-032.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int n; 5 | int ans = 0; 6 | 7 | vector nums = {"1"}; 8 | 9 | 10 | void GetNumbers() 11 | { 12 | string num = "1"; 13 | 14 | while(num.length() <= n/2) 15 | { 16 | num.back()++; 17 | 18 | while(num.find(num.back()) != num.rfind(num.back())) 19 | { 20 | num.back()++; 21 | } 22 | 23 | int carry = (num.back()-'0') / (n+1); 24 | int index = num.size()-2; 25 | 26 | while(carry) 27 | { 28 | if(index < 0) 29 | { 30 | num = string("123456789").substr(0, num.size()+1); 31 | break; 32 | } 33 | num[index+1] = '1'; 34 | num[index]++; 35 | carry = (num[index]-'0') / (n+1); 36 | index--; 37 | } 38 | bool check[n+1] = {0}; 39 | 40 | for(int i=0; i> n; 55 | 56 | GetNumbers(); 57 | string compare = string("123456789").substr(0, n); 58 | 59 | for(int i=nums.size()-1; i >= n/2; i--) 60 | { 61 | int product = stoi(nums[i]); 62 | 63 | for(int j=0; nums[j].size() < nums[i].size() ; j++) 64 | { 65 | int factor = stoi(nums[j]); 66 | int quotient = product / factor; 67 | 68 | if(product % factor != 0) continue; 69 | 70 | string s = nums[i] + nums[j] + to_string(quotient); 71 | sort(s.begin(), s.end()); 72 | 73 | if(s == compare) 74 | { 75 | ans += product; 76 | break; 77 | } 78 | } 79 | } 80 | cout << ans << endl; 81 | return 0; 82 | } -------------------------------------------------------------------------------- /Euler-033.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int powers[6] = {1, 10, 100, 1000, 10000, 100000}; 5 | 6 | set found; 7 | 8 | int N, K; 9 | long n_sum = 0, d_sum = 0; 10 | 11 | void Generate(int num, int den, int size, double target) 12 | { 13 | if(size == N) 14 | { 15 | double ratio = (double)num / (double)den; 16 | 17 | if(ratio == target) 18 | { 19 | if(floor(log10(num)) != size-1 || floor(log10(den)) != size-1) return; 20 | 21 | unsigned long id = num * powers[size + 1] + den; 22 | 23 | if(found.count(id)) return; 24 | 25 | n_sum += num; 26 | d_sum += den; 27 | 28 | found.insert(id); 29 | } 30 | return; 31 | } 32 | for(int d=1; d<10; d++) 33 | { 34 | vector next_num, next_den; 35 | 36 | for(int i=0; i<=size; i++) 37 | { 38 | int num_l = floor(num / powers[i]); 39 | int num_r = num % powers[i]; 40 | int num_temp = num_l * 10 + d; 41 | num_temp = num_temp * powers[i] + num_r; 42 | 43 | int den_l = floor(den / powers[i]); 44 | int den_r = den % powers[i]; 45 | int den_temp = den_l * 10 + d; 46 | den_temp = den_temp * powers[i] + den_r; 47 | 48 | next_num.push_back(num_temp); 49 | next_den.push_back(den_temp); 50 | } 51 | for(auto n : next_num) 52 | { 53 | for(auto d : next_den) 54 | { 55 | Generate(n, d, size + 1, target); 56 | } 57 | } 58 | } 59 | } 60 | 61 | int main() 62 | { 63 | cin >> N >> K; 64 | 65 | int S = (N - K == 1) ? 0 : powers[(N - K) - 1]; 66 | int E = powers[N - K]; 67 | 68 | if(N == 4 && K == 2) S = 1; 69 | 70 | for(int num = S; num < E; num++) 71 | { 72 | for(int den = E - 1; den > num; den--) 73 | { 74 | double target = (double)num / (double)den; 75 | 76 | Generate(num, den, N - K, target); 77 | } 78 | } 79 | cout << n_sum << " " << d_sum; 80 | return 0; 81 | } -------------------------------------------------------------------------------- /Euler-034.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | 9 | int fact[10] = 10 | { 11 | 1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880 12 | }; 13 | 14 | 15 | int main() 16 | { 17 | int N; 18 | cin >> N; 19 | 20 | long long ans = 0; 21 | 22 | for(int i = N-1; i>=10; i--) 23 | { 24 | string num = to_string(i); 25 | 26 | int digitSum = 0; 27 | 28 | for(int j=0; j 2 | using namespace std; 3 | 4 | 5 | bool IsPrime(int n) 6 | { 7 | if(n % 2 == 0) return false; 8 | 9 | for(int i=3; i<=sqrt(n); i+=2) 10 | { 11 | if(n % i == 0) return false; 12 | } 13 | return true; 14 | } 15 | 16 | set primes; 17 | 18 | 19 | int main() 20 | { 21 | int N; 22 | cin >> N; 23 | 24 | long long int ans = 0; 25 | 26 | for(int i=N; i>=3; i--) 27 | { 28 | if(IsPrime(i)) primes.insert(to_string(i)); 29 | } 30 | 31 | set valid; 32 | 33 | 34 | for(auto it : primes) 35 | { 36 | if(valid.count(it) != 0) 37 | { 38 | ans += stoi(it); 39 | continue; 40 | } 41 | 42 | string num = it; 43 | vector rotations; 44 | 45 | for(int i=0; i 2 | using namespace std; 3 | 4 | set primes; 5 | 6 | void Sieve(int n) 7 | { 8 | vector sieve(1000010, false); 9 | int p = 2; 10 | sieve[2] = true; 11 | 12 | while(p <= n) 13 | { 14 | primes.insert(p); 15 | 16 | for(int i=2; p*i <= n; i++) sieve[p*i] = true; 17 | while(p < sieve.size() && sieve[p]) p++; 18 | sieve[p] = true; 19 | } 20 | } 21 | 22 | int main() 23 | { 24 | Sieve(1000000); 25 | 26 | int n; 27 | cin >> n; 28 | 29 | long ans = 0; 30 | 31 | for(auto p : primes) 32 | { 33 | if(p <= 7) continue; 34 | if(p >= n) break; 35 | 36 | string s = to_string(p); 37 | bool valid = true; 38 | 39 | 40 | for(int i=1; i 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int n, k; 7 | cin >> n >> k; 8 | 9 | set S; 10 | 11 | for(char i = 1; i<=k; i++) S.insert(i+'0'); 12 | 13 | for(int i=k; i temp = S; 16 | 17 | for(int j=1; j<=5; j++) 18 | { 19 | int num = i*j; 20 | string s = to_string(num); 21 | 22 | for(auto c : s) 23 | { 24 | if(temp.count(c) == 0) 25 | { 26 | goto next; 27 | } 28 | temp.erase(c); 29 | } 30 | if(temp.empty()) 31 | { 32 | cout << i << endl; 33 | break; 34 | } 35 | } 36 | next: 37 | continue; 38 | } 39 | return 0; 40 | } -------------------------------------------------------------------------------- /Euler-039.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int M[5000001]; 5 | vector C; 6 | vector ans; 7 | 8 | int MAX = 5000000; 9 | 10 | void GenerateTriples(int a, int b, int c) 11 | { 12 | int triangle = a + b + c; 13 | 14 | if(triangle > MAX) return; 15 | 16 | for(int k=2; triangle <= MAX; k++) 17 | { 18 | C[triangle]++; 19 | 20 | if(M[C[triangle]]) 21 | { 22 | M[C[triangle]] = min((int)triangle, M[C[triangle]]); 23 | ans[triangle] = min(ans[triangle], M[C[triangle]]); 24 | } 25 | else 26 | { 27 | M[C[triangle]] = triangle; 28 | ans[triangle] = triangle; 29 | } 30 | triangle = (a * k) + (b * k) + (c * k); 31 | } 32 | 33 | for(int i=0; i<3; i++) 34 | { 35 | int next_a, next_b, next_c; 36 | 37 | switch(i) 38 | { 39 | case 0: 40 | { 41 | next_a = -a + (2 * b) + (2 * c); 42 | next_b = -(2 * a) + b + (2 * c); 43 | next_c = -(2 * a) + (2 * b) + (3 * c); 44 | GenerateTriples(next_a, next_b, next_c); 45 | break; 46 | } 47 | case 1: 48 | { 49 | next_a = a + (2 * b) + (2 * c); 50 | next_b = (2 * a) + b + (2 * c); 51 | next_c = (2 * a) + (2 * b) + (3 * c); 52 | GenerateTriples(next_a, next_b, next_c); 53 | break; 54 | } 55 | case 2: 56 | { 57 | next_a = a - (2 * b) + (2 * c); 58 | next_b = (2 * a) - b + (2 * c); 59 | next_c = (2 * a) - (2 * b) + (3 * c); 60 | GenerateTriples(next_a, next_b, next_c); 61 | break; 62 | } 63 | } 64 | } 65 | } 66 | 67 | 68 | int main() 69 | { 70 | ios_base::sync_with_stdio(0); 71 | cin.tie(0); 72 | 73 | int t; 74 | cin >> t; 75 | 76 | C = vector(MAX + 1, 0); 77 | ans = vector(MAX + 1); 78 | 79 | for(int i=0; i<=MAX; i++) ans[i] = i; 80 | 81 | GenerateTriples(3, 4, 5); 82 | 83 | int maxCount = 1; 84 | int index = 12; 85 | 86 | for(int i=12; i <= MAX; i++) 87 | { 88 | if(C[i] > maxCount) 89 | { 90 | maxCount = C[i]; 91 | index = i; 92 | } 93 | ans[i] = index; 94 | } 95 | 96 | while(t--) 97 | { 98 | int n; 99 | cin >> n; 100 | 101 | cout << ans[n] << endl; 102 | } 103 | return 0; 104 | } -------------------------------------------------------------------------------- /Euler-040.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | typedef unsigned long long ULL; 5 | 6 | ULL FindDigit(ULL target) 7 | { 8 | if(target < 10) return target; 9 | 10 | ULL pos, power, number, digits, next; 11 | 12 | pos = power = number = 10; 13 | next = 90; 14 | digits = 2; 15 | 16 | while(pos + (next * digits) <= target) 17 | { 18 | pos += (next * digits); 19 | next *= 10; 20 | power *= 10; 21 | number = power; 22 | digits++; 23 | } 24 | 25 | while(pos != target) 26 | { 27 | next = pos + (digits * power); 28 | 29 | if(next == target) 30 | { 31 | number += power; 32 | pos = next; 33 | break; 34 | } 35 | if(next >= target) 36 | { 37 | if(power == 10 && next >= target - 10) 38 | { 39 | break; 40 | } 41 | power /= 10; 42 | continue; 43 | } 44 | number += power; 45 | pos = next; 46 | } 47 | string s = to_string(number); 48 | 49 | int index = 0; 50 | 51 | for(; pos < target; pos++) 52 | { 53 | index++; 54 | 55 | if(index == s.size()) 56 | { 57 | number++; 58 | s = to_string(number); 59 | index = 0; 60 | } 61 | } 62 | return s[index]-'0'; 63 | } 64 | 65 | int main() 66 | { 67 | ios_base::sync_with_stdio(0); 68 | cin.tie(0); 69 | 70 | int t; 71 | cin >> t; 72 | 73 | while(t--) 74 | { 75 | ULL ans = 1; 76 | ULL n, digit; 77 | 78 | for(int i=0; i<7; i++) 79 | { 80 | cin >> n; 81 | 82 | digit = FindDigit(n); 83 | ans *= digit; 84 | } 85 | cout << ans << "\n"; 86 | } 87 | } --------------------------------------------------------------------------------