QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#823247 | #8428. Partition into Teams | Zi_Gao | AC ✓ | 8ms | 15388kb | C++23 | 4.5kb | 2024-12-20 20:51:04 | 2024-12-20 20:51:08 |
Judging History
answer
#include<bits/stdc++.h>
// #define ONLINE_JUDGE
#define INPUT_DATA_TYPE long long
#define OUTPUT_DATA_TYPE int
inline __attribute((always_inline)) INPUT_DATA_TYPE read(){register INPUT_DATA_TYPE x=0;register char f=0,c=getchar();while(c<'0'||'9'<c)f=(c=='-'),c=getchar();while('0'<=c&&c<='9')x=(x<<3)+(x<<1)+(c&15),c=getchar();return f?-x:x;}void print(OUTPUT_DATA_TYPE x){if(x<0)x=-x,putchar('-');if(x>9)print(x/10);putchar(x%10^48);return;}
#pragma once
#include <iostream>
using namespace std;
template <typename Int, typename UInt, typename Long, typename ULong, int id>
struct ArbitraryLazyMontgomeryModIntBase {
using mint = ArbitraryLazyMontgomeryModIntBase;
inline static UInt mod;
inline static UInt r;
inline static UInt n2;
static constexpr int bit_length = sizeof(UInt) * 8;
static UInt get_r() {
UInt ret = mod;
while (mod * ret != 1) ret *= UInt(2) - mod * ret;
return ret;
}
static void set_mod(UInt m) {
assert(m < (UInt(1u) << (bit_length - 2)));
assert((m & 1) == 1);
mod = m, n2 = -ULong(m) % m, r = get_r();
}
UInt a;
ArbitraryLazyMontgomeryModIntBase() : a(0) {}
ArbitraryLazyMontgomeryModIntBase(const Long &b)
: a(reduce(ULong(b % mod + mod) * n2)){};
static UInt reduce(const ULong &b) {
return (b + ULong(UInt(b) * UInt(-r)) * mod) >> bit_length;
}
mint &operator+=(const mint &b) {
if (Int(a += b.a - 2 * mod) < 0) a += 2 * mod;
return *this;
}
mint &operator-=(const mint &b) {
if (Int(a -= b.a) < 0) a += 2 * mod;
return *this;
}
mint &operator*=(const mint &b) {
a = reduce(ULong(a) * b.a);
return *this;
}
mint &operator/=(const mint &b) {
*this *= b.inverse();
return *this;
}
mint operator+(const mint &b) const { return mint(*this) += b; }
mint operator-(const mint &b) const { return mint(*this) -= b; }
mint operator*(const mint &b) const { return mint(*this) *= b; }
mint operator/(const mint &b) const { return mint(*this) /= b; }
bool operator==(const mint &b) const {
return (a >= mod ? a - mod : a) == (b.a >= mod ? b.a - mod : b.a);
}
bool operator!=(const mint &b) const {
return (a >= mod ? a - mod : a) != (b.a >= mod ? b.a - mod : b.a);
}
mint operator-() const { return mint(0) - mint(*this); }
mint operator+() const { return mint(*this); }
mint pow(ULong n) const {
mint ret(1), mul(*this);
while (n > 0) {
if (n & 1) ret *= mul;
mul *= mul, n >>= 1;
}
return ret;
}
friend ostream &operator<<(ostream &os, const mint &b) {
return os << b.get();
}
friend istream &operator>>(istream &is, mint &b) {
Long t;
is >> t;
b = ArbitraryLazyMontgomeryModIntBase(t);
return (is);
}
mint inverse() const {
Int x = get(), y = get_mod(), u = 1, v = 0;
while (y > 0) {
Int t = x / y;
swap(x -= t * y, y);
swap(u -= t * v, v);
}
return mint{u};
}
UInt get() const {
UInt ret = reduce(a);
return ret >= mod ? ret - mod : ret;
}
static UInt get_mod() { return mod; }
};
// id に適当な乱数を割り当てて使う
template <int id>
using ArbitraryLazyMontgomeryModInt =
ArbitraryLazyMontgomeryModIntBase<int, unsigned int, long long,
unsigned long long, id>;
template <int id>
using ArbitraryLazyMontgomeryModInt64bit =
ArbitraryLazyMontgomeryModIntBase<long long, unsigned long long, __int128_t,
__uint128_t, id>;
using mint=ArbitraryLazyMontgomeryModInt<1>;
mint fact[1000010],invfact[1000010],C2[1000010];
unsigned int p;
inline __attribute((always_inline)) mint C(int n,int m){
if(n<0||m<0||n<m) return 0;
return fact[n]*invfact[n-m]*invfact[m];
}
int main(){
#ifndef ONLINE_JUDGE
freopen("name.in", "r", stdin);
freopen("name.out", "w", stdout);
#endif
register int i,lim;
long long n=read();
long long _n=n;
p=read();mint _;_.set_mod(p);
register mint res=1,now;
for(i=1,fact[0]=1;i<p;++i) fact[i]=fact[i-1]*i;
invfact[p-1]=fact[p-1].inverse();
for(i=p-1;i;--i) invfact[i-1]=invfact[i]*i;
for(i=0;i<=p/2;++i) C2[i]=C(i*2,i);
while(n){
lim=n%p;
now=0;
for(i=0;i<=lim/2;++i) now+=C(lim,i*2)*C2[i];
res*=now;
n/=p;
}
res=mint(3).pow(_n)-res;
res*=mint(2).inverse();
print(res.get());
#ifndef ONLINE_JUDGE
fclose(stdin);
fclose(stdout);
#endif
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 15364kb
input:
5 5
output:
1
result:
ok 1 number(s): "1"
Test #2:
score: 0
Accepted
time: 0ms
memory: 15300kb
input:
5 7
output:
5
result:
ok 1 number(s): "5"
Test #3:
score: 0
Accepted
time: 0ms
memory: 15388kb
input:
789 97
output:
53
result:
ok 1 number(s): "53"
Test #4:
score: 0
Accepted
time: 0ms
memory: 15380kb
input:
98 23
output:
10
result:
ok 1 number(s): "10"
Test #5:
score: 0
Accepted
time: 0ms
memory: 15312kb
input:
398 7
output:
4
result:
ok 1 number(s): "4"
Test #6:
score: 0
Accepted
time: 0ms
memory: 15368kb
input:
272 31
output:
18
result:
ok 1 number(s): "18"
Test #7:
score: 0
Accepted
time: 3ms
memory: 15304kb
input:
920 199
output:
39
result:
ok 1 number(s): "39"
Test #8:
score: 0
Accepted
time: 3ms
memory: 15312kb
input:
390 5167
output:
1236
result:
ok 1 number(s): "1236"
Test #9:
score: 0
Accepted
time: 0ms
memory: 15248kb
input:
445 24337
output:
4546
result:
ok 1 number(s): "4546"
Test #10:
score: 0
Accepted
time: 6ms
memory: 15240kb
input:
28 586501
output:
269032
result:
ok 1 number(s): "269032"
Test #11:
score: 0
Accepted
time: 3ms
memory: 15304kb
input:
304 5
output:
0
result:
ok 1 number(s): "0"
Test #12:
score: 0
Accepted
time: 0ms
memory: 15368kb
input:
7158 41
output:
16
result:
ok 1 number(s): "16"
Test #13:
score: 0
Accepted
time: 3ms
memory: 15304kb
input:
8487 331
output:
148
result:
ok 1 number(s): "148"
Test #14:
score: 0
Accepted
time: 0ms
memory: 15312kb
input:
501 6763
output:
363
result:
ok 1 number(s): "363"
Test #15:
score: 0
Accepted
time: 0ms
memory: 15248kb
input:
3011 61129
output:
49319
result:
ok 1 number(s): "49319"
Test #16:
score: 0
Accepted
time: 7ms
memory: 15264kb
input:
7266 358159
output:
7643
result:
ok 1 number(s): "7643"
Test #17:
score: 0
Accepted
time: 0ms
memory: 15304kb
input:
360860 7
output:
1
result:
ok 1 number(s): "1"
Test #18:
score: 0
Accepted
time: 3ms
memory: 15308kb
input:
154939 19
output:
8
result:
ok 1 number(s): "8"
Test #19:
score: 0
Accepted
time: 0ms
memory: 15300kb
input:
813268 47
output:
40
result:
ok 1 number(s): "40"
Test #20:
score: 0
Accepted
time: 0ms
memory: 15368kb
input:
965601 1531
output:
1147
result:
ok 1 number(s): "1147"
Test #21:
score: 0
Accepted
time: 0ms
memory: 15364kb
input:
689332 78079
output:
17208
result:
ok 1 number(s): "17208"
Test #22:
score: 0
Accepted
time: 0ms
memory: 15308kb
input:
287719 34369
output:
15373
result:
ok 1 number(s): "15373"
Test #23:
score: 0
Accepted
time: 0ms
memory: 15304kb
input:
439237583 7
output:
6
result:
ok 1 number(s): "6"
Test #24:
score: 0
Accepted
time: 0ms
memory: 15372kb
input:
319142531 79
output:
21
result:
ok 1 number(s): "21"
Test #25:
score: 0
Accepted
time: 0ms
memory: 15244kb
input:
592330255 631
output:
530
result:
ok 1 number(s): "530"
Test #26:
score: 0
Accepted
time: 0ms
memory: 15304kb
input:
164278463 4517
output:
3038
result:
ok 1 number(s): "3038"
Test #27:
score: 0
Accepted
time: 0ms
memory: 15308kb
input:
753671285 65371
output:
22369
result:
ok 1 number(s): "22369"
Test #28:
score: 0
Accepted
time: 5ms
memory: 15372kb
input:
289665297 663127
output:
168503
result:
ok 1 number(s): "168503"
Test #29:
score: 0
Accepted
time: 0ms
memory: 15340kb
input:
350585676619 7
output:
5
result:
ok 1 number(s): "5"
Test #30:
score: 0
Accepted
time: 3ms
memory: 15372kb
input:
4283693890775 31
output:
1
result:
ok 1 number(s): "1"
Test #31:
score: 0
Accepted
time: 0ms
memory: 15308kb
input:
1234727503131 151
output:
67
result:
ok 1 number(s): "67"
Test #32:
score: 0
Accepted
time: 3ms
memory: 15240kb
input:
9399186742989 1327
output:
678
result:
ok 1 number(s): "678"
Test #33:
score: 0
Accepted
time: 3ms
memory: 15384kb
input:
1409645481800 8719
output:
1939
result:
ok 1 number(s): "1939"
Test #34:
score: 0
Accepted
time: 4ms
memory: 15308kb
input:
3782178721166 552859
output:
458256
result:
ok 1 number(s): "458256"
Test #35:
score: 0
Accepted
time: 3ms
memory: 15284kb
input:
827235816874722972 5
output:
2
result:
ok 1 number(s): "2"
Test #36:
score: 0
Accepted
time: 0ms
memory: 15316kb
input:
648249960855836992 61
output:
56
result:
ok 1 number(s): "56"
Test #37:
score: 0
Accepted
time: 3ms
memory: 15244kb
input:
467138518199434341 563
output:
202
result:
ok 1 number(s): "202"
Test #38:
score: 0
Accepted
time: 4ms
memory: 15368kb
input:
616207494477524619 653
output:
95
result:
ok 1 number(s): "95"
Test #39:
score: 0
Accepted
time: 0ms
memory: 15312kb
input:
129689263663821851 34403
output:
30422
result:
ok 1 number(s): "30422"
Test #40:
score: 0
Accepted
time: 8ms
memory: 15224kb
input:
605206340470214709 479971
output:
381982
result:
ok 1 number(s): "381982"