QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#135221 | #6637. Perfect Strings | Delay_for_five_minutes# | ML | 0ms | 0kb | C++20 | 2.7kb | 2023-08-05 12:56:51 | 2023-08-05 12:56:54 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int n , c;
const int mod = 1e9 + 7;
const int N = 2e7 + 5;
int t[20000005] , rt[20000005];
int st[N + 5] , rst[N + 5];
int fpow(int a,int b)
{
int ans = 1;
while(b) {
if(b & 1) ans = 1LL*ans*a%mod;
a = 1LL*a*a%mod;b >>= 1;
}
return ans;
}
int C(int i,int j)
{
assert(i >= j);
return 1LL*t[i]*rt[j] % mod * rt[i - j] % mod;
}
int cata(int n)
{
if(n == 0) return 1;
return (C(2*n , n) - C(2*n , n + 1) + mod) % mod;
}
int f[N + 5] , g[N + 5];
void solve()
{
cin >> n >> c;
if(c == 1) {
cout << 1 << '\n' ; return ;
}
int ans = 0;
int t = 1LL*c*fpow(c - 1 , mod - 2) % mod;
t = (t - 1 + mod) % mod;
/* f[0] = 1;
for(int i = 1;i <= n;i++) {
for(int j = 0 ; j <i ;j++) {
f[i] = (f[i] + 1LL*f[j]*cata(i - j) % mod * t) % mod;
}
}
ans = 1LL*f[n]*fpow(c - 1, n + 1) % mod;
ans = 1LL*ans*(t + 1) % mod;
cout << ans << '\n';*/
int g4 = 16; f[0] = 1 ; f[1] = mod - 2;
for(int i = 2;i <= n + 2;i++) {
f[i] = 1LL * g4 * st[2*i - 3] % mod * rst[2 * i] % mod;
f[i] = mod - f[i];
// printf("I %d %d , %d %d\n",i,mod - f[i],g4,st[2*i - 3]);
g4 = 1LL*g4*4 % mod;
}
for(int i = 0;i <= n;i++) f[i] = 1LL*f[i]*(mod - t) % mod;
f[0] = (f[0] - t + mod) % mod;
f[1] = (f[1] + 2LL*(1 + t)) % mod;
int a = 1LL * (mod - t) * fpow(1LL*(1+t)*(1+t)%mod , mod - 2) % mod, fn = 0;
//a = 1LL*a*fpow(4 , mod - 2) % mod;
//printf("A %d\n",a);
if(a == 0) {
assert(0);
}
else{
int dt = 1 , sp = mod - fpow(a , mod - 2);
for(int i = 0;i <= n;i++) {
fn = (fn + 1LL * dt * f[n - i]) % mod;
dt = 1LL * dt * sp % mod;
}
int d = 2LL*a*(1 + t) % mod * (1 + t) % mod;
fn = 1LL*fn*fpow(d , mod - 2) % mod;
}
ans = 1LL*fn*fpow(c - 1, n + 1) % mod;
ans = 1LL*ans*(t + 1) % mod;
cout << ans << '\n';
}
int main()
{
// freopen("in.txt","r",stdin);
// freopen("out2.txt","w",stdout);
ios::sync_with_stdio(false) ; cin.tie(0) ; cout.tie(0);
t[0] = 1;
for(int i = 1;i <= N;i++) t[i] = 1LL*t[i - 1]*i % mod;
rt[N] = fpow(t[N] , mod - 2);
for(int i = N - 1;i >= 0;i--) rt[i] = 1LL*rt[i + 1]*(i + 1) % mod;
st[0] = st[1] = 1;
for(int i = 2;i <= N;i++) st[i] = 1LL*st[i - 2]*i % mod;
rst[N] = fpow(st[N] , mod - 2) ; rst[N - 1] = fpow(st[N - 1] , mod - 2);
for(int i = N - 2;i >= 0;i--) {
rst[i] = 1LL*rst[i + 2]*(i + 2) % mod;
}
int T;cin >> T;
while(T--) solve();
return 0;
}
详细
Test #1:
score: 0
Memory Limit Exceeded
input:
2 3 1 2 2