QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#821138 | #4987. 图 | Crysfly | 100 ✓ | 766ms | 589200kb | C++14 | 3.2kb | 2024-12-19 13:43:53 | 2024-12-19 13:43:54 |
Judging History
answer
// what is matter? never mind.
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("unroll-loops")
//#pragma GCC target("sse,sse2,sse3,sse4,popcnt,abm,mmx,avx,avx2")
#include<bits/stdc++.h>
#define For(i,a,b) for(int i=(a);i<=(b);++i)
#define Rep(i,a,b) for(int i=(a);i>=(b);--i)
#define ll long long
//#define ull unsigned long long
//#define int long long
#define SZ(x) ((int)((x).size()))
#define ALL(x) (x).begin(),(x).end()
using namespace std;
inline int read()
{
char c=getchar();int x=0;bool f=0;
for(;!isdigit(c);c=getchar())f^=!(c^45);
for(;isdigit(c);c=getchar())x=(x<<1)+(x<<3)+(c^48);
return f?-x:x;
}
int mod;
typedef unsigned long long ull;
namespace FM{
typedef __uint128_t L;
struct FastMod{
ull b,m;
FastMod(ull b):b(b),m(ull((L(1)<<64)/b)){}
ull reduce(ull a){ull q=(ull)((L(m)*a)>>64),r=a-q*b;return r>=b?r-b:r;}
};
FastMod F(2);
}
struct modint{
int x;
modint(int o=0){x=o;}
modint &operator = (int o){return x=o,*this;}
modint &operator +=(modint o){return x=x+o.x>=mod?x+o.x-mod:x+o.x,*this;}
modint &operator -=(modint o){return x=x-o.x<0?x-o.x+mod:x-o.x,*this;}
modint &operator *=(modint o){return x=FM::F.reduce(1ull*x*o.x),*this;}
modint &operator ^=(int b){
modint a=*this,c=1;
for(;b;b>>=1,a*=a)if(b&1)c*=a;
return x=c.x,*this;
}
modint &operator /=(modint o){return *this *=o^=mod-2;}
friend modint operator +(modint a,modint b){return a+=b;}
friend modint operator -(modint a,modint b){return a-=b;}
friend modint operator *(modint a,modint b){return a*=b;}
friend modint operator /(modint a,modint b){return a/=b;}
friend modint operator ^(modint a,int b){return a^=b;}
friend bool operator ==(modint a,modint b){return a.x==b.x;}
friend bool operator !=(modint a,modint b){return a.x!=b.x;}
bool operator ! () {return !x;}
modint operator - () {return x?mod-x:0;}
bool operator <(const modint&b)const{return x<b.x;}
};
inline modint qpow(modint x,int y){return x^y;}
void initmod(){mod=read(),FM::F=FM::FastMod(mod);}
vector<modint> fac,ifac,iv;
inline void initC(int n)
{
if(iv.empty())fac=ifac=iv=vector<modint>(2,1);
int m=iv.size(); ++n;
if(m>=n)return;
iv.resize(n),fac.resize(n),ifac.resize(n);
For(i,m,n-1){
iv[i]=iv[mod%i]*(mod-mod/i);
fac[i]=fac[i-1]*i,ifac[i]=ifac[i-1]*iv[i];
}
}
inline modint C(int n,int m){
if(m<0||n<m)return 0;
return initC(n),fac[n]*ifac[m]*ifac[n-m];
}
inline modint sign(int n){return (n&1)?(mod-1):(1);}
#define fi first
#define se second
#define pb push_back
#define mkp make_pair
typedef pair<int,int>pii;
typedef vector<int>vi;
#define maxn 30000005
#define inf 0x3f3f3f3f
modint f[maxn],g[maxn];
void prew(int n){
initC(n+5);
g[0]=1;
For(i,3,n) if(i%3==0) g[i]=g[i-3]*iv[6];
For(i,3,n) if(i%3==0) g[i]*=ifac[i/3];
f[0]=f[1]=1;
f[2]=iv[2];
For(i,3,n){
f[i]=(f[i-1]+f[i-3]*iv[2])*iv[i];
}
}
signed main()
{
int T=read();initmod();
prew(3e7);
while(T--){
int n=read();
modint res=0;
if(n%3==0)res=g[n];
else if(n%3==1)res=f[n-1];
else res=f[n-1]-g[n-2]*iv[2];
res*=fac[n];
cout<<res.x<<"\n";
}
return 0;
}
/*
0 1 0
1 1 1 1
1 1 2 1
1 1 1 0 0 1 1 0
0 1 1 1 1 1 1 4 4 5 8 9 9
*/
詳細信息
Subtask #1:
score: 20
Accepted
Test #1:
score: 20
Accepted
time: 759ms
memory: 589200kb
input:
8 998244353 1 2 3 4 5 6 7 8
output:
1 1 1 8 15 10 217 568
result:
ok 8 numbers
Subtask #2:
score: 15
Accepted
Dependency #1:
100%
Accepted
Test #2:
score: 15
Accepted
time: 527ms
memory: 588828kb
input:
200 321402013 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 9...
output:
1 1 1 8 15 10 217 568 280 12050 39831 15400 1123993 4448458 1401400 157764896 80087671 190590400 187957435 189964100 215150544 210537812 320589946 116147435 77322237 258401726 143880854 163578365 60466855 241862371 40755093 241051099 80785167 247356411 251514276 178274428 312164931 154916780 3015398...
result:
ok 200 numbers
Subtask #3:
score: 15
Accepted
Dependency #1:
100%
Accepted
Dependency #2:
100%
Accepted
Test #3:
score: 15
Accepted
time: 666ms
memory: 588928kb
input:
3000 621098477 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 ...
output:
1 1 1 8 15 10 217 568 280 12050 39831 15400 1123993 4448458 1401400 157764896 101793220 190590400 608725310 26465057 188464334 181746076 546229079 477992250 553788308 95041077 72862000 377999633 453324334 390343581 378383862 26333899 448789829 467634276 16859121 578701622 560223528 54126595 7737831 ...
result:
ok 3000 numbers
Subtask #4:
score: 20
Accepted
Dependency #1:
100%
Accepted
Dependency #2:
100%
Accepted
Dependency #3:
100%
Accepted
Test #4:
score: 20
Accepted
time: 755ms
memory: 589040kb
input:
100000 987654337 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 9...
output:
1 1 1 8 15 10 217 568 280 12050 39831 15400 1123993 4448458 1401400 157764896 722891697 190590400 425266236 890968006 656619868 590941449 783143242 198897988 819863574 553350865 444314195 349243413 573657492 638473836 972976899 210482843 633634816 467619442 823146932 716413123 883383200 284524945 92...
result:
ok 100000 numbers
Subtask #5:
score: 30
Accepted
Dependency #1:
100%
Accepted
Dependency #2:
100%
Accepted
Dependency #3:
100%
Accepted
Dependency #4:
100%
Accepted
Test #5:
score: 30
Accepted
time: 766ms
memory: 589048kb
input:
100000 987654439 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 9...
output:
1 1 1 8 15 10 217 568 280 12050 39831 15400 1123993 4448458 1401400 157764896 722891697 190590400 425263074 890951482 656616196 590100051 778293856 197951836 535903428 732607737 136808165 102841042 564792893 235466406 348855375 548639079 248113574 958091288 425942518 467065119 697029461 583676776 44...
result:
ok 100000 numbers