链接:https://ac.nowcoder.com/acm/problem/14731
来源:牛客网
求所有长度为n的01串中满足如下条件的二元组个数:
设第i位和第j位分别位ai和aj(i<j),则ai=1,aj=0。
答案对1e9+7取模。
思路
数据范围 n < 10^18,暴力肯定不太可能,应该考虑推公式
根据题意,一个字符串所有满足题意的二元组个数为所有0位的前面的1的数量的和,或者每一个1位后面0的数量求和。
这里说一下第一种
对于所有可能的01串的前i-1位,1的个数有 (i-1) /2*2^(i-1)。对于第 i + 1 ~ n位(2^(n-i)种可能),对于第i位的0的贡献没有影响,所以再乘上
2^(n-i)
所以对于第i位是0的所有字符串该位对答案的贡献是 (i-1)*2^(n-2)
i 2~n 求和 ans = (n-1)n2(n-3)
代码
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll mod = 1e9 + 7;
ll fun(ll x,ll y){
ll f = x;
x = 0;
while(y){
if(y&1)
x = (x + f) %mod;
y >>=1;
f = (f + f)%mod;
}
return x;
}
ll qqow(ll x){
ll f = 2;
ll ans = 1;
while(x){
if(x&1)
ans = fun(ans,f)%mod;
x>>=1;
f = fun(f,f)%mod;
}
return ans;
}
int main(){
ll n;
cin >> n;
ll ans;
if(n <= 1){
cout << 0;
return 0;
}
if(n == 2){
cout << 1;
return 0;
}
ans=(n%mod)*((n-1)%mod)%mod*qqow(n-3)%mod;
//ans = fun(fun(n,n-1),qqow(n-3));
cout << ans << endl;
return 0;
}