题解:P13561 「WWOI R1」WsW 的笔
这里是出题人题解。
思路
为了方便表达,令
如果编号为
组和组之间互不影响,假设分出了
如何求出有多少组呢?可以用每一组中最小的编号来指代这一组。可以发现,满足
所以只需要求出满足这两个条件的
对于特殊性质 A,上面的容斥会出错,需要特判。
特判时如果直接 unsigned long long,要把式子转化成
最后用快速幂计算方案数。
时间复杂度为
代码
#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
const ull mod=1e9+7;
ull p(ull x){
ull ans=1,t=2;
while(x){
if(x&1)(ans*=t)%=mod;
(t*=t)%=mod;
x>>=1;
}
return ans;
}
ull g(ull l,ull r,ull k){
return (r-r/k)-(l-l/k);
}
void work(){
ull k,a,b; cin>>k>>a>>b;
if(b/k<a)cout<<p(b-a+1)<<'\n';
else cout<<p(g(a,b,k)-g(a,a*k-1,k)+a*(k-1))<<'\n';
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
int T=1; cin>>T;
while(T--)work();
return 0;
}