結果

問題 No.288 貯金箱の仕事
ユーザー LayCurse
提出日時 2015-10-09 22:41:27
言語 C++11
(gcc 4.8.5)
結果
AC  
実行時間 237 ms
コード長 3,297 Byte
コンパイル時間 1,046 ms
使用メモリ 3,424 KB
最終ジャッジ日時 2019-10-25 16:26:52

テストケース

テストケース表示
入力 結果 実行時間
使用メモリ
00-sample-01.txt AC 9 ms
3,404 KB
00-sample-02.txt AC 7 ms
3,404 KB
00-sample-03.txt AC 8 ms
3,404 KB
01-small-01.txt AC 7 ms
3,404 KB
01-small-02.txt AC 7 ms
3,408 KB
01-small-03.txt AC 10 ms
3,404 KB
01-small-04.txt AC 6 ms
3,404 KB
01-small-05.txt AC 29 ms
3,416 KB
01-small-06.txt AC 13 ms
3,408 KB
01-small-07.txt AC 24 ms
3,420 KB
01-small-08.txt AC 25 ms
3,416 KB
01-small-09.txt AC 14 ms
3,420 KB
01-small-10.txt AC 23 ms
3,420 KB
01-small-11.txt AC 20 ms
3,416 KB
01-small-12.txt AC 22 ms
3,416 KB
01-small-13.txt AC 27 ms
3,420 KB
01-small-14.txt AC 23 ms
3,420 KB
01-small-15.txt AC 19 ms
3,420 KB
01-small-16.txt AC 16 ms
3,424 KB
01-small-17.txt AC 18 ms
3,420 KB
01-small-18.txt AC 13 ms
3,404 KB
01-small-19.txt AC 14 ms
3,416 KB
01-small-20.txt AC 23 ms
3,416 KB
02-large-01.txt AC 237 ms
3,416 KB
02-large-02.txt AC 119 ms
3,420 KB
02-large-03.txt AC 6 ms
3,404 KB
02-large-04.txt AC 6 ms
3,404 KB
02-large-05.txt AC 7 ms
3,408 KB
02-large-06.txt AC 7 ms
3,408 KB
02-large-07.txt AC 7 ms
3,404 KB
02-large-08.txt AC 63 ms
3,416 KB
02-large-09.txt AC 63 ms
3,420 KB
02-large-10.txt AC 120 ms
3,416 KB
02-large-11.txt AC 120 ms
3,420 KB
02-large-12.txt AC 177 ms
3,420 KB
02-large-13.txt AC 177 ms
3,420 KB
02-large-14.txt AC 236 ms
3,416 KB
02-large-15.txt AC 70 ms
3,416 KB
02-large-16.txt AC 164 ms
3,420 KB
02-large-17.txt AC 81 ms
3,416 KB
02-large-18.txt AC 84 ms
3,416 KB
02-large-19.txt AC 226 ms
3,416 KB
02-large-20.txt AC 197 ms
3,420 KB
02-large-21.txt AC 139 ms
3,420 KB
02-large-22.txt AC 99 ms
3,416 KB
02-large-23.txt AC 184 ms
3,420 KB
02-large-24.txt AC 202 ms
3,416 KB
02-large-25.txt AC 134 ms
3,416 KB
02-large-26.txt AC 145 ms
3,416 KB
02-large-27.txt AC 89 ms
3,416 KB
02-large-28.txt AC 115 ms
3,420 KB
02-large-29.txt AC 84 ms
3,420 KB
02-large-30.txt AC 118 ms
3,420 KB
02-large-31.txt AC 153 ms
3,420 KB
02-large-32.txt AC 107 ms
3,416 KB
challenge01.txt AC 7 ms
3,400 KB
テストケース一括ダウンロード

ソースコード

diff #
#include<bits/stdc++.h>
using namespace std;

#define REP(i,a,b) for(i=a;i<b;i++)
#define rep(i,n) REP(i,0,n)

#define mygc(c) (c)=getchar_unlocked()
#define mypc(c) putchar_unlocked(c)

#define ll long long
#define ull unsigned ll

