結果

問題 No.1303 Inconvenient Kingdom
ユーザー LayCurse
提出日時 2020-11-27 22:51:19
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 383 ms / 3,000 ms
コード長 16,716 bytes
コンパイル時間 3,289 ms
コンパイル使用メモリ 227,516 KB
最終ジャッジ日時 2025-01-16 08:12:39
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 34
権限があれば一括ダウンロードができます
コンパイルメッセージ
In member function ‘void Polynomial<T>::change(int, T) [with T = Modint]’,
    inlined from ‘Polynomial<T> polationPoly_L(int, T*, T*) [with T = Modint]’ at main.cpp:655:14:
main.cpp:370:7: warning: ‘void* __builtin_memset(void*, int, long unsigned int)’ writing between 24 and 8589934580 bytes into a region of size 0 overflows the destination [-Wstringop-overflow=]
  370 |       c[++d] = 0;
      |       ^
In member function ‘void Polynomial<T>::expand(int) [with T = Modint]’,
    inlined from ‘void Polynomial<T>::change(int, T) [with T = Modint]’ at main.cpp:368:11,
    inlined from ‘Polynomial<T> polationPoly_L(int, T*, T*) [with T = Modint]’ at main.cpp:653:12:
main.cpp:360:10: note: at offset [-8589934576, -20] into destination object of size 8 allocated by ‘operator new []’
  360 |     cc = new T[z];
      |          ^~~~~~~~
In member function ‘void Polynomial<T>::change(int, T) [with T = Modint]’,
    inlined from ‘Polynomial<T> polationPoly_L(int, T*, T*) [with T = Modint]’ at main.cpp:666:14:
main.cpp:370:7: warning: ‘void* __builtin_memset(void*, int, long unsigned int)’ writing between 20 and 8589934580 bytes into a region of size 0 overflows the destination [-Wstringop-overflow=]
  370 |       c[++d] = 0;
      |       ^
In member function ‘void Polynomial<T>::expand(int) [with T = Modint]’,
    inlined from ‘void Polynomial<T>::change(int, T) [with T = Modint]’ at main.cpp:368:11,
    inlined from ‘Polynomial<T> polationPoly_L(int, T*, T*) [with T = Modint]’ at main.cpp:653:12:
main.cpp:360:10: note: at offset [-8589934576, -16] into destination object of size 8 allocated by ‘operator new []’
  360 |     cc = new T[z];
      |          ^~~~~~~~

ソースコード

diff #
プレゼンテーションモードにする

