#include <iostream> #include <vector> #include "dzilib.h" #include <algorithm> #include <cmath> using namespace std; using u64 = uint64_t; using u128 = uint64_t; //using u128 = __uint128_t; u64 binpower(u64 base, u64 e, u64 mod) { u64 result = 1; base %= mod; while (e) { if (e & 1) result = (u128)result * base % mod; base = (u128)base * base % mod; e >>= 1; } return result; } bool check_composite(u64 n, u64 a, u64 d, int s) { u64 x = binpower(a, d, n); if (x == 1 || x == n - 1) return false; for (int r = 1; r < s; r++) { x = (u128)x * x % n; if (x == n - 1) return false; } return true; }; bool test_prime(u64 n) { // returns true if n is prime, else returns false. if (n < 2) return false; int r = 0; u64 d = n - 1; while ((d & 1) == 0) { d >>= 1; r++; } for (int a : {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37}) { if (n == a) return true; if (check_composite(n, a, d, r)) return false; } return true; } bool isPerfectSquare(long double x) { // Find floating point value of // square root of x. if (x >= 0) { long long sr = sqrt(x); // if product of square root //is equal, then // return T/F return (sr * sr == x); } // else return false if n<0 return false; } int tau(long long n, vector<int> &primes) { int ans = 1; for (int i = 0; i < primes.size(); i++) { if (primes[i] * primes[i] * primes[i] > n) break; int count = 1; while (n % primes[i] == 0) { n /= primes[i]; count++; } ans *= count; } if (test_prime(n)) { ans *= 2; } else if (isPerfectSquare(n) && test_prime(sqrt(n))) { ans *= 3; } else if (n != 1) { ans *= 4; } return ans; } int main() { int primes_number = 1000050; vector<bool> is_prime(primes_number + 1, true); is_prime[0] = is_prime[1] = false; for (int i = 2; i * i <= primes_number; i++) { if (is_prime[i]) { for (int j = i * i; j <= primes_number; j += i) is_prime[j] = false; } } vector<int> primes; for (int i = 0; i < is_prime.size(); i++) { if (is_prime[i]) { primes.push_back(i); } } int t = GetT(); int q = GetQ(); long long c = GetC(); long long n = GetN(); vector<int> taus; for (int i = 0; i <= 1000200; i++) { //cout << i << ": " << tau(i, primes) << endl; taus.push_back(tau(i, primes)); } // &T, &N, &Q, &C while (t--) { vector<int> observed_taus; for (int i = 0; i < 100; i++) { observed_taus.push_back(Ask(i)); } auto it = search(begin(taus), end(taus), begin(observed_taus), end(observed_taus)); if (it != end(taus)) { //cout << "found at offset " << distance(taus.begin(), it) << endl; Answer(distance(taus.begin(), it)); } else { //cout << "not found" << endl; Answer(1000); } } return 0; }
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 158 159 160 161 162 163 | #include <iostream> #include <vector> #include "dzilib.h" #include <algorithm> #include <cmath> using namespace std; using u64 = uint64_t; using u128 = uint64_t; //using u128 = __uint128_t; u64 binpower(u64 base, u64 e, u64 mod) { u64 result = 1; base %= mod; while (e) { if (e & 1) result = (u128)result * base % mod; base = (u128)base * base % mod; e >>= 1; } return result; } bool check_composite(u64 n, u64 a, u64 d, int s) { u64 x = binpower(a, d, n); if (x == 1 || x == n - 1) return false; for (int r = 1; r < s; r++) { x = (u128)x * x % n; if (x == n - 1) return false; } return true; }; bool test_prime(u64 n) { // returns true if n is prime, else returns false. if (n < 2) return false; int r = 0; u64 d = n - 1; while ((d & 1) == 0) { d >>= 1; r++; } for (int a : {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37}) { if (n == a) return true; if (check_composite(n, a, d, r)) return false; } return true; } bool isPerfectSquare(long double x) { // Find floating point value of // square root of x. if (x >= 0) { long long sr = sqrt(x); // if product of square root //is equal, then // return T/F return (sr * sr == x); } // else return false if n<0 return false; } int tau(long long n, vector<int> &primes) { int ans = 1; for (int i = 0; i < primes.size(); i++) { if (primes[i] * primes[i] * primes[i] > n) break; int count = 1; while (n % primes[i] == 0) { n /= primes[i]; count++; } ans *= count; } if (test_prime(n)) { ans *= 2; } else if (isPerfectSquare(n) && test_prime(sqrt(n))) { ans *= 3; } else if (n != 1) { ans *= 4; } return ans; } int main() { int primes_number = 1000050; vector<bool> is_prime(primes_number + 1, true); is_prime[0] = is_prime[1] = false; for (int i = 2; i * i <= primes_number; i++) { if (is_prime[i]) { for (int j = i * i; j <= primes_number; j += i) is_prime[j] = false; } } vector<int> primes; for (int i = 0; i < is_prime.size(); i++) { if (is_prime[i]) { primes.push_back(i); } } int t = GetT(); int q = GetQ(); long long c = GetC(); long long n = GetN(); vector<int> taus; for (int i = 0; i <= 1000200; i++) { //cout << i << ": " << tau(i, primes) << endl; taus.push_back(tau(i, primes)); } // &T, &N, &Q, &C while (t--) { vector<int> observed_taus; for (int i = 0; i < 100; i++) { observed_taus.push_back(Ask(i)); } auto it = search(begin(taus), end(taus), begin(observed_taus), end(observed_taus)); if (it != end(taus)) { //cout << "found at offset " << distance(taus.begin(), it) << endl; Answer(distance(taus.begin(), it)); } else { //cout << "not found" << endl; Answer(1000); } } return 0; } |