Codeforces 997 C - Sky Full of Stars

时间:2023-03-08 17:50:53

C - Sky Full of Stars

思路:

容斥原理

题解:http://codeforces.com/blog/entry/60357

注意当i > 1 且 j > 1,是同一种颜色

代码:

#include<iostream>
#include<cstdio>
#include<queue>
#include<deque>
#include<set>
#include<cstring>
using namespace std;
#define fi first
#define se second
#define pi acos(-1.0)
#define LL long long
#define mp make_pair
#define pb push_back
#define ls rt<<1, l, m
#define rs rt<<1|1, m+1, r
#define ULL unsigned LL
#define pll pair<LL, LL>
#define pii pair<int, int>
#define mem(a, b) memset(a, b, sizeof(a))
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define fopen freopen("in.txt", "r", stdin);freopen("out.txt", "w", stout);
//head const int MOD = ; LL q_pow(LL n, LL k) {
LL ans = ;
while(k) {
if(k&) ans = (ans * n) % MOD;
n = (n*n) % MOD;
k >>= ;
}
return ans;
}
int main() {
int n;
scanf("%d", &n);
LL res = , t = , sign = -;
for (int i = ; i <= n; i++) {
t = (t * (n-i+)) % MOD;
t = (t * q_pow(i, MOD - )) % MOD;
sign = -sign;
LL tt = q_pow(, 1LL*n*(n-i)+i);
res = (res + tt*t*sign) % MOD;
}
res = (res * ) % MOD;
res = (res + MOD) % MOD;
LL ans = ;
t = , sign = -;
for (int i = ; i < n; i++) {
LL tt = q_pow( - q_pow(, i), n);
tt = (tt - q_pow( - q_pow(, i), n)) % MOD;
ans = (ans + tt*t*sign) % MOD;
t = (t * (n-i)) % MOD;
t = (t * q_pow(i+, MOD-)) % MOD;
sign = -sign;
}
ans = (ans * ) % MOD;
ans = (ans + MOD) % MOD;
printf("%lld\n", (res + ans) % MOD);
return ;
}