#pragma GCC optimize ("Ofast")
#include<bits/stdc++.h>
using namespace std;
#define MD (998244353U)
void*wmem;
char memarr[96000000];
template<class S, class T> inline S max_L(S a,T b){
return a>=b?a:b;
}
template<class T> inline void walloc1d(T **arr, int x, void **mem = &wmem){
static int skip[16] = {0, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
(*mem) = (void*)( ((char*)(*mem)) + skip[((unsigned long long)(*mem)) & 15] );
(*arr)=(T*)(*mem);
(*mem)=((*arr)+x);
}
template<class T> inline void walloc1d(T **arr, int x1, int x2, void **mem = &wmem){
walloc1d(arr, x2-x1, mem);
(*arr) -= x1;
}
template<class T1> void sortA_L(int N, T1 a[], void *mem = wmem){
sort(a, a+N);
}
struct Modint{
unsigned val;
Modint(){
val=0;
}
Modint(int a){
val = ord(a);
}
Modint(unsigned a){
val = ord(a);
}
Modint(long long a){
val = ord(a);
}
Modint(unsigned long long a){
val = ord(a);
}
inline unsigned ord(unsigned a){
return a%MD;
}
inline unsigned ord(int a){
a %= (int)MD;
if(a < 0){
a += MD;
}
return a;
}
inline unsigned ord(unsigned long long a){
return a%MD;
}
inline unsigned ord(long long a){
a %= (int)MD;
if(a < 0){
a += MD;
}
return a;
}
inline unsigned get(){
return val;
}
inline Modint &operator+=(Modint a){
val += a.val;
if(val >= MD){
val -= MD;
}
return *this;
}
inline Modint &operator-=(Modint a){
if(val < a.val){
val = val + MD - a.val;
}
else{
val -= a.val;
}
return *this;
}
inline Modint &operator*=(Modint a){
val = ((unsigned long long)val*a.val)%MD;
return *this;
}
inline Modint &operator/=(Modint a){
return *this *= a.inverse();
}
inline Modint operator+(Modint a){
return Modint(*this)+=a;
}
inline Modint operator-(Modint a){
return Modint(*this)-=a;
}
inline Modint operator*(Modint a){
return Modint(*this)*=a;
}
inline Modint operator/(Modint a){
return Modint(*this)/=a;
}
inline Modint operator+(int a){
return Modint(*this)+=Modint(a);
}
inline Modint operator-(int a){
return Modint(*this)-=Modint(a);
}
inline Modint operator*(int a){
return Modint(*this)*=Modint(a);
}
inline Modint operator/(int a){
return Modint(*this)/=Modint(a);
}
inline Modint operator+(long long a){
return Modint(*this)+=Modint(a);
}
inline Modint operator-(long long a){
return Modint(*this)-=Modint(a);
}
inline Modint operator*(long long a){
return Modint(*this)*=Modint(a);
}
inline Modint operator/(long long a){
return Modint(*this)/=Modint(a);
}
inline Modint operator-(void){
Modint res;
if(val){
res.val=MD-val;
}
else{
res.val=0;
}
return res;
}
inline operator bool(void){
return val!=0;
}
inline operator int(void){
return get();
}
inline operator long long(void){
return get();
}
inline Modint inverse(){
int a = val;
int b = MD;
int u = 1;
int v = 0;
int t;
Modint res;
while(b){
t = a / b;
a -= t * b;
swap(a, b);
u -= t * v;
swap(u, v);
}
if(u < 0){
u += MD;
}
res.val = u;
return res;
}
inline Modint pw(unsigned long long b){
Modint a(*this);
Modint res;
res.val = 1;
while(b){
if(b&1){
res *= a;
}
b >>= 1;
a *= a;
}
return res;
}
inline bool operator==(int a){
return ord(a)==val;
}
inline bool operator!=(int a){
return ord(a)!=val;
}
}
;
inline Modint operator+(int a, Modint b){
return Modint(a)+=b;
}
inline Modint operator-(int a, Modint b){
return Modint(a)-=b;
}
inline Modint operator*(int a, Modint b){
return Modint(a)*=b;
}
inline Modint operator/(int a, Modint b){
return Modint(a)/=b;
}
inline Modint operator+(long long a, Modint b){
return Modint(a)+=b;
}
inline Modint operator-(long long a, Modint b){
return Modint(a)-=b;
}
inline Modint operator*(long long a, Modint b){
return Modint(a)*=b;
}
inline Modint operator/(long long a, Modint b){
return Modint(a)/=b;
}
inline int my_getchar_unlocked(){
static char buf[1048576];
static int s = 1048576;
static int e = 1048576;
if(s == e && e == 1048576){
e = fread_unlocked(buf, 1, 1048576, stdin);
s = 0;
}
if(s == e){
return EOF;
}
return buf[s++];
}
inline void rd(int &x){
int k;
int m=0;
x=0;
for(;;){
k = my_getchar_unlocked();
if(k=='-'){
m=1;
break;
}
if('0'<=k&&k<='9'){
x=k-'0';
break;
}
}
for(;;){
k = my_getchar_unlocked();
if(k<'0'||k>'9'){
break;
}
x=x*10+k-'0';
}
if(m){
x=-x;
}
}
inline int rd_int(void){
int x;
rd(x);
return x;
}
struct MY_WRITER{
char buf[1048576];
int s;
int e;
MY_WRITER(){
s = 0;
e = 1048576;
}
~MY_WRITER(){
if(s){
fwrite_unlocked(buf, 1, s, stdout);
}
}
}
;
MY_WRITER MY_WRITER_VAR;
void my_putchar_unlocked(int a){
if(MY_WRITER_VAR.s == MY_WRITER_VAR.e){
fwrite_unlocked(MY_WRITER_VAR.buf, 1, MY_WRITER_VAR.s, stdout);
MY_WRITER_VAR.s = 0;
}
MY_WRITER_VAR.buf[MY_WRITER_VAR.s++] = a;
}
inline void wt_L(char a){
my_putchar_unlocked(a);
}
inline void wt_L(int x){
int s=0;
int m=0;
char f[10];
if(x<0){
m=1;
x=-x;
}
while(x){
f[s++]=x%10;
x/=10;
}
if(!s){
f[s++]=0;
}
if(m){
my_putchar_unlocked('-');
}
while(s--){
my_putchar_unlocked(f[s]+'0');
}
}
inline void wt_L(long long x){
int s=0;
int m=0;
char f[20];
if(x<0){
m=1;
x=-x;
}
while(x){
f[s++]=x%10;
x/=10;
}
if(!s){
f[s++]=0;
}
if(m){
my_putchar_unlocked('-');
}
while(s--){
my_putchar_unlocked(f[s]+'0');
}
}
inline void wt_L(Modint x){
int i;
i = (int)x;
wt_L(i);
}
template<class S, class T> inline S chmax(S &a, T b){
if(a<b){
a=b;
}
return a;
}
template<class T> struct Polynomial{
int d;
int mem;
T*c;
Polynomial(){
mem = 1;
c = new T[mem];
d = 0;
c[0] = 0;
}
Polynomial(const Polynomial<T> &a){
int i;
d = a.d;
mem = d + 1;
c = new T[mem];
for(i=(0);i<(d+1);i++){
c[i] = a.c[i];
}
}
~Polynomial(){
delete [] c;
}
void expand(int z){
int i;
T*cc;
if(z <= mem){
return;
}
mem = (chmax(z, 2 * mem));
cc = new T[z];
for(i=(0);i<(d+1);i++){
cc[i] = c[i];
}
delete [] c;
c = cc;
}
inline void change(const int dg, const T cf){
expand(dg+1);
while(d < dg){
c[++d] = 0;
}
c[dg] = cf;
while(d && c[d]==0){
d--;
}
}
inline int deg(void){
return d;
}
inline T coef(const int k){
if(k > d){
return 0;
}
return c[k];
}
Polynomial<T>& operator=(const Polynomial<T> &a){
int i;
d = a.d;
mem = d + 1;
c = new T[mem];
for(i=(0);i<(d+1);i++){
c[i] = a.c[i];
}
return *this;
}
Polynomial<T>& operator+=(const Polynomial<T> &a){
int i;
int k;
k =max_L(d, a.d);
expand(k+1);
while(d < k){
c[++d] = 0;
}
for(i=(0);i<(a.d+1);i++){
c[i] += a.c[i];
}
while(d && c[d]==0){
d--;
}
return *this;
}
Polynomial<T> operator+(const Polynomial<T> &a){
return Polynomial<T>(*this) += a;
}
Polynomial<T>& operator-=(const Polynomial<T> &a){
int i;
int k;
k =max_L(d, a.d);
expand(k+1);
while(d < k){
c[++d] = 0;
}
for(i=(0);i<(a.d+1);i++){
c[i] -= a.c[i];
}
while(d && c[d]==0){
d--;
}
return *this;
}
Polynomial<T> operator-(const Polynomial<T> &a){
return Polynomial<T>(*this) -= a;
}
Polynomial<T>& operator*=(const Polynomial<T> &a){
int i;
int j;
int k;
T*cc;
void*mem = wmem;
k = d + a.d;
expand(k+1);
walloc1d(&cc, k+1, &mem);
for(i=(0);i<(k+1);i++){
cc[i] = 0;
}
for(i=(0);i<(d+1);i++){
for(j=(0);j<(a.d+1);j++){
cc[i+j] += c[i] * a.c[j];
}
}
for(i=(0);i<(k+1);i++){
c[i] = cc[i];
}
d = k;
while(d && c[d]==0){
d--;
}
return *this;
}
Polynomial<T> operator*(const Polynomial<T> &a){
return Polynomial<T>(*this) *= a;
}
Polynomial<T>& operator/=(const Polynomial<T> &a){
int i;
int j;
int k;
T*cc;
T e;
void*mem = wmem;
walloc1d(&cc, d-a.d, &mem);
for(i=d; i>=a.d; i--){
cc[i-a.d] = e = c[i] / a.c[a.d];
for(j=(0);j<(a.d+1);j++){
c[i-j] -= e * a.c[a.d-j];
}
}
d -= a.d;
for(i=(0);i<(d+1);i++){
c[i] = cc[i];
}
return *this;
}
Polynomial<T> operator/(const Polynomial<T> &a){
return Polynomial<T>(*this) /= a;
}
Polynomial<T>& operator%=(const Polynomial<T> &a){
int i;
int j;
int k;
T*cc;
T e;
void*mem = wmem;
walloc1d(&cc, d-a.d, &mem);
for(i=d; i>=a.d; i--){
cc[i-a.d] = e = c[i] / a.c[a.d];
for(j=(0);j<(a.d+1);j++){
c[i-j] -= e * a.c[a.d-j];
}
}
while(d && c[d]==0){
d--;
}
return *this;
}
Polynomial<T> operator%(const Polynomial<T> &a){
return Polynomial<T>(*this) %= a;
}
Polynomial<T>& operator*=(const T &a){
int i;
for(i=(0);i<(d+1);i++){
c[i] *= a;
}
while(d && c[d]==0){
d--;
}
return *this;
}
Polynomial<T> operator*(const T &a){
return Polynomial<T>(*this) *= a;
}
Polynomial<T>& operator/=(const T &a){
int i;
for(i=(0);i<(d+1);i++){
c[i] /= a;
}
while(d && c[d]==0){
d--;
}
return *this;
}
Polynomial<T> operator/(const T &a){
return Polynomial<T>(*this) /= a;
}
inline T operator()(const T x){
int i;
T res;
res = 0;
for(i=d;i>=0;i--){
res = res * x + c[i];
}
return res;
}
}
;
template<class T> Polynomial<T> operator*(const T a, const Polynomial<T> &b){
return Polynomial<T>(b)*=a;
}
struct unionFind{
int*d;
int N;
int M;
inline void malloc(const int n){
d = (int*)std::malloc(n*sizeof(int));
M = n;
}
inline void malloc(const int n, const int fg){
d = (int*)std::malloc(n*sizeof(int));
M = n;
if(fg){
init(n);
}
}
inline void free(void){
std::free(d);
}
inline void walloc(const int n, void **mem=&wmem){
walloc1d(&d, n, mem);
M = n;
}
inline void walloc(const int n, const int fg, void **mem=&wmem){
walloc1d(&d, n, mem);
M = n;
if(fg){
init(n);
}
}
inline void init(const int n){
int i;
N = n;
for(i=(0);i<(n);i++){
d[i] = -1;
}
}
inline void init(void){
init(M);
}
inline int get(int a){
int t = a;
int k;
while(d[t]>=0){
t=d[t];
}
while(d[a]>=0){
k=d[a];
d[a]=t;
a=k;
}
return a;
}
inline int connect(int a, int b){
if(d[a]>=0){
a=get(a);
}
if(d[b]>=0){
b=get(b);
}
if(a==b){
return 0;
}
if(d[a] < d[b]){
d[a] += d[b];
d[b] = a;
}
else{
d[b] += d[a];
d[a] = b;
}
return 1;
}
inline int operator()(int a){
return get(a);
}
inline int operator()(int a, int b){
return connect(a,b);
}
inline int& operator[](const int a){
return d[a];
}
inline int size(int a){
a = get(a);
return -d[a];
}
inline int sizeList(int res[]){
int i;
int sz=0;
for(i=(0);i<(N);i++){
if(d[i]<0){
res[sz++] = -d[i];
}
}
return sz;
}
}
;
template<class T> Polynomial<T> polationPoly_L(int n, T x[], T y[]){
int i;
int j;
T c;
Polynomial<T> res;
Polynomial<T> tmp;
Polynomial<T> t1;
tmp.change(0, 1);
t1.change(1, 1);
for(i=(0);i<(n);i++){
t1.change(0, -x[i]);
tmp *= t1;
}
for(i=(0);i<(n);i++){
c = 1;
for(j=(0);j<(n);j++){
if(j!=i){
c *= (x[i] - x[j]);
}
}
c = y[i] / c;
t1.change(0, -x[i]);
res += c * tmp / t1;
}
return res;
}
int N;
int mat[102][102];
int n;
Modint m[102][102];
int sz;
int lis[100];
Modint calc_det(void){
int i;
int j;
int k;
Modint res = 1;
Modint t;
for(k=(0);k<(n);k++){
for(i=(k);i<(n);i++){
if(m[i][k] != 0){
break;
}
}
if(i == n){
return 0;
}
if(i != k){
for(j=(k);j<(n);j++){
swap(m[k][j], m[i][j]);
}
}
for(i=(k+1);i<(n);i++){
t = m[i][k] / m[k][k];
for(j=(k);j<(n);j++){
m[i][j] -= t * m[k][j];
}
}
}
for(i=(0);i<(n);i++){
res *= m[i][i];
}
return res;
}
int main(){
int tU__gIr_;
wmem = memarr;
int i;
int j;
int k;
int x;
int y;
long long c;
unionFind uf;
long long res1 = 0;
Modint res2 = 1;
Modint tmp;
Modint xx[105];
Modint arr[105];
Polynomial<Modint> f;
rd(N);
uf.walloc(N,1);
int a2conNHc = rd_int();
for(tU__gIr_=(0);tU__gIr_<(a2conNHc);tU__gIr_++){
rd(i);i += (-1);
rd(j);j += (-1);
mat[i][j] = mat[j][i] = 1;
uf(i,j);
}
for(k=(0);k<(N);k++){
n = 0;
for(i=(0);i<(N);i++){
if(uf(i) == k){
lis[n++] = i;
}
}
if(n<=1){
continue;
}
for(i=(0);i<(n);i++){
for(j=(0);j<(n);j++){
m[i][j] = -mat[lis[i]][lis[j]];
}
}
for(i=(0);i<(n);i++){
for(j=(0);j<(n);j++){
if(j!=i){
m[i][i] -= m[i][j];
}
}
}
n--;
res2 *= calc_det();
}
sz = uf.sizeList(lis);
sortA_L(sz, lis);
if(sz >= 2){
x = lis[sz-2];
y = lis[sz-1];
c = 0;
for(i=(0);i<(sz);i++){
for(j=(i+1);j<(sz);j++){
if(lis[i]==x && lis[j]==y){
c++;
}
}
}
res2 *= c *x * y;
lis[sz-2] = x + y;
sz--;
}
else{
tmp = res2;
n = N;
for(k=(0);k<(n+3);k++){
for(x=(0);x<(n);x++){
for(y=(0);y<(n);y++){
m[x][y] = -mat[x][y];
}
}
for(x=(0);x<(n);x++){
for(y=(0);y<(n);y++){
if(x!=y && mat[x][y]==0){
m[x][y] = -k;
}
}
}
for(x=(0);x<(n);x++){
for(y=(0);y<(n);y++){
if(x!=y){
m[x][x] -= m[x][y];
}
}
}
n--;
xx[k] = k;
arr[k] = calc_det();
n++;
}
f =polationPoly_L(n+3, xx, arr);
f =polationPoly_L(n+3, xx, arr);
res2 = f.coef(1) + f.coef(0);
}
for(i=(0);i<(sz);i++){
res1 += (N - lis[i]) * lis[i];
}
wt_L(res1);
wt_L('\n');
wt_L(res2);
wt_L('\n');
return 0;
}
// cLay version 20201123-1
// --- original code ---
// #define MD 998244353
// int N, mat[102][102];
//
// int n;
// Modint m[102][102];
//
// int sz, lis[100];
//
// Modint calc_det(void){
// int i, j, k;
// Modint res = 1, t;
//
// // wt("---");
// // wt(m(n,n));
//
// rep(k,n){
// rep(i,k,n) if(m[i][k] != 0) break;
// if(i == n) return 0;
// if(i != k){
// rep(j,k,n) swap(m[k][j], m[i][j]);
// }
// rep(i,k+1,n){
// t = m[i][k] / m[k][k];
// rep(j,k,n) m[i][j] -= t * m[k][j];
// }
// }
//
// // wt("+++");
// // wt(m(n,n));
// rep(i,n) res *= m[i][i];
// // wt("res",res);
// return res;
// }
//
// {
// int i, j, k, x, y;
// ll c;
// unionFind uf;
// ll res1 = 0;
// Modint res2 = 1, tmp;
// Modint xx[105], arr[105];
// Polynomial<Modint> f;
//
// rd(N);
// uf.walloc(N,1);
// REP(rd_int()){
// rd(i--, j--);
// mat[i][j] = mat[j][i] = 1;
// uf(i,j);
// }
//
// rep(k,N){
// n = 0;
// rep(i,N) if(uf(i) == k) lis[n++] = i;
// if(n<=1) continue;
// rep(i,n) rep(j,n) m[i][j] = -mat[lis[i]][lis[j]];
// rep(i,n) rep(j,n) if(j!=i) m[i][i] -= m[i][j];
// n--;
// res2 *= calc_det();
// }
//
// sz = uf.sizeList(lis);
// sortA(sz, lis);
// if(sz >= 2){
// x = lis[sz-2];
// y = lis[sz-1];
// c = 0;
// rep(i,sz) rep(j,i+1,sz) if(lis[i]==x && lis[j]==y) c++;
// res2 *= c *x * y;
//
// lis[sz-2] = x + y;
// sz--;
// } else {
// tmp = res2;
// n = N;
// rep(k,n+3){
// rep(x,n) rep(y,n) m[x][y] = -mat[x][y];
// rep(x,n) rep(y,n) if(x!=y && mat[x][y]==0) m[x][y] = -k;
// rep(x,n) rep(y,n) if(x!=y) m[x][x] -= m[x][y];
// n--;
// xx[k] = k;
// arr[k] = calc_det();
// n++;
// }
// f = polationPoly(n+3, xx, arr);
// // wt(res2);
// // rep(k,n+3) wt(k,arr[k],f.coef(k));
// f = polationPoly(n+3, xx, arr);
// res2 = f.coef(1) + f.coef(0);
// }
// rep(i,sz) res1 += (N - lis[i]) * lis[i];
//
// wtLn(res1, res2);
// }
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0