Link: https://ac.nowcoder.com/acm/problem/14731
Source: Niuke
Find the number of two-tuples in all 01 strings of length n that meet the following conditions:
set the i-th and j-th bits to be ai and aj (i<j), then ai=1, aj=0.
The answer is modulo 1e9+7.
Ideas
Data range n <10^18, violence is definitely unlikely, and the formula should be considered
According to the meaning of the question, the number of two-tuples in a string that meets the meaning of the question is the sum of the number of 1s in front of all 0 digits, or the sum of the number of 0s behind each 1 digit. Let's
talk about the first type.
For the first i-1 digits of all possible 01 strings, the number of 1s is (i-1) /2*2^(i-1). For the i + 1 ~ n bits (2^(ni) possible), it has no effect on the contribution of 0 in the i bit, so multiply it by
2^(ni),
so for all strings where the i bit is 0 The contribution of bits to the answer is (i-1)*2^(n-2)
i 2~n sum ans = (n-1) n 2(n-3)
Code
#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;
}