「CF529E」The Art of Dealing with ATM
给定n种面额,每次ATM机能吐k张纸钞。(且每次ATM机只能吐至多2种面额)
暴力
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 |
#include<cmath> #include<queue> #include<vector> #include<cstdio> #include<cstring> #include<cstdlib> #include<iostream> #include<algorithm> #define pa pair<int,int> #define inf 1000000000 #define eps 1e-8 #define ll long long using namespace std; ll read() { ll x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f; } int n,K,Q; int v[5005]; bool vis[10000005]; int cal(int x,int now) { for(int i=1;i<=K-now;i++) if(x%i==0&&x/i<=10000000) if(vis[x/i])return now+i; return K+1; } int main() { n=read();K=read(); for(int i=1;i<=n;i++) v[i]=read(),vis[v[i]]=1; Q=read(); while(Q--) { int x=read(),ans=K+1; for(int i=1;i<=n;i++) for(int j=1;j<=K&&v[i]*j<=x;j++) { if(v[i]*j==x){ans=min(ans,j);break;} ans=min(ans,cal(x-v[i]*j,j)); } if(ans>K)puts("-1"); else printf("%d\n",ans); } return 0; } |
Subscribe