7226: Memory Banks
时间限制: 1 Sec 内存限制: 256 MB
提交: 224 解决: 47
[提交] [状态] [讨论版] [命题人:admin]
题目描述
We have purchased 60 different types of memory banks, typed as 0 to 59, A bank of type i has 2i memory capacity. We have Xi memory banks of type i.
We also have n workstations numbered from 1 to n. The ith workstation needs exactly (no more and no less) Wi memory capacity to work.
Each workstation can use unlimited amount of memory banks, Total memory capacity of this workstation is the sum of capacity of all memory banks it used.
We need to make all workstations work and calculate the total capacity of unused memory banks, can you help us?
输入
Input is given from Standard Input in the following format:
X0 ... X59
n
W1 ... Wn
Constraints
0 ≤ Xi ≤ 260
1 ≤ n ≤ 100000
0 ≤ Wi ≤ 260
All of them are integers.
输出
Print one line denotes the answer.
If it is possible to make all workstations work, output a single number represents the total capacity of unused memory banks. It maybe very large, you should modulo 1000000007(109 + 7)
Otherwise output -1.
样例输入
4 2 2 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0
3
10 8 16
样例输出
6
来源/分类
#include<bits/stdc++.h>
#define mod 1000000007
#define rep(i,j,k) for(int i=j;i<k;i++)
using namespace std;
const int maxn = 1e6+5;
typedef long long ll;
ll w[maxn];
ll val[61];
bool cmp(ll a,ll b)
{
return a>b;
}
int main(void)
{
std::ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
ll num[61];
memset(num,0,sizeof(num));
rep(i,0,60)
{
ll t;
cin>>t;
num[i]=t;
val[i]=1ll<<i;
}
int n;
cin>>n;
rep(i,0,n)
cin>>w[i];
//sort(w,w+n,cmp);
int cnt=0;
for(int i=59;i>=0;i--)
{
if(cnt==n)
break;
if(num[i]==0)
continue;
rep(j,0,n)
{
if(w[j]==0) continue;
ll des= w[j]/val[i];
if(des==0) continue;
if(num[i]<des) des=num[i];
w[j]-=des*val[i];
num[i]-=des;
if(w[j]==0)
cnt++;
if(num[i]==0) break;
}
}
if(cnt!=n)
cout<<"-1"<<endl;
else
{
ll res=0;
rep(i,0,60)
{
if(num[i]==0)
continue;
res +=(num[i]%mod) * (val[i]%mod)%mod;
res%=mod;
}
cout<<res<<endl;
}
return 0;
}