結果

問題 No.1646 Avoid Palindrome
ユーザー tailstails
提出日時 2021-08-16 19:23:44
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 106 ms / 3,000 ms
コード長 4,732 bytes
コンパイル時間 610 ms
コンパイル使用メモリ 38,400 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-26 03:53:28
合計ジャッジ時間 4,256 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,812 KB
testcase_01 AC 1 ms
6,940 KB
testcase_02 AC 1 ms
6,944 KB
testcase_03 AC 1 ms
6,940 KB
testcase_04 AC 99 ms
6,940 KB
testcase_05 AC 99 ms
6,940 KB
testcase_06 AC 97 ms
6,940 KB
testcase_07 AC 100 ms
6,944 KB
testcase_08 AC 102 ms
6,940 KB
testcase_09 AC 96 ms
6,944 KB
testcase_10 AC 99 ms
6,940 KB
testcase_11 AC 96 ms
6,944 KB
testcase_12 AC 100 ms
6,944 KB
testcase_13 AC 100 ms
6,944 KB
testcase_14 AC 51 ms
6,944 KB
testcase_15 AC 49 ms
6,944 KB
testcase_16 AC 38 ms
6,944 KB
testcase_17 AC 46 ms
6,940 KB
testcase_18 AC 47 ms
6,940 KB
testcase_19 AC 58 ms
6,940 KB
testcase_20 AC 40 ms
6,944 KB
testcase_21 AC 46 ms
6,940 KB
testcase_22 AC 49 ms
6,944 KB
testcase_23 AC 57 ms
6,944 KB
testcase_24 AC 58 ms
6,944 KB
testcase_25 AC 44 ms
6,940 KB
testcase_26 AC 44 ms
6,940 KB
testcase_27 AC 50 ms
6,940 KB
testcase_28 AC 54 ms
6,940 KB
testcase_29 AC 104 ms
6,944 KB
testcase_30 AC 102 ms
6,940 KB
testcase_31 AC 106 ms
6,944 KB
testcase_32 AC 104 ms
6,940 KB
testcase_33 AC 106 ms
6,944 KB
testcase_34 AC 2 ms
6,944 KB
testcase_35 AC 1 ms
6,940 KB
testcase_36 AC 1 ms
6,944 KB
testcase_37 AC 1 ms
6,944 KB
testcase_38 AC 0 ms
6,940 KB
testcase_39 AC 1 ms
6,940 KB
testcase_40 AC 1 ms
6,944 KB
testcase_41 AC 1 ms
6,940 KB
testcase_42 AC 1 ms
6,944 KB
testcase_43 AC 105 ms
6,940 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.c: In function 'm_zero':
main.c:30:9: warning: implicit declaration of function 'memset' [-Wimplicit-function-declaration]
   30 |         memset(&r,0,sizeof r);
      |         ^~~~~~
main.c:1:1: note: include '<string.h>' or provide a declaration of 'memset'
  +++ |+#include <string.h>
    1 | #pragma GCC optimize("Ofast")
main.c:30:9: warning: incompatible implicit declaration of built-in function 'memset' [-Wbuiltin-declaration-mismatch]
   30 |         memset(&r,0,sizeof r);
      |         ^~~~~~
