#include<iostream> #include<vector> #include<set> #include<map> #include<algorithm> using namespace std; #define ll unsigned long long //unsigned unsigned long long #define pb push_back #define FOR(i, a, b) for(int i =(a); i <=(b); ++i) #define REP(i, n) for(int i = 0;i <(n); ++i) ll p[100]; ll T[100]; ll T2[100]; int N[100]; ll wynik = 0; ll min_wynik; //set<unsigned long long> zbior; vector<unsigned long long>zbior; void generuj( int k, ll n){ REP(i,k)T[i]=1; REP(i,k)N[i]=0; int level = k-1; unsigned long long suma = 1; zbior.pb(n+1); while( suma>0 ){ if( level >=0 && level < k && suma<= n/p[level] ){ suma*=(unsigned long long)p[level]; zbior.pb(suma); //cout<<suma<<"\n"; N[level]++; } else{ level--; } if(level <0 ){ level++; while(N[level]==0 && level<k)level++; if(level>=k)break; if(N[level] >= 0){ suma/=(unsigned long long)p[level]; N[level]--; } level--; } } sort(zbior.begin(),zbior.end()); // cout<<zbior.size()<<"\n"; } void solve(int k, ll n){ // k = 15; sort(p,p+k); min_wynik = n+1; unsigned long long wynik_t = 1; bool ok = false; if ( k > 14){ generuj(5,n); REP(i,k)p[i]=p[i+5]; k-=5; //cout<<"n:"<<n<<"\n"; //set<unsigned long long>::iterator it = zbior.lower_bound(n); vector<unsigned long long>::iterator it = std::lower_bound (zbior.begin(), zbior.end(), n); if (it != zbior.begin()) it--; unsigned long long ss = *it; // cout<<ss<<"\n"; if((unsigned long long)ss <= (unsigned long long)n){ min_wynik = (unsigned long long)n-(unsigned long long)ss; wynik_t = ss; // cout<<wynik_t<<"\n"; } ok = true; } REP(i,k)T[i]=1; REP(i,k)N[i]=0; int level = k-1; ll suma = 1; int u; while( suma>0 ){ // cout<<suma<<" "; if( level >=0 && level < k && suma<= n/p[level] ){ suma*=p[level]; N[level]++; } else{ level--; } if(n-suma < min_wynik){ min_wynik = n-suma; wynik = suma; REP(i,k)T2[i]=N[i]; wynik_t = 1; } if ( ok ){ ll g = (ll)n/(ll)suma; // set<unsigned long long>::iterator it = zbior.lower_bound(g); // if (it != zbior.begin()) it--; // unsigned long long t = *zbior.lower_bound(g); //set<unsigned long long>::iterator it = zbior.lower_bound(g); vector<unsigned long long>::iterator it = std::lower_bound (zbior.begin(), zbior.end(), g); unsigned long long t = *it; // if(n-suma*t >=0 && n-suma*t < min_wynik){ if(n >=suma*t && (n-min_wynik)< suma*t){ min_wynik = n-suma*t; wynik = suma*t; wynik_t = t; REP(i,k)T2[i]=N[i]; }else{ if (it != zbior.begin()) it--; t = *it; if(t != 0 ){ if(n >=suma*t && (n-min_wynik)< suma*t){ min_wynik = n-suma*t; wynik = suma*t; wynik_t = t; REP(i,k)T2[i]=N[i]; } } } } if(level <0 ){ level++; while(N[level]==0 && level<k)level++; if(level>=k)break; if(N[level] >= 0){ suma/=p[level]; N[level]--; } level--; } } // cout<<min_wynik<<"\n"; unsigned long long wynik2=1; REP(i,k) REP(j,T2[i])wynik2*=(unsigned long long)p[i]; cout<<wynik2*(unsigned long long)wynik_t<<"\n"; // cout<<999724108260935936ll; } int main(){ ios_base::sync_with_stdio(0); int k; ll n; cin>>k>>n; REP(i,k)cin>>p[i]; solve(k,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 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 | #include<iostream> #include<vector> #include<set> #include<map> #include<algorithm> using namespace std; #define ll unsigned long long //unsigned unsigned long long #define pb push_back #define FOR(i, a, b) for(int i =(a); i <=(b); ++i) #define REP(i, n) for(int i = 0;i <(n); ++i) ll p[100]; ll T[100]; ll T2[100]; int N[100]; ll wynik = 0; ll min_wynik; //set<unsigned long long> zbior; vector<unsigned long long>zbior; void generuj( int k, ll n){ REP(i,k)T[i]=1; REP(i,k)N[i]=0; int level = k-1; unsigned long long suma = 1; zbior.pb(n+1); while( suma>0 ){ if( level >=0 && level < k && suma<= n/p[level] ){ suma*=(unsigned long long)p[level]; zbior.pb(suma); //cout<<suma<<"\n"; N[level]++; } else{ level--; } if(level <0 ){ level++; while(N[level]==0 && level<k)level++; if(level>=k)break; if(N[level] >= 0){ suma/=(unsigned long long)p[level]; N[level]--; } level--; } } sort(zbior.begin(),zbior.end()); // cout<<zbior.size()<<"\n"; } void solve(int k, ll n){ // k = 15; sort(p,p+k); min_wynik = n+1; unsigned long long wynik_t = 1; bool ok = false; if ( k > 14){ generuj(5,n); REP(i,k)p[i]=p[i+5]; k-=5; //cout<<"n:"<<n<<"\n"; //set<unsigned long long>::iterator it = zbior.lower_bound(n); vector<unsigned long long>::iterator it = std::lower_bound (zbior.begin(), zbior.end(), n); if (it != zbior.begin()) it--; unsigned long long ss = *it; // cout<<ss<<"\n"; if((unsigned long long)ss <= (unsigned long long)n){ min_wynik = (unsigned long long)n-(unsigned long long)ss; wynik_t = ss; // cout<<wynik_t<<"\n"; } ok = true; } REP(i,k)T[i]=1; REP(i,k)N[i]=0; int level = k-1; ll suma = 1; int u; while( suma>0 ){ // cout<<suma<<" "; if( level >=0 && level < k && suma<= n/p[level] ){ suma*=p[level]; N[level]++; } else{ level--; } if(n-suma < min_wynik){ min_wynik = n-suma; wynik = suma; REP(i,k)T2[i]=N[i]; wynik_t = 1; } if ( ok ){ ll g = (ll)n/(ll)suma; // set<unsigned long long>::iterator it = zbior.lower_bound(g); // if (it != zbior.begin()) it--; // unsigned long long t = *zbior.lower_bound(g); //set<unsigned long long>::iterator it = zbior.lower_bound(g); vector<unsigned long long>::iterator it = std::lower_bound (zbior.begin(), zbior.end(), g); unsigned long long t = *it; // if(n-suma*t >=0 && n-suma*t < min_wynik){ if(n >=suma*t && (n-min_wynik)< suma*t){ min_wynik = n-suma*t; wynik = suma*t; wynik_t = t; REP(i,k)T2[i]=N[i]; }else{ if (it != zbior.begin()) it--; t = *it; if(t != 0 ){ if(n >=suma*t && (n-min_wynik)< suma*t){ min_wynik = n-suma*t; wynik = suma*t; wynik_t = t; REP(i,k)T2[i]=N[i]; } } } } if(level <0 ){ level++; while(N[level]==0 && level<k)level++; if(level>=k)break; if(N[level] >= 0){ suma/=p[level]; N[level]--; } level--; } } // cout<<min_wynik<<"\n"; unsigned long long wynik2=1; REP(i,k) REP(j,T2[i])wynik2*=(unsigned long long)p[i]; cout<<wynik2*(unsigned long long)wynik_t<<"\n"; // cout<<999724108260935936ll; } int main(){ ios_base::sync_with_stdio(0); int k; ll n; cin>>k>>n; REP(i,k)cin>>p[i]; solve(k,n); return 0; } |