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
| #include "iostream" #include "algorithm" #include "cstring" #include "cstdio" #include "cmath" #include "vector" #include "map" #include "set" #include "queue" using namespace std; #define MAXN 1000006
#define rep(i, a, b) for (int i = (a), i##end = (b); i <= i##end; ++i) #define per(i, a, b) for (int i = (a), i##end = (b); i >= i##end; --i) #define pii pair<int,int> #define fi first #define se second #define mp make_pair #define pb push_back #define eb emplace_back #define vi vector<int> #define all(x) (x).begin() , (x).end() #define mem( a ) memset( a , 0 , sizeof a ) #define min( a , b ) ( (a) < (b) ? (a) : (b) ) #define max( a , b ) ( (a) > (b) ? (a) : (b) ) typedef long long ll; int n , m; long long k , G; long long A[MAXN]; int E[MAXN]; vector<ll> fac;
inline long long gcd( ll a , ll b ) { return b ? gcd( b , a % b ) : a; }
map<ll,vi> M; int ok[MAXN];
inline void FWTand( int* A , int len ) { for( int mid = 2 ; mid <= len ; mid <<= 1 ) for( int i = 0 ; i < len ; i += mid ) for( int j = i ; j < i + ( mid >> 1 ) ; ++ j ) A[j] += A[j + (mid >> 1)]; }
ll dp[12][( 1 << 11 ) + 6];
void solve() { cin >> n >> k; rep( i , 1 , n ) scanf("%lld",A + i) , G = !G ? A[i] : gcd( G , A[i] ); rep( i , 1 , n ) scanf("%d",E + i); ll c = G; if( G == 1 ) return void( puts("0") ); for( ll i = 2 ; i * i <= G ; ++ i ) if( c % i == 0 ) { while( c % i == 0 ) c /= i; fac.pb( i ); } if( c != 1 ) fac.pb( c ); m = fac.size(); rep( i , 1 , n ) { ll t = 1 , a = A[i]; rep( j , 0 , m - 1 ) while( a % fac[j] == 0 ) t *= fac[j] , a /= fac[j]; M[t].pb( E[i] ); } vi us; memset( dp , 0x3f , sizeof dp ); dp[0][0] = 0; for( auto& t : M ) { sort( t.se.begin() , t.se.end() ); long long x , y; rep( i , 1 , ( 1 << m ) - 1 ) { x = t.fi , y = 1; rep( j , 0 , m - 1 ) if( i & ( 1 << j ) ) while( x % fac[j] == 0 ) x /= fac[j] , y *= fac[j]; if( y <= k ) ok[i] = 1; } FWTand( ok , ( 1 << m ) ); us.clear(); rep( i , 0 , ( 1 << m ) - 1 ) { if( ok[i] == 1 ) us.pb( i ); ok[i] = 0; } for( auto& v : t.se ) { bool flg = 0; per( i , ( 1 << m ) - 1 , 0 ) per( j , m - 1 , 0 ) for( auto& q : us ) if( dp[j + 1][i | q] > dp[j][i] + v ) dp[j + 1][i | q] = dp[j][i] + v , flg = 1; if( !flg ) break; } } long long res = 0x3f3f3f3f3f3f3f3f; rep( i , 1 , m ) if( dp[i][( 1 << m ) - 1] < 0x3f3f3f3f3f3f3f3f ) res = min( res , i * dp[i][( 1 << m ) - 1] ); printf("%lld\n",res == 0x3f3f3f3f3f3f3f3f ? -1 : res); }
signed main() {
solve(); }
|