main.c:30:9: note: include '<string.h>' or provide a declaration of 'memset'
main.c: At top level:
main.c:145:1: warning: return type defaults to 'int' [-Wimplicit-int]
  145 | main(){
      | ^~~~
main.c: In function 'main':
main.c:150:25: warning: implicit declaration of function 'write' [-Wimplicit-function-declaration]
  150 |                         write(1,"26",2);
      |                         ^~~~~
main.c:154:17: warning: implicit declaration of function '_exit'; did you mean '_Exit'? [-Wimplicit-function-declaration]
  154 |                 _exit(0);
      |                 ^~~~~
      |                 _Exit

ソースコード

diff #

#pragma GCC optimize("Ofast")
#pragma GCC target("avx2")

char*mmap();

#define rd(v) long v=0;{long _c;while(_c=*rp++-48,_c>=0)v=v*10+_c;}
#define wt(v) {long _z=v;do*--wp=_z%10+48;while(_z/=10);}
#define rep(v,e) for(long v=0;v<e;++v)

#define MD 998244353

typedef long D[26][26];
D d1,d2;

void nextd(){
	for(int j=0;j<26;++j){
		for(int i=0;i<26;++i){
			d1[j][i]=d2[j][i]%MD;
			d2[j][i]=0;
		}
	}
}

typedef struct {
	long v[2][2][2][2]; // [i==a][i==b][j==a][j==b]
} M;

static M m_zero(){
	M r;
	memset(&r,0,sizeof r);
	return r;
}

static M m_mul(M a,M b){
	M r=m_zero();
	r.v[0][0][0][0]=(((
		a.v[0][0][0][0]*b.v[0][0][0][0]+
	0)%MD*21+
		a.v[0][0][0][0]*b.v[0][0][0][1]+
		a.v[0][0][0][0]*b.v[0][0][1][0]+
		a.v[0][0][0][0]*b.v[0][1][0][0]+
		a.v[0][0][0][0]*b.v[1][0][0][0]+
		a.v[0][0][0][1]*b.v[0][0][0][0]+
		a.v[0][0][1][0]*b.v[0][0][0][0]+
		a.v[0][1][0][0]*b.v[0][0][0][0]+
		a.v[1][0][0][0]*b.v[0][0][0][0]+
	0)%MD*22+
		a.v[0][0][0][0]*b.v[0][1][1][0]+
		a.v[0][0][0][0]*b.v[1][0][0][1]+
		a.v[0][0][0][1]*b.v[0][0][1][0]+
		a.v[0][0][0][1]*b.v[1][0][0][0]+
		a.v[0][0][1][0]*b.v[0][0][1][0]+
		a.v[0][0][1][0]*b.v[1][0][0][0]+
		a.v[0][1][0][0]*b.v[0][0][0][1]+
		a.v[0][1][0][0]*b.v[0][1][0][0]+
		a.v[0][1][1][0]*b.v[0][0][0][0]+
		a.v[1][0][0][0]*b.v[0][0][0][1]+
		a.v[1][0][0][0]*b.v[0][1][0][0]+
		a.v[1][0][0][1]*b.v[0][0][0][0]+
	0)%MD;

	r.v[0][0][0][1]=(((
		a.v[0][0][0][0]*b.v[0][0][0][0]+
	0)%MD*22+
		a.v[0][0][0][0]*b.v[0][1][0][0]+
		a.v[0][0][0][0]*b.v[1][0][0][0]+
		a.v[0][0][0][1]*b.v[0][0][0][1]+
		a.v[0][0][1][0]*b.v[0][0][0][0]+
		a.v[0][1][0][0]*b.v[0][0][1][0]+
		a.v[1][0][0][0]*b.v[0][0][0][0]+
	0)%MD*23+
		a.v[0][0][0][1]*b.v[1][0][0][1]+
		a.v[0][0][1][0]*b.v[1][0][0][0]+
		a.v[0][1][0][0]*b.v[0][1][1][0]+
		a.v[0][1][1][0]*b.v[0][0][1][0]+
		a.v[1][0][0][0]*b.v[0][1][0][0]+
		a.v[1][0][0][1]*b.v[0][0][0][1]+
	0)%MD;

	r.v[0][0][1][0]=(((
		a.v[0][0][0][0]*b.v[0][0][0][0]+
	0)%MD*22+
		a.v[0][0][0][0]*b.v[0][1][0][0]+
		a.v[0][0][0][0]*b.v[1][0][0][0]+
		a.v[0][0][0][1]*b.v[0][0][0][0]+
		a.v[0][0][1][0]*b.v[0][0][0][1]+
		a.v[0][1][0][0]*b.v[0][0][0][0]+
		a.v[1][0][0][0]*b.v[0][0][1][0]+
	0)%MD*23+
		a.v[0][0][0][1]*b.v[1][0][0][0]+
		a.v[0][0][1][0]*b.v[1][0][0][1]+
		a.v[0][1][0][0]*b.v[0][1][0][0]+
		a.v[0][1][1][0]*b.v[0][0][0][1]+
		a.v[1][0][0][0]*b.v[0][1][1][0]+
		a.v[1][0][0][1]*b.v[0][0][1][0]+
	0)%MD;

	r.v[0][1][0][0]=(((
		a.v[0][0][0][0]*b.v[0][0][0][0]+
	0)%MD*22+
		a.v[0][0][0][0]*b.v[0][0][0][1]+
		a.v[0][0][0][0]*b.v[0][0][1][0]+
		a.v[0][0][0][1]*b.v[0][1][0][0]+
		a.v[0][0][1][0]*b.v[0][0][0][0]+
		a.v[0][1][0][0]*b.v[1][0][0][0]+
		a.v[1][0][0][0]*b.v[0][0][0][0]+
	0)%MD*23+
		a.v[0][0][0][1]*b.v[0][1][1][0]+
		a.v[0][0][1][0]*b.v[0][0][1][0]+
		a.v[0][1][0][0]*b.v[1][0][0][1]+
		a.v[0][1][1][0]*b.v[1][0][0][0]+
		a.v[1][0][0][0]*b.v[0][0][0][1]+
		a.v[1][0][0][1]*b.v[0][1][0][0]+
	0)%MD;

	r.v[0][1][1][0]=(((
		a.v[0][0][0][0]*b.v[0][0][0][0]+
	0)%MD*23+
		a.v[0][0][0][1]*b.v[0][1][0][0]+
		a.v[0][0][1][0]*b.v[0][0][0][1]+
		a.v[0][1][0][0]*b.v[1][0][0][0]+
		a.v[1][0][0][0]*b.v[0][0][1][0]+
	0)%MD*24+
		a.v[0][1][1][0]*b.v[1][0][0][1]+
		a.v[1][0][0][1]*b.v[0][1][1][0]+
	0)%MD;

	r.v[1][0][0][0]=r.v[0][0][0][1];

	r.v[1][0][0][1]=(((
		a.v[0][0][0][0]*b.v[0][0][0][0]+
	0)%MD*23+
		a.v[0][0][0][1]*b.v[0][0][0][1]+
		a.v[0][0][1][0]*b.v[0][1][0][0]+
		a.v[0][1][0][0]*b.v[0][0][1][0]+
		a.v[1][0][0][0]*b.v[1][0][0][0]+
	0)%MD*24+
		a.v[0][1][1][0]*b.v[0][1][1][0]+
		a.v[1][0][0][1]*b.v[1][0][0][1]+
	0)%MD;

	return r;
}

main(){
	char*rp=mmap(0l,1l<<25,1,2,0,0ll);
	rd(n);
	if(n==1){
		if(*rp=='?'){
			write(1,"26",2);
		}else{
			write(1,"1",1);
		}
		_exit(0);
	}

	{
		long c0=*rp++;
		long c1=*rp++;
		rep(i,26){
			rep(j,26){
				d1[i][j]=(c0=='?'|c0-'a'==i)&(c1=='?'|c1-'a'==j)&(i!=j);
			}
		}
	}

	while(1){
		char*rp0=rp;
		while(*rp=='?'){
			++rp;
		}
		long l=rp-rp0;
		if(l>=1<<10){
			M e=m_zero();
			e.v[0][1][0][0]=1;
			M m=e;
			for(--l;l;l>>=1){
				if(l&1){
					m=m_mul(m,e);
				}
				e=m_mul(e,e);
			}
			rep(i,26){
				rep(j,26){
					if(i!=j){
						rep(a,26){
							rep(b,26){
								d2[i][j]+=m.v[i==a][i==b][j==a][j==b]*d1[a][b]%MD;
							}
						}
					}
				}
			}
			nextd();
		}else{
			for(;--l>=0;){
				rep(i,26){
					long s=0;
					rep(j,26){
						s+=d1[j][i];
					}
					rep(j,26){
						d2[i][j]=i==j?0:s-d1[j][i];
					}
				}
				nextd();
			}
		}

		long c=*rp++-'a';
		if(c<0){
			break;
		}
		rep(i,26){
			if(i!=c){
				rep(j,26){
					if(j!=c){
						d2[i][c]+=d1[j][i];
					}
				}
			}
		}
		nextd();
	}
	long z=0;
	rep(i,26){
		rep(j,26){
			z+=d1[i][j];
		}
	}
	z%=MD;
	char wbuf[64],*wp=wbuf+sizeof wbuf;
	wt(z);
	write(1,wp,wbuf+sizeof wbuf-wp);
	_exit(0);
}
0