D1T1 煎蛋的疑惑excatalan
Input 1
2 1
Output 1
3
Input 2
9 3
Output 2
9996
Input 3
996 223
Output 3
361421692
Input 4
514223 0
Output 4
287888483
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const ll mod = 998244353;
const int N = 2e6+10;
ll M(ll a) {return a%mod;}
int n, m;
ll fac[N], inv[N];
ll Pow(ll a, ll b) {ll c=1;while(b){if(b&1) c=M(c*a);b>>=1;a=M(a*a);}return c;}
ll C(ll x, ll y) {
if(x<y || x<0 || y<0) return 0;
return M(M(fac[x]*inv[y])*inv[x-y]);
}
ll Ca(ll a, ll b) {
if(b<0) return 0;
return M(C(a<<1, a) - C((a<<1), a-b-1) + mod);
}
void work() {
fac[0] = 1;
for(int i = 1; i <= (n<<1); i++)
fac[i] = M(fac[i-1]*i);
inv[n<<1] = Pow(fac[n<<1], mod-2);
for(int i = (n<<1); i; i--)
inv[i-1] = M(inv[i]*i);
}
int main() {
scanf("%d%d", &n, &m);
work();
printf("%lld\n", M(Ca(n,m)-Ca(n,m-1)+mod));
return 0;
}
D1T2 蘑菇shimeji
Input 1
5
1 2
1 3
2 4
2 5
Output 1
748683268
Input 2
10
1 2
2 3
1 4
2 5
4 6
6 7
3 8
3 9
4 10
Output 2
132579339
Input 3
514
每一条边都是i与i+1相连(0<i<514)
Output 2
831885280
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const ll mod = 998244353;
const int N = 1e6+5;
struct psx{int y, next;} e[N<<1];
int lin[N], len=0, n;
ll f[N];//该点所在连通块被选择
ll g[N];//该点所在连通块没有被选择
ll M(ll a){return a%mod;}//取模
ll add(ll a, ll b) {return M(a+b);}//加法
ll Pow(ll a, ll b) {ll c=1;while(b){if(b&1) c=M(c*a); b>>=1; a=M(a*a);}return c;}//快速幂
void insert(int xx, int yy) {
e[++len].next = lin[xx];
lin[xx] = len;
e[len].y = yy;
}
void dfs(int k, int fa) {
f[k] = g[k] = 1;//本身
for(int i = lin[k]; i; i = e[i].next) {
int y = e[i].y;
if(y == fa) continue;
dfs(y, k);
f[k] = add(f[k]*g[y],g[k]*f[y]);//k被选择,可以由选择y和选择k的其他儿子(或本身)两种情况传递
g[k] = M(g[k]*g[y]);//k不被选择,那么儿子y必然不被选择
}
g[k] = add(g[k], f[k]);
//如果k与fa[k]的边断了,相当于不选择k
//如果没断,那么选择k就可以传递到选择fa[k]
}
int main() {
scanf("%d", &n);
for(int i = 1; i < n; i++) {
int u, v;
scanf("%d%d", &u, &v);
insert(u, v);
insert(v, u);
}
dfs(1, 0);
printf("%lld\n", M(f[1]*Pow((mod+1)/2,n-1)));
//因为所求为期望,所以还需要乘2^(n-1)
//(mod+1)/2为模mod意义下2的逆元
return 0;
}
D1T3 墙wall
Input 1
3 514223
2 1 3
Output 1
4
Input 2
8 514223
1 5 6 3 4 8 7 2
Output 2
32
Input 3
223 514223
墙的排列为1~223
Output 3
223
#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll mod, ans=0, a, n;
ll f1[6005], f2[6005], p[6005];
ll M(ll a) {return a%mod;}
void add(ll &a, ll b){a = M(a+b);}
void solve() {
for(int i = 1; i <= n; i++)
for(int j = i-1; j >= 1; j--)
if(p[i] < p[j]) add(f2[j], f1[i]);
else add(f1[i], f2[j]);
}
int main() {
scanf("%lld%lld", &n, &mod);
for(int i = 1; i <= n; i++) {
scanf("%lld", &a);
p[a] = i;
}
for(int i = 1; i <= n; i++)
f1[i] = 1, f2[i] = 0;
solve();
for(int i = 1; i <= n; i++)
add(ans, f1[i]);
for(int i = 1; i <= n; i++)
f1[i] = 0, f2[i] = 1;
solve();
for(int i = 1; i <= n; i++)
add(ans, f2[i]);
//由样例可知,单点有贡献,以上两种情况单点记录两次,要减去
add(ans, mod-n);
printf("%lld\n", M(ans+mod));
return 0;
}
D2T1 a
Input 1
5
1
3
5
7
11
Output 1
2
554580199
522281449
849291288
19307947
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 1e7+10;
const ll mod = 998244353;
ll M(ll a) { return a%mod; }
int q;
ll f[N];
快读dread()快输print()略
ll Pow(ll a,ll b){ll c=1;while(b){if(b&1)c=M(c*a);b>>=1;a=M(a*a);}return c;}
int main() {
q = dread();
ll ans=1;
while( q-- ) {
int n = dread();
ll k = Pow(n, mod-2);
f[n] = 1;
for(int i = n-1; i>=0; i--)
f[i] = M( M(f[i+1]*(n-i))*k+1 );
print(f[0]);
}
return 0;
}
D2T2 b
Input 1
1
9
1 2 2
2 3 2
3 4 6
3 8 4
2 5 5
5 6 2
2 7 6
1 9 3
4
2 5
3 8
1 7
1 10
Output 1
1
-1
2
3
D2T3 c
Input 1
3
2 3
4 5
6 7
Output 1
1
6
960