NC14731 逆序对
逆序对
https://ac.nowcoder.com/acm/problem/14731
题意
求所有长度为n的01串中满足如下条件的二元组个数:
设第i位和第j位分别位ai和aj(i<j),则ai=1,aj=0。
答案对1e9+7取模。
分析
这可以竞争每日一题最简单题了吧。。
选两个点 ,让前面那个点是
,后面那个点是
,这样
参与的次数是
(其他点0,1随便选)。
这样的 有
对。
所以答案为 ,快速幂即可。
代码如下
#include <bits/stdc++.h>
#include<ext/pb_ds/hash_policy.hpp>
#include<ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
typedef long long LL;
typedef unsigned long long uLL;
const int mod = 1e9 + 7;
struct custom_hash {
static uint64_t splitmix64(uint64_t x) {
x += 0x9e3779b97f4a7c15;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
return x ^ (x >> 31);
}
size_t operator()(uint64_t x) const {
static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
return splitmix64(x + FIXED_RANDOM);
}
};
LL z = 1;
LL read(){
LL x, f = 1;
char ch;
while(ch = getchar(), ch < '0' || ch > '9') if(ch == '-') f = -1;
x = ch - '0';
while(ch = getchar(), ch >= '0' && ch <= '9') x = x * 10 + ch - 48;
return x * f;
}
int ksm(int a, int b, int p){
int s = 1;
while(b){
if(b & 1) s = z * s * a % p;
a = z * a * a % p;
b >>= 1;
}
return s;
}
int main(){
int i, j, m;
LL n;
n = read();
if(n == 1) printf("0");
else if(n == 2) printf("1");
else{
i = n % mod, j = (n - 1) % mod;
printf("%d", z * ksm(2, n - 3, mod) * i % mod * j % mod);
}
return 0;
}
每日一题 文章被收录于专栏
牛客的每日一题(换卫衣之路(bushi
