QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#109931 | #6513. Expression 3 | liparcanjo | TL | 30ms | 5108kb | C++14 | 5.5kb | 2023-05-31 01:43:32 | 2023-05-31 01:43:34 |
Judging History
answer
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define dbg(x) cout << "[" << #x << "]: " << x << endl;
using ll = long long;
using pi = pair<int,int>;
using pl = pair<ll,ll>;
using vi = vector<int>;
using vl = vector<ll>;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
const int MAXN = 2e5 + 5;
const int MOD = 998244353;
const int INF = 0x3f3f3f3f;
const ll INF64 = ll(4e18) + 5;
int add(int a, int b){
a += b;
if( a >= MOD) a -= MOD;
if(a < 0) a += MOD;
return a;
}
int mul(int a, int b){
int ret = (1LL * a * b)%MOD;
return ret;
}
int pw(int a, int n){
int ret = 1;
while(n > 0){
if(n&1 == 1){
ret = mul(ret, a);
}
a = mul(a,a);
n = n>> 1;
}
return ret;
}
int inv(int a){
return pw(a,MOD-2);
}
int primitive_root(int p){
vector<int> fact;
int phi = p-1, n = phi;
for(int i=2; i*i <=n; i++) if(n%i == 0){
fact.pb(i);
while(n%i == 0){
n /= i;
}
}
if(n > 1){
fact.pb(n);
}
int szf = fact.size();
for(int res = 2; res <= p; res++){
bool ok = true;
for(int i = 0; i < szf && ok; i++){
ok &= pw(res, phi/fact[i]) != 1;
}
if(ok) return res;
}
return -1;
}
void ntt( vi &a, ll n, ll x , bool invert ){
ll ctzll_n = __builtin_ctzll(n);
for(int i = 0; i < n; i++){
ll y = 0;
for(int j = 0; j < ctzll_n; j++){
if( (1LL << j)&i ){
y |= ( 1LL << (ctzll_n - j - 1) );
}
}
if(y > i){
swap(a[i], a[y]);
}
}
if(invert) x = inv(x);
for(int s = 1; s < ctzll_n + 1; s++){
int y = pw(x, (n/(1LL<<s)));
int cur = (1LL << s);
int pre = (1LL<<(s-1));
for(int j = 0; j < n/cur; j++ ){
int r = 1;
for(int i = 0; i < pre ; i++ ){
int u = a[i + j*cur];
int v = mul( r,a[ i + j*cur + pre ] );
a[i+j*cur] = add(u,v);
a[ i+j*cur + pre ] = add(u, -v);
r = mul(r,y);
}
}
}
if(invert){
ll invn = inv(n);
for(int i = 0; i < n; i++){
a[i] = (a[i]*1LL*invn)%MOD;
}
}
return;
}
void poly_multiplication( vi &a, vi &b, vi &c, int p_root = 0){
ll n = 1;
ll sum_tam = ((ll)a.size()) + ((ll)b.size());
while(n < sum_tam){
n <<= 1;
}
vi fa(a.begin(), a.end());
fa.resize(n,0);
vi fb(b.begin(), b.end());
fb.resize(n,0);
ll x = pw(p_root, (MOD - 1)/n);
ntt(fa,n, x, false);
ntt(fb,n, x, false);
c.resize(n,0);
for(int i = 0; i < n; i++){
c[i] = mul( fa[i], fb[i] );
}
ntt(c, n, x, true);
c.resize( sum_tam - 1 , 0LL);
return ;
}
int fat[MAXN];
int invfat[MAXN];
void pre(){
fat[0] = fat[1] = invfat[0] = invfat[1] = 1;
for(int i = 2; i < MAXN; i++) fat[i] = mul(fat[i-1],i);
for(int i = 2; i < MAXN; i++) invfat[i] = mul(invfat[i-1], inv(i));
}
void solve(){
pre();
int n;
cin >> n;
vector<int> a(n);
for(int i = 0; i < n; i++) cin >> a[i];
string s;
cin >> s;
vector<int> b(n-1);
for(int i = 0; i < n-1; i++) b[i] = s[i] == '-';
vector<int> h(n-1);
for(int i = 1; i < n; i++){
//dis == i, tam == i+1
int op = mul(fat[i-1], invfat[i+1]);
h[i] = inv(i);
}
vector<array<int,2>> dp(n-1);
for(int i = 0; i < n-1; i++){
dp[i][b[i]] = 1;
for(int j = i-1; j >= 0; j--){
if(!b[j]) continue;
int prob = inv(i-j+1);
int cn = add(1,-prob);
int n0=0;
int n1=0;
n0 = add( mul(dp[i][0],cn), mul(dp[i][1],prob));
n1 = add( mul(dp[i][1],cn), mul(dp[i][0],prob));
dp[i][0] = n0;
dp[i][1] = n1;
}
}
/*
function<void(int,int)> dc = [&](int i, int j){
if(i == j){
int eu = mul(fat[i], invfat[i+1]);
dp[i][b[i]] = add(dp[i][b[i]],eu);
cout << dp[i][0] << ' ' << dp[i][1] << '\n';
cout << mul(dp[i][0],fat[n-1]) << ' ' << mul(dp[i][1],fat[n-1]) << '\n';
return;
}
int m = (i+j)/2;
dc(i,m);
//faz a contribuicao para m+1-j
vector<vector<int>> c(2, vector<int>(m-i+1));
for(int k=0,l=i=1;l<=m;l++,k++){
c[0][k] = dp[l][0];
c[1][k] = dp[l][1];
}
vector<int> d(j-i+1), e(j-i+1);
for(int k = 0,l=i; k < j-i+1; k++,l++) d[k] = (b[l] ? h[k];
for(int k = 0; k < j-i+1; k++) e[k] = add(1,-h[k]);
vector<vector<int>> res(2);
poly_multiplication(c[0],d,res[0],3);
poly_multiplication(c[1],d,res[1],3);
for(int k = m+1,l=m-i+1; k <= j; k++,l++){
dp[k][0] = add(dp[k][0],res[b[k]][l]);
dp[k][1] = add(dp[k][1],res[1^b[k]][l]);
}
dc(m+1,j);
};
*/
int op = fat[n-1];
//dc(0,2);
int ans = 0;
for(int i = 0; i < n-1; i++) for(int j = 0; j < 2; j++){
dp[i][j] = mul(dp[i][j],op);
}
for(int i = 1; i < n; i++){
int at = add(dp[i-1][0],-dp[i-1][1]);
at = mul(at, a[i]);
ans = add(ans, at);
}
ans = add(ans, mul(op,a[0]));
cout << ans << '\n';
}
int main(){
ios::sync_with_stdio(false); cin.tie(NULL);
int t = 1;
//cin >> t;
while(t--){
solve();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 26ms
memory: 5108kb
input:
4 9 1 4 1 -+-
output:
46
result:
ok 1 number(s): "46"
Test #2:
score: 0
Accepted
time: 30ms
memory: 5064kb
input:
5 1 2 3 4 5 +-+-
output:
998244313
result:
ok 1 number(s): "998244313"
Test #3:
score: -100
Time Limit Exceeded
input:
100000 664815434 205025136 871445392 797947979 379688564 336946672 231295524 401655676 526374414 670533644 156882283 372427821 700299596 166140732 677498490 44858761 185182210 559696133 813911251 842364231 681916958 114039865 222372111 784286397 437994571 152137641 650875922 613727135 209302742 5321...