Lottery(二进制)

Lottery

[Link](Problem - L - Codeforces)

题意

给你 n n n组数,每一组 a i , x i a_i,x_i ai,xi表示有 x i 个 2 a i x_i个2^{a_i} xi2ai,然后问你从这些数里选最多可以拼出多少个不同的数。

题解

我们把它转化为二进制来思考,假设这些数的二进制的和是111111,那么每一位选与不选一共有 2 6 2^6 26种情况,但是现在是可能会出现某一位超过1的情况例如100015,因为可以任意的选就相当于每个数都可以选与不选,我们把低位的超过1的往前面进位就等价于100111。因此我们可以统计每一个小段内的组合的情况,然后因为符合乘法原理,把小段一段之间乘起来就是答案,不会重复也不会遗漏。如果两组的指数相差超过32直接跳过即可,因为x最大是 1 e 9 1e9 1e9,这两段是不会相交的,防止爆LL。

Code

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <set>
#include <queue>
#include <vector>
#include <map>
#include <bitset>
#include <unordered_map>
#include <cmath> 
#include <stack>
#include <iomanip>
#include <deque> 
#include <sstream>
#define x first
#define y second
#define debug(x) cout<<#x<<":"<<x<<endl;
using namespace std;
typedef long double ld;
typedef long long LL;
typedef pair<LL, LL> PII;
typedef pair<double, double> PDD;
typedef unsigned long long ULL;
const int N = 1e5 + 10, M = 2 * N, INF = 0x3f3f3f3f, mod = 1e9 + 7;
const double eps = 1e-8, pi = acos(-1), inf = 1e20;
#define tpyeinput int
inline char nc() {static char buf[1000000],*p1=buf,*p2=buf;return p1==p2&&(p2=(p1=buf)+fread(buf,1,1000000,stdin),p1==p2)?EOF:*p1++;}
inline void read(tpyeinput &sum) {char ch=nc();sum=0;while(!(ch>='0'&&ch<='9')) ch=nc();while(ch>='0'&&ch<='9') sum=(sum<<3)+(sum<<1)+(ch-48),ch=nc();}
int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1};
int h[N], e[M], ne[M], w[M], idx;
void add(int a, int b, int v = 0) {
    e[idx] = b, w[idx] = v, ne[idx] = h[a], h[a] = idx ++;
}
int n, m, k;
PII a[N];
LL b[N];
LL qmi(LL a, LL b, LL p) {
    LL res = 1;
    while (b) {
        if (b & 1) res = res * a % p;
        a = a * a % p;
        b >>= 1;
    }
    return res;
}
int main() {
    int T, C = 0;
    cin >> T;
    while (T -- ) {
        cin >> n;
        for(int i = 1; i <= n; i ++ ) cin >> a[i].x >> a[i].y;
        sort(a + 1, a + n + 1);
        for (int i = 1; i <= n; i ++ ) b[i] = a[i].y;
        LL res = 1; int r;
        for (int l = 1; l <= n; l = r + 1) {
            r = l;
            while (r < n && (b[r] >> (a[r + 1].x - a[r].x)) && a[r + 1].x - a[r].x <= 32 ) {
                b[r + 1] += (b[r] >> (a[r + 1].x - a[r].x));
                r ++;
            }
            for (int i = r; i > l; i --) a[i - 1].y = (a[i - 1].y + a[i].y * qmi(2, a[i].x - a[i - 1].x, mod)) % mod;
            res = res * (a[l].y + 1) % mod;
        }
        printf("Case #%d: %lld\n", ++ C, res);
    }
    return 0;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值