結果
| 問題 |
No.1403 調和の魔法陣
|
| コンテスト | |
| ユーザー |
CuriousFairy315
|
| 提出日時 | 2020-07-22 12:58:37 |
| 言語 | Java (openjdk 23) |
| 結果 |
RE
|
| 実行時間 | - |
| コード長 | 10,874 bytes |
| コンパイル時間 | 2,617 ms |
| コンパイル使用メモリ | 86,744 KB |
| 実行使用メモリ | 50,144 KB |
| 最終ジャッジ日時 | 2024-09-14 20:42:40 |
| 合計ジャッジ時間 | 4,637 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | RE * 3 |
| other | RE * 19 |
ソースコード
package yukicoder_4596;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.BitSet;
import java.util.NoSuchElementException;
public class Main {
public static void main(String[] args) {
new Main();
}
public Main() {
InputChecker ic = new InputChecker(System.in);
java.io.PrintWriter out = new java.io.PrintWriter(System.out);
solve(ic, out);
out.flush();
ic.checkEOF();
}
public void solve(InputChecker ic, java.io.PrintWriter out) {
int W = ic.nextInt(1, 600);
ic.nextChar(' ');
int H = ic.nextInt(1, 600);
ic.nextChar(' ');
int X = ic.nextInt(0, 81);
ic.readNewLine();
final int MOD = 998244353;
final int N = 10;
int ans = 0;
final int Cw = (W + 2) / 3, Ch = (H + 2) / 3;
if (W % 3 != 2) {
if (H % 3 != 2) ans = solve11(Cw, Ch, X, N, MOD);
else ans = solve12(Cw, Ch, X, N, MOD);
} else {
if (H % 3 != 2) ans = solve21(Cw, Ch, X, N, MOD);
else ans = solve22(Cw, Ch, X, N, MOD);
}
out.println(ans);
}
private static int solve11(int w, int h, int X, int N, int mod) {
return X <= N ? 1 : 0;
}
private static int solve12(int w, int h, int X, int N, int mod) {
int ans = 0;
if (N <= X) ans = N + 1;
else if (N <= 2 * X) ans = 2 * X - N + 1;
else ans = 0;
return pow(ans, w, mod);
}
private static int solve21(int w, int h, int X, int N, int mod) {
return solve12(h, w, X, N, mod);
}
private static int solve22(int w, int h, int X, int N, int mod) {
int ans = 0;
for (int a = 0;a < N;++ a) { // M_{1,1}
for (int l1 = 0, r1 = 0;;++ l1) { // M_{1, 2w}
if (l1 > r1) {
l1 = 0;
if (++ r1 >= N) break;
}
int comb1 = comb(l1, r1, w, mod);
if (comb1 == 0) continue;
for (int l2 = 0,r2 = 0;;++ l2) { // M_{1, 2w + 1}
if (l2 > r2) {
l2 = 0;
if (++ r2 >= N) break;
if (w == 1) break;
}
int comb2 = comb(l2, r2, w - 1, mod);
if (comb2 == 0) continue;
cont1: for (int l3 = 0, r3 = 0;;++ l3) { // M_{2h, 1}
if (l3 > r3) {
l3 = 0;
if (++ r3 >= N) break;
}
for (int y = l3;y <= r3;++ y) {
for (int x = l1;x <= r1;++ x) { // X - N < M_{1, 1} + M_{1, 2w} + M_{2h, 1} <= X
if (!isRanged(a + x + y, X + 1 - N, X)) continue cont1;
}
if (w != 1) {
for (int x = l2;x <= r2;++ x) { // 0 <= M_{1, 1} + M_{2h, 1} - M_{1, 2w + 1} < N
if (!isRanged(a - x + y, 0, N - 1)) continue cont1;
}
}
}
int comb3 = comb(l3, r3, h, mod);
if (comb3 == 0) continue;
cont2: for (int l4 = 0, r4 = 0;;++ l4) { // M_{2h + 1, 1}
if (l4 > r4) {
l4 = 0;
if (++ r4 >= N) break;
if (h == 1) break;
}
if (h != 1) {
for (int y = l4;y <= r4;++ y) {
for (int x = l1;x <= r1;++ x) { // 0 <= M_{1, 1} + M_{1, 2w} - M_{2h + 1, 1} < N
if (!isRanged(a + x - y, 0, N - 1)) continue cont2;
}
if (w != 1) {
for (int x = l2;x <= r2;++ x) { // -N < M_{1, 1} - M_{1, 2w + 1} - M_{2h + 1, 1} <= 0
if (!isRanged(a - x - y, 1 - N, 0)) continue cont2;
}
}
}
}
int comb4 = comb(l4, r4, h - 1, mod);
if (comb4 == 0) continue;
ans += (int)((long) comb1 * comb2 % mod * comb3 % mod * comb4 % mod);
ans %= mod;
}
}
}
}
}
return ans;
}
public static boolean isRanged(int n, int l, int r) {
return l <= n && n <= r;
}
public static int comb(int l, int r, int N, int mod) {
final int d = r - l;
if (d <= 0) return Math.max(0, d + 1);
long ans = (long)pow(d + 1, N, mod) - (pow(d, N, mod) * 2) + pow(d - 1, N, mod);
ans %= mod;
if (ans < 0) ans += mod;
return (int)ans;
}
public static int pow(int a, int b, int mod) {
if (b < 0) b = b % (mod - 1) + mod - 1;
long ans = 1;
for (long mul = a;b > 0;b >>= 1, mul = mul * mul % mod) if ((b & 1) != 0) ans = ans * mul % mod;
return (int)ans;
}
public static class CharSet {
private final BitSet set = new BitSet(256);
public void add(char... c) {
for (char i : c) set.set(i);
}
public void add(CharSet... s) {
for (CharSet i : s) set.or(i.set);
}
public boolean contains(char... c) {
for (char i : c) if (!set.get(i)) return false;
return true;
}
public boolean contains(String s) {
return contains(s.toCharArray());
}
private static final class Chars extends CharSet {
public Chars(char... c) {
super.add(c);
}
public Chars(CharSet... s) {
super.add(s);
}
@Override
public void add(char... c) {
throw new UnsupportedOperationException();
}
@Override
public void add(CharSet... s) {
throw new UnsupportedOperationException();
}
}
public static final CharSet NUMBER = new Chars('0','1','2','3','4','5','6','7','8','9');
public static final CharSet LOWER = new Chars('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z');
public static final CharSet UPPER = new Chars('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z');
public static final CharSet ALPHABET = new Chars(LOWER, UPPER);
}
public static class InputChecker {
private InputStream in;
private final byte[] buffer = new byte[1024];
private final byte[] undo = new byte[1024];
private int undoSize = 0;
private int read = 0;
private int length = 0;
public InputChecker(InputStream in) {
this.in = in;
}
public final void setInputStream(InputStream in) {
this.in = in;
}
public final void setInputStream(File in) {
try {
this.in = new FileInputStream(in);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
private boolean hasNextByte() {
if (undoSize > 0) return true;
if (read < length) return true;
read = 0;
try {
length = in.read(buffer);
} catch (IOException e) {
e.printStackTrace();
}
return length > 0;
}
private byte readByte() {
if (hasNextByte()) return undoSize > 0 ? undo[--undoSize] : buffer[read++];
throw new NoSuchElementException();
}
private void undo(byte b) {
undo[undoSize ++] = b;
}
private void undo(char c) {
if ((c & 0xFF80) == 0) {
undo((byte)c);
return;
}
undo((byte)(c & 0x3F | 0x80));
if ((c & 0xF800) == 0) {
undo((byte)(c >> 6 & 0x1F | 0xC0));
} else {
undo((byte)(c >> 6 & 0x3F | 0x80));
undo((byte)(c >> 12 | 0xE0));
}
}
public final boolean hasNext() {
return hasNextByte();
}
public final char nextChar() {
byte b = readByte();
if ((b & 0x80) == 0) return (char)b;
if ((b & 0x20) == 0) return (char)((b & 0x1F) << 6 | (readByte() & 0x3F));
return (char)((b & 0xF) << 12 | (readByte() & 0x3F) << 6 | (readByte() & 0x3F));
}
public final char nextChar(char estimate) {
char c = nextChar();
if (estimate == c) return estimate;
undo(c);
throw new AssertionError();
}
public final char nextChar(CharSet estimate) {
char c = nextChar();
if (estimate.contains(c)) return c;
undo(c);
throw new AssertionError();
}
public final void readNewLine() {
char c = nextChar();
if (c == '\r') {
c = nextChar();
if (c != '\n') undo(c);
return;
} else if (c == '\n') return;
undo(c);
throw new AssertionError();
}
public final void checkEOF() {
if (hasNextByte()) throw new AssertionError();
}
public final String next(CharSet contains) {
StringBuilder sb = new StringBuilder();
try {
do {
char c = nextChar();
if (!contains.contains(c)) {
undo(c);
return sb.toString();
}
sb.append(c);
} while(true);
} catch (NoSuchElementException e) {
if (sb.length() <= 0) throw new AssertionError();
return sb.toString();
}
}
public final int nextInt() {
byte b = readByte();
int n = 0;
if (b == '-') {
if (!isNumber(b = readByte())) {
undo(b);
throw new NumberFormatException();
}
try {
if (b == '0') {
if (!isNumber(b = readByte())) {
undo(b);
return 0;
}
throw new NumberFormatException();
}
do n = Math.addExact(Math.multiplyExact(n, 10), '0' - b); while(isNumber(b = readByte()));
undo(b);
} catch (NoSuchElementException e) {
}
return n;
}
if (!isNumber(b)) {
undo(b);
throw new NumberFormatException();
}
try {
if (b == '0') {
if (!isNumber(b = readByte())) {
undo(b);
return 0;
}
throw new NumberFormatException();
}
do n = Math.addExact(Math.multiplyExact(n, 10), b - '0'); while(isNumber(b = readByte()));
undo(b);
} catch (NoSuchElementException e) {
}
return n;
}
public final int nextInt(int min, int max) {
int n = nextInt();
if (min <= n && n <= max) return n;
throw new NumberFormatException();
}
private static boolean isNumber(byte c) {
return '0' <= c && c <= '9';
}
public final long nextLong() {
byte b = readByte();
long n = 0;
if (b == '-') {
if (!isNumber(b = readByte())) {
undo(b);
throw new NumberFormatException();
}
try {
if (b == '0') {
if (!isNumber(b = readByte())) {
undo(b);
return 0;
}
throw new NumberFormatException();
}
do n = Math.addExact(Math.multiplyExact(n, 10), '0' - b); while(isNumber(b = readByte()));
undo(b);
} catch (NoSuchElementException e) {
}
return n;
}
if (!isNumber(b)) {
undo(b);
throw new NumberFormatException();
}
try {
if (b == '0') {
if (!isNumber(b = readByte())) {
undo(b);
return 0;
}
throw new NumberFormatException();
}
do n = Math.addExact(Math.multiplyExact(n, 10), b - '0'); while(isNumber(b = readByte()));
undo(b);
} catch (NoSuchElementException e) {
}
return n;
}
public final long nextLong(long min, long max) {
long n = nextLong();
if (min <= n && n <= max) return n;
throw new NumberFormatException();
}
public final double nextDouble() {
StringBuilder sb = new StringBuilder();
byte b = readByte();
if (b == '-') {
sb.append(b);
b = readByte();
}
if (b == '0') {
sb.append(b);
b = readByte();
} else {
while(isNumber(b)) {
sb.append(b);
b = readByte();
}
}
if (b == '.') {
sb.append(b);
b = readByte();
while(isNumber(b)) {
sb.append(b);
b = readByte();
}
}
if (b == 'e' || b == 'E') {
sb.append(b);
b = readByte();
if (b == '-' || b == '+') {
sb.append(b);
b = readByte();
}
while(isNumber(b)) {
sb.append(b);
b = readByte();
}
}
undo(b);
return Double.parseDouble(sb.toString());
}
}
}
CuriousFairy315