Solution
容易看出,能凑成“一对”的数是很有限的,其中最大的一对是 所以我们容易知道, 到 这段区间中的数字都可以作为优秀方案留或者送,我们有 组这样的数对可以满足优秀方案,这些数对我们可以任选每组中的一个留下,另一个送出去,每组两种可能,一共 种可能,剩下的没有配对的数也可以选择留下或者送出,所以剩下的数有 种可能,其中 是区间长度,乘二因为每对中有两个数,所以是对数乘二。因为我们要全部在一次分配种计算可能,所以这个需要用乘法原理,把上面两个相乘即可。
Code
#include<bits/stdc++.h>
#define int long long
using namespace std;
int k,a,b;
const int P=1e9+7;
int qpow(int base,int exp){
int ret=1;
while(exp){
if(exp&1) ret=ret*base%P;
base=base*base%P;
exp>>=1;
}
return ret%P;
}
signed main(){
int T; cin>>T;
srand(time(0));
while(T--){
cin>>k;
cin>>a>>b;
// x x/k
// 0 1
// 1 0
// so a pair of (x,x/k) has two possiblity
// the max number which can be matched is b
// b can be matched with b/k
// so we have a range [a, b/k] that can be matched
int t=max(0ll,b/k-a+1);
int r=b-a+1-2*t;
int ans=qpow(2,t)%P*qpow(2,r)%P;
cout<<ans<<'\n';
}
}