#include <stdio.h> #include <stdlib.h> int ok(int* p, int k, long long n, int has2, int has3, int has5) { int i; if ((!has2 && n%2==0) || (!has3 && n%3==0) || (!has5 && n%5==0)) return 0==1; for (i=0;i<k;i++) while (n % p[i]==0) n /= p[i]; return n==1; } int has(int* p, int k, int d) { int i; for (i=0;i<k;i++) if (p[i]==d) return 1==1; return 1==0; } int mklog(long long n, int p) { int s = 1; while (n>0) { n /= p; s++; } return s; } unsigned long long go_long(unsigned long long v, unsigned long long n, int* p, int k, int ix) { if (k==ix) return v; unsigned long long m = v; while (v<=n) { unsigned long long mm = go_long(v, n, p, k, ix+1); if (mm<=n && mm>m) m = mm; v *= p[ix]; } return m; } int main() { int k, i, j; long long n; long long pp; int p[100]; int mp[100]; scanf("%d%lld", &k, &n); for (i=0;i<k;i++) scanf("%d", &p[i]); for (i=0;i<k;i++) mp[i] = mklog(n, p[i]); pp = 10000000; for (i=0;i<k;i++) pp /= mp[i]; if (pp>0 || k<15) { unsigned long long vv = go_long(1, n, p, k, 0); printf("%llu\n", vv); } else { int has2 = has(p, k, 2); int has3 = has(p, k, 3); int has5 = has(p, k, 5); //unsigned long long vv = go_long(1, n, p, k, 0); //printf("%llu\n", vv); for (;n>=1;n--) { if (ok(p, k, n, has2, has3, has5)) { printf("%lld\n", n); return 0; } } printf("1\n"); } 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 | #include <stdio.h> #include <stdlib.h> int ok(int* p, int k, long long n, int has2, int has3, int has5) { int i; if ((!has2 && n%2==0) || (!has3 && n%3==0) || (!has5 && n%5==0)) return 0==1; for (i=0;i<k;i++) while (n % p[i]==0) n /= p[i]; return n==1; } int has(int* p, int k, int d) { int i; for (i=0;i<k;i++) if (p[i]==d) return 1==1; return 1==0; } int mklog(long long n, int p) { int s = 1; while (n>0) { n /= p; s++; } return s; } unsigned long long go_long(unsigned long long v, unsigned long long n, int* p, int k, int ix) { if (k==ix) return v; unsigned long long m = v; while (v<=n) { unsigned long long mm = go_long(v, n, p, k, ix+1); if (mm<=n && mm>m) m = mm; v *= p[ix]; } return m; } int main() { int k, i, j; long long n; long long pp; int p[100]; int mp[100]; scanf("%d%lld", &k, &n); for (i=0;i<k;i++) scanf("%d", &p[i]); for (i=0;i<k;i++) mp[i] = mklog(n, p[i]); pp = 10000000; for (i=0;i<k;i++) pp /= mp[i]; if (pp>0 || k<15) { unsigned long long vv = go_long(1, n, p, k, 0); printf("%llu\n", vv); } else { int has2 = has(p, k, 2); int has3 = has(p, k, 3); int has5 = has(p, k, 5); //unsigned long long vv = go_long(1, n, p, k, 0); //printf("%llu\n", vv); for (;n>=1;n--) { if (ok(p, k, n, has2, has3, has5)) { printf("%lld\n", n); return 0; } } printf("1\n"); } return 0; } |