#include <bits/stdc++.h> using namespace std; long long t[5000005]; bool odw[5000005]; long long t2[5000005]; long long n,k,z = 100; long long dod = 1,skok = 2; long long skok2_start = 3, skok2; long long indeks,wynik = INT_MAX; void preprocessing() { t[1] = 1; for(int i = 2, skok = 2; i <= (n * (n + 1)) / 2; i += skok, skok++) t[i] = skok, odw[i] = true; for(int i = 3, skok = 3; i <= (n * (n + 1)) / 2; i += skok, skok++) t[i] = skok - 1; for(int i = 2; i <= (n * (n + 1)) / 2; i++) { if(odw[i]) { int poz = i; skok2 = skok2_start; for(int j = i + skok2; j <= (n * (n + 1)) / 2; skok2++, j += skok2) { if(!t[j]) t[j] = t[j - skok2] + t[poz]; } skok2_start++; } } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> k; preprocessing(); for(int i = 1; i <= (n * (n + 1)) / 2; i++) { cin >> t2[i]; if(t[i] <= k) { if(t2[i] <= wynik) { wynik = t2[i]; indeks = i; } } } cout << t2[indeks] << endl; }
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 | #include <bits/stdc++.h> using namespace std; long long t[5000005]; bool odw[5000005]; long long t2[5000005]; long long n,k,z = 100; long long dod = 1,skok = 2; long long skok2_start = 3, skok2; long long indeks,wynik = INT_MAX; void preprocessing() { t[1] = 1; for(int i = 2, skok = 2; i <= (n * (n + 1)) / 2; i += skok, skok++) t[i] = skok, odw[i] = true; for(int i = 3, skok = 3; i <= (n * (n + 1)) / 2; i += skok, skok++) t[i] = skok - 1; for(int i = 2; i <= (n * (n + 1)) / 2; i++) { if(odw[i]) { int poz = i; skok2 = skok2_start; for(int j = i + skok2; j <= (n * (n + 1)) / 2; skok2++, j += skok2) { if(!t[j]) t[j] = t[j - skok2] + t[poz]; } skok2_start++; } } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> k; preprocessing(); for(int i = 1; i <= (n * (n + 1)) / 2; i++) { cin >> t2[i]; if(t[i] <= k) { if(t2[i] <= wynik) { wynik = t2[i]; indeks = i; } } } cout << t2[indeks] << endl; } |