题意
有 n n n 个筐,每个筐里有 x i x_i xi 个球,每个球上有一个标签 a i a_i ai,表示 2 a i 2^{a_i} 2ai,同一个筐中的球的标签相同,不同筐中球的标签一定不同。从筐中取任意个球,问和的个数有多少个。 ( 1 ≤ x i ≤ 1 0 9 , 0 ≤ a i ≤ 1 0 9 , 1 ≤ n ≤ 1 0 5 ) (1\leq x_i \leq 10^9,0\leq a_i \leq 10^9,1\leq n \leq 10^5) (1≤xi≤109,0≤ai≤109,1≤n≤105)
分析
将二进制位分成一些段,分别计算每段的种类数,然后把所有段的答案乘起来。
对于样例:
3
1 1
2 1
3 1
代码
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int mod=1e9+7;
const int N=1e5+5;
pii ball[N];
ll power(ll x,ll y){
ll res=1;
while(y){
if(y&1) res=res*x%mod;
x=x*x%mod;
y>>=1;
}
return res;
}
int main(){
int T,cas=0;
scanf("%d",&T);
while(T--){
int n,a,x;
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%d%d",&a,&x);
ball[i]=make_pair(a,x);
}
sort(ball+1,ball+1+n);
ll res=0,ans=1,add=0;
int d=ball[1].first;//当前段的最低位
for(int i=1;i<=n;i++){
res=(res+1LL*ball[i].second*power(2LL,1LL*ball[i].first-d)%mod)%mod;//计算该段可以表示成最低位的个数
if(i==n){
ans=ans*(res+1)%mod;
break;
}
if(ball[i].first>d)//上一位add的转换为当前位
add>>=(ball[i].first-ball[i-1].first);
if(ball[i].second+add>2)//可以向下一位进的位数
add=(ball[i].second+add-1);
else add=0;
int flag=1;
if(ball[i+1].first-ball[i].first>32) flag=0;//x的最大值是1e9
else{
ll tmp=2LL*((1LL<<(ball[i+1].first-ball[i].first-1))-1);//保持连续需要的上一位需要满足的个数
if(tmp>add) flag=0;
}
if(!flag){//不能进
ans=ans*(res+1)%mod;
res=0;
add=0;
d=ball[i+1].first;
}
}
printf("Case #%d: %lld\n",++cas,ans);
}
return 0;
}