分段打表

什么是分段打表:

分段打表结合了前缀和的知识。比如我们要求1-n的和,但是n到了1e8,不能开到1e8的数组,然后我们就可以考虑没100个数记录和,sum[1]表示前100个数的和,sum[2]表示前200个数的和,依次类推,sum[1000000]就表示前1e8的和。所有如果我们要求1-123的和,那么就是sum[1]+101+...+123,前一段O(1)求,后一段暴力求,后一段暴力复杂度在O(100)内,所以要恰当的选择段大小。

这下就不用怕数组存不下了。

题目:https://ac.nowcoder.com/acm/contest/7872/A

#include <bits/stdc++.h>
#define ull unsigned long long
#define ll long long
const int inf = 0x3f3f3f3f;
const ll mod = 1e9+7;
const int N = 3e6+7;
const ll ds = 1e15+7;
const double PI = 3.141592653589793238462643383;
 
using namespace std;

ll sum[N];
void init(){
    ll tmp = 1;
    sum[0] = 1;
    for(int i = 1; i <= 100000005; i++){
        tmp = (tmp*tmp%mod+tmp)%mod;
        if(i%100 == 0) sum[i/100] = tmp;
    }
}

ll qpow(ll x,ll y){
    ll res = 1;
    while(y){
        if(y&1) res = (res*x)%mod;
        y >>= 1;
        x = (x*x)%mod;
    }
    return res;
}


int main(){
    ll n,x,y;
    init();
    cin >> n;
    while(n--){
        ll xx,xys,yy,yys;
        ll xsum = 0,ysum = 0;
        cin >> x >> y;
        if(y > x) swap(y,x);
        xx = x/100;
        xys = x%100;
        yy = y/100;
        yys = y%100;
        xsum = sum[xx];
        ysum = sum[yy];
        for(int i=1;i<=xys;i++){
            xsum = (xsum*xsum%mod+xsum)%mod;
        }
        for(int i=1;i<=yys;i++){
            ysum = (ysum*ysum%mod+ysum)%mod;
        }
        ll ans =  xsum*qpow(ysum,mod-2)%mod ;
        cout << ans << endl;
    }
}

 

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值