void reader(int *x){int k,m=0;*x=0;for(;;){mygc(k);if(k=='-'){m=1;break;}if('0'<=k&&k<='9'){*x=k-'0';break;}}for(;;){mygc(k);if(k<'0'||k>'9')break;*x=(*x)*10+k-'0';}if(m)(*x)=-(*x);}
void reader(ll *x){int k,m=0;*x=0;for(;;){mygc(k);if(k=='-'){m=1;break;}if('0'<=k&&k<='9'){*x=k-'0';break;}}for(;;){mygc(k);if(k<'0'||k>'9')break;*x=(*x)*10+k-'0';}if(m)(*x)=-(*x);}
void reader(double *x){scanf("%lf",x);}
int reader(char c[]){int i,s=0;for(;;){mygc(i);if(i!=' '&&i!='\n'&&i!='\r'&&i!='\t'&&i!=EOF) break;}c[s++]=i;for(;;){mygc(i);if(i==' '||i=='\n'||i=='\r'||i=='\t'||i==EOF) break;c[s++]=i;}c[s]='\0';return s;}
template <class T, class S> void reader(T *x, S *y){reader(x);reader(y);}
template <class T, class S, class U> void reader(T *x, S *y, U *z){reader(x);reader(y);reader(z);}
template <class T, class S, class U, class V> void reader(T *x, S *y, U *z, V *w){reader(x);reader(y);reader(z);reader(w);}

void writer(int x, char c){int s=0,m=0;char f[10];if(x<0)m=1,x=-x;while(x)f[s++]=x%10,x/=10;if(!s)f[s++]=0;if(m)mypc('-');while(s--)mypc(f[s]+'0');mypc(c);}
void writer(ll x, char c){int s=0,m=0;char f[20];if(x<0)m=1,x=-x;while(x)f[s++]=x%10,x/=10;if(!s)f[s++]=0;if(m)mypc('-');while(s--)mypc(f[s]+'0');mypc(c);}
void writer(double x, char c){printf("%.15f",x);mypc(c);}
void writer(const char c[]){int i;for(i=0;c[i]!='\0';i++)mypc(c[i]);}
void writer(const char x[], char c){int i;for(i=0;x[i]!='\0';i++)mypc(x[i]);mypc(c);}
template<class T> void writerLn(T x){writer(x,'\n');}
template<class T, class S> void writerLn(T x, S y){writer(x,' ');writer(y,'\n');}
template<class T, class S, class U> void writerLn(T x, S y, U z){writer(x,' ');writer(y,' ');writer(z,'\n');}
template<class T> void writerArr(T x[], int n){int i;if(!n){mypc('\n');return;}rep(i,n-1)writer(x[i],' ');writer(x[n-1],'\n');}

char memarr[17000000]; void *mem = memarr;
#define MD 1000000007

template<class T> inline T min(T a, T b, T c){return min(min(a,b),c);}
template<class T> inline T min(T a, T b, T c, T d){return min(min(a,b),min(c,d));}
template<class T> inline T max(T a, T b, T c){return max(max(a,b),c);}
template<class T> inline T max(T a, T b, T c, T d){return max(max(a,b),max(c,d));}

template<class S, class T> inline void chmin(S &a, T b){if(a>b)a=b;}
template<class S, class T> inline void chmax(S &a, T b){if(a<b)a=b;}

template<class T> T GCD(T a,T b){T r; while(a)r=b,b=a,a=r%a; return b;}

int N; ll M;
int A[500]; ll K[500], sum;

int dp[500000];

int main(){
  int i, j, k;
  ll g, r;

  reader(&N,&M);
  rep(i,N) reader(A+i);
  rep(i,N) reader(K+i);

  sort(A, A+N);

  g = A[0];
  REP(i,1,N) g = GCD(g, (ll)A[i]);

  if(M%g){ writerLn(-1); return 0; }

  M /= g;
  rep(i,N) A[i] /= g;

  rep(i,500000) dp[i] = MD;
  dp[0] = 0;

  rep(k,N){
    REP(i,A[k],500000) chmin(dp[i], dp[i-A[k]]+1);
  }

  sum = 0;
  rep(i,N) sum += A[i] * K[i];
  if(sum < M){ writerLn(-1); return 0; }

  sum -= M;

  r = max((sum - 490000) / A[N-1], 0LL);
  if(dp[sum - A[N-1]*r] >= MD){ writerLn(-1); return 0; }
  r += dp[sum - A[N-1]*r];

  writerLn(r);

  return 0;
}
0