QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#106917 | #6417. Classical Summation Problem | bulijiojiodibuliduo# | AC ✓ | 226ms | 17716kb | C++ | 3.4kb | 2023-05-19 19:36:50 | 2023-05-19 19:36:51 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
typedef vector<int> VI;
typedef basic_string<int> BI;
typedef long long ll;
typedef pair<int,int> PII;
typedef double db;
mt19937 mrand(random_device{}());
const ll mod=1000000007;
int rnd(int x) { return mrand() % x;}
ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
// head
template<int MOD, int RT> struct mint {
static const int mod = MOD;
static constexpr mint rt() { return RT; } // primitive root for FFT
int v; explicit operator int() const { return v; } // explicit -> don't silently convert to int
mint():v(0) {}
mint(ll _v) { v = int((-MOD < _v && _v < MOD) ? _v : _v % MOD);
if (v < 0) v += MOD; }
bool operator==(const mint& o) const {
return v == o.v; }
friend bool operator!=(const mint& a, const mint& b) {
return !(a == b); }
friend bool operator<(const mint& a, const mint& b) {
return a.v < b.v; }
mint& operator+=(const mint& o) {
if ((v += o.v) >= MOD) v -= MOD;
return *this; }
mint& operator-=(const mint& o) {
if ((v -= o.v) < 0) v += MOD;
return *this; }
mint& operator*=(const mint& o) {
v = int((ll)v*o.v%MOD); return *this; }
mint& operator/=(const mint& o) { return (*this) *= inv(o); }
friend mint pow(mint a, ll p) {
mint ans = 1; assert(p >= 0);
for (; p; p /= 2, a *= a) if (p&1) ans *= a;
return ans; }
friend mint inv(const mint& a) { assert(a.v != 0);
return pow(a,MOD-2); }
mint operator-() const { return mint(-v); }
mint& operator++() { return *this += 1; }
mint& operator--() { return *this -= 1; }
friend mint operator+(mint a, const mint& b) { return a += b; }
friend mint operator-(mint a, const mint& b) { return a -= b; }
friend mint operator*(mint a, const mint& b) { return a *= b; }
friend mint operator/(mint a, const mint& b) { return a /= b; }
};
const int MOD=998244353;
using mi = mint<MOD,5>; // 5 is primitive root for both common mods
namespace simp {
vector<mi> fac,ifac,invn;
void check(int x) {
if (fac.empty()) {
fac={mi(1),mi(1)};
ifac={mi(1),mi(1)};
invn={mi(0),mi(1)};
}
while (SZ(fac)<=x) {
int n=SZ(fac),m=SZ(fac)*2;
fac.resize(m);
ifac.resize(m);
invn.resize(m);
for (int i=n;i<m;i++) {
fac[i]=fac[i-1]*mi(i);
invn[i]=mi(MOD-MOD/i)*invn[MOD%i];
ifac[i]=ifac[i-1]*invn[i];
}
}
}
mi gfac(int x) {
check(x); return fac[x];
}
mi ginv(int x) {
check(x); return invn[x];
}
mi gifac(int x) {
check(x); return ifac[x];
}
mi binom(int n,int m) {
if (m < 0 || m > n) return mi(0);
return gfac(n)*gifac(m)*gifac(n - m);
}
}
//const int N=501000;
int n,k;
int main() {
scanf("%d%d",&n,&k);
if (k%2==1) {
mi ans=pow(mi(n),k)*(n+1)/2;
printf("%d\n",(int)ans);
return 0;
}
mi ans=0;
for (int i=0;i*2<=n;i++) {
mi w=0;
if (i*2<n) {
ans+=pow(mi(n),k)-simp::binom(k,k/2)*pow(mi(i),k/2)*pow(mi(n-i),k/2);
} else {
for (int p=0;p<=k/2-1;p++) {
w+=simp::binom(k,p)*pow(mi(i),p)*pow(mi(n-i),k-p);
}
}
ans+=w;
//printf("%d %d\n",(int)w,pow(mi(n),k));
}
printf("%d\n",(int)ans);
}
详细
Test #1:
score: 100
Accepted
time: 2ms
memory: 3704kb
input:
3 2
output:
14
result:
ok 1 number(s): "14"
Test #2:
score: 0
Accepted
time: 1ms
memory: 3704kb
input:
5 3
output:
375
result:
ok 1 number(s): "375"
Test #3:
score: 0
Accepted
time: 2ms
memory: 3576kb
input:
2 2
output:
5
result:
ok 1 number(s): "5"
Test #4:
score: 0
Accepted
time: 2ms
memory: 3552kb
input:
10 9
output:
508778235
result:
ok 1 number(s): "508778235"
Test #5:
score: 0
Accepted
time: 2ms
memory: 3648kb
input:
69 3
output:
11497815
result:
ok 1 number(s): "11497815"
Test #6:
score: 0
Accepted
time: 0ms
memory: 3620kb
input:
994 515
output:
33689623
result:
ok 1 number(s): "33689623"
Test #7:
score: 0
Accepted
time: 2ms
memory: 3668kb
input:
4476 6182
output:
114894183
result:
ok 1 number(s): "114894183"
Test #8:
score: 0
Accepted
time: 2ms
memory: 3664kb
input:
58957 12755
output:
932388891
result:
ok 1 number(s): "932388891"
Test #9:
score: 0
Accepted
time: 19ms
memory: 3708kb
input:
218138 28238
output:
392861201
result:
ok 1 number(s): "392861201"
Test #10:
score: 0
Accepted
time: 69ms
memory: 10264kb
input:
644125 316810
output:
420621854
result:
ok 1 number(s): "420621854"
Test #11:
score: 0
Accepted
time: 119ms
memory: 10476kb
input:
612914 505428
output:
973686286
result:
ok 1 number(s): "973686286"
Test #12:
score: 0
Accepted
time: 1ms
memory: 3560kb
input:
998216 938963
output:
251335926
result:
ok 1 number(s): "251335926"
Test #13:
score: 0
Accepted
time: 2ms
memory: 3564kb
input:
990516 996933
output:
549551960
result:
ok 1 number(s): "549551960"
Test #14:
score: 0
Accepted
time: 133ms
memory: 17544kb
input:
999019 999012
output:
637189128
result:
ok 1 number(s): "637189128"
Test #15:
score: 0
Accepted
time: 216ms
memory: 17716kb
input:
999928 999950
output:
185229465
result:
ok 1 number(s): "185229465"
Test #16:
score: 0
Accepted
time: 2ms
memory: 3660kb
input:
999999 999999
output:
384164916
result:
ok 1 number(s): "384164916"
Test #17:
score: 0
Accepted
time: 142ms
memory: 17708kb
input:
999999 1000000
output:
696165930
result:
ok 1 number(s): "696165930"
Test #18:
score: 0
Accepted
time: 2ms
memory: 3540kb
input:
1000000 999999
output:
219071706
result:
ok 1 number(s): "219071706"
Test #19:
score: 0
Accepted
time: 222ms
memory: 17476kb
input:
1000000 1000000
output:
128206597
result:
ok 1 number(s): "128206597"
Test #20:
score: 0
Accepted
time: 2ms
memory: 3620kb
input:
2 10
output:
1410
result:
ok 1 number(s): "1410"
Test #21:
score: 0
Accepted
time: 1ms
memory: 3668kb
input:
84 16
output:
297627153
result:
ok 1 number(s): "297627153"
Test #22:
score: 0
Accepted
time: 2ms
memory: 3728kb
input:
643 800
output:
489237163
result:
ok 1 number(s): "489237163"
Test #23:
score: 0
Accepted
time: 2ms
memory: 3596kb
input:
9903 880
output:
595167333
result:
ok 1 number(s): "595167333"
Test #24:
score: 0
Accepted
time: 21ms
memory: 4864kb
input:
97446 89750
output:
410205549
result:
ok 1 number(s): "410205549"
Test #25:
score: 0
Accepted
time: 94ms
memory: 17620kb
input:
186460 646474
output:
32638530
result:
ok 1 number(s): "32638530"
Test #26:
score: 0
Accepted
time: 80ms
memory: 6772kb
input:
508940 244684
output:
598321755
result:
ok 1 number(s): "598321755"
Test #27:
score: 0
Accepted
time: 132ms
memory: 17500kb
input:
583646 557758
output:
858695621
result:
ok 1 number(s): "858695621"
Test #28:
score: 0
Accepted
time: 226ms
memory: 17476kb
input:
969610 992608
output:
256683498
result:
ok 1 number(s): "256683498"
Test #29:
score: 0
Accepted
time: 219ms
memory: 17704kb
input:
995106 996434
output:
411791999
result:
ok 1 number(s): "411791999"
Test #30:
score: 0
Accepted
time: 135ms
memory: 17472kb
input:
999961 999872
output:
61222370
result:
ok 1 number(s): "61222370"
Test #31:
score: 0
Accepted
time: 132ms
memory: 17532kb
input:
999977 999908
output:
831096762
result:
ok 1 number(s): "831096762"
Test #32:
score: 0
Accepted
time: 221ms
memory: 17700kb
input:
999992 999998
output:
562977678
result:
ok 1 number(s): "562977678"
Test #33:
score: 0
Accepted
time: 18ms
memory: 3560kb
input:
1000000 2
output:
118436113
result:
ok 1 number(s): "118436113"
Test #34:
score: 0
Accepted
time: 105ms
memory: 17628kb
input:
2 1000000
output:
298823641
result:
ok 1 number(s): "298823641"