QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#76119 | #5464. Dice Game | zhouhuanyi | RE | 2ms | 3540kb | C++14 | 1.7kb | 2023-02-07 19:16:35 | 2023-02-07 19:16:36 |
Judging History
answer
#include<iostream>
#include<cstdio>
#include<algorithm>
#define N 29
#define M 100000
#define mod 998244353
using namespace std;
int read()
{
char c=0;
int sum=0;
while (c<'0'||c>'9') c=getchar();
while ('0'<=c&&c<='9') sum=sum*10+c-'0',c=getchar();
return sum;
}
void Adder(int &x,int d)
{
x+=d;
if (x>=mod) x-=mod;
return;
}
void Adder2(int &x,int d)
{
x+=d;
if (x<0) x+=mod;
return;
}
int MD(int x)
{
return (x>=mod)?x-mod:x;
}
int MD2(int x)
{
return (x<0)?x+mod:x;
}
int fast_pow(int a,int b)
{
int res=1,mul=a;
while (b)
{
if (b&1) res=1ll*res*mul%mod;
mul=1ll*mul*mul%mod,b>>=1;
}
return res;
}
int T,n,cnt,inv2,F[N+1],ans;
bool used[N+1];
long long delta[N+1];
bool cmp(long long x,long long y)
{
return x>y;
}
void dfs(int x,bool op,long long res)
{
int dst=0;
long long rst=0;
for (int i=0;i<=x;++i) rst+=delta[i];
if (res+rst<=0) return;
if (res-rst>0&&op)
{
for (int i=x+1;i<=cnt;++i)
{
if (!used[i]) Adder(dst,delta[i]%mod);
else Adder2(dst,-delta[i]%mod);
}
for (int i=0;i<=x;++i) dst=2ll*dst%mod;
Adder(ans,dst);
return;
}
used[x]=0,dfs(x-1,op|(((n-1)>>x)&1),res+delta[x]);
if (op||(((n-1)>>x)&1)) used[x]=1,dfs(x-1,op,res-delta[x]);
return;
}
int main()
{
T=read(),inv2=(mod+1)>>1;
while (T--)
{
n=read(),cnt=ans=0;
while ((1<<cnt)<n) cnt++;
for (int i=0;i<=cnt;++i) F[i]=n/(1<<(i+1))*(1<<i)+max(n%(1<<(i+1))-(1<<i),0),delta[i]=1ll*F[i]*(1<<i);
dfs(cnt,0,0),ans=1ll*ans*fast_pow(n,mod-2)%mod,Adder(ans,1ll*n*(n-1)%mod*inv2%mod),printf("%lld\n",1ll*ans*fast_pow(n,mod-2)%mod);
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 3540kb
input:
4 1 2 3 4
output:
0 249561089 776412276 2
result:
ok 4 number(s): "0 249561089 776412276 2"
Test #2:
score: -100
Runtime Error
input:
100 119 75 29 10 17 29 449 71 72 12 79 117 83 80 35 272 105 497 346 287 362 140 297 167 111 419 210 212 170 413 373 210 196 39 1 101 258 496 333 293 392 2 187 431 157 342 436 106 449 136 378 243 357 325 237 254 22 292 62 435 18 446 471 18 42 377 181 350 19 389 212 58 45 70 52 63 107 71 66 355 381 30...