CODE FESTIVAL 2016 Final

Submission #994591

Source codeソースコード

#include <bits/stdc++.h>
#define MOD 1000000007
using namespace std;
typedef long long LL;
LL gcd(LL a, LL b){
	if(b == 0) return a;
	return gcd(b,a%b);
}
LL powmod(LL a, LL n){
	if(n == 0) return 1;
	if(n % 2) return (a*powmod(a,n-1)) % MOD;
	LL c = powmod(a,n/2);
	return (c*c) % MOD;
}
LL inv(LL a){
	return powmod(a,MOD-2) % MOD;
}

vector<int> edges[400000];
int vis[400000];
int ncc = 0;
void dfs(int a){
	if(vis[a]) return;
	vis[a] = 1;
	for(int c = 0; c < edges[a].size(); c++) dfs(edges[a][c]);
}
int main(){
	int a, b;
	cin >> a >> b;
	int freq[26];
	string s[a];
	for(int i = 0; i < a; i++) cin >> s[i];
	LL ans = 1;
	LL num24 = 0;
	int aa = a/2;
	int bb = b/2;
	int zz = 0;
	for(int i = 0; i < 400000; i++) vis[i] = 0;
	for(int i = 0; i < a-1-i; i++){
		for(int j = 0; j < b-1-j; j++){
			//cout << i << " " << j << endl;
			for(int z = 0; z < 26; z++) freq[z] = 0;
			freq[s[i][j]-'a']++;
			freq[s[a-1-i][j]-'a']++;
			freq[s[i][b-1-j]-'a']++;
			freq[s[a-1-i][b-1-j]-'a']++;
			LL r = 24;
			for(int z = 0; z < 26; z++){
				if(freq[z] == 4) r /= 24;
				if(freq[z] == 3) r /= 6;
				if(freq[z] == 2) r /= 2;
			}
			if(r == 24){
				edges[i].push_back(aa+j);
				edges[aa+j].push_back(i);
				zz++;
			}
			ans *= r;
			ans %= MOD;
		}
	}

	for(int i = 0; i < aa+bb; i++) vis[i] = 0;
	for(int i = 0; i < aa+bb; i++){
		if(!vis[i]){
			ncc++;
			dfs(i);
		}
	}
	//cout << aa << " " << bb << " " << zz << endl;
	//cout << ncc << endl;
	ans *= powmod(inv(2), zz+ncc-aa-bb);
	if(a % 2){
		int c = (a-1)/2;
		LL ok = 1;
		for(int j = 0; j < b; j++){
			if(s[c][j] != s[c][b-1-j]) ok = 2;
		}
		ans *= ok;
		ans %= MOD;
	}
	if(b % 2){
		int c = (b-1)/2;
		LL ok = 1;
		for(int j = 0; j < a; j++){
			if(s[j][c] != s[a-1-j][c]) ok = 2;
		}
		ans *= ok;
		ans %= MOD;
	}
	cout << ans % MOD << endl;
}

Submission

Task問題 I - Reverse Grid
User nameユーザ名 ksun48
Created time投稿日時
Language言語 C++14 (GCC 5.4.1)
Status状態 AC
Score得点 1900
Source lengthソースコード長 1901 Byte
File nameファイル名
Exec time実行時間 17 ms
Memory usageメモリ使用量 11392 KB

Test case

Set

Set name Score得点 / Max score Cases
sample - sample-01.txt,sample-02.txt
all 1900 / 1900 sample-01.txt,sample-02.txt,01-01.txt,01-02.txt,01-03.txt,01-04.txt,01-05.txt,01-06.txt,01-07.txt,01-08.txt,01-09.txt,01-10.txt,01-11.txt,01-12.txt,01-13.txt,01-14.txt,01-15.txt,01-16.txt,01-17.txt,01-18.txt,01-19.txt,01-20.txt,01-21.txt,01-22.txt,01-23.txt,01-24.txt,01-25.txt,01-26.txt,01-27.txt,01-28.txt,01-29.txt,01-30.txt,01-31.txt,01-32.txt,01-33.txt,01-34.txt,01-35.txt

Test case

Case name Status状態 Exec time実行時間 Memory usageメモリ使用量
01-01.txt AC 14 ms 11136 KB
01-02.txt AC 14 ms 11136 KB
01-03.txt AC 14 ms 11136 KB
01-04.txt AC 14 ms 11136 KB
01-05.txt AC 14 ms 11136 KB
01-06.txt AC 14 ms 11136 KB
01-07.txt AC 14 ms 11136 KB
01-08.txt AC 14 ms 11136 KB
01-09.txt AC 14 ms 11136 KB
01-10.txt AC 16 ms 11264 KB
01-11.txt AC 16 ms 11264 KB
01-12.txt AC 16 ms 11264 KB
01-13.txt AC 16 ms 11264 KB
01-14.txt AC 14 ms 11264 KB
01-15.txt AC 14 ms 11264 KB
01-16.txt AC 16 ms 11392 KB
01-17.txt AC 16 ms 11392 KB
01-18.txt AC 16 ms 11392 KB
01-19.txt AC 16 ms 11264 KB
01-20.txt AC 16 ms 11264 KB
01-21.txt AC 16 ms 11264 KB
01-22.txt AC 14 ms 11136 KB
01-23.txt AC 15 ms 11264 KB
01-24.txt AC 16 ms 11264 KB
01-25.txt AC 16 ms 11264 KB
01-26.txt AC 16 ms 11264 KB
01-27.txt AC 14 ms 11136 KB
01-28.txt AC 14 ms 11264 KB
01-29.txt AC 14 ms 11136 KB
01-30.txt AC 14 ms 11264 KB
01-31.txt AC 16 ms 11392 KB
01-32.txt AC 17 ms 11392 KB
01-33.txt AC 17 ms 11392 KB
01-34.txt AC 16 ms 11392 KB
01-35.txt AC 17 ms 11392 KB
sample-01.txt AC 14 ms 11136 KB
sample-02.txt AC 14 ms 11136 KB