exec_outcome
stringclasses 1
value | code_uid
stringlengths 32
32
| file_name
stringclasses 111
values | prob_desc_created_at
stringlengths 10
10
| prob_desc_description
stringlengths 63
3.8k
| prob_desc_memory_limit
stringclasses 18
values | source_code
stringlengths 117
65.5k
| lang_cluster
stringclasses 1
value | prob_desc_sample_inputs
stringlengths 2
802
| prob_desc_time_limit
stringclasses 27
values | prob_desc_sample_outputs
stringlengths 2
796
| prob_desc_notes
stringlengths 4
3k
⌀ | lang
stringclasses 5
values | prob_desc_input_from
stringclasses 3
values | tags
listlengths 0
11
| src_uid
stringlengths 32
32
| prob_desc_input_spec
stringlengths 28
2.37k
⌀ | difficulty
int64 -1
3.5k
⌀ | prob_desc_output_spec
stringlengths 17
1.47k
⌀ | prob_desc_output_to
stringclasses 3
values | hidden_unit_tests
stringclasses 1
value |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
PASSED
|
d6ba31f3ae90cf93a324492b8aa51754
|
train_109.jsonl
|
1664462100
|
Consider a game with $$$n$$$ cards ($$$n$$$ is even). Each card has a number written on it, between $$$1$$$ and $$$n$$$. All numbers on the cards are different. We say that a card with number $$$x$$$ is stronger than a card with number $$$y$$$ if $$$x > y$$$.Two players, Alex and Boris, play this game. In the beginning, each of them receives exactly $$$\frac{n}{2}$$$ cards, so each card belongs to exactly one player. Then, they take turns. Alex goes first, then Boris, then Alex again, and so on.On a player's turn, he must play exactly one of his cards. Then, if the opponent doesn't have any cards stronger than the card played, the opponent loses, and the game ends. Otherwise, the opponent has to play a stronger card (exactly one card as well). These two cards are removed from the game, and the turn ends. If there are no cards left, the game ends in a draw; otherwise it's the opponent's turn.Consider all possible ways to distribute the cards between two players, so that each of them receives exactly half of the cards. You have to calculate three numbers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. You may assume that both players play optimally (i. e. if a player can win no matter how his opponent plays, he wins). Two ways to distribute the cards are different if there is at least one card such that, in one of these ways, it is given to Alex, and in the other way, it is given to Boris.For example, suppose $$$n = 4$$$, Alex receives the cards $$$[2, 3]$$$, and Boris receives the cards $$$[1, 4]$$$. Then the game may go as follows: if Alex plays the card $$$2$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$3$$$. So, the game ends in a draw; if Alex plays the card $$$3$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$2$$$. So, the game ends in a draw. So, in this case, the game ends in a draw.
|
512 megabytes
|
/* package codechef; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
public class Codechef
{
static class Reader {
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
public Reader()
{
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public Reader(String file_name) throws IOException
{
din = new DataInputStream(
new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String readLine() throws IOException
{
byte[] buf = new byte[64]; // line length
int cnt = 0, c;
while ((c = read()) != -1) {
if (c == '\n') {
if (cnt != 0) {
break;
}
else {
continue;
}
}
buf[cnt++] = (byte)c;
}
return new String(buf, 0, cnt);
}
public int nextInt() throws IOException
{
int ret = 0;
byte c = read();
while (c <= ' ') {
c = read();
}
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public long nextLong() throws IOException
{
long ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public double nextDouble() throws IOException
{
double ret = 0, div = 1;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (c == '.') {
while ((c = read()) >= '0' && c <= '9') {
ret += (c - '0') / (div *= 10);
}
}
if (neg)
return -ret;
return ret;
}
private void fillBuffer() throws IOException
{
bytesRead = din.read(buffer, bufferPointer = 0,
BUFFER_SIZE);
if (bytesRead == -1)
buffer[0] = -1;
}
private byte read() throws IOException
{
if (bufferPointer == bytesRead)
fillBuffer();
return buffer[bufferPointer++];
}
public void close() throws IOException
{
if (din == null)
return;
din.close();
}
}
static long pow(long a , long b ,long c)
{
if(b == 0)
return 1;
long ans = pow(a,b/2,c);
if(b%2 == 0)
return ans*ans%c;
return ans*ans%c*a%c;
}
static void dfs(int n , ArrayList<Integer> arr[] , int p , int water[] , int cnt[] , int d)
{
if(water[d] == -1)
water[d] = 0;
water[d]++;
cnt[d]++;
for(Integer v : arr[n])
{
if(v != p)
{
dfs(v,arr,n,water,cnt,d+1);
}
}
}
public static void main (String[] args) throws IOException
{
Reader sc = new Reader();
int t = sc.nextInt();
long fact[] = new long[61];
long inv[] = new long[61];
fact[0] = 1;
inv[0] = 1;
long mod = 998244353;
for(int i = 1 ; i <= 60 ; i++)
{
fact[i] = fact[i-1]*(long)i%mod;
inv[i] = pow(fact[i],mod-2,mod);
}
for(int tt = 1 ; tt <= t ; tt++)
{
int n = sc.nextInt();
int to = 1;
long ans = 0;
for(int i = n ; i > 0 ; i -= 2)
{
if(to == 1)
{
int upper = i-1;
int lower = i/2;
ans += fact[upper]*inv[lower]%mod*inv[upper-lower]%mod;
ans %= mod;
}
else
{
int upper = i-2;
int lower = upper/2-1;
if(upper >= lower && upper >= 0 && lower >= 0)
ans += fact[upper]*inv[lower]%mod*inv[upper-lower]%mod;
ans %= mod;
}
to = 1-to;
}
long last = 1;
long mid = fact[n]*inv[n/2]%mod*inv[n/2]%mod;
mid -= (1+ans)%mod;
mid %= mod;
mid = (mid+mod)%mod;
System.out.println(ans + " " + mid + " " + 1);
}
}
}
|
Java
|
["5\n\n2\n\n4\n\n6\n\n8\n\n60"]
|
2 seconds
|
["1 0 1\n3 2 1\n12 7 1\n42 27 1\n341102826 248150916 1"]
|
NoteIn the first test case, Alex wins if he receives the card $$$2$$$ (he plays it, and Boris cannot respond). If Alex receives the card $$$1$$$, the game ends in a draw.In the second test case: Alex wins if he receives the cards $$$[3, 4]$$$, $$$[2, 4]$$$ or $$$[1, 4]$$$; Boris wins if Alex receives the cards $$$[1, 2]$$$ or $$$[1, 3]$$$; the game ends in a draw if Alex receives the cards $$$[2, 3]$$$.
|
Java 8
|
standard input
|
[
"combinatorics",
"constructive algorithms",
"dp",
"games"
] |
63fc2fb08d248f8ccdb3f5364c96dac1
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 30$$$) — the number of test cases. Then, $$$t$$$ lines follow. The $$$i$$$-th line contains one even integer $$$n$$$ ($$$2 \le n \le 60$$$).
| 1,500
|
For each test case, print three integers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. Since the answers can be large, print them modulo $$$998244353$$$.
|
standard output
| |
PASSED
|
6640ede22fd97bb2f7723699588e600f
|
train_109.jsonl
|
1664462100
|
Consider a game with $$$n$$$ cards ($$$n$$$ is even). Each card has a number written on it, between $$$1$$$ and $$$n$$$. All numbers on the cards are different. We say that a card with number $$$x$$$ is stronger than a card with number $$$y$$$ if $$$x > y$$$.Two players, Alex and Boris, play this game. In the beginning, each of them receives exactly $$$\frac{n}{2}$$$ cards, so each card belongs to exactly one player. Then, they take turns. Alex goes first, then Boris, then Alex again, and so on.On a player's turn, he must play exactly one of his cards. Then, if the opponent doesn't have any cards stronger than the card played, the opponent loses, and the game ends. Otherwise, the opponent has to play a stronger card (exactly one card as well). These two cards are removed from the game, and the turn ends. If there are no cards left, the game ends in a draw; otherwise it's the opponent's turn.Consider all possible ways to distribute the cards between two players, so that each of them receives exactly half of the cards. You have to calculate three numbers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. You may assume that both players play optimally (i. e. if a player can win no matter how his opponent plays, he wins). Two ways to distribute the cards are different if there is at least one card such that, in one of these ways, it is given to Alex, and in the other way, it is given to Boris.For example, suppose $$$n = 4$$$, Alex receives the cards $$$[2, 3]$$$, and Boris receives the cards $$$[1, 4]$$$. Then the game may go as follows: if Alex plays the card $$$2$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$3$$$. So, the game ends in a draw; if Alex plays the card $$$3$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$2$$$. So, the game ends in a draw. So, in this case, the game ends in a draw.
|
512 megabytes
|
import java.io.*;
import java.util.*;
public class Main {
static Main2 admin = new Main2();
public static void main(String[] args) {
admin.start();
}
}
class Main2 {
//---------------------------------INPUT READER-----------------------------------------//
public BufferedReader br;
StringTokenizer st = new StringTokenizer("");
String next() {
while (!st.hasMoreTokens()) {
try { st = new StringTokenizer(br.readLine());} catch (IOException e) { e.printStackTrace(); }
}
return st.nextToken();
}
int ni() { return Integer.parseInt(next()); }
long nl() { return Long.parseLong(next()); }
double nd() { return Double.parseDouble(next()); }
String ns() { return next(); }
int[] na(long n) {int[]ret=new int[(int)n]; for(int i=0;i<n;i++) ret[i]=ni(); return ret;}
long[] nal(long n) {long[]ret=new long[(int)n]; for(int i=0;i<n;i++) ret[i]=nl(); return ret;}
Integer[] nA(long n) {Integer[]ret=new Integer[(int)n]; for(int i=0;i<n;i++) ret[i]=ni(); return ret;}
Long[] nAl(long n) {Long[]ret=new Long[(int)n]; for(int i=0;i<n;i++) ret[i]=nl(); return ret;}
//--------------------------------------PRINTER------------------------------------------//
PrintWriter w;
void p(int i) {w.println(i);} void p(long l) {w.println(l);}
void p(double d) {w.println(d);} void p(String s) { w.println(s);}
void pr(int i) {w.print(i);} void pr(long l) {w.print(l);}
void pr(double d) {w.print(d);} void pr(String s) { w.print(s);}
void pl() {w.println();}
//--------------------------------------VARIABLES-----------------------------------------//
long lma = Long.MAX_VALUE, lmi = Long.MIN_VALUE;
int ima = Integer.MAX_VALUE, imi = Integer.MIN_VALUE;
long mod = 998244353;
{
w = new PrintWriter(System.out);
br = new BufferedReader(new InputStreamReader(System.in));
}
//----------------------START---------------------//
void start() {
int t = ni(); while(t-- > 0)
solve();
w.close();
}
void solve() {
int n = ni();
long alex = 0;
long cardsLeft = n;
for(int i = 0; i < n/2; i++) {
if(i%2==0) {
alex += ncr(cardsLeft - 1, cardsLeft/2 - 1);
alex %= mod;
} else {
if(cardsLeft == 2) continue;
alex += ncr(cardsLeft - 2, cardsLeft/2 - 2);
alex %= mod;
}
cardsLeft -= 2;
}
long total = ncr(n, n/2);
long boris = (total - alex + mod) % mod;
boris = (boris - 1 + mod) % mod;
p(alex + " " + boris + " " + 1);
}
long[] fact;
{
int lim = 10000;
fact = new long[lim];
fact[0] = 1;
fact[1] = 1;
for(int i = 2; i < lim; i++) {
fact[i] = fact[i-1]*i;
fact[i] %= mod;
}
}
long ncr(long n, long r) {
long num = fact[(int)n];
long den = fact[(int)(n-r)];
den *= fact[(int)(r)];
den %= mod;
return ((num%mod)*mp((den%mod), mod-2))%mod;
}
long mp (long b, long x) {
if (x == 0) return 1;
if (x == 1) return b;
if (x % 2 == 0) return mp (b * b % mod, x / 2) % mod;
return b * mp (b * b % mod, x / 2) % mod;
}
}
|
Java
|
["5\n\n2\n\n4\n\n6\n\n8\n\n60"]
|
2 seconds
|
["1 0 1\n3 2 1\n12 7 1\n42 27 1\n341102826 248150916 1"]
|
NoteIn the first test case, Alex wins if he receives the card $$$2$$$ (he plays it, and Boris cannot respond). If Alex receives the card $$$1$$$, the game ends in a draw.In the second test case: Alex wins if he receives the cards $$$[3, 4]$$$, $$$[2, 4]$$$ or $$$[1, 4]$$$; Boris wins if Alex receives the cards $$$[1, 2]$$$ or $$$[1, 3]$$$; the game ends in a draw if Alex receives the cards $$$[2, 3]$$$.
|
Java 8
|
standard input
|
[
"combinatorics",
"constructive algorithms",
"dp",
"games"
] |
63fc2fb08d248f8ccdb3f5364c96dac1
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 30$$$) — the number of test cases. Then, $$$t$$$ lines follow. The $$$i$$$-th line contains one even integer $$$n$$$ ($$$2 \le n \le 60$$$).
| 1,500
|
For each test case, print three integers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. Since the answers can be large, print them modulo $$$998244353$$$.
|
standard output
| |
PASSED
|
5bd1eff5026d817c79eb627bcc952e25
|
train_109.jsonl
|
1664462100
|
Consider a game with $$$n$$$ cards ($$$n$$$ is even). Each card has a number written on it, between $$$1$$$ and $$$n$$$. All numbers on the cards are different. We say that a card with number $$$x$$$ is stronger than a card with number $$$y$$$ if $$$x > y$$$.Two players, Alex and Boris, play this game. In the beginning, each of them receives exactly $$$\frac{n}{2}$$$ cards, so each card belongs to exactly one player. Then, they take turns. Alex goes first, then Boris, then Alex again, and so on.On a player's turn, he must play exactly one of his cards. Then, if the opponent doesn't have any cards stronger than the card played, the opponent loses, and the game ends. Otherwise, the opponent has to play a stronger card (exactly one card as well). These two cards are removed from the game, and the turn ends. If there are no cards left, the game ends in a draw; otherwise it's the opponent's turn.Consider all possible ways to distribute the cards between two players, so that each of them receives exactly half of the cards. You have to calculate three numbers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. You may assume that both players play optimally (i. e. if a player can win no matter how his opponent plays, he wins). Two ways to distribute the cards are different if there is at least one card such that, in one of these ways, it is given to Alex, and in the other way, it is given to Boris.For example, suppose $$$n = 4$$$, Alex receives the cards $$$[2, 3]$$$, and Boris receives the cards $$$[1, 4]$$$. Then the game may go as follows: if Alex plays the card $$$2$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$3$$$. So, the game ends in a draw; if Alex plays the card $$$3$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$2$$$. So, the game ends in a draw. So, in this case, the game ends in a draw.
|
512 megabytes
|
import java.io.*;
import java.lang.*;
import java.math.BigInteger;
import java.util.*;
public class Solution {
public static long MOD = 998244353L;
public static void main(String[] args) throws Exception {
FastScanner fs = new FastScanner();
int t = fs.nextInt();
fac[0] = fac[1] = ifac[0] = 1L;
for (int i = 2 ; i <= 70; ++i) fac[i] = fac[i - 1] * i % MOD;
ifac[70] = pow(fac[70], MOD - 2);
for (int i = 69; i >= 1; --i) ifac[i] = ifac[i + 1] * (i + 1) % MOD;
long[][] dp = new long[70][3];
dp[2][0] = 1L;
dp[2][1] = 0L;
dp[2][2] = 1L;
for (int i = 4; i <= 60; i += 2) {
dp[i][0] = (combination((i - 2) / 2, i - 1) + dp[i - 2][1]) % MOD;
dp[i][1] = (combination((i - 4) / 2, i - 2) + dp[i - 2][0]) % MOD;
dp[i][2] = dp[i - 2][2];
}
while (t-- != 0) {
int n = fs.nextInt();
out.println(dp[n][0] + " " + dp[n][1] + " " + dp[n][2]);
}
out.flush();
out.close();
}
public static long[] fac = new long[73], ifac = new long[73];
public static long combination(int a, int b) {
return fac[b] * ifac[a] % MOD * ifac[b - a] % MOD;
}
public static long pow(long a, long b) {
long ans = 1L;
while (b != 0) {
if (b % 2 == 1L) ans = (ans * a) % MOD;
a = a * a % MOD;
b /= 2;
}
return ans;
}
static final Random random = new Random();
static void ruffleSort(long arr[]) {
int n = arr.length;
for(int i = 0; i < n; i++) {
int j = random.nextInt(n);
long temp = arr[j];
arr[j] = arr[i];
arr[i] = temp;
}
Arrays.sort(arr);
}
static PrintWriter out = new PrintWriter(System.out);
static class FastScanner {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
String next() throws Exception {
while (!st.hasMoreTokens()) {
st = new StringTokenizer(br.readLine());
}
return st.nextToken();
}
int nextInt() throws Exception {
return Integer.parseInt(next());
}
long nextLong() throws Exception {
return Long.parseLong(next());
}
}
}
|
Java
|
["5\n\n2\n\n4\n\n6\n\n8\n\n60"]
|
2 seconds
|
["1 0 1\n3 2 1\n12 7 1\n42 27 1\n341102826 248150916 1"]
|
NoteIn the first test case, Alex wins if he receives the card $$$2$$$ (he plays it, and Boris cannot respond). If Alex receives the card $$$1$$$, the game ends in a draw.In the second test case: Alex wins if he receives the cards $$$[3, 4]$$$, $$$[2, 4]$$$ or $$$[1, 4]$$$; Boris wins if Alex receives the cards $$$[1, 2]$$$ or $$$[1, 3]$$$; the game ends in a draw if Alex receives the cards $$$[2, 3]$$$.
|
Java 8
|
standard input
|
[
"combinatorics",
"constructive algorithms",
"dp",
"games"
] |
63fc2fb08d248f8ccdb3f5364c96dac1
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 30$$$) — the number of test cases. Then, $$$t$$$ lines follow. The $$$i$$$-th line contains one even integer $$$n$$$ ($$$2 \le n \le 60$$$).
| 1,500
|
For each test case, print three integers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. Since the answers can be large, print them modulo $$$998244353$$$.
|
standard output
| |
PASSED
|
3684b05a05caaff90501c8c508578cc2
|
train_109.jsonl
|
1664462100
|
Consider a game with $$$n$$$ cards ($$$n$$$ is even). Each card has a number written on it, between $$$1$$$ and $$$n$$$. All numbers on the cards are different. We say that a card with number $$$x$$$ is stronger than a card with number $$$y$$$ if $$$x > y$$$.Two players, Alex and Boris, play this game. In the beginning, each of them receives exactly $$$\frac{n}{2}$$$ cards, so each card belongs to exactly one player. Then, they take turns. Alex goes first, then Boris, then Alex again, and so on.On a player's turn, he must play exactly one of his cards. Then, if the opponent doesn't have any cards stronger than the card played, the opponent loses, and the game ends. Otherwise, the opponent has to play a stronger card (exactly one card as well). These two cards are removed from the game, and the turn ends. If there are no cards left, the game ends in a draw; otherwise it's the opponent's turn.Consider all possible ways to distribute the cards between two players, so that each of them receives exactly half of the cards. You have to calculate three numbers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. You may assume that both players play optimally (i. e. if a player can win no matter how his opponent plays, he wins). Two ways to distribute the cards are different if there is at least one card such that, in one of these ways, it is given to Alex, and in the other way, it is given to Boris.For example, suppose $$$n = 4$$$, Alex receives the cards $$$[2, 3]$$$, and Boris receives the cards $$$[1, 4]$$$. Then the game may go as follows: if Alex plays the card $$$2$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$3$$$. So, the game ends in a draw; if Alex plays the card $$$3$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$2$$$. So, the game ends in a draw. So, in this case, the game ends in a draw.
|
512 megabytes
|
import java.util.*;
import java.io.*;
public class tr0 {
static StringBuilder sb;
static int inf = (int) 1e9;
static int mod = 998244353;
static int n, m, k;
static TreeSet<Integer>[] ad, mult;
static long[][][][] memo;
static long[] f, val;
static HashMap<Integer, Integer> hm;
static ArrayList<int[]> av;
static TreeMap<int[], Integer> tm;
static boolean[] vis;
static char[][] g;
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
PrintWriter out = new PrintWriter(System.out);
int t = sc.nextInt();
int N = 70;
long[][] comb = new long[N][N];
comb[0][0] = 1;
for (int i = 1; i < N; i++) {
comb[i][0] = 1;
for (int j = 1; j <= i; j++)
comb[i][j] = (comb[i - 1][j] + comb[i - 1][j - 1]) % mod;
}
long[] arr = new long[66];
arr[2] = 1;
for (int i = 4; i <= 60; i += 2) {
arr[i] = arr[i - 4];
arr[i] += comb[i - 1][i / 2];
if (i > 4) {
arr[i] += comb[i - 4][(i / 2) - 1];
}
arr[i] %= mod;
}
while (t-- > 0) {
int n = sc.nextInt();
long all = comb[n][n / 2];
long ans = arr[n];
// int n1 = n - 4;
// int cc = (n / 2) - 2;
// System.out.println(ans);
// if (n1 > 0) {
// ans += (comb[n1 - 1][cc] * 1l * comb[n - 3][n / 2 - 1]) % mod;
// ans %= mod;
// }
out.println(ans + " " + ((all - ans - 1 + 5 * 1l * mod) % mod) + " " + 1);
}
out.flush();
}
static class Scanner {
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream system) {
br = new BufferedReader(new InputStreamReader(system));
}
public Scanner(String file) throws Exception {
br = new BufferedReader(new FileReader(file));
}
public String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public String nextLine() throws IOException {
return br.readLine();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public double nextDouble() throws IOException {
return Double.parseDouble(next());
}
public char nextChar() throws IOException {
return next().charAt(0);
}
public Long nextLong() throws IOException {
return Long.parseLong(next());
}
public int[] nextArrInt(int n) throws IOException {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
public long[] nextArrLong(int n) throws IOException {
long[] a = new long[n];
for (int i = 0; i < n; i++)
a[i] = nextLong();
return a;
}
public int[] nextArrIntSorted(int n) throws IOException {
int[] a = new int[n];
Integer[] a1 = new Integer[n];
for (int i = 0; i < n; i++)
a1[i] = nextInt();
Arrays.sort(a1);
for (int i = 0; i < n; i++)
a[i] = a1[i].intValue();
return a;
}
public long[] nextArrLongSorted(int n) throws IOException {
long[] a = new long[n];
Long[] a1 = new Long[n];
for (int i = 0; i < n; i++)
a1[i] = nextLong();
Arrays.sort(a1);
for (int i = 0; i < n; i++)
a[i] = a1[i].longValue();
return a;
}
public boolean ready() throws IOException {
return br.ready();
}
public void waitForInput() throws InterruptedException {
Thread.sleep(3000);
}
}
}
|
Java
|
["5\n\n2\n\n4\n\n6\n\n8\n\n60"]
|
2 seconds
|
["1 0 1\n3 2 1\n12 7 1\n42 27 1\n341102826 248150916 1"]
|
NoteIn the first test case, Alex wins if he receives the card $$$2$$$ (he plays it, and Boris cannot respond). If Alex receives the card $$$1$$$, the game ends in a draw.In the second test case: Alex wins if he receives the cards $$$[3, 4]$$$, $$$[2, 4]$$$ or $$$[1, 4]$$$; Boris wins if Alex receives the cards $$$[1, 2]$$$ or $$$[1, 3]$$$; the game ends in a draw if Alex receives the cards $$$[2, 3]$$$.
|
Java 8
|
standard input
|
[
"combinatorics",
"constructive algorithms",
"dp",
"games"
] |
63fc2fb08d248f8ccdb3f5364c96dac1
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 30$$$) — the number of test cases. Then, $$$t$$$ lines follow. The $$$i$$$-th line contains one even integer $$$n$$$ ($$$2 \le n \le 60$$$).
| 1,500
|
For each test case, print three integers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. Since the answers can be large, print them modulo $$$998244353$$$.
|
standard output
| |
PASSED
|
ae7ed8e59775fcfbc368a16e75c11ec0
|
train_109.jsonl
|
1664462100
|
Consider a game with $$$n$$$ cards ($$$n$$$ is even). Each card has a number written on it, between $$$1$$$ and $$$n$$$. All numbers on the cards are different. We say that a card with number $$$x$$$ is stronger than a card with number $$$y$$$ if $$$x > y$$$.Two players, Alex and Boris, play this game. In the beginning, each of them receives exactly $$$\frac{n}{2}$$$ cards, so each card belongs to exactly one player. Then, they take turns. Alex goes first, then Boris, then Alex again, and so on.On a player's turn, he must play exactly one of his cards. Then, if the opponent doesn't have any cards stronger than the card played, the opponent loses, and the game ends. Otherwise, the opponent has to play a stronger card (exactly one card as well). These two cards are removed from the game, and the turn ends. If there are no cards left, the game ends in a draw; otherwise it's the opponent's turn.Consider all possible ways to distribute the cards between two players, so that each of them receives exactly half of the cards. You have to calculate three numbers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. You may assume that both players play optimally (i. e. if a player can win no matter how his opponent plays, he wins). Two ways to distribute the cards are different if there is at least one card such that, in one of these ways, it is given to Alex, and in the other way, it is given to Boris.For example, suppose $$$n = 4$$$, Alex receives the cards $$$[2, 3]$$$, and Boris receives the cards $$$[1, 4]$$$. Then the game may go as follows: if Alex plays the card $$$2$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$3$$$. So, the game ends in a draw; if Alex plays the card $$$3$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$2$$$. So, the game ends in a draw. So, in this case, the game ends in a draw.
|
512 megabytes
|
import java.io.*;
import java.util.*;
public class Solution {
private static boolean useInFile = false;
private static boolean useOutFile = false;
public static void main(String args[]) throws IOException {
InOut inout = new InOut();
Resolver resolver = new Resolver(inout);
resolver.solve();
inout.flush();
}
private static class Resolver {
final long LONG_INF = (long) 1e18;
final int INF = (int) (1e9 + 7);
final int MOD = 998244353;
long f[], inv[];
InOut inout;
Resolver(InOut inout) {
this.inout = inout;
}
void initF(int n, int mod) {
f = new long[n + 1];
f[1] = 1;
for (int i = 2; i <= n; i++) {
f[i] = (f[i - 1] * i) % mod;
}
}
void initInv(int n, int mod) {
inv = new long[n + 1];
inv[n] = pow(f[n], mod - 2, mod);
for (int i = inv.length - 2; i >= 0; i--) {
inv[i] = inv[i + 1] * (i + 1) % mod;
}
}
long cmn(int n, int m, int mod) {
return f[n] * inv[m] % mod * inv[n - m] % mod;
}
int d[] = {0, -1, 0, 1, 0};
boolean legal(int r, int c, int n, int m) {
return r >= 0 && r < n && c >= 0 && c < m;
}
boolean dfs(int f, int p, int target, List<Integer> path) {
List<Integer> es = adj[f];
for (int i = 0; i < es.size(); i++) {
int t = es.get(i);
if (t == p) {
continue;
}
if (t == target) {
path.add(t);
return true;
}
if (dfs(t, f, target, path)) {
path.add(t);
return true;
}
}
return false;
}
void solve() throws IOException {
int tt = 1;
boolean hvt = true;
if (hvt) {
tt = nextInt();
// tt = Integer.parseInt(nextLine());
}
initF(200001, MOD);
initInv(200001, MOD);
for (int cs = 1; cs <= tt; cs++) {
long rs = 0;
boolean ok = true;
int n = nextInt();
long a = 0, b = 0, c = 1;
a = cmn(n - 1, n / 2 - 1, MOD);
for (int i = 4; i + 2 <= n; i+=4) {
a = (a + cmn(n - i, n / 2 - i / 2 - 1, MOD)) % MOD;
}
for (int i = 5; i + 1 <= n; i+=4) {
a = (a + cmn(n - i, n / 2 - i / 2 - 1, MOD)) % MOD;
}
b = (cmn(n, n / 2, MOD) - a - c + MOD) % MOD;
format("%d %d %d", a, b, c);
// format("%s", ok ? "YES" : "NO");
// format("%f", ans);
// format("%d", rs);
// format("Case #%d: %d", cs, rs);
if (cs < tt) {
format("\n");
}
// flush();
}
}
private void updateSegTree(int n, long l, SegmentTree lft) {
long lazy;
lazy = 1;
for (int j = 1; j <= l; j++) {
lazy = (lazy + cmn((int) l, j, INF)) % INF;
lft.modify(1, j, j, lazy);
}
lft.modify(1, (int) (l + 1), n, lazy);
}
String next() throws IOException {
return inout.next();
}
String next(int n) throws IOException {
return inout.next(n);
}
String nextLine() throws IOException {
return inout.nextLine();
}
int nextInt() throws IOException {
return inout.nextInt();
}
long nextLong(int n) throws IOException {
return inout.nextLong(n);
}
int[] anInt(int i, int j) throws IOException {
int a[] = new int[j + 1];
for (int k = i; k <= j; k++) {
a[k] = nextInt();
}
return a;
}
long[] anLong(int i, int j, int len) throws IOException {
long a[] = new long[j + 1];
for (int k = i; k <= j; k++) {
a[k] = nextLong(len);
}
return a;
}
void print(String s, boolean nextLine) {
inout.print(s, nextLine);
}
void format(String format, Object... obj) {
inout.format(format, obj);
}
void flush() {
inout.flush();
}
void swap(long a[], int i, int j) {
a[i] ^= a[j];
a[j] ^= a[i];
a[i] ^= a[j];
}
int getP(int x, int p[]) {
if (p[x] == 0 || p[x] == x) {
return x;
}
return p[x] = getP(p[x], p);
}
void union(int x, int y, int p[]) {
if (x < y) {
p[y] = x;
} else {
p[x] = y;
}
}
boolean topSort() {
int n = adj2.length - 1;
int d[] = new int[n + 1];
for (int i = 1; i <= n; i++) {
for (int j = 0; j < adj2[i].size(); j++) {
d[adj2[i].get(j)[0]]++;
}
}
List<Integer> list = new ArrayList<>();
for (int i = 1; i <= n; i++) {
if (d[i] == 0) {
list.add(i);
}
}
for (int i = 0; i < list.size(); i++) {
for (int j = 0; j < adj2[list.get(i)].size(); j++) {
int t = adj2[list.get(i)].get(j)[0];
d[t]--;
if (d[t] == 0) {
list.add(t);
}
}
}
return list.size() == n;
}
class SegmentTreeNode {
long defaultVal = 0;
int l, r;
long val = defaultVal, lazy = defaultVal;
SegmentTreeNode(int l, int r) {
this.l = l;
this.r = r;
}
}
class SegmentTree {
SegmentTreeNode tree[];
long defaultVal = 0;
long inf = Long.MIN_VALUE;
long mod = INF;
int type = 0;
int MODIFY_VAL = 1;
int MODIFY_LAZY = 2;
int MODIFY_ALL = 3;
SegmentTree(int n) {
this(n, 0);
}
SegmentTree(int n, int type) {
this.type = type;
assert n > 0;
tree = new SegmentTreeNode[n << 2];
}
SegmentTree build(int k, int l, int r) {
if (l > r) {
return this;
}
if (null == tree[k]) {
tree[k] = new SegmentTreeNode(l, r);
}
if (l == r) {
return this;
}
int mid = (l + r) >> 1;
build(k << 1, l, mid);
build(k << 1 | 1, mid + 1, r);
return this;
}
void pushDown(int k) {
if (tree[k].l == tree[k].r) {
return;
}
long lazy = tree[k].lazy;
modify(tree[k << 1], MODIFY_ALL, lazy);
modify(tree[k << 1 | 1], MODIFY_ALL, lazy);
tree[k].lazy = defaultVal;
}
void modify(int k, int l, int r, long val) {
if (l > r) {
return;
}
if (tree[k].l >= l && tree[k].r <= r) {
modify(tree[k], MODIFY_ALL, val);
return;
}
int mid = (tree[k].l + tree[k].r) >> 1;
if (mid >= l) {
modify(k << 1, l, r, val);
}
if (mid + 1 <= r) {
modify(k << 1 | 1, l, r, val);
}
tree[k].val = Math.max(tree[k << 1].val, tree[k << 1 | 1].val);
}
long query(int k, int l, int r) {
if (tree[k].l > r || tree[k].r < l) {
return inf;
}
if (tree[k].lazy != defaultVal) {
pushDown(k);
}
if (tree[k].l >= l && tree[k].r <= r) {
return tree[k].val;
}
long ans = Math.max(query(k << 1, l, r), query(k << 1 | 1, l, r));
if (tree[k].l < tree[k].r) {
tree[k].val = Math.max(tree[k << 1].val, tree[k << 1 | 1].val);
}
return ans;
}
private void modify(SegmentTreeNode node, int x, long val) {
switch (type) {
case 0:
if (x == MODIFY_VAL) {
node.val += val;
} else if (x == MODIFY_LAZY) {
node.lazy += val;
} else {
node.val += val;
node.lazy += val;
}
break;
case 1:
if (x == MODIFY_VAL) {
node.val = node.val * val % mod;
} else if (x == MODIFY_LAZY) {
node.lazy = node.lazy * val % mod;
} else {
node.val = node.val * val % mod;
node.lazy = node.lazy * val % mod;
}
break;
}
}
}
class BinaryIndexedTree {
int n = 1;
long C[];
BinaryIndexedTree(int sz) {
while (n <= sz) {
n <<= 1;
}
C = new long[n];
}
int lowbit(int x) {
return x & -x;
}
void add(int x, long val) {
while (x < n) {
C[x] += val;
x += lowbit(x);
}
}
long getSum(int x) {
long res = 0;
while (x > 0) {
res += C[x];
x -= lowbit(x);
}
return res;
}
int binSearch(long sum) {
if (sum == 0) {
return 0;
}
int n = C.length;
int mx = 1;
while (mx < n) {
mx <<= 1;
}
int res = 0;
for (int i = mx / 2; i >= 1; i >>= 1) {
if (C[res + i] < sum) {
sum -= C[res + i];
res += i;
}
}
return res + 1;
}
}
int mnS;
int mxS;
Map<Integer, TrieNode> SS = new HashMap<>();
class TrieNode {
int cnt = 0;
boolean isMin = false;
boolean isMax = false;
int mnIdx = 26;
int mxIdx = -1;
TrieNode next[];
TrieNode() {
next = new TrieNode[26];
}
private void insert(TrieNode trie, char ch[], int i, int j) {
while (i < ch.length) {
int idx = ch[i] - 'a';
if (null == trie.next[idx]) {
trie.next[idx] = new TrieNode();
}
trie.cnt++;
int lastMnIdx = trie.mnIdx;
int lastMxIdx = trie.mxIdx;
trie.mnIdx = Math.min(trie.mnIdx, idx);
trie.mxIdx = Math.max(trie.mxIdx, idx);
if (trie.isMin && lastMnIdx != trie.mnIdx) {
if (lastMnIdx != 26) {
notMin(trie.next[lastMnIdx]);
}
trie.next[trie.mnIdx].isMin = true;
}
if (trie.isMax && lastMxIdx != trie.mxIdx) {
if (lastMxIdx >= 0) {
notMax(trie.next[lastMxIdx]);
}
trie.next[trie.mxIdx].isMax = true;
}
trie = trie.next[idx];
i++;
}
SS.put(j + 1, trie);
if (trie.cnt == 0) {
if (trie.isMin) {
mnS = j + 1;
}
if (trie.isMax) {
mxS = j + 1;
}
}
}
private void notMin(TrieNode trie) {
while (null != trie) {
trie.isMin = false;
int val = trie.mnIdx;
if (val >= 26 || val < 0 || null == trie.next[val]) {
break;
}
trie = trie.next[val];
}
}
private void notMax(TrieNode trie) {
while (null != trie) {
trie.isMax = false;
int val = trie.mxIdx;
if (val >= 26 || val < 0 || null == trie.next[val]) {
break;
}
trie = trie.next[val];
}
}
}
//Binary tree
class TreeNode {
int val;
int tier = -1;
TreeNode parent;
TreeNode left;
TreeNode right;
TreeNode(int val) {
this.val = val;
}
}
//binary tree dfs
void tierTree(TreeNode root) {
if (null == root) {
return;
}
if (null != root.parent) {
root.tier = root.parent.tier + 1;
} else {
root.tier = 0;
}
tierTree(root.left);
tierTree(root.right);
}
//LCA start
TreeNode[][] lca;
TreeNode[] tree;
void lcaDfsTree(TreeNode root) {
if (null == root) {
return;
}
tree[root.val] = root;
TreeNode nxt = root.parent;
int idx = 0;
while (null != nxt) {
lca[root.val][idx] = nxt;
nxt = lca[nxt.val][idx];
idx++;
}
lcaDfsTree(root.left);
lcaDfsTree(root.right);
}
TreeNode lcaTree(TreeNode root, int n, TreeNode x, TreeNode y) throws IOException {
if (null == root) {
return null;
}
if (-1 == root.tier) {
tree = new TreeNode[n + 1];
tierTree(root);
}
if (null == lca) {
lca = new TreeNode[n + 1][31];
lcaDfsTree(root);
}
int z = Math.abs(x.tier - y.tier);
int xx = x.tier > y.tier ? x.val : y.val;
while (z > 0) {
final int zz = z;
int l = (int) BinSearch.bs(0, 31
, k -> zz < (1 << k));
xx = lca[xx][l].val;
z -= 1 << l;
}
int yy = y.val;
if (x.tier <= y.tier) {
yy = x.val;
}
while (xx != yy) {
final int xxx = xx;
final int yyy = yy;
int l = (int) BinSearch.bs(0, 31
, k -> (1 << k) <= tree[xxx].tier && lca[xxx][(int) k] != lca[yyy][(int) k]);
xx = lca[xx][l].val;
yy = lca[yy][l].val;
}
return tree[xx];
}
//LCA end
//graph
List<Integer> adj[];
List<int[]> adj2[];
void initGraph(int n, int m, boolean hasW, boolean directed, int type) throws IOException {
if (type == 1) {
adj = new List[n + 1];
} else {
adj2 = new List[n + 1];
}
for (int i = 1; i <= n; i++) {
if (type == 1) {
adj[i] = new ArrayList<>();
} else {
adj2[i] = new ArrayList<>();
}
}
for (int i = 0; i < m; i++) {
int f = nextInt();
int t = nextInt();
if (type == 1) {
adj[f].add(t);
if (!directed) {
adj[t].add(f);
}
} else {
int w = hasW ? nextInt() : 0;
adj2[f].add(new int[]{t, w});
if (!directed) {
adj2[t].add(new int[]{f, w});
}
}
}
}
void getDiv(Map<Integer, Integer> map, int n) {
int sqrt = (int) Math.sqrt(n);
for (int i = 2; i <= sqrt; i++) {
int cnt = 0;
while (n % i == 0) {
cnt++;
n /= i;
}
if (cnt > 0) {
map.put(i, cnt);
}
}
if (n > 1) {
map.put(n, 1);
}
}
boolean[] generatePrime(int n) {
boolean p[] = new boolean[n + 1];
p[2] = true;
for (int i = 3; i <= n; i += 2) {
p[i] = true;
}
for (int i = 3; i <= Math.sqrt(n); i += 2) {
if (!p[i]) {
continue;
}
for (int j = i * i; j <= n; j += i << 1) {
p[j] = false;
}
}
return p;
}
boolean isPrime(long n) { //determines if n is a prime number
int p[] = {2, 3, 5, 233, 331};
int pn = p.length;
long s = 0, t = n - 1;//n - 1 = 2^s * t
while ((t & 1) == 0) {
t >>= 1;
++s;
}
for (int i = 0; i < pn; ++i) {
if (n == p[i]) {
return true;
}
long pt = pow(p[i], t, n);
for (int j = 0; j < s; ++j) {
long cur = llMod(pt, pt, n);
if (cur == 1 && pt != 1 && pt != n - 1) {
return false;
}
pt = cur;
}
if (pt != 1) {
return false;
}
}
return true;
}
long llMod(long a, long b, long mod) {
return (a * b - (long) ((double) a / mod * b + 0.5) * mod + mod) % mod;
// long r = 0;
// a %= mod;
// b %= mod;
// while (b > 0) {
// if ((b & 1) == 1) {
// r = (r + a) % mod;
// }
// b >>= 1;
// a = (a << 1) % mod;
// }
// return r;
}
long pow(long a, long n) {
long ans = 1;
while (n > 0) {
if ((n & 1) == 1) {
ans = ans * a;
}
a = a * a;
n >>= 1;
}
return ans;
}
long pow(long a, long n, long mod) {
long ans = 1;
while (n > 0) {
if ((n & 1) == 1) {
ans = llMod(ans, a, mod);
}
a = llMod(a, a, mod);
n >>= 1;
}
return ans;
}
private long[][] initC(int n) {
long c[][] = new long[n][n];
for (int i = 0; i < n; i++) {
c[i][0] = 1;
}
for (int i = 1; i < n; i++) {
for (int j = 1; j <= i; j++) {
c[i][j] = c[i - 1][j - 1] + c[i - 1][j];
}
}
return c;
}
/**
* ps: n >= m, choose m from n;
*/
// private int cmn(long n, long m) {
// if (m > n) {
// n ^= m;
// m ^= n;
// n ^= m;
// }
// m = Math.min(m, n - m);
//
// long top = 1;
// long bot = 1;
// for (long i = n - m + 1; i <= n; i++) {
// top = (top * i) % MOD;
// }
// for (int i = 1; i <= m; i++) {
// bot = (bot * i) % MOD;
// }
//
// return (int) ((top * pow(bot, MOD - 2, MOD)) % MOD);
// }
long gcd(long a, long b) {
if (a < b) {
return gcd(b, a);
}
while (b != 0) {
long tmp = a % b;
a = b;
b = tmp;
}
return a;
}
int[] unique(int a[], Map<Integer, Integer> idx, Map<Integer, Integer> cnt) {
Integer order[] = new Integer[a.length];
for (int i = 0; i < a.length; i++) {
order[i] = i;
}
Arrays.sort(order, Comparator.comparingInt(o -> a[o]));
int tmp[] = new int[a.length];
for (int i = 0; i < a.length; i++) {
tmp[i] = a[order[i]];
}
int j = 0;
for (int i = 0; i < tmp.length; i++) {
if (i == 0 || tmp[i] > tmp[i - 1]) {
idx.put(tmp[i], j++);
}
cnt.put(idx.get(tmp[i]), cnt.getOrDefault(idx.get(tmp[i]), 0) + 1);
}
int rs[] = new int[j];
j = 0;
for (int key : idx.keySet()) {
rs[j++] = key;
}
Arrays.sort(rs);
return rs;
}
boolean isEven(long n) {
return (n & 1) == 0;
}
static class BinSearch {
static long bs(long l, long r, IBinSearch sort) throws IOException {
while (l < r) {
long m = l + (r - l) / 2;
if (sort.binSearchCmp(m)) {
l = m + 1;
} else {
r = m;
}
}
return l;
}
interface IBinSearch {
boolean binSearchCmp(long k) throws IOException;
}
}
}
private static class InOut {
private BufferedReader br;
private StreamTokenizer st;
private PrintWriter pw;
InOut() throws FileNotFoundException {
if (useInFile) {
System.setIn(new FileInputStream("resources/inout/in.text"));
}
if (useOutFile) {
System.setOut(new PrintStream("resources/inout/out.text"));
}
br = new BufferedReader(new InputStreamReader(System.in));
st = new StreamTokenizer(br);
pw = new PrintWriter(new OutputStreamWriter(System.out));
st.ordinaryChar('\'');
st.ordinaryChar('\"');
st.ordinaryChar('/');
}
private long[] anLong(int n) throws IOException {
long a[] = new long[n];
for (int i = 0; i < n; i++) {
a[i] = nextInt();
}
return a;
}
private String next() throws IOException {
st.nextToken();
return st.sval;
}
private String next(int len) throws IOException {
char ch[] = new char[len];
int cur = 0;
char c;
while ((c = (char) br.read()) == '\n' || c == '\r' || c == ' ' || c == '\t') ;
do {
ch[cur++] = c;
} while (!((c = (char) br.read()) == '\n' || c == '\r' || c == ' ' || c == '\t'));
return String.valueOf(ch, 0, cur);
}
private int nextInt() throws IOException {
st.nextToken();
return (int) st.nval;
}
private long nextLong(int n) throws IOException {
return Long.parseLong(next(n));
}
private double nextDouble() throws IOException {
st.nextToken();
return st.nval;
}
private String[] nextSS(String reg) throws IOException {
return br.readLine().split(reg);
}
private String nextLine() throws IOException {
return br.readLine();
}
private void print(String s, boolean newLine) {
if (null != s) {
pw.print(s);
}
if (newLine) {
pw.println();
}
}
private void format(String format, Object... obj) {
pw.format(format, obj);
}
private void flush() {
pw.flush();
}
}
private static class FFT {
double[] roots;
int maxN;
public FFT(int maxN) {
this.maxN = maxN;
initRoots();
}
public long[] multiply(int[] a, int[] b) {
int minSize = a.length + b.length - 1;
int bits = 1;
while (1 << bits < minSize) bits++;
int N = 1 << bits;
double[] aa = toComplex(a, N);
double[] bb = toComplex(b, N);
fftIterative(aa, false);
fftIterative(bb, false);
double[] c = new double[aa.length];
for (int i = 0; i < N; i++) {
c[2 * i] = aa[2 * i] * bb[2 * i] - aa[2 * i + 1] * bb[2 * i + 1];
c[2 * i + 1] = aa[2 * i] * bb[2 * i + 1] + aa[2 * i + 1] * bb[2 * i];
}
fftIterative(c, true);
long[] ret = new long[minSize];
for (int i = 0; i < ret.length; i++) {
ret[i] = Math.round(c[2 * i]);
}
return ret;
}
static double[] toComplex(int[] arr, int size) {
double[] ret = new double[size * 2];
for (int i = 0; i < arr.length; i++) {
ret[2 * i] = arr[i];
}
return ret;
}
void initRoots() {
roots = new double[2 * (maxN + 1)];
double ang = 2 * Math.PI / maxN;
for (int i = 0; i <= maxN; i++) {
roots[2 * i] = Math.cos(i * ang);
roots[2 * i + 1] = Math.sin(i * ang);
}
}
int bits(int N) {
int ret = 0;
while (1 << ret < N) ret++;
if (1 << ret != N) throw new RuntimeException();
return ret;
}
void fftIterative(double[] array, boolean inv) {
int bits = bits(array.length / 2);
int N = 1 << bits;
for (int from = 0; from < N; from++) {
int to = Integer.reverse(from) >>> (32 - bits);
if (from < to) {
double tmpR = array[2 * from];
double tmpI = array[2 * from + 1];
array[2 * from] = array[2 * to];
array[2 * from + 1] = array[2 * to + 1];
array[2 * to] = tmpR;
array[2 * to + 1] = tmpI;
}
}
for (int n = 2; n <= N; n *= 2) {
int delta = 2 * maxN / n;
for (int from = 0; from < N; from += n) {
int rootIdx = inv ? 2 * maxN : 0;
double tmpR, tmpI;
for (int arrIdx = 2 * from; arrIdx < 2 * from + n; arrIdx += 2) {
tmpR = array[arrIdx + n] * roots[rootIdx] - array[arrIdx + n + 1] * roots[rootIdx + 1];
tmpI = array[arrIdx + n] * roots[rootIdx + 1] + array[arrIdx + n + 1] * roots[rootIdx];
array[arrIdx + n] = array[arrIdx] - tmpR;
array[arrIdx + n + 1] = array[arrIdx + 1] - tmpI;
array[arrIdx] += tmpR;
array[arrIdx + 1] += tmpI;
rootIdx += (inv ? -delta : delta);
}
}
}
if (inv) {
for (int i = 0; i < array.length; i++) {
array[i] /= N;
}
}
}
}
}
|
Java
|
["5\n\n2\n\n4\n\n6\n\n8\n\n60"]
|
2 seconds
|
["1 0 1\n3 2 1\n12 7 1\n42 27 1\n341102826 248150916 1"]
|
NoteIn the first test case, Alex wins if he receives the card $$$2$$$ (he plays it, and Boris cannot respond). If Alex receives the card $$$1$$$, the game ends in a draw.In the second test case: Alex wins if he receives the cards $$$[3, 4]$$$, $$$[2, 4]$$$ or $$$[1, 4]$$$; Boris wins if Alex receives the cards $$$[1, 2]$$$ or $$$[1, 3]$$$; the game ends in a draw if Alex receives the cards $$$[2, 3]$$$.
|
Java 8
|
standard input
|
[
"combinatorics",
"constructive algorithms",
"dp",
"games"
] |
63fc2fb08d248f8ccdb3f5364c96dac1
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 30$$$) — the number of test cases. Then, $$$t$$$ lines follow. The $$$i$$$-th line contains one even integer $$$n$$$ ($$$2 \le n \le 60$$$).
| 1,500
|
For each test case, print three integers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. Since the answers can be large, print them modulo $$$998244353$$$.
|
standard output
| |
PASSED
|
aca2a292b6765dbe00dcf41b833809f4
|
train_109.jsonl
|
1664462100
|
Consider a game with $$$n$$$ cards ($$$n$$$ is even). Each card has a number written on it, between $$$1$$$ and $$$n$$$. All numbers on the cards are different. We say that a card with number $$$x$$$ is stronger than a card with number $$$y$$$ if $$$x > y$$$.Two players, Alex and Boris, play this game. In the beginning, each of them receives exactly $$$\frac{n}{2}$$$ cards, so each card belongs to exactly one player. Then, they take turns. Alex goes first, then Boris, then Alex again, and so on.On a player's turn, he must play exactly one of his cards. Then, if the opponent doesn't have any cards stronger than the card played, the opponent loses, and the game ends. Otherwise, the opponent has to play a stronger card (exactly one card as well). These two cards are removed from the game, and the turn ends. If there are no cards left, the game ends in a draw; otherwise it's the opponent's turn.Consider all possible ways to distribute the cards between two players, so that each of them receives exactly half of the cards. You have to calculate three numbers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. You may assume that both players play optimally (i. e. if a player can win no matter how his opponent plays, he wins). Two ways to distribute the cards are different if there is at least one card such that, in one of these ways, it is given to Alex, and in the other way, it is given to Boris.For example, suppose $$$n = 4$$$, Alex receives the cards $$$[2, 3]$$$, and Boris receives the cards $$$[1, 4]$$$. Then the game may go as follows: if Alex plays the card $$$2$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$3$$$. So, the game ends in a draw; if Alex plays the card $$$3$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$2$$$. So, the game ends in a draw. So, in this case, the game ends in a draw.
|
512 megabytes
|
import java.io.*;
import java.util.*;
public class CardGame {
private static final int START_TEST_CASE = 1;
private static final int MOD = 998244353;
/**
* Pre-compute factorial function and mod-inverse of factorial function in linear time.
*/
private static final long[] F = new long[222];
private static final long[] INV = new long[F.length];
private static final long[] FI = new long[F.length];
static {
INV[1] = 1;
for (int i = 2; i < INV.length; ++i) {
INV[i] = MOD - (MOD / i) * INV[MOD % i] % MOD;
}
F[0] = FI[0] = 1;
for (int i = 1; i < F.length; ++i) {
F[i] = (i * F[i - 1]) % MOD;
FI[i] = (INV[i] * FI[i - 1]) % MOD;
}
}
/**
* Computes the modulo result of (n choose k).
* Requires pre-computing the factorial function and mod-inverse of factorial function.
*/
private static long C(int n, int k) {
return F[n] * FI[k] % MOD * FI[n - k] % MOD;
}
private static final int N_MAX = 60;
private static final long[] FP_WINS = new long[N_MAX + 1];
private static final long[] FP_LOSES = new long[N_MAX + 1];
private static final long[] DRAWS = new long[N_MAX + 1];
private static void init() {
FP_WINS[2] = 1;
DRAWS[2] = 1;
for (int i = 4; i <= N_MAX; ++i) {
FP_WINS[i] = add(C(i - 1, i >> 1), FP_LOSES[i - 2]);
FP_LOSES[i] = add(C(i - 2, i >> 1), FP_WINS[i - 2]);
DRAWS[i] = add(C(i, i >> 1), MOD - FP_WINS[i], MOD - FP_LOSES[i]);
}
}
public static void solveCase(FastIO io, int testCase) {
final int N = io.nextInt();
io.println(FP_WINS[N], FP_LOSES[N], DRAWS[N]);
}
private static long add(long a, long b) {
long ans = a + b;
if (ans >= MOD) {
ans -= MOD;
}
return ans;
}
private static long add(long... arr) {
long ans = 0;
for (long x : arr) {
ans = add(ans, x);
}
return ans;
}
public static void solve(FastIO io) {
final int T = io.nextInt();
for (int t = 0; t < T; ++t) {
solveCase(io, START_TEST_CASE + t);
}
}
public static class FastIO {
private InputStream reader;
private PrintWriter writer;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
public FastIO(InputStream r, OutputStream w) {
reader = r;
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(w)));
}
public int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = reader.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public String nextLine() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isEndOfLine(c));
return res.toString();
}
public String nextString() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public long nextLong() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public int nextInt() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
// TODO: read this byte-by-byte like the other read functions.
public double nextDouble() {
return Double.parseDouble(nextString());
}
public int[] nextIntArray(int n) {
return nextIntArray(n, 0);
}
public int[] nextIntArray(int n, int off) {
int[] arr = new int[n + off];
for (int i = 0; i < n; i++) {
arr[i + off] = nextInt();
}
return arr;
}
public long[] nextLongArray(int n) {
return nextLongArray(n, 0);
}
public long[] nextLongArray(int n, int off) {
long[] arr = new long[n + off];
for (int i = 0; i < n; i++) {
arr[i + off] = nextLong();
}
return arr;
}
private boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
private boolean isEndOfLine(int c) {
return c == '\n' || c == '\r' || c == -1;
}
public void print(Object... objects) {
for (int i = 0; i < objects.length; i++) {
if (i != 0) {
writer.print(' ');
}
writer.print(objects[i]);
}
}
public void println(Object... objects) {
print(objects);
writer.println();
}
public void printArray(int[] arr) {
for (int i = 0; i < arr.length; i++) {
if (i != 0) {
writer.print(' ');
}
writer.print(arr[i]);
}
}
public void printArray(long[] arr) {
for (int i = 0; i < arr.length; i++) {
if (i != 0) {
writer.print(' ');
}
writer.print(arr[i]);
}
}
public void printlnArray(int[] arr) {
printArray(arr);
writer.println();
}
public void printlnArray(long[] arr) {
printArray(arr);
writer.println();
}
public void printf(String format, Object... args) {
print(String.format(format, args));
}
public void flush() {
writer.flush();
}
}
public static void main(String[] args) {
FastIO io = new FastIO(System.in, System.out);
init();
solve(io);
io.flush();
}
}
|
Java
|
["5\n\n2\n\n4\n\n6\n\n8\n\n60"]
|
2 seconds
|
["1 0 1\n3 2 1\n12 7 1\n42 27 1\n341102826 248150916 1"]
|
NoteIn the first test case, Alex wins if he receives the card $$$2$$$ (he plays it, and Boris cannot respond). If Alex receives the card $$$1$$$, the game ends in a draw.In the second test case: Alex wins if he receives the cards $$$[3, 4]$$$, $$$[2, 4]$$$ or $$$[1, 4]$$$; Boris wins if Alex receives the cards $$$[1, 2]$$$ or $$$[1, 3]$$$; the game ends in a draw if Alex receives the cards $$$[2, 3]$$$.
|
Java 8
|
standard input
|
[
"combinatorics",
"constructive algorithms",
"dp",
"games"
] |
63fc2fb08d248f8ccdb3f5364c96dac1
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 30$$$) — the number of test cases. Then, $$$t$$$ lines follow. The $$$i$$$-th line contains one even integer $$$n$$$ ($$$2 \le n \le 60$$$).
| 1,500
|
For each test case, print three integers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. Since the answers can be large, print them modulo $$$998244353$$$.
|
standard output
| |
PASSED
|
44f1003f31d4f964911d248c3fd2af1c
|
train_109.jsonl
|
1664462100
|
Consider a game with $$$n$$$ cards ($$$n$$$ is even). Each card has a number written on it, between $$$1$$$ and $$$n$$$. All numbers on the cards are different. We say that a card with number $$$x$$$ is stronger than a card with number $$$y$$$ if $$$x > y$$$.Two players, Alex and Boris, play this game. In the beginning, each of them receives exactly $$$\frac{n}{2}$$$ cards, so each card belongs to exactly one player. Then, they take turns. Alex goes first, then Boris, then Alex again, and so on.On a player's turn, he must play exactly one of his cards. Then, if the opponent doesn't have any cards stronger than the card played, the opponent loses, and the game ends. Otherwise, the opponent has to play a stronger card (exactly one card as well). These two cards are removed from the game, and the turn ends. If there are no cards left, the game ends in a draw; otherwise it's the opponent's turn.Consider all possible ways to distribute the cards between two players, so that each of them receives exactly half of the cards. You have to calculate three numbers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. You may assume that both players play optimally (i. e. if a player can win no matter how his opponent plays, he wins). Two ways to distribute the cards are different if there is at least one card such that, in one of these ways, it is given to Alex, and in the other way, it is given to Boris.For example, suppose $$$n = 4$$$, Alex receives the cards $$$[2, 3]$$$, and Boris receives the cards $$$[1, 4]$$$. Then the game may go as follows: if Alex plays the card $$$2$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$3$$$. So, the game ends in a draw; if Alex plays the card $$$3$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$2$$$. So, the game ends in a draw. So, in this case, the game ends in a draw.
|
512 megabytes
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
import static java.lang.Math.*;
public class Solution {
static long[] AWins = new long[61];
static long[] BWins = new long[61];
static long[] factorials = new long[61];
static long mod = 998244353;
static FastReader fastReader = new FastReader();
public static void main(String[] args) {
factorials[0] = 1;
for (int i = 1; i <= 60; i++) {
factorials[i] = (factorials[i - 1] * i) % mod;
}
AWins[2] = 1;
BWins[2] = 0;
for (int i = 4; i <= 60; i += 2) {
int x = (i / 2) - 1;
long forAwin = (((factorials[i -1] * moduloInverse(factorials[x], mod)) % mod) * moduloInverse(factorials[i - 1 - x], mod)) % mod;
AWins[i] = (forAwin + BWins[i - 2]) % mod;
long totalWins =(((factorials[i] * moduloInverse(factorials[i / 2], mod)) % mod )* moduloInverse(factorials[i / 2], mod)) % mod;
long t = (AWins[i] + 1) % mod;
BWins[i] = moduloSubtraction(totalWins, t, mod);
}
int t = fastReader.nextInt();
int x = 1;
while (x++ <= t) {
String s = solve();
System.out.println(s);
}
}
private static String solve() {
int n = fastReader.nextInt();
return AWins[n] + " " + BWins[n] + " " + 1;
}
private static long power(long x, long y, long p) {
long res = 1;
x = x % p;
while (y > 0) {
if (y % 2 == 1)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
private static long modInverse(long n, long p) {
return power(n, p - 2, p);
}
public static long moduloSubtraction(long a, long b , long mod){
if(a >= b) return (a - b);
return (mod + (a - b)) % mod;
}
public static long moduloInverse(long num, long mod) {
long[] coff = new long[2];
long gcd = extendedEuclid(num, mod, coff);
if (gcd != 1) {
return 0; // No inverse exist
} else {
long inverse = (coff[0] % mod + mod) % mod;
return inverse;
}
}
// public static long extendedEuclid(long a, long b, long[] coff) {
// long x = 0, y = 1, lastx = 1, lasty = 0, temp;
// while (b != 0)
// {
// long q = a / b;
// long r = a % b;
//
// a = b;
// b = r;
//
// temp = x;
// x = lastx - q * x;
// lastx = temp;
//
// temp = y;
// y = lasty - q * y;
// lasty = temp;
// }
// coff[0] = lastx;
// coff[1] = lasty;
// return a;
// }
public static long extendedEuclid(long a, long b, long[] coff) {
if (b == 0) {
long gcd = a;
coff[0] = 1;
coff[1] = 0;
return gcd;
}
long gcd = extendedEuclid(b, a % b, coff);
long x = coff[1];
long y = coff[0] - coff[1] * (a / b);
coff[0] = x;
coff[1] = y;
return gcd;
}
}
class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
|
Java
|
["5\n\n2\n\n4\n\n6\n\n8\n\n60"]
|
2 seconds
|
["1 0 1\n3 2 1\n12 7 1\n42 27 1\n341102826 248150916 1"]
|
NoteIn the first test case, Alex wins if he receives the card $$$2$$$ (he plays it, and Boris cannot respond). If Alex receives the card $$$1$$$, the game ends in a draw.In the second test case: Alex wins if he receives the cards $$$[3, 4]$$$, $$$[2, 4]$$$ or $$$[1, 4]$$$; Boris wins if Alex receives the cards $$$[1, 2]$$$ or $$$[1, 3]$$$; the game ends in a draw if Alex receives the cards $$$[2, 3]$$$.
|
Java 17
|
standard input
|
[
"combinatorics",
"constructive algorithms",
"dp",
"games"
] |
63fc2fb08d248f8ccdb3f5364c96dac1
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 30$$$) — the number of test cases. Then, $$$t$$$ lines follow. The $$$i$$$-th line contains one even integer $$$n$$$ ($$$2 \le n \le 60$$$).
| 1,500
|
For each test case, print three integers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. Since the answers can be large, print them modulo $$$998244353$$$.
|
standard output
| |
PASSED
|
775de2b3a3e417c3288e9be9eaf2c7f1
|
train_109.jsonl
|
1664462100
|
Consider a game with $$$n$$$ cards ($$$n$$$ is even). Each card has a number written on it, between $$$1$$$ and $$$n$$$. All numbers on the cards are different. We say that a card with number $$$x$$$ is stronger than a card with number $$$y$$$ if $$$x > y$$$.Two players, Alex and Boris, play this game. In the beginning, each of them receives exactly $$$\frac{n}{2}$$$ cards, so each card belongs to exactly one player. Then, they take turns. Alex goes first, then Boris, then Alex again, and so on.On a player's turn, he must play exactly one of his cards. Then, if the opponent doesn't have any cards stronger than the card played, the opponent loses, and the game ends. Otherwise, the opponent has to play a stronger card (exactly one card as well). These two cards are removed from the game, and the turn ends. If there are no cards left, the game ends in a draw; otherwise it's the opponent's turn.Consider all possible ways to distribute the cards between two players, so that each of them receives exactly half of the cards. You have to calculate three numbers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. You may assume that both players play optimally (i. e. if a player can win no matter how his opponent plays, he wins). Two ways to distribute the cards are different if there is at least one card such that, in one of these ways, it is given to Alex, and in the other way, it is given to Boris.For example, suppose $$$n = 4$$$, Alex receives the cards $$$[2, 3]$$$, and Boris receives the cards $$$[1, 4]$$$. Then the game may go as follows: if Alex plays the card $$$2$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$3$$$. So, the game ends in a draw; if Alex plays the card $$$3$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$2$$$. So, the game ends in a draw. So, in this case, the game ends in a draw.
|
512 megabytes
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
import static java.lang.Math.*;
public class Solution {
static long[] AWins = new long[61];
static long[] BWins = new long[61];
static long[] factorials = new long[61];
static long mod = 998244353;
static FastReader fastReader = new FastReader();
public static void main(String[] args) {
factorials[0] = 1;
for (int i = 1; i <= 60; i++) {
factorials[i] = (factorials[i - 1] * i) % mod;
}
AWins[2] = 1;
BWins[2] = 0;
for (int i = 4; i <= 60; i += 2) {
int x = (i / 2) - 1;
long forAwin = nCrModPFermat(i - 1, x, mod);
AWins[i] = (forAwin + BWins[i - 2]) % mod;
long totalWins = nCrModPFermat(i, i / 2, mod);
long t = (AWins[i] + 1) % mod;
BWins[i] = moduloSubtraction(totalWins, t, mod);
}
int t = fastReader.nextInt();
int x = 1;
while (x++ <= t) {
String s = solve();
System.out.println(s);
}
}
private static String solve() {
int n = fastReader.nextInt();
return AWins[n] + " " + BWins[n] + " " + 1;
}
private static long power(long x, long y, long p) {
long res = 1;
x = x % p;
while (y > 0) {
if (y % 2 == 1)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
private static long modInverse(long n, long p) {
return power(n, p - 2, p);
}
private static int nCrModPFermat(int n, long r,
long p) {
if (n < r)
return 0;
if (r == 0)
return 1;
return (int) ((factorials[n] * modInverse(factorials[(int) r], (int) p)
% p * modInverse(factorials[(int) (n - r)], (int) p)
% p)
% p);
}
public static long moduloSubtraction(long a, long b , long mod){
if(a >= b) return (a - b);
return (mod + (a - b)) % mod;
}
}
class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
|
Java
|
["5\n\n2\n\n4\n\n6\n\n8\n\n60"]
|
2 seconds
|
["1 0 1\n3 2 1\n12 7 1\n42 27 1\n341102826 248150916 1"]
|
NoteIn the first test case, Alex wins if he receives the card $$$2$$$ (he plays it, and Boris cannot respond). If Alex receives the card $$$1$$$, the game ends in a draw.In the second test case: Alex wins if he receives the cards $$$[3, 4]$$$, $$$[2, 4]$$$ or $$$[1, 4]$$$; Boris wins if Alex receives the cards $$$[1, 2]$$$ or $$$[1, 3]$$$; the game ends in a draw if Alex receives the cards $$$[2, 3]$$$.
|
Java 17
|
standard input
|
[
"combinatorics",
"constructive algorithms",
"dp",
"games"
] |
63fc2fb08d248f8ccdb3f5364c96dac1
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 30$$$) — the number of test cases. Then, $$$t$$$ lines follow. The $$$i$$$-th line contains one even integer $$$n$$$ ($$$2 \le n \le 60$$$).
| 1,500
|
For each test case, print three integers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. Since the answers can be large, print them modulo $$$998244353$$$.
|
standard output
| |
PASSED
|
05dfe9ecedab43ba0325888aaa55614f
|
train_109.jsonl
|
1664462100
|
Consider a game with $$$n$$$ cards ($$$n$$$ is even). Each card has a number written on it, between $$$1$$$ and $$$n$$$. All numbers on the cards are different. We say that a card with number $$$x$$$ is stronger than a card with number $$$y$$$ if $$$x > y$$$.Two players, Alex and Boris, play this game. In the beginning, each of them receives exactly $$$\frac{n}{2}$$$ cards, so each card belongs to exactly one player. Then, they take turns. Alex goes first, then Boris, then Alex again, and so on.On a player's turn, he must play exactly one of his cards. Then, if the opponent doesn't have any cards stronger than the card played, the opponent loses, and the game ends. Otherwise, the opponent has to play a stronger card (exactly one card as well). These two cards are removed from the game, and the turn ends. If there are no cards left, the game ends in a draw; otherwise it's the opponent's turn.Consider all possible ways to distribute the cards between two players, so that each of them receives exactly half of the cards. You have to calculate three numbers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. You may assume that both players play optimally (i. e. if a player can win no matter how his opponent plays, he wins). Two ways to distribute the cards are different if there is at least one card such that, in one of these ways, it is given to Alex, and in the other way, it is given to Boris.For example, suppose $$$n = 4$$$, Alex receives the cards $$$[2, 3]$$$, and Boris receives the cards $$$[1, 4]$$$. Then the game may go as follows: if Alex plays the card $$$2$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$3$$$. So, the game ends in a draw; if Alex plays the card $$$3$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$2$$$. So, the game ends in a draw. So, in this case, the game ends in a draw.
|
512 megabytes
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
import static java.lang.Math.*;
public class Solution {
static long[] AWins = new long[61];
static long[] BWins = new long[61];
static long[] factorials = new long[61];
static long mod = 998244353;
static FastReader fastReader = new FastReader();
public static void main(String[] args) {
factorials[0] = 1;
for (int i = 1; i <= 60; i++) {
factorials[i] = (factorials[i - 1] * i) % mod;
}
AWins[2] = 1;
BWins[2] = 0;
for (int i = 4; i <= 60; i += 2) {
int x = (i / 2) - 1;
long forAwin = nCrModPFermat(i - 1, x, mod);
AWins[i] = (forAwin + BWins[i - 2]) % mod;
long totalWins = nCrModPFermat(i, i / 2, mod);
long t = (AWins[i] + 1) % mod;
BWins[i] = totalWins - t;
BWins[i] = BWins[i] < 0 ? (mod + BWins[i]) % mod : BWins[i];
}
int t = fastReader.nextInt();
int x = 1;
while (x++ <= t) {
String s = solve();
System.out.println(s);
}
}
private static String solve() {
int n = fastReader.nextInt();
return AWins[n] + " " + BWins[n] + " " + 1;
}
private static long power(long x, long y, long p) {
long res = 1;
x = x % p;
while (y > 0) {
if (y % 2 == 1)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
private static long modInverse(long n, long p) {
return power(n, p - 2, p);
}
private static int nCrModPFermat(int n, long r,
long p) {
if (n < r)
return 0;
if (r == 0)
return 1;
return (int) ((factorials[n] * modInverse(factorials[(int) r], (int) p)
% p * modInverse(factorials[(int) (n - r)], (int) p)
% p)
% p);
}
}
class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
|
Java
|
["5\n\n2\n\n4\n\n6\n\n8\n\n60"]
|
2 seconds
|
["1 0 1\n3 2 1\n12 7 1\n42 27 1\n341102826 248150916 1"]
|
NoteIn the first test case, Alex wins if he receives the card $$$2$$$ (he plays it, and Boris cannot respond). If Alex receives the card $$$1$$$, the game ends in a draw.In the second test case: Alex wins if he receives the cards $$$[3, 4]$$$, $$$[2, 4]$$$ or $$$[1, 4]$$$; Boris wins if Alex receives the cards $$$[1, 2]$$$ or $$$[1, 3]$$$; the game ends in a draw if Alex receives the cards $$$[2, 3]$$$.
|
Java 17
|
standard input
|
[
"combinatorics",
"constructive algorithms",
"dp",
"games"
] |
63fc2fb08d248f8ccdb3f5364c96dac1
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 30$$$) — the number of test cases. Then, $$$t$$$ lines follow. The $$$i$$$-th line contains one even integer $$$n$$$ ($$$2 \le n \le 60$$$).
| 1,500
|
For each test case, print three integers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. Since the answers can be large, print them modulo $$$998244353$$$.
|
standard output
| |
PASSED
|
37d01b9f21eeafcdeccb630629ebbf34
|
train_109.jsonl
|
1664462100
|
Consider a game with $$$n$$$ cards ($$$n$$$ is even). Each card has a number written on it, between $$$1$$$ and $$$n$$$. All numbers on the cards are different. We say that a card with number $$$x$$$ is stronger than a card with number $$$y$$$ if $$$x > y$$$.Two players, Alex and Boris, play this game. In the beginning, each of them receives exactly $$$\frac{n}{2}$$$ cards, so each card belongs to exactly one player. Then, they take turns. Alex goes first, then Boris, then Alex again, and so on.On a player's turn, he must play exactly one of his cards. Then, if the opponent doesn't have any cards stronger than the card played, the opponent loses, and the game ends. Otherwise, the opponent has to play a stronger card (exactly one card as well). These two cards are removed from the game, and the turn ends. If there are no cards left, the game ends in a draw; otherwise it's the opponent's turn.Consider all possible ways to distribute the cards between two players, so that each of them receives exactly half of the cards. You have to calculate three numbers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. You may assume that both players play optimally (i. e. if a player can win no matter how his opponent plays, he wins). Two ways to distribute the cards are different if there is at least one card such that, in one of these ways, it is given to Alex, and in the other way, it is given to Boris.For example, suppose $$$n = 4$$$, Alex receives the cards $$$[2, 3]$$$, and Boris receives the cards $$$[1, 4]$$$. Then the game may go as follows: if Alex plays the card $$$2$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$3$$$. So, the game ends in a draw; if Alex plays the card $$$3$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$2$$$. So, the game ends in a draw. So, in this case, the game ends in a draw.
|
512 megabytes
|
import java.sql.Array;
import java.util.*;
import java.io.*;
import java.math.BigInteger;
import java.util.stream.IntStream;
import java.util.stream.LongStream;
public class Main {
public static FastReader cin;
public static PrintWriter out;
public static long MOD = 998244353L;
public static long[][] C = new long[1001][1001];
public static void main(String[] args) throws Exception {
out = new PrintWriter(new BufferedOutputStream(System.out));
cin = new FastReader();
for (int i = 0; i <= 1000; i++)
C[i][0] = 1;
for (int i = 1; i <= 1000; i++){
for (int j = 1; j <= i; j++){
C[i][j] = ((C[i - 1][j - 1] % MOD) + (C[i - 1][j] % MOD)) % MOD;
}
}
long[] dp = new long[65];
dp[2] = 1;
dp[4] = 3;
for(int i = 6 ; i <= 60; i+=2){
dp[i] = (dp[i - 4] + C[i - 1][i / 2] % MOD + C[i - 4][i / 2 - 3] % MOD) % MOD;
}
int ttt = cin.nextInt();
label:for(int qqq = 1; qqq <= ttt; qqq++){
int n = cin.nextInt();
long []ans = new long[3];
ans[0] = dp[n];
ans[2] = 1;
ans[1] = (C[n][n / 2] - dp[n] - 1 + MOD) % MOD;
out.println(ans[0]+" "+ans[1]+" "+ans[2]);
}
out.close();
}
public static long lcm(long a,long b ){
long ans = a / gcd(a,b) * b ;
return ans;
}
public static long gcd(long a,long b){
if(b==0)return a;
else return gcd(b,a%b);
}
static class FastReader {
BufferedReader br;
StringTokenizer str;
public FastReader() {
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next() {
while (str == null || !str.hasMoreElements()) {
try {
str = new StringTokenizer(br.readLine());
} catch (IOException lastMonthOfVacation) {
lastMonthOfVacation.printStackTrace();
}
}
return str.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException lastMonthOfVacation) {
lastMonthOfVacation.printStackTrace();
}
return str;
}
}
}
|
Java
|
["5\n\n2\n\n4\n\n6\n\n8\n\n60"]
|
2 seconds
|
["1 0 1\n3 2 1\n12 7 1\n42 27 1\n341102826 248150916 1"]
|
NoteIn the first test case, Alex wins if he receives the card $$$2$$$ (he plays it, and Boris cannot respond). If Alex receives the card $$$1$$$, the game ends in a draw.In the second test case: Alex wins if he receives the cards $$$[3, 4]$$$, $$$[2, 4]$$$ or $$$[1, 4]$$$; Boris wins if Alex receives the cards $$$[1, 2]$$$ or $$$[1, 3]$$$; the game ends in a draw if Alex receives the cards $$$[2, 3]$$$.
|
Java 17
|
standard input
|
[
"combinatorics",
"constructive algorithms",
"dp",
"games"
] |
63fc2fb08d248f8ccdb3f5364c96dac1
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 30$$$) — the number of test cases. Then, $$$t$$$ lines follow. The $$$i$$$-th line contains one even integer $$$n$$$ ($$$2 \le n \le 60$$$).
| 1,500
|
For each test case, print three integers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. Since the answers can be large, print them modulo $$$998244353$$$.
|
standard output
| |
PASSED
|
dab7205cc7e26862e69cd04c03f5ecc5
|
train_109.jsonl
|
1664462100
|
Consider a game with $$$n$$$ cards ($$$n$$$ is even). Each card has a number written on it, between $$$1$$$ and $$$n$$$. All numbers on the cards are different. We say that a card with number $$$x$$$ is stronger than a card with number $$$y$$$ if $$$x > y$$$.Two players, Alex and Boris, play this game. In the beginning, each of them receives exactly $$$\frac{n}{2}$$$ cards, so each card belongs to exactly one player. Then, they take turns. Alex goes first, then Boris, then Alex again, and so on.On a player's turn, he must play exactly one of his cards. Then, if the opponent doesn't have any cards stronger than the card played, the opponent loses, and the game ends. Otherwise, the opponent has to play a stronger card (exactly one card as well). These two cards are removed from the game, and the turn ends. If there are no cards left, the game ends in a draw; otherwise it's the opponent's turn.Consider all possible ways to distribute the cards between two players, so that each of them receives exactly half of the cards. You have to calculate three numbers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. You may assume that both players play optimally (i. e. if a player can win no matter how his opponent plays, he wins). Two ways to distribute the cards are different if there is at least one card such that, in one of these ways, it is given to Alex, and in the other way, it is given to Boris.For example, suppose $$$n = 4$$$, Alex receives the cards $$$[2, 3]$$$, and Boris receives the cards $$$[1, 4]$$$. Then the game may go as follows: if Alex plays the card $$$2$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$3$$$. So, the game ends in a draw; if Alex plays the card $$$3$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$2$$$. So, the game ends in a draw. So, in this case, the game ends in a draw.
|
512 megabytes
|
import java.util.*;
public class Test1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int tc = sc.nextInt();
while(tc-->0) {
int n = sc.nextInt();
long mod = 998244353;
long[] fact = new long[n+1];
fact[0] = 1;
for(int i=1;i<=n;i++) fact[i] = (fact[i-1] * i) % mod;
boolean aliceFirst = true;
long alice = 0, bob = 0;
for(int i=n;i > 0;i-=2) {
long first = ncr(i-1, i/2 -1, mod, fact);
long sec = ncr(i-2, i/2 - 2, mod, fact);
if(aliceFirst) {
alice = (alice + first) % mod;
bob = (bob + sec) % mod;
}else {
bob = (bob + first) % mod;
alice = (alice + sec) % mod;
}
aliceFirst = !aliceFirst;
}
System.out.println(alice+" "+bob+" "+1);
}
// ####################
}
// ####################
static long ncr(int n, int r, long mod, long[] fact) {
if(n < r || r < 0) return 0;
long deno = (fact[r] * fact[n-r]) % mod;
return (fact[n] * pow(deno, mod-2, mod)) % mod;
}
static long pow(long a, long n, long mod) {
long res = 1;
while(n > 0) {
if(n % 2 != 0) res = (res * a) % mod;
n = n / 2;
a = (a * a) % mod;
}
return res;
}
}
|
Java
|
["5\n\n2\n\n4\n\n6\n\n8\n\n60"]
|
2 seconds
|
["1 0 1\n3 2 1\n12 7 1\n42 27 1\n341102826 248150916 1"]
|
NoteIn the first test case, Alex wins if he receives the card $$$2$$$ (he plays it, and Boris cannot respond). If Alex receives the card $$$1$$$, the game ends in a draw.In the second test case: Alex wins if he receives the cards $$$[3, 4]$$$, $$$[2, 4]$$$ or $$$[1, 4]$$$; Boris wins if Alex receives the cards $$$[1, 2]$$$ or $$$[1, 3]$$$; the game ends in a draw if Alex receives the cards $$$[2, 3]$$$.
|
Java 17
|
standard input
|
[
"combinatorics",
"constructive algorithms",
"dp",
"games"
] |
63fc2fb08d248f8ccdb3f5364c96dac1
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 30$$$) — the number of test cases. Then, $$$t$$$ lines follow. The $$$i$$$-th line contains one even integer $$$n$$$ ($$$2 \le n \le 60$$$).
| 1,500
|
For each test case, print three integers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. Since the answers can be large, print them modulo $$$998244353$$$.
|
standard output
| |
PASSED
|
f11c7dd3e10e826a3b527fa955122906
|
train_109.jsonl
|
1664462100
|
Consider a game with $$$n$$$ cards ($$$n$$$ is even). Each card has a number written on it, between $$$1$$$ and $$$n$$$. All numbers on the cards are different. We say that a card with number $$$x$$$ is stronger than a card with number $$$y$$$ if $$$x > y$$$.Two players, Alex and Boris, play this game. In the beginning, each of them receives exactly $$$\frac{n}{2}$$$ cards, so each card belongs to exactly one player. Then, they take turns. Alex goes first, then Boris, then Alex again, and so on.On a player's turn, he must play exactly one of his cards. Then, if the opponent doesn't have any cards stronger than the card played, the opponent loses, and the game ends. Otherwise, the opponent has to play a stronger card (exactly one card as well). These two cards are removed from the game, and the turn ends. If there are no cards left, the game ends in a draw; otherwise it's the opponent's turn.Consider all possible ways to distribute the cards between two players, so that each of them receives exactly half of the cards. You have to calculate three numbers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. You may assume that both players play optimally (i. e. if a player can win no matter how his opponent plays, he wins). Two ways to distribute the cards are different if there is at least one card such that, in one of these ways, it is given to Alex, and in the other way, it is given to Boris.For example, suppose $$$n = 4$$$, Alex receives the cards $$$[2, 3]$$$, and Boris receives the cards $$$[1, 4]$$$. Then the game may go as follows: if Alex plays the card $$$2$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$3$$$. So, the game ends in a draw; if Alex plays the card $$$3$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$2$$$. So, the game ends in a draw. So, in this case, the game ends in a draw.
|
512 megabytes
|
import java.util.*;
public class Main {
static Scanner sc = null;
static long C[][] = new long[100][100];
static final int mod = 998244353 ;
public static void main(String[] args) {
sc = new Scanner(System.in);
C[ 0 ][ 0 ] = 1;
for (int n = 0; n <= 60; n++) {
for (int k = 0; k <= n; k++) {
if (k == 0 || n == k) C[k][n] = 1;
else {
C[k][n] = (C[k][n - 1] + C[k - 1][n - 1]) % mod;
}
}
}
int t = sc.nextInt();
while (t -- > 0) {
solve();
}
}
public static void solve() {
int n = sc.nextInt();
long dp[][ ] = new long[n + 1][2];
dp[2][0] = 1;
dp[2][1] = 0;
for (int i = 4; i <= n; i += 2) {
dp[i][ 0 ] = (C[i / 2 - 1][i - 2] + dp[i - 2][ 1 ] + C[i / 2 - 2][i - 2]) % mod;
dp[ i ][ 1 ] = (dp[i - 2][ 0 ] + C[i / 2 - 2][i - 2]) % mod;
}
System.out.println(dp[ n ][ 0 ] + " " + dp[ n ][ 1 ] + " " + 1);
}
}
|
Java
|
["5\n\n2\n\n4\n\n6\n\n8\n\n60"]
|
2 seconds
|
["1 0 1\n3 2 1\n12 7 1\n42 27 1\n341102826 248150916 1"]
|
NoteIn the first test case, Alex wins if he receives the card $$$2$$$ (he plays it, and Boris cannot respond). If Alex receives the card $$$1$$$, the game ends in a draw.In the second test case: Alex wins if he receives the cards $$$[3, 4]$$$, $$$[2, 4]$$$ or $$$[1, 4]$$$; Boris wins if Alex receives the cards $$$[1, 2]$$$ or $$$[1, 3]$$$; the game ends in a draw if Alex receives the cards $$$[2, 3]$$$.
|
Java 17
|
standard input
|
[
"combinatorics",
"constructive algorithms",
"dp",
"games"
] |
63fc2fb08d248f8ccdb3f5364c96dac1
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 30$$$) — the number of test cases. Then, $$$t$$$ lines follow. The $$$i$$$-th line contains one even integer $$$n$$$ ($$$2 \le n \le 60$$$).
| 1,500
|
For each test case, print three integers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. Since the answers can be large, print them modulo $$$998244353$$$.
|
standard output
| |
PASSED
|
bfdba5d755c26fa54acca5f95db402d2
|
train_109.jsonl
|
1664462100
|
Consider a game with $$$n$$$ cards ($$$n$$$ is even). Each card has a number written on it, between $$$1$$$ and $$$n$$$. All numbers on the cards are different. We say that a card with number $$$x$$$ is stronger than a card with number $$$y$$$ if $$$x > y$$$.Two players, Alex and Boris, play this game. In the beginning, each of them receives exactly $$$\frac{n}{2}$$$ cards, so each card belongs to exactly one player. Then, they take turns. Alex goes first, then Boris, then Alex again, and so on.On a player's turn, he must play exactly one of his cards. Then, if the opponent doesn't have any cards stronger than the card played, the opponent loses, and the game ends. Otherwise, the opponent has to play a stronger card (exactly one card as well). These two cards are removed from the game, and the turn ends. If there are no cards left, the game ends in a draw; otherwise it's the opponent's turn.Consider all possible ways to distribute the cards between two players, so that each of them receives exactly half of the cards. You have to calculate three numbers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. You may assume that both players play optimally (i. e. if a player can win no matter how his opponent plays, he wins). Two ways to distribute the cards are different if there is at least one card such that, in one of these ways, it is given to Alex, and in the other way, it is given to Boris.For example, suppose $$$n = 4$$$, Alex receives the cards $$$[2, 3]$$$, and Boris receives the cards $$$[1, 4]$$$. Then the game may go as follows: if Alex plays the card $$$2$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$3$$$. So, the game ends in a draw; if Alex plays the card $$$3$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$2$$$. So, the game ends in a draw. So, in this case, the game ends in a draw.
|
512 megabytes
|
import java.io.*;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws IOException{
new Main().run();
}
void run()throws IOException{
new Solve().setIO(System.in,System.out).run();
}
public class Solve extends IOTask{
int t,n,i;
long[]a=new long[31];
long[]b=new long[31];
long[]all=new long[31];
long []inv=new long[31];
long p=998244353;
long c(long n,long m) {
long ans=1;
for(int i=1;i<=m;i++) {
ans=((ans*n--)%p*inv[i])%p;
}
return ans;
}
void run()throws IOException {
inv[1]=1;
for(i=2;i<=30;i++) {
inv[i]=(p-p/i)*inv[(int)(p%i)]%p;
}
for(i=1;i<=30;i++) {
all[i]=c(i<<1,i);
}
a[1]=1;
for(i=2;i<=30;i++) {
a[i]=((all[i]*inv[2]%p+b[i-1])%p+p)%p;
b[i]=((all[i]-1-a[i])%p+p)%p;
}
t=in.in();
while(t-->0) {
n=in.in()>>1;
out.println(a[n]+" "+b[n]+" 1");
}
out.close();
}
}
class In{
private StringTokenizer st;
private BufferedReader br;
private InputStream is;
private void init(){
br=new BufferedReader(new InputStreamReader(is));
st=new StringTokenizer("");
}
public In(InputStream is){
this.is=is;
init();
}
public In(File file)throws IOException {
is=new FileInputStream(file);
init();
}
public String ins()throws IOException{
while(!st.hasMoreTokens())st=new StringTokenizer(br.readLine());
return st.nextToken();
}
public int in()throws IOException{
return Integer.parseInt(ins());
}
public long inl()throws IOException{
return Long.parseLong(ins());
}
public double ind()throws IOException{
return Double.parseDouble(ins());
}
}
class Out{
public PrintWriter out;
OutputStream os;
private void init(){
out=new PrintWriter(new BufferedWriter(new OutputStreamWriter(os)));
}
public Out(OutputStream os){
this.os=os;
init();
}
public Out(File file)throws IOException{
this.os=new FileOutputStream(file);
init();
}
}
abstract class IOTask{
In in;
PrintWriter out;
public IOTask setIO(InputStream is,OutputStream os)throws IOException{
in=new In(is);
out=new Out(os).out;
return this;
}
public IOTask setIO(File is,OutputStream os)throws IOException{
in=new In(is);
out=new Out(os).out;
return this;
}
public IOTask setIO(InputStream is,File os)throws IOException{
in=new In(is);
out=new Out(os).out;
return this;
}
public IOTask setIO(File is,File os)throws IOException{
in=new In(is);
out=new Out(os).out;
return this;
}
void run()throws IOException{
}
}
}
|
Java
|
["5\n\n2\n\n4\n\n6\n\n8\n\n60"]
|
2 seconds
|
["1 0 1\n3 2 1\n12 7 1\n42 27 1\n341102826 248150916 1"]
|
NoteIn the first test case, Alex wins if he receives the card $$$2$$$ (he plays it, and Boris cannot respond). If Alex receives the card $$$1$$$, the game ends in a draw.In the second test case: Alex wins if he receives the cards $$$[3, 4]$$$, $$$[2, 4]$$$ or $$$[1, 4]$$$; Boris wins if Alex receives the cards $$$[1, 2]$$$ or $$$[1, 3]$$$; the game ends in a draw if Alex receives the cards $$$[2, 3]$$$.
|
Java 17
|
standard input
|
[
"combinatorics",
"constructive algorithms",
"dp",
"games"
] |
63fc2fb08d248f8ccdb3f5364c96dac1
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 30$$$) — the number of test cases. Then, $$$t$$$ lines follow. The $$$i$$$-th line contains one even integer $$$n$$$ ($$$2 \le n \le 60$$$).
| 1,500
|
For each test case, print three integers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. Since the answers can be large, print them modulo $$$998244353$$$.
|
standard output
| |
PASSED
|
2aed67c72154a4e70cca7cfe7c72e1a6
|
train_109.jsonl
|
1664462100
|
Consider a game with $$$n$$$ cards ($$$n$$$ is even). Each card has a number written on it, between $$$1$$$ and $$$n$$$. All numbers on the cards are different. We say that a card with number $$$x$$$ is stronger than a card with number $$$y$$$ if $$$x > y$$$.Two players, Alex and Boris, play this game. In the beginning, each of them receives exactly $$$\frac{n}{2}$$$ cards, so each card belongs to exactly one player. Then, they take turns. Alex goes first, then Boris, then Alex again, and so on.On a player's turn, he must play exactly one of his cards. Then, if the opponent doesn't have any cards stronger than the card played, the opponent loses, and the game ends. Otherwise, the opponent has to play a stronger card (exactly one card as well). These two cards are removed from the game, and the turn ends. If there are no cards left, the game ends in a draw; otherwise it's the opponent's turn.Consider all possible ways to distribute the cards between two players, so that each of them receives exactly half of the cards. You have to calculate three numbers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. You may assume that both players play optimally (i. e. if a player can win no matter how his opponent plays, he wins). Two ways to distribute the cards are different if there is at least one card such that, in one of these ways, it is given to Alex, and in the other way, it is given to Boris.For example, suppose $$$n = 4$$$, Alex receives the cards $$$[2, 3]$$$, and Boris receives the cards $$$[1, 4]$$$. Then the game may go as follows: if Alex plays the card $$$2$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$3$$$. So, the game ends in a draw; if Alex plays the card $$$3$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$2$$$. So, the game ends in a draw. So, in this case, the game ends in a draw.
|
512 megabytes
|
import java.util.Scanner;
public class Main {
private final static long mod = 998244353;
private static long[] fact = new long[107];
private static long[] finv = new long[107];
private static long[] dp = new long[107];
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
init();
long t = in.nextLong();
while (t -- > 0) {
int n = in.nextInt();
long b = ((C(n, n / 2) - dp[n] - 1) % mod + mod) % mod;
System.out.println(dp[n] + " " + b + " " + 1);
}
}
private static long ksm(long a, long b) {
long res = 1;
while (b > 0) {
if (b % 2 == 1) res = res * a % mod;
b = b / 2;
a = a * a % mod;
}
return res;
}
private static long C(int n, int m) {
return fact[n] * finv[n - m] % mod * finv[m] % mod;
}
private static void init() {
fact[0] = finv[0] = 1;
for (int i = 1; i <= 100; ++ i) {
fact[i] = fact[i - 1] * i % mod;
finv[i] = finv[i - 1] * ksm(i, mod - 2) % mod;
}
dp[2] = 1; dp[4] = 3;
for (int i = 6; i <= 60; i += 2) {
dp[i] = (C(i - 1, i / 2 - 1) + C(i - 4, i / 2 - 1) + dp[i - 4]) % mod;
}
}
}
|
Java
|
["5\n\n2\n\n4\n\n6\n\n8\n\n60"]
|
2 seconds
|
["1 0 1\n3 2 1\n12 7 1\n42 27 1\n341102826 248150916 1"]
|
NoteIn the first test case, Alex wins if he receives the card $$$2$$$ (he plays it, and Boris cannot respond). If Alex receives the card $$$1$$$, the game ends in a draw.In the second test case: Alex wins if he receives the cards $$$[3, 4]$$$, $$$[2, 4]$$$ or $$$[1, 4]$$$; Boris wins if Alex receives the cards $$$[1, 2]$$$ or $$$[1, 3]$$$; the game ends in a draw if Alex receives the cards $$$[2, 3]$$$.
|
Java 17
|
standard input
|
[
"combinatorics",
"constructive algorithms",
"dp",
"games"
] |
63fc2fb08d248f8ccdb3f5364c96dac1
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 30$$$) — the number of test cases. Then, $$$t$$$ lines follow. The $$$i$$$-th line contains one even integer $$$n$$$ ($$$2 \le n \le 60$$$).
| 1,500
|
For each test case, print three integers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. Since the answers can be large, print them modulo $$$998244353$$$.
|
standard output
| |
PASSED
|
e42be7ff5b0812ebb455aeb5bb21d686
|
train_109.jsonl
|
1664462100
|
Consider a game with $$$n$$$ cards ($$$n$$$ is even). Each card has a number written on it, between $$$1$$$ and $$$n$$$. All numbers on the cards are different. We say that a card with number $$$x$$$ is stronger than a card with number $$$y$$$ if $$$x > y$$$.Two players, Alex and Boris, play this game. In the beginning, each of them receives exactly $$$\frac{n}{2}$$$ cards, so each card belongs to exactly one player. Then, they take turns. Alex goes first, then Boris, then Alex again, and so on.On a player's turn, he must play exactly one of his cards. Then, if the opponent doesn't have any cards stronger than the card played, the opponent loses, and the game ends. Otherwise, the opponent has to play a stronger card (exactly one card as well). These two cards are removed from the game, and the turn ends. If there are no cards left, the game ends in a draw; otherwise it's the opponent's turn.Consider all possible ways to distribute the cards between two players, so that each of them receives exactly half of the cards. You have to calculate three numbers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. You may assume that both players play optimally (i. e. if a player can win no matter how his opponent plays, he wins). Two ways to distribute the cards are different if there is at least one card such that, in one of these ways, it is given to Alex, and in the other way, it is given to Boris.For example, suppose $$$n = 4$$$, Alex receives the cards $$$[2, 3]$$$, and Boris receives the cards $$$[1, 4]$$$. Then the game may go as follows: if Alex plays the card $$$2$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$3$$$. So, the game ends in a draw; if Alex plays the card $$$3$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$2$$$. So, the game ends in a draw. So, in this case, the game ends in a draw.
|
512 megabytes
|
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
final int mod = 998244353;
Long[][] a = new Long[63][63];
a[0][0] = 1L;
a[1][0] = 1L;
a[1][1] = 1L;
for(int i=2;i<=62;i++)
{
a[i][0] = a[i][i] = 1L;
for(int j=1;j<i;j++)
{
a[i][j] = a[i-1][j-1] + a[i-1][j];
a[i][j]%=mod;
}
}
Long[][] dp = new Long[63][3];
dp[2][0] = 1L;
dp[2][2] = 1L;
dp[2][1] = 0L;
for(int i=4;i<=62;i+=2)
{
dp[i][0] = (a[i-1][i/2-1] + dp[i-2][1])%mod;
dp[i][2] = 1L;
dp[i][1] = (a[i][i/2] - dp[i][0] -1 +mod)%mod;
}
int t;
Scanner sc = new Scanner(System.in);
t = sc.nextInt();
while(t>0)
{
--t;
int n = sc.nextInt();
System.out.println(dp[n][0]+" "+dp[n][1]+" "+dp[n][2]);
}
}
}
|
Java
|
["5\n\n2\n\n4\n\n6\n\n8\n\n60"]
|
2 seconds
|
["1 0 1\n3 2 1\n12 7 1\n42 27 1\n341102826 248150916 1"]
|
NoteIn the first test case, Alex wins if he receives the card $$$2$$$ (he plays it, and Boris cannot respond). If Alex receives the card $$$1$$$, the game ends in a draw.In the second test case: Alex wins if he receives the cards $$$[3, 4]$$$, $$$[2, 4]$$$ or $$$[1, 4]$$$; Boris wins if Alex receives the cards $$$[1, 2]$$$ or $$$[1, 3]$$$; the game ends in a draw if Alex receives the cards $$$[2, 3]$$$.
|
Java 17
|
standard input
|
[
"combinatorics",
"constructive algorithms",
"dp",
"games"
] |
63fc2fb08d248f8ccdb3f5364c96dac1
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 30$$$) — the number of test cases. Then, $$$t$$$ lines follow. The $$$i$$$-th line contains one even integer $$$n$$$ ($$$2 \le n \le 60$$$).
| 1,500
|
For each test case, print three integers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. Since the answers can be large, print them modulo $$$998244353$$$.
|
standard output
| |
PASSED
|
c8cbcd6e9519dc99631adb18047f15bc
|
train_109.jsonl
|
1664462100
|
Consider a game with $$$n$$$ cards ($$$n$$$ is even). Each card has a number written on it, between $$$1$$$ and $$$n$$$. All numbers on the cards are different. We say that a card with number $$$x$$$ is stronger than a card with number $$$y$$$ if $$$x > y$$$.Two players, Alex and Boris, play this game. In the beginning, each of them receives exactly $$$\frac{n}{2}$$$ cards, so each card belongs to exactly one player. Then, they take turns. Alex goes first, then Boris, then Alex again, and so on.On a player's turn, he must play exactly one of his cards. Then, if the opponent doesn't have any cards stronger than the card played, the opponent loses, and the game ends. Otherwise, the opponent has to play a stronger card (exactly one card as well). These two cards are removed from the game, and the turn ends. If there are no cards left, the game ends in a draw; otherwise it's the opponent's turn.Consider all possible ways to distribute the cards between two players, so that each of them receives exactly half of the cards. You have to calculate three numbers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. You may assume that both players play optimally (i. e. if a player can win no matter how his opponent plays, he wins). Two ways to distribute the cards are different if there is at least one card such that, in one of these ways, it is given to Alex, and in the other way, it is given to Boris.For example, suppose $$$n = 4$$$, Alex receives the cards $$$[2, 3]$$$, and Boris receives the cards $$$[1, 4]$$$. Then the game may go as follows: if Alex plays the card $$$2$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$3$$$. So, the game ends in a draw; if Alex plays the card $$$3$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$2$$$. So, the game ends in a draw. So, in this case, the game ends in a draw.
|
512 megabytes
|
import java.util.*;
import java.io.*;
public class Solution {
private static SuperFastReader sc = new SuperFastReader();
// private static final FastReader sc = new FastReader();
// private static final Scanner sc = new Scanner(System.in);
private static PrintWriter out = new PrintWriter(System.out);
// static {
// try {
// sc = new SuperFastReader("input.txt");
// out = new PrintWriter("output.txt");
// } catch (Exception e) {
// debug(e);
// }
// }
public static final int MOD = 998244353;// ((a + b) % MOD + MOD) % MOD
public static long powMod(long base, long exp) {
long ans = 1;
for (; exp != 0;) {
if ((exp & 1) == 1) {
ans *= base;
ans %= MOD;
}
base *= base;
base %= MOD;
exp = exp >> 1;
}
return ans;
}
public static long mulMod(long a, long b) {
long ans = 0;
for (; b != 0;) {
if ((b & 1) == 1) {
ans += a;
ans %= MOD;
}
a += a;
a %= MOD;
b >>= 1;
}
return ans;
}
static long invMod(long num) {
return powMod(num, MOD - 2); // only works if MOD is prime
}
public static long nCrMod(int n, int r) {
if(n < r) return 0;
if(r == 0) return 1;
long fact[] = new long[n + 1];
fact[0] = 1;
for (int i = 1; i <= n; i++) {
fact[i] = mulMod(fact[i - 1], i);
}
return mulMod(fact[n], mulMod(invMod(fact[n - r]), invMod(fact[r])));
}
public static void main(String[] args) throws IOException {
int t = sc.Int();
for (int i = 1; i <= t; ++i) {
// System.out.print("Case #" + i + ": ");
solve();
}
// solve();
sc.close();
out.close();
}
public static void solve() throws IOException {
int n = sc.Int();
long dp[][] = new long[n + 1][3];
dp[2][0] = 1;
dp[2][1] = 0;
dp[2][2] = 1;
for(int i = 4;i <= n;i += 2){
dp[i][0] = (nCrMod(i - 1, i / 2) + dp[i - 2][1]) % MOD;
dp[i][1] = (nCrMod(i - 2, i / 2) + dp[i - 2][0]) % MOD;
dp[i][2] = dp[i - 2][2];
}
println(dp[n][0] + " " + dp[n][1] + " " + dp[n][2]);
}
public static <T> void debug(T data) {
System.out.println(data);
}
public static <T> void print(T data) {
out.print(data);
}
public static <T> void println(T data) {
out.println(data);
}
public static void println() {
out.println();
}
public static void read(int arr[]) throws IOException {
for (int i = 0; i < arr.length; i++) {
arr[i] = sc.Int();
}
}
public static void read(long arr[]) throws IOException {
for (int i = 0; i < arr.length; i++) {
arr[i] = sc.Long();
}
}
public static void read(String arr[]) throws IOException {
for (int i = 0; i < arr.length; i++) {
arr[i] = sc.next();
}
}
public static void read(int mat[][]) throws IOException {
for (int i = 0; i < mat.length; i++) {
for (int j = 0; j < mat[i].length; j++) {
mat[i][j] = sc.Int();
}
}
}
public static void read(long mat[][]) throws IOException {
for (int i = 0; i < mat.length; i++) {
for (int j = 0; j < mat[i].length; j++) {
mat[i][j] = sc.Long();
}
}
}
}
class DSU {
public int par[];
private long size[];
public DSU(int n) {
par = new int[n];
size = new long[n];
for (int i = 0; i < n; i++) {
par[i] = i;
size[i] = 1;
}
}
public int get(int c) {
if (c == par[c])
return c;
return par[c] = get(par[c]);
}
public void union(int x, int y) {
x = get(x);
y = get(y);
if (x != y) {
if (size[x] < size[y]) {
int t = x;
x = y;
y = t;
}
par[y] = x;
size[x] += size[y];
}
}
}
class Pair<K, V> {
public K key;
public V value;
Pair(K k, V v) {
key = k;
value = v;
}
}
class SuperFastReader {
private InputStream stream;
private byte[] buf = new byte[1 << 16];
private int curChar, numChars;
// standard input
public SuperFastReader() {
stream = System.in;
}
// file input
public SuperFastReader(String file) throws IOException {
stream = new FileInputStream(file);
}
private int nextByte() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars == -1)
return -1; // end of file
}
return buf[curChar++];
}
public String next() {
int c;
do {
c = nextByte();
} while (c <= ' ');
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = nextByte();
} while (c > ' ');
return res.toString();
}
public String nextLine() {
int c;
do {
c = nextByte();
} while (c < '\n');
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = nextByte();
} while (c > '\n');
return res.toString().trim(); // .trim() used to remove '\n' from either ends
}
public int Int() {
int c;
do {
c = nextByte();
} while (c <= ' ');
int sgn = 1;
if (c == '-') {
sgn = -1;
c = nextByte();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res = 10 * res + c - '0';
c = nextByte();
} while (c > ' ');
return res * sgn;
}
public long Long() {
int c;
do {
c = nextByte();
} while (c <= ' ');
int sgn = 1;
if (c == '-') {
sgn = -1;
c = nextByte();
}
long res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res = 10 * res + c - '0';
c = nextByte();
} while (c > ' ');
return res * sgn;
}
public double Double() {
return Double.parseDouble(next());
}
public char Char() {
return next().charAt(0);
}
public void close() throws IOException {
stream.close();
}
}
|
Java
|
["5\n\n2\n\n4\n\n6\n\n8\n\n60"]
|
2 seconds
|
["1 0 1\n3 2 1\n12 7 1\n42 27 1\n341102826 248150916 1"]
|
NoteIn the first test case, Alex wins if he receives the card $$$2$$$ (he plays it, and Boris cannot respond). If Alex receives the card $$$1$$$, the game ends in a draw.In the second test case: Alex wins if he receives the cards $$$[3, 4]$$$, $$$[2, 4]$$$ or $$$[1, 4]$$$; Boris wins if Alex receives the cards $$$[1, 2]$$$ or $$$[1, 3]$$$; the game ends in a draw if Alex receives the cards $$$[2, 3]$$$.
|
Java 17
|
standard input
|
[
"combinatorics",
"constructive algorithms",
"dp",
"games"
] |
63fc2fb08d248f8ccdb3f5364c96dac1
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 30$$$) — the number of test cases. Then, $$$t$$$ lines follow. The $$$i$$$-th line contains one even integer $$$n$$$ ($$$2 \le n \le 60$$$).
| 1,500
|
For each test case, print three integers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. Since the answers can be large, print them modulo $$$998244353$$$.
|
standard output
| |
PASSED
|
28dde64936836d13dfd3afebaa655404
|
train_109.jsonl
|
1664462100
|
Consider a game with $$$n$$$ cards ($$$n$$$ is even). Each card has a number written on it, between $$$1$$$ and $$$n$$$. All numbers on the cards are different. We say that a card with number $$$x$$$ is stronger than a card with number $$$y$$$ if $$$x > y$$$.Two players, Alex and Boris, play this game. In the beginning, each of them receives exactly $$$\frac{n}{2}$$$ cards, so each card belongs to exactly one player. Then, they take turns. Alex goes first, then Boris, then Alex again, and so on.On a player's turn, he must play exactly one of his cards. Then, if the opponent doesn't have any cards stronger than the card played, the opponent loses, and the game ends. Otherwise, the opponent has to play a stronger card (exactly one card as well). These two cards are removed from the game, and the turn ends. If there are no cards left, the game ends in a draw; otherwise it's the opponent's turn.Consider all possible ways to distribute the cards between two players, so that each of them receives exactly half of the cards. You have to calculate three numbers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. You may assume that both players play optimally (i. e. if a player can win no matter how his opponent plays, he wins). Two ways to distribute the cards are different if there is at least one card such that, in one of these ways, it is given to Alex, and in the other way, it is given to Boris.For example, suppose $$$n = 4$$$, Alex receives the cards $$$[2, 3]$$$, and Boris receives the cards $$$[1, 4]$$$. Then the game may go as follows: if Alex plays the card $$$2$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$3$$$. So, the game ends in a draw; if Alex plays the card $$$3$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$2$$$. So, the game ends in a draw. So, in this case, the game ends in a draw.
|
512 megabytes
|
import java.io.*;
import java.util.*;
public class C {
static int mod = 998244353;
public static void main(String[] args) throws IOException {
// Scanner sc = new Scanner(new File("second_friend_input.txt"));
// PrintWriter pw = new PrintWriter("B1_output.txt");
Scanner sc = new Scanner(System.in);
PrintWriter pw = new PrintWriter(System.out);
int t = sc.nextInt();
preProcessNCR(60);
while (t-- > 0) {
int n = sc.nextInt();
long win = 0;
long lose = ncr(n, n / 2);
while (n > 3) {
win += ncr(n - 1, n / 2 - 1);
win %= mod;
if (n - 4 > 0) {
win += ncr(n - 4, (n - 4) / 2 - 1);
win %= mod;
}
n -= 4;
}
// System.out.println(win);
if (n == 2) {
win++;
win %= mod;
}
lose = lose - 1 - win;
lose %= mod;
lose += mod;
lose %= mod;
pw.println(win + " " + lose + " 1");
}
pw.flush();
}
static long fastpow(long n, long ti) {
if (ti == 0)
return 1l;
if (ti % 2 == 0) {
long y = fastpow(n, ti / 2);
long k = y * y;
k %= mod;
return k;
} else {
long y = fastpow(n, ti / 2);
long k = ((n * y) % mod) * y;
k %= mod;
return k;
}
}
static long modInverse(long x) {
return fastpow(x, mod - 2);
}
static long[] fac, facInv;
static void preProcessNCR(int maxN) {
//compute factorial for all numbers smaller than or equal maxN
fac = new long[maxN + 1];
fac[0] = 1;
for (int i = 1; i < fac.length; i++) fac[i] = (fac[i - 1] * i) % mod;
facInv = new long[fac.length];
facInv[fac.length - 1] = modInverse(fac[fac.length - 1]);//modInverse(n!)
for (int i = fac.length - 2; i >= 0; i--) {
//modInverse(x!)=(x+1)/(x+1)!=(x+1)*modInverse((x+1)!)
facInv[i] = (facInv[i + 1] * (i + 1)) % mod;
}
}
public static long ncr(int n, int r) {
if (n < r) return 0;
long num = fac[n];
long den = (facInv[n - r] * facInv[r]) % mod;
long ans = (num * den) % mod;
return ans;
}
static long lcm(long a, long b) {
return (a / gcd(a, b)) * b;
}
static long gcd(long a, long b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
static class Scanner {
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream s) {
br = new BufferedReader(new InputStreamReader(s));
}
public Scanner(File s) throws FileNotFoundException {
br = new BufferedReader(new FileReader(s));
}
public String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public String nextLine() throws IOException {
return br.readLine();
}
public double nextDouble() throws IOException {
String x = next();
StringBuilder sb = new StringBuilder("0");
double res = 0, f = 1;
boolean dec = false, neg = false;
int start = 0;
if (x.charAt(0) == '-') {
neg = true;
start++;
}
for (int i = start; i < x.length(); i++)
if (x.charAt(i) == '.') {
res = Long.parseLong(sb.toString());
sb = new StringBuilder("0");
dec = true;
} else {
sb.append(x.charAt(i));
if (dec)
f *= 10;
}
res += Long.parseLong(sb.toString()) / f;
return res * (neg ? -1 : 1);
}
public boolean ready() throws IOException {
return br.ready();
}
}
}
|
Java
|
["5\n\n2\n\n4\n\n6\n\n8\n\n60"]
|
2 seconds
|
["1 0 1\n3 2 1\n12 7 1\n42 27 1\n341102826 248150916 1"]
|
NoteIn the first test case, Alex wins if he receives the card $$$2$$$ (he plays it, and Boris cannot respond). If Alex receives the card $$$1$$$, the game ends in a draw.In the second test case: Alex wins if he receives the cards $$$[3, 4]$$$, $$$[2, 4]$$$ or $$$[1, 4]$$$; Boris wins if Alex receives the cards $$$[1, 2]$$$ or $$$[1, 3]$$$; the game ends in a draw if Alex receives the cards $$$[2, 3]$$$.
|
Java 17
|
standard input
|
[
"combinatorics",
"constructive algorithms",
"dp",
"games"
] |
63fc2fb08d248f8ccdb3f5364c96dac1
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 30$$$) — the number of test cases. Then, $$$t$$$ lines follow. The $$$i$$$-th line contains one even integer $$$n$$$ ($$$2 \le n \le 60$$$).
| 1,500
|
For each test case, print three integers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. Since the answers can be large, print them modulo $$$998244353$$$.
|
standard output
| |
PASSED
|
a1716c8bb1b7c08720d08c7094429d2b
|
train_109.jsonl
|
1664462100
|
Consider a game with $$$n$$$ cards ($$$n$$$ is even). Each card has a number written on it, between $$$1$$$ and $$$n$$$. All numbers on the cards are different. We say that a card with number $$$x$$$ is stronger than a card with number $$$y$$$ if $$$x > y$$$.Two players, Alex and Boris, play this game. In the beginning, each of them receives exactly $$$\frac{n}{2}$$$ cards, so each card belongs to exactly one player. Then, they take turns. Alex goes first, then Boris, then Alex again, and so on.On a player's turn, he must play exactly one of his cards. Then, if the opponent doesn't have any cards stronger than the card played, the opponent loses, and the game ends. Otherwise, the opponent has to play a stronger card (exactly one card as well). These two cards are removed from the game, and the turn ends. If there are no cards left, the game ends in a draw; otherwise it's the opponent's turn.Consider all possible ways to distribute the cards between two players, so that each of them receives exactly half of the cards. You have to calculate three numbers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. You may assume that both players play optimally (i. e. if a player can win no matter how his opponent plays, he wins). Two ways to distribute the cards are different if there is at least one card such that, in one of these ways, it is given to Alex, and in the other way, it is given to Boris.For example, suppose $$$n = 4$$$, Alex receives the cards $$$[2, 3]$$$, and Boris receives the cards $$$[1, 4]$$$. Then the game may go as follows: if Alex plays the card $$$2$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$3$$$. So, the game ends in a draw; if Alex plays the card $$$3$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$2$$$. So, the game ends in a draw. So, in this case, the game ends in a draw.
|
512 megabytes
|
import java.util.*;
import java.io.*;
public class CF1737 {
// For fast input output
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
try {
br = new BufferedReader(
new FileReader("input.txt"));
PrintStream out = new PrintStream(new FileOutputStream("output.txt"));
System.setOut(out);
} catch (Exception e) {
br = new BufferedReader(new InputStreamReader(System.in));
}
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
int[] readArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
// end of fast i/o code
public static boolean isPalindrome(String str) {
int i = 0;
int j = str.length() - 1;
int flag = 0;
while (i <= j) {
if (str.charAt(i) != str.charAt(j)) {
flag = 1;
break;
}
i++;
j--;
}
return flag == 1 ? false : true;
}
public static int gcd(int a, int b) {
if (a == 0)
return b;
return gcd(b % a, a);
}
class Pair {
int x1;
int x2;
public Pair(int x1, int x2) {
this.x1 = x1;
this.x2 = x2;
}
}
static int mod=998244353;
static long fact[]=new long[100000];
static long ifact[]=new long[100];
static long inv(long n ,int p){
long res=1;
while(p>0){
if((p&1)==1)
res=(res%mod*n%mod)%mod;
n=(n%mod*n%mod)%mod;
p>>=1;
}
return res%mod;
}
static long nCr(int n ,int r){
if(n<r)
return 0;
return ((fact[n]%mod*ifact[r]%mod*ifact[n-r]%mod))%mod;
}
public static void main(String[] args) {
FastReader fs = new FastReader();
PrintWriter out = new PrintWriter(System.out);
// fact();
fact[0]=fact[1]=1;
for(int i =2;i<100;i++){
fact[i]=fact[i-1]*i%mod;
}
for(int i =0;i<100;i++){
ifact[i]=inv(fact[i],mod-2)%mod;
// System.out.println(ifact[i]);
}
long dp[][]=new long[100][3];
dp[2][0]=1;
dp[2][1]=0;
dp[2][2]=1;
for(int i =4;i<70;i+=2){
dp[i][0]=(nCr(i-1,i/2)%mod + dp[i-2][1])%mod;
dp[i][1]=(nCr(i-2,i/2)%mod + dp[i-2][0])%mod;
dp[i][2]+=dp[i-2][2]%mod;
}
int t =fs.nextInt();
while(t-->0){
int n =fs.nextInt();
// solve(n);
out.println(dp[n][0]+" "+dp[n][1]+" "+dp[n][2]);
}
out.close();
}
}
|
Java
|
["5\n\n2\n\n4\n\n6\n\n8\n\n60"]
|
2 seconds
|
["1 0 1\n3 2 1\n12 7 1\n42 27 1\n341102826 248150916 1"]
|
NoteIn the first test case, Alex wins if he receives the card $$$2$$$ (he plays it, and Boris cannot respond). If Alex receives the card $$$1$$$, the game ends in a draw.In the second test case: Alex wins if he receives the cards $$$[3, 4]$$$, $$$[2, 4]$$$ or $$$[1, 4]$$$; Boris wins if Alex receives the cards $$$[1, 2]$$$ or $$$[1, 3]$$$; the game ends in a draw if Alex receives the cards $$$[2, 3]$$$.
|
Java 17
|
standard input
|
[
"combinatorics",
"constructive algorithms",
"dp",
"games"
] |
63fc2fb08d248f8ccdb3f5364c96dac1
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 30$$$) — the number of test cases. Then, $$$t$$$ lines follow. The $$$i$$$-th line contains one even integer $$$n$$$ ($$$2 \le n \le 60$$$).
| 1,500
|
For each test case, print three integers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. Since the answers can be large, print them modulo $$$998244353$$$.
|
standard output
| |
PASSED
|
28766bcbac418fc2e44f5fb9bb3ae4ea
|
train_109.jsonl
|
1664462100
|
Consider a game with $$$n$$$ cards ($$$n$$$ is even). Each card has a number written on it, between $$$1$$$ and $$$n$$$. All numbers on the cards are different. We say that a card with number $$$x$$$ is stronger than a card with number $$$y$$$ if $$$x > y$$$.Two players, Alex and Boris, play this game. In the beginning, each of them receives exactly $$$\frac{n}{2}$$$ cards, so each card belongs to exactly one player. Then, they take turns. Alex goes first, then Boris, then Alex again, and so on.On a player's turn, he must play exactly one of his cards. Then, if the opponent doesn't have any cards stronger than the card played, the opponent loses, and the game ends. Otherwise, the opponent has to play a stronger card (exactly one card as well). These two cards are removed from the game, and the turn ends. If there are no cards left, the game ends in a draw; otherwise it's the opponent's turn.Consider all possible ways to distribute the cards between two players, so that each of them receives exactly half of the cards. You have to calculate three numbers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. You may assume that both players play optimally (i. e. if a player can win no matter how his opponent plays, he wins). Two ways to distribute the cards are different if there is at least one card such that, in one of these ways, it is given to Alex, and in the other way, it is given to Boris.For example, suppose $$$n = 4$$$, Alex receives the cards $$$[2, 3]$$$, and Boris receives the cards $$$[1, 4]$$$. Then the game may go as follows: if Alex plays the card $$$2$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$3$$$. So, the game ends in a draw; if Alex plays the card $$$3$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$2$$$. So, the game ends in a draw. So, in this case, the game ends in a draw.
|
512 megabytes
|
import java.util.*;
public class Main {
static Scanner sc=null;
static long C[][]=new long[100][100];
static final int mod = 998244353 ;
public static void main(String[] args) {
sc = new Scanner(System.in);
C[ 0 ][ 0 ] = 1;
for(int n = 0;n <= 60;n++) {
for(int k = 0;k <= n;k++) {
if(k == 0 || n == k) C[k][n] = 1;
else {
C[k][n] = (C[k][n-1] + C[k-1][n-1]) % mod;
}
}
}
int t=sc.nextInt();
while(t --> 0) {
solve();
}
}
public static void solve(){
int n=sc.nextInt();
long dp[][ ] = new long[n+1][2];
dp[2][0] = 1;
dp[2][1] = 0;
for(int i = 4;i <= n;i += 2) {
dp[i][ 0 ] = (C[i / 2 - 1][i - 2] + dp[i - 2][ 1 ] + C[i / 2 - 2][i - 2]) % mod;
dp[ i ][ 1 ] = (dp[i - 2][ 0 ] + C[i / 2 - 2][i - 2]) % mod;
}
System.out.println(dp[ n ][ 0 ]+" "+dp[ n ][ 1 ]+" "+1);
}
}
|
Java
|
["5\n\n2\n\n4\n\n6\n\n8\n\n60"]
|
2 seconds
|
["1 0 1\n3 2 1\n12 7 1\n42 27 1\n341102826 248150916 1"]
|
NoteIn the first test case, Alex wins if he receives the card $$$2$$$ (he plays it, and Boris cannot respond). If Alex receives the card $$$1$$$, the game ends in a draw.In the second test case: Alex wins if he receives the cards $$$[3, 4]$$$, $$$[2, 4]$$$ or $$$[1, 4]$$$; Boris wins if Alex receives the cards $$$[1, 2]$$$ or $$$[1, 3]$$$; the game ends in a draw if Alex receives the cards $$$[2, 3]$$$.
|
Java 17
|
standard input
|
[
"combinatorics",
"constructive algorithms",
"dp",
"games"
] |
63fc2fb08d248f8ccdb3f5364c96dac1
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 30$$$) — the number of test cases. Then, $$$t$$$ lines follow. The $$$i$$$-th line contains one even integer $$$n$$$ ($$$2 \le n \le 60$$$).
| 1,500
|
For each test case, print three integers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. Since the answers can be large, print them modulo $$$998244353$$$.
|
standard output
| |
PASSED
|
c8f43ab24ac9ac80cc73493b5f17a574
|
train_109.jsonl
|
1664462100
|
Consider a game with $$$n$$$ cards ($$$n$$$ is even). Each card has a number written on it, between $$$1$$$ and $$$n$$$. All numbers on the cards are different. We say that a card with number $$$x$$$ is stronger than a card with number $$$y$$$ if $$$x > y$$$.Two players, Alex and Boris, play this game. In the beginning, each of them receives exactly $$$\frac{n}{2}$$$ cards, so each card belongs to exactly one player. Then, they take turns. Alex goes first, then Boris, then Alex again, and so on.On a player's turn, he must play exactly one of his cards. Then, if the opponent doesn't have any cards stronger than the card played, the opponent loses, and the game ends. Otherwise, the opponent has to play a stronger card (exactly one card as well). These two cards are removed from the game, and the turn ends. If there are no cards left, the game ends in a draw; otherwise it's the opponent's turn.Consider all possible ways to distribute the cards between two players, so that each of them receives exactly half of the cards. You have to calculate three numbers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. You may assume that both players play optimally (i. e. if a player can win no matter how his opponent plays, he wins). Two ways to distribute the cards are different if there is at least one card such that, in one of these ways, it is given to Alex, and in the other way, it is given to Boris.For example, suppose $$$n = 4$$$, Alex receives the cards $$$[2, 3]$$$, and Boris receives the cards $$$[1, 4]$$$. Then the game may go as follows: if Alex plays the card $$$2$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$3$$$. So, the game ends in a draw; if Alex plays the card $$$3$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$2$$$. So, the game ends in a draw. So, in this case, the game ends in a draw.
|
512 megabytes
|
import java.util.*;
import java.util.function.*;
import java.io.*;
// you can compare with output.txt and expected out
public class RoundEdu136C {
MyPrintWriter out;
MyScanner in;
// final static long FIXED_RANDOM;
// static {
// FIXED_RANDOM = System.currentTimeMillis();
// }
final static String IMPOSSIBLE = "IMPOSSIBLE";
final static String POSSIBLE = "POSSIBLE";
final static String YES = "YES";
final static String NO = "NO";
private void initIO(boolean isFileIO) {
if (System.getProperty("ONLINE_JUDGE") == null && isFileIO) {
try{
in = new MyScanner(new FileInputStream("input.txt"));
out = new MyPrintWriter(new FileOutputStream("output.txt"));
}
catch(FileNotFoundException e){
e.printStackTrace();
}
}
else{
in = new MyScanner(System.in);
out = new MyPrintWriter(new BufferedOutputStream(System.out));
}
}
public static void main(String[] args){
// Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
// the code for the deep recursion (more than 100k or 3~400k or so)
// Thread t = new Thread(null, new RoundEdu130F(), "threadName", 1<<28);
// t.start();
// t.join();
RoundEdu136C sol = new RoundEdu136C();
sol.run();
}
private void run() {
boolean isDebug = false;
boolean isFileIO = true;
boolean hasMultipleTests = true;
initIO(isFileIO);
int t = hasMultipleTests? in.nextInt() : 1;
for (int i = 1; i <= t; ++i) {
if(isDebug){
out.printf("Test %d\n", i);
}
getInput();
solve();
printOutput();
}
in.close();
out.close();
}
// use suitable one between matrix(2, n), matrix(n, 2), transposedMatrix(2, n) for graph edges, pairs, ...
final long MOD = 998244353;
int n;
void getInput() {
n = in.nextInt();
}
void printOutput() {
out.print(alex);
out.print(' ');
out.print(boris);
out.print(' ');
out.println(draw);
}
long alex, boris, draw;
void solve(){
alex = 0;
boris = 0;
draw = 0;
// if alex has n, she always wins
// if boris has n and n-1, he always wins
// if boris has n and alex has n-1,
// alex must play n-1 at the first turn
long[][] choose = new long[n+1][n+1];
choose[0][0] = 1;
for(int i=1; i<=n; i++) {
choose[i][0] = 1;
for(int j=1; j<=i; j++)
choose[i][j] = (choose[i-1][j] + choose[i-1][j-1]) % MOD;
}
boolean isAlexTurn = true;
for(int i=n; i>=1; i-=2) {
// alex has i
if(isAlexTurn)
alex += choose[i-1][i/2];
else
boris += choose[i-1][i/2];
// boris has i and i-1
if(i >= 4) {
if(isAlexTurn)
boris += choose[i-2][i/2];
else
alex += choose[i-2][i/2];
}
isAlexTurn ^= true;
}
draw = 1;
alex %= MOD;
boris %= MOD;
}
// Optional<T> solve()
// return Optional.empty();
static class Pair implements Comparable<Pair>{
final static long FIXED_RANDOM = System.currentTimeMillis();
int first, second;
public Pair(int first, int second) {
this.first = first;
this.second = second;
}
@Override
public int hashCode() {
// http://xorshift.di.unimi.it/splitmix64.c
long x = first;
x <<= 32;
x += second;
x += FIXED_RANDOM;
x += 0x9e3779b97f4a7c15l;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9l;
x = (x ^ (x >> 27)) * 0x94d049bb133111ebl;
return (int)(x ^ (x >> 31));
}
@Override
public boolean equals(Object obj) {
// if (this == obj)
// return true;
// if (obj == null)
// return false;
// if (getClass() != obj.getClass())
// return false;
Pair other = (Pair) obj;
return first == other.first && second == other.second;
}
@Override
public String toString() {
return "[" + first + "," + second + "]";
}
@Override
public int compareTo(Pair o) {
int cmp = Integer.compare(first, o.first);
return cmp != 0? cmp: Integer.compare(second, o.second);
}
}
public static class MyScanner {
BufferedReader br;
StringTokenizer st;
// 32768?
public MyScanner(InputStream is, int bufferSize) {
br = new BufferedReader(new InputStreamReader(is), bufferSize);
}
public MyScanner(InputStream is) {
br = new BufferedReader(new InputStreamReader(is));
// br = new BufferedReader(new InputStreamReader(System.in));
// br = new BufferedReader(new InputStreamReader(new FileInputStream("input.txt")));
}
public void close() {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine(){
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
int[][] nextMatrix(int n, int m) {
return nextMatrix(n, m, 0);
}
int[][] nextMatrix(int n, int m, int offset) {
int[][] mat = new int[n][m];
for(int i=0; i<n; i++) {
for(int j=0; j<m; j++) {
mat[i][j] = nextInt()+offset;
}
}
return mat;
}
int[][] nextTransposedMatrix(int n, int m){
return nextTransposedMatrix(n, m, 0);
}
int[][] nextTransposedMatrix(int n, int m, int offset){
int[][] mat = new int[m][n];
for(int i=0; i<n; i++) {
for(int j=0; j<m; j++) {
mat[j][i] = nextInt()+offset;
}
}
return mat;
}
int[] nextIntArray(int len) {
return nextIntArray(len, 0);
}
int[] nextIntArray(int len, int offset){
int[] a = new int[len];
for(int j=0; j<len; j++)
a[j] = nextInt()+offset;
return a;
}
long[] nextLongArray(int len) {
return nextLongArray(len, 0);
}
long[] nextLongArray(int len, int offset){
long[] a = new long[len];
for(int j=0; j<len; j++)
a[j] = nextLong()+offset;
return a;
}
String[] nextStringArray(int len) {
String[] s = new String[len];
for(int i=0; i<len; i++)
s[i] = next();
return s;
}
}
public static class MyPrintWriter extends PrintWriter{
public MyPrintWriter(OutputStream os) {
super(os);
}
// public <T> void printlnAns(Optional<T> ans) {
// if(ans.isEmpty())
// println(-1);
// else
// printlnAns(ans.get());
// }
public void printlnAns(OptionalInt ans) {
println(ans.orElse(-1));
}
public void printlnAns(long ans) {
println(ans);
}
public void printlnAns(int ans) {
println(ans);
}
public void printlnAns(boolean[] ans) {
for(boolean b: ans)
printlnAns(b);
}
public void printlnAns(boolean ans) {
if(ans)
println(YES);
else
println(NO);
}
public void printAns(long[] arr){
if(arr != null && arr.length > 0){
print(arr[0]);
for(int i=1; i<arr.length; i++){
print(" ");
print(arr[i]);
}
}
}
public void printlnAns(long[] arr){
printAns(arr);
println();
}
public void printAns(int[] arr){
if(arr != null && arr.length > 0){
print(arr[0]);
for(int i=1; i<arr.length; i++){
print(" ");
print(arr[i]);
}
}
}
public void printlnAns(int[] arr){
printAns(arr);
println();
}
public <T> void printAns(ArrayList<T> arr){
if(arr != null && arr.size() > 0){
print(arr.get(0));
for(int i=1; i<arr.size(); i++){
print(" ");
print(arr.get(i));
}
}
}
public <T> void printlnAns(ArrayList<T> arr){
printAns(arr);
println();
}
public void printAns(int[] arr, int add){
if(arr != null && arr.length > 0){
print(arr[0]+add);
for(int i=1; i<arr.length; i++){
print(" ");
print(arr[i]+add);
}
}
}
public void printlnAns(int[] arr, int add){
printAns(arr, add);
println();
}
public void printAns(ArrayList<Integer> arr, int add) {
if(arr != null && arr.size() > 0){
print(arr.get(0)+add);
for(int i=1; i<arr.size(); i++){
print(" ");
print(arr.get(i)+add);
}
}
}
public void printlnAns(ArrayList<Integer> arr, int add){
printAns(arr, add);
println();
}
public void printlnAnsSplit(long[] arr, int split){
if(arr != null){
for(int i=0; i<arr.length; i+=split){
print(arr[i]);
for(int j=i+1; j<i+split; j++){
print(" ");
print(arr[j]);
}
println();
}
}
}
public void printlnAnsSplit(int[] arr, int split){
if(arr != null){
for(int i=0; i<arr.length; i+=split){
print(arr[i]);
for(int j=i+1; j<i+split; j++){
print(" ");
print(arr[j]);
}
println();
}
}
}
public <T> void printlnAnsSplit(ArrayList<T> arr, int split){
if(arr != null && !arr.isEmpty()){
for(int i=0; i<arr.size(); i+=split){
print(arr.get(i));
for(int j=i+1; j<i+split; j++){
print(" ");
print(arr.get(j));
}
println();
}
}
}
}
static private void permutateAndSort(long[] a) {
int n = a.length;
Random R = new Random(System.currentTimeMillis());
for(int i=0; i<n; i++) {
int t = R.nextInt(n-i);
long temp = a[n-1-i];
a[n-1-i] = a[t];
a[t] = temp;
}
Arrays.sort(a);
}
static private void permutateAndSort(int[] a) {
int n = a.length;
Random R = new Random(System.currentTimeMillis());
for(int i=0; i<n; i++) {
int t = R.nextInt(n-i);
int temp = a[n-1-i];
a[n-1-i] = a[t];
a[t] = temp;
}
Arrays.sort(a);
}
static private int[][] constructChildren(int n, int[] parent, int parentRoot){
int[][] childrens = new int[n][];
int[] numChildren = new int[n];
for(int i=0; i<parent.length; i++) {
if(parent[i] != parentRoot)
numChildren[parent[i]]++;
}
for(int i=0; i<n; i++) {
childrens[i] = new int[numChildren[i]];
}
int[] idx = new int[n];
for(int i=0; i<parent.length; i++) {
if(parent[i] != parentRoot)
childrens[parent[i]][idx[parent[i]]++] = i;
}
return childrens;
}
static private int[][][] constructDirectedNeighborhood(int n, int[][] e){
int[] inDegree = new int[n];
int[] outDegree = new int[n];
for(int i=0; i<e.length; i++) {
int u = e[i][0];
int v = e[i][1];
outDegree[u]++;
inDegree[v]++;
}
int[][] inNeighbors = new int[n][];
int[][] outNeighbors = new int[n][];
for(int i=0; i<n; i++) {
inNeighbors[i] = new int[inDegree[i]];
outNeighbors[i] = new int[outDegree[i]];
}
for(int i=0; i<e.length; i++) {
int u = e[i][0];
int v = e[i][1];
outNeighbors[u][--outDegree[u]] = v;
inNeighbors[v][--inDegree[v]] = u;
}
return new int[][][] {inNeighbors, outNeighbors};
}
static private int[][] constructNeighborhood(int n, int[][] e) {
int[] degree = new int[n];
for(int i=0; i<e.length; i++) {
int u = e[i][0];
int v = e[i][1];
degree[u]++;
degree[v]++;
}
int[][] neighbors = new int[n][];
for(int i=0; i<n; i++)
neighbors[i] = new int[degree[i]];
for(int i=0; i<e.length; i++) {
int u = e[i][0];
int v = e[i][1];
neighbors[u][--degree[u]] = v;
neighbors[v][--degree[v]] = u;
}
return neighbors;
}
static private void drawGraph(int[][] e) {
makeDotUndirected(e);
try {
final Process process = new ProcessBuilder("dot", "-Tpng", "graph.dot")
.redirectOutput(new File("graph.png"))
.start();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
static private void makeDotUndirected(int[][] e) {
MyPrintWriter out2 = null;
try {
out2 = new MyPrintWriter(new FileOutputStream("graph.dot"));
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
out2.println("strict graph {");
for(int i=0; i<e.length; i++){
out2.println(e[i][0] + "--" + e[i][1] + ";");
}
out2.println("}");
out2.close();
}
static private void makeDotDirected(int[][] e) {
MyPrintWriter out2 = null;
try {
out2 = new MyPrintWriter(new FileOutputStream("graph.dot"));
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
out2.println("strict digraph {");
for(int i=0; i<e.length; i++){
out2.println(e[i][0] + "->" + e[i][1] + ";");
}
out2.println("}");
out2.close();
}
}
|
Java
|
["5\n\n2\n\n4\n\n6\n\n8\n\n60"]
|
2 seconds
|
["1 0 1\n3 2 1\n12 7 1\n42 27 1\n341102826 248150916 1"]
|
NoteIn the first test case, Alex wins if he receives the card $$$2$$$ (he plays it, and Boris cannot respond). If Alex receives the card $$$1$$$, the game ends in a draw.In the second test case: Alex wins if he receives the cards $$$[3, 4]$$$, $$$[2, 4]$$$ or $$$[1, 4]$$$; Boris wins if Alex receives the cards $$$[1, 2]$$$ or $$$[1, 3]$$$; the game ends in a draw if Alex receives the cards $$$[2, 3]$$$.
|
Java 17
|
standard input
|
[
"combinatorics",
"constructive algorithms",
"dp",
"games"
] |
63fc2fb08d248f8ccdb3f5364c96dac1
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 30$$$) — the number of test cases. Then, $$$t$$$ lines follow. The $$$i$$$-th line contains one even integer $$$n$$$ ($$$2 \le n \le 60$$$).
| 1,500
|
For each test case, print three integers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. Since the answers can be large, print them modulo $$$998244353$$$.
|
standard output
| |
PASSED
|
e7eda6e33593e4121f32706e161e67d5
|
train_109.jsonl
|
1664462100
|
Consider a game with $$$n$$$ cards ($$$n$$$ is even). Each card has a number written on it, between $$$1$$$ and $$$n$$$. All numbers on the cards are different. We say that a card with number $$$x$$$ is stronger than a card with number $$$y$$$ if $$$x > y$$$.Two players, Alex and Boris, play this game. In the beginning, each of them receives exactly $$$\frac{n}{2}$$$ cards, so each card belongs to exactly one player. Then, they take turns. Alex goes first, then Boris, then Alex again, and so on.On a player's turn, he must play exactly one of his cards. Then, if the opponent doesn't have any cards stronger than the card played, the opponent loses, and the game ends. Otherwise, the opponent has to play a stronger card (exactly one card as well). These two cards are removed from the game, and the turn ends. If there are no cards left, the game ends in a draw; otherwise it's the opponent's turn.Consider all possible ways to distribute the cards between two players, so that each of them receives exactly half of the cards. You have to calculate three numbers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. You may assume that both players play optimally (i. e. if a player can win no matter how his opponent plays, he wins). Two ways to distribute the cards are different if there is at least one card such that, in one of these ways, it is given to Alex, and in the other way, it is given to Boris.For example, suppose $$$n = 4$$$, Alex receives the cards $$$[2, 3]$$$, and Boris receives the cards $$$[1, 4]$$$. Then the game may go as follows: if Alex plays the card $$$2$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$3$$$. So, the game ends in a draw; if Alex plays the card $$$3$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$2$$$. So, the game ends in a draw. So, in this case, the game ends in a draw.
|
512 megabytes
|
//package codeForces._1739;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class c {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine());
int[][] arr = {{1,0,1},
{3,2,1},
{12,7,1},
{42,27,1},
{153,98,1},
{560,363,1},
{2079,1352,1},
{7787,5082,1},
{29392,19227,1},
{111605,73150,1},
{425866,279565,1},
{1631643,1072512,1},
{6272812,4127787,1},
{24186087,15930512,1},
{93489272,61628247,1},
{362168442,238911947,1},
{407470704,927891162,1},
{474237047,614943428,1},
{319176974,87534470,1},
{131938523,955113935,1},
{558075845,644336680,1},
{544270478,253841470,1},
{209493498,700054910,1},
{859106804,457241336,1},
{921005664,6522991,1},
{366933608,353887625,1},
{142064435,432533537,1},
{741221694,874398972,1},
{297907370,545598131,1},
{341102826,248150916,1}
};
while (t-->0){
int n = Integer.parseInt(br.readLine());
System.out.println(arr[n/2-1][0]+" "+arr[n/2-1][1]+" "+arr[n/2-1][2]);
}
}
}
|
Java
|
["5\n\n2\n\n4\n\n6\n\n8\n\n60"]
|
2 seconds
|
["1 0 1\n3 2 1\n12 7 1\n42 27 1\n341102826 248150916 1"]
|
NoteIn the first test case, Alex wins if he receives the card $$$2$$$ (he plays it, and Boris cannot respond). If Alex receives the card $$$1$$$, the game ends in a draw.In the second test case: Alex wins if he receives the cards $$$[3, 4]$$$, $$$[2, 4]$$$ or $$$[1, 4]$$$; Boris wins if Alex receives the cards $$$[1, 2]$$$ or $$$[1, 3]$$$; the game ends in a draw if Alex receives the cards $$$[2, 3]$$$.
|
Java 17
|
standard input
|
[
"combinatorics",
"constructive algorithms",
"dp",
"games"
] |
63fc2fb08d248f8ccdb3f5364c96dac1
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 30$$$) — the number of test cases. Then, $$$t$$$ lines follow. The $$$i$$$-th line contains one even integer $$$n$$$ ($$$2 \le n \le 60$$$).
| 1,500
|
For each test case, print three integers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. Since the answers can be large, print them modulo $$$998244353$$$.
|
standard output
| |
PASSED
|
d9103d3558737f2cdc20ffb9d2729363
|
train_109.jsonl
|
1664462100
|
Consider a game with $$$n$$$ cards ($$$n$$$ is even). Each card has a number written on it, between $$$1$$$ and $$$n$$$. All numbers on the cards are different. We say that a card with number $$$x$$$ is stronger than a card with number $$$y$$$ if $$$x > y$$$.Two players, Alex and Boris, play this game. In the beginning, each of them receives exactly $$$\frac{n}{2}$$$ cards, so each card belongs to exactly one player. Then, they take turns. Alex goes first, then Boris, then Alex again, and so on.On a player's turn, he must play exactly one of his cards. Then, if the opponent doesn't have any cards stronger than the card played, the opponent loses, and the game ends. Otherwise, the opponent has to play a stronger card (exactly one card as well). These two cards are removed from the game, and the turn ends. If there are no cards left, the game ends in a draw; otherwise it's the opponent's turn.Consider all possible ways to distribute the cards between two players, so that each of them receives exactly half of the cards. You have to calculate three numbers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. You may assume that both players play optimally (i. e. if a player can win no matter how his opponent plays, he wins). Two ways to distribute the cards are different if there is at least one card such that, in one of these ways, it is given to Alex, and in the other way, it is given to Boris.For example, suppose $$$n = 4$$$, Alex receives the cards $$$[2, 3]$$$, and Boris receives the cards $$$[1, 4]$$$. Then the game may go as follows: if Alex plays the card $$$2$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$3$$$. So, the game ends in a draw; if Alex plays the card $$$3$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$2$$$. So, the game ends in a draw. So, in this case, the game ends in a draw.
|
512 megabytes
|
/**
* @author vivek
* <>
*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class C {
// function to calculate ncr mod k
static int nCrModp(int n, int r, int p)
{
// The array C is going to store last
// row of pascal triangle at the end.
// And last entry of last row is nCr
int C[] = new int[r + 1];
C[0] = 1; // Top row of Pascal Triangle
// One by constructs remaining rows of Pascal
// Triangle from top to bottom
for (int i = 1; i <= n; i++) {
// Fill entries of current row using previous
// row values
for (int j = Math.min(i, r); j > 0; j--)
// nCj = (n-1)Cj + (n-1)C(j-1);
C[j] = (C[j] + C[j - 1]) % p;
}
return C[r];
}
private static long fun(int n){
if (n<2){
return 0;
}
if (dp[n]!=-1){
return dp[n];
}
long ans = 0;
int mod = 998244353;
ans += nCrModp(n-1, n/2-1, mod);
if(n>4)
ans+= nCrModp(n-4, n/2-3, mod);
ans%=mod;
ans += fun(n-4);
ans%=mod;
dp[n] = ans;
return ans;
}
private static long[] dp ;
private static void solveTC(int __) {
/* For Google */
// ans.append("Case #").append(__).append(": ");
//code start
int n=scn.nextInt();
int mod = 998244353;
long total = nCrModp(n, n/2, mod);
long alex = fun(n);
long ans = total - alex - 1 + mod;
ans%=mod;
print(alex + " " + ans + " "+1);
//code end
print("\n");
}
public static void main(String[] args) {
scn = new Scanner();
ans = new StringBuilder();
int t = scn.nextInt();
dp = new long[100];
Arrays.fill(dp, -1);
// int t = 1;
// int limit= ;
// sieve(limit);
/*
try {
System.setOut(new PrintStream(new File("file_i_o\\output.txt")));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
*/
for (int i = 1; i <= t; i++) {
solveTC(i);
}
System.out.print(ans);
}
//Stuff for prime start
/**
* sorting algos
*/
private static void sort(int[] arr) {
ArrayList<Integer> li = new ArrayList<>(arr.length);
for (int ele : arr) li.add(ele);
Collections.sort(li);
for (int i = 0; i < li.size(); i++) {
arr[i] = li.get(i);
}
}
private static void sort(long[] arr) {
ArrayList<Long> li = new ArrayList<>(arr.length);
for (long ele : arr) li.add(ele);
Collections.sort(li);
for (int i = 0; i < li.size(); i++) {
arr[i] = li.get(i);
}
}
private static void sort(float[] arr) {
ArrayList<Float> li = new ArrayList<>(arr.length);
for (float ele : arr) li.add(ele);
Collections.sort(li);
for (int i = 0; i < li.size(); i++) {
arr[i] = li.get(i);
}
}
private static void sort(double[] arr) {
ArrayList<Double> li = new ArrayList<>(arr.length);
for (double ele : arr) li.add(ele);
Collections.sort(li);
for (int i = 0; i < li.size(); i++) {
arr[i] = li.get(i);
}
}
/**
* List containing prime numbers <br>
* <b>i<sup>th</sup></b> position contains <b>i<sup>th</sup></b> prime number <br>
* 0th index is <b>null</b>
*/
private static ArrayList<Integer> listOfPrimes;
/**
* query <b>i<sup>th</sup></b> element to get if its prime of not
*/
private static boolean[] isPrime;
/**
* Performs Sieve of Erathosnesis and initialise isPrime array and listOfPrimes list
*
* @param limit the number till which sieve is to be performed
*/
private static void sieve(int limit) {
listOfPrimes = new ArrayList<>();
listOfPrimes.add(null);
boolean[] array = new boolean[limit + 1];
Arrays.fill(array, true);
array[0] = false;
array[1] = false;
for (int i = 2; i <= limit; i++) {
if (array[i]) {
for (long j = (long) i * i; j <= limit; j += i) {
array[(int) j] = false;
}
}
}
isPrime = array;
for (int i = 0; i <= limit; i++) {
if (array[i]) {
listOfPrimes.add(i);
}
}
}
//stuff for prime end
/**
* Calculates the Least Common Multiple of two numbers
*
* @param a First number
* @param b Second Number
* @return Least Common Multiple of <b>a</b> and <b>b</b>
*/
private static long lcm(long a, long b) {
return a * b / gcd(a, b);
}
/**
* Calculates the Greatest Common Divisor of two numbers
*
* @param a First number
* @param b Second Number
* @return Greatest Common Divisor of <b>a</b> and <b>b</b>
*/
private static long gcd(long a, long b) {
return (b == 0) ? a : gcd(b, a % b);
}
static void print(Object obj) {
ans.append(obj.toString());
}
static Scanner scn;
static StringBuilder ans;
//Fast Scanner
static class Scanner {
BufferedReader br;
StringTokenizer st;
public Scanner() {
br = new BufferedReader(new
InputStreamReader(System.in));
/*
try {
br = new BufferedReader(new
InputStreamReader(new FileInputStream(new File("file_i_o\\input.txt"))));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
*/
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
int[] nextIntArray(int n) {
int[] array = new int[n];
for (int i = 0; i < n; i++) {
array[i] = nextInt();
}
return array;
}
Integer[] nextIntegerArray(int n) {
Integer[] array = new Integer[n];
for (int i = 0; i < n; i++) {
array[i] = nextInt();
}
return array;
}
long[] nextLongArray(int n) {
long[] array = new long[n];
for (int i = 0; i < n; i++) {
array[i] = nextLong();
}
return array;
}
String[] nextStringArray() {
return nextLine().split(" ");
}
String[] nextStringArray(int n) {
String[] array = new String[n];
for (int i = 0; i < n; i++) {
array[i] = next();
}
return array;
}
}
}
|
Java
|
["5\n\n2\n\n4\n\n6\n\n8\n\n60"]
|
2 seconds
|
["1 0 1\n3 2 1\n12 7 1\n42 27 1\n341102826 248150916 1"]
|
NoteIn the first test case, Alex wins if he receives the card $$$2$$$ (he plays it, and Boris cannot respond). If Alex receives the card $$$1$$$, the game ends in a draw.In the second test case: Alex wins if he receives the cards $$$[3, 4]$$$, $$$[2, 4]$$$ or $$$[1, 4]$$$; Boris wins if Alex receives the cards $$$[1, 2]$$$ or $$$[1, 3]$$$; the game ends in a draw if Alex receives the cards $$$[2, 3]$$$.
|
Java 17
|
standard input
|
[
"combinatorics",
"constructive algorithms",
"dp",
"games"
] |
63fc2fb08d248f8ccdb3f5364c96dac1
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 30$$$) — the number of test cases. Then, $$$t$$$ lines follow. The $$$i$$$-th line contains one even integer $$$n$$$ ($$$2 \le n \le 60$$$).
| 1,500
|
For each test case, print three integers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. Since the answers can be large, print them modulo $$$998244353$$$.
|
standard output
| |
PASSED
|
733f88040b80c36072e822b5dadca7e3
|
train_109.jsonl
|
1664462100
|
Consider a game with $$$n$$$ cards ($$$n$$$ is even). Each card has a number written on it, between $$$1$$$ and $$$n$$$. All numbers on the cards are different. We say that a card with number $$$x$$$ is stronger than a card with number $$$y$$$ if $$$x > y$$$.Two players, Alex and Boris, play this game. In the beginning, each of them receives exactly $$$\frac{n}{2}$$$ cards, so each card belongs to exactly one player. Then, they take turns. Alex goes first, then Boris, then Alex again, and so on.On a player's turn, he must play exactly one of his cards. Then, if the opponent doesn't have any cards stronger than the card played, the opponent loses, and the game ends. Otherwise, the opponent has to play a stronger card (exactly one card as well). These two cards are removed from the game, and the turn ends. If there are no cards left, the game ends in a draw; otherwise it's the opponent's turn.Consider all possible ways to distribute the cards between two players, so that each of them receives exactly half of the cards. You have to calculate three numbers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. You may assume that both players play optimally (i. e. if a player can win no matter how his opponent plays, he wins). Two ways to distribute the cards are different if there is at least one card such that, in one of these ways, it is given to Alex, and in the other way, it is given to Boris.For example, suppose $$$n = 4$$$, Alex receives the cards $$$[2, 3]$$$, and Boris receives the cards $$$[1, 4]$$$. Then the game may go as follows: if Alex plays the card $$$2$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$3$$$. So, the game ends in a draw; if Alex plays the card $$$3$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$2$$$. So, the game ends in a draw. So, in this case, the game ends in a draw.
|
512 megabytes
|
import static java.lang.Math.*;
import static java.lang.System.*;
import java.io.*;
import java.util.*;
public class Template{
public static void main(String[] args)throws IOException
{
FastReader fr = new FastReader();
int t = fr.nextInt();
int max = 6;
int[] inp = new int[t];
for(int z=0;z<t;z++){
inp[z] = fr.nextInt();
max = max(max,inp[z]);
}
long[] A = new long[max+1];
long[] B = new long[max+1];
A[2] = 1;
B[2] = 0;
A[4] = 3;
B[4] = 2;
for(int i=6;i<=max;i+=2)
{
A[i] = (B[i-2] + nCrModp(i-1,i/2-1,998244353))% 998244353;
if(A[i] < 0) A[i] += 998244353;
B[i] = (A[i-2] + nCrModp(i-2,i/2-2,998244353))% 998244353;
if(B[i] < 0) B[i] += 998244353;
// System.out.println(C(i-2,i/2-2));
}
for(int i=0;i<t;i++)
{
System.out.println( A[inp[i]] + " " + B[inp[i]] + " " + 1);
}
// int t = sc.nextInt();
// for(int z=0;z<t;z++){
// int n= sc.nextInt();
// int[] arr = new int[n];
// for(int i = 0;i<n;i++)
// {
// arr[i] = sc.nextInt();
// }
// int sum = arr[0];
// String output = arr[0]+"";
// for(int i=1;i<n;i++)
// {
// if(arr[i] > sum || arr[i] == 0)
// {
// sum += arr[i];
// output += " " + sum;
// }
// else
// {
// output = "-1";
// break;
// }
// }
// out.println(output);
// }
// sc.close();
}
static int nCrModp(int n, int r, int p)
{
if (r > n - r)
r = n - r;
// The array C is going to store last
// row of pascal triangle at the end.
// And last entry of last row is nCr
int C[] = new int[r + 1];
C[0] = 1; // Top row of Pascal Triangle
// One by constructs remaining rows of Pascal
// Triangle from top to bottom
for (int i = 1; i <= n; i++)
{
// Fill entries of current row using previous
// row values
for (int j = Math.min(i, r); j > 0; j--)
{
// nCj = (n-1)Cj + (n-1)C(j-1);
C[j] = (C[j] + C[j - 1]) % p;
}
}
return C[r];
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements()) {
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble(){ return Double.parseDouble(next()); }
String nextLine()
{
String str = "";
try
{
if(st == null)
{
st = new StringTokenizer(br.readLine());
}
if(st.hasMoreTokens())
{
str = st.nextToken("\n");
}
else
{
str = br.readLine();
st = null;
}
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
}
|
Java
|
["5\n\n2\n\n4\n\n6\n\n8\n\n60"]
|
2 seconds
|
["1 0 1\n3 2 1\n12 7 1\n42 27 1\n341102826 248150916 1"]
|
NoteIn the first test case, Alex wins if he receives the card $$$2$$$ (he plays it, and Boris cannot respond). If Alex receives the card $$$1$$$, the game ends in a draw.In the second test case: Alex wins if he receives the cards $$$[3, 4]$$$, $$$[2, 4]$$$ or $$$[1, 4]$$$; Boris wins if Alex receives the cards $$$[1, 2]$$$ or $$$[1, 3]$$$; the game ends in a draw if Alex receives the cards $$$[2, 3]$$$.
|
Java 11
|
standard input
|
[
"combinatorics",
"constructive algorithms",
"dp",
"games"
] |
63fc2fb08d248f8ccdb3f5364c96dac1
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 30$$$) — the number of test cases. Then, $$$t$$$ lines follow. The $$$i$$$-th line contains one even integer $$$n$$$ ($$$2 \le n \le 60$$$).
| 1,500
|
For each test case, print three integers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. Since the answers can be large, print them modulo $$$998244353$$$.
|
standard output
| |
PASSED
|
883517e87e03775a673f218c14c7e2f7
|
train_109.jsonl
|
1664462100
|
Consider a game with $$$n$$$ cards ($$$n$$$ is even). Each card has a number written on it, between $$$1$$$ and $$$n$$$. All numbers on the cards are different. We say that a card with number $$$x$$$ is stronger than a card with number $$$y$$$ if $$$x > y$$$.Two players, Alex and Boris, play this game. In the beginning, each of them receives exactly $$$\frac{n}{2}$$$ cards, so each card belongs to exactly one player. Then, they take turns. Alex goes first, then Boris, then Alex again, and so on.On a player's turn, he must play exactly one of his cards. Then, if the opponent doesn't have any cards stronger than the card played, the opponent loses, and the game ends. Otherwise, the opponent has to play a stronger card (exactly one card as well). These two cards are removed from the game, and the turn ends. If there are no cards left, the game ends in a draw; otherwise it's the opponent's turn.Consider all possible ways to distribute the cards between two players, so that each of them receives exactly half of the cards. You have to calculate three numbers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. You may assume that both players play optimally (i. e. if a player can win no matter how his opponent plays, he wins). Two ways to distribute the cards are different if there is at least one card such that, in one of these ways, it is given to Alex, and in the other way, it is given to Boris.For example, suppose $$$n = 4$$$, Alex receives the cards $$$[2, 3]$$$, and Boris receives the cards $$$[1, 4]$$$. Then the game may go as follows: if Alex plays the card $$$2$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$3$$$. So, the game ends in a draw; if Alex plays the card $$$3$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$2$$$. So, the game ends in a draw. So, in this case, the game ends in a draw.
|
512 megabytes
|
import java.io.*; import java.util.*;
public class Main
{
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter pw = new PrintWriter(System.out);
long[][] arr = new long[61][2];
arr[2][0] = 1; arr[2][1] = 0;
for(int i=4; i<61; i+=2){
long mult = 1;
ArrayList<Integer> u = new ArrayList<>();
for(int j=1; j<=i/2; j++){
u.add(j);
}
int j = i;
while(j>i/2){
mult*=j;
j--;
int f = 0;
for(int k: u){
if(mult%k==0){
mult/=k;
u.remove(f);
f--;
break;
}
f++;
}
}
arr[i][0]=mult/2+arr[i-2][1];
arr[i][1]=mult-arr[i][0]-1;
arr[i][0]%=998244353;
arr[i][1]%=998244353;
}
int t = Integer.parseInt(br.readLine());
while(t-->0){
int n = Integer.parseInt(br.readLine());
pw.println(arr[n][0]+" "+arr[n][1]+" 1");
}
pw.close();
}
}
|
Java
|
["5\n\n2\n\n4\n\n6\n\n8\n\n60"]
|
2 seconds
|
["1 0 1\n3 2 1\n12 7 1\n42 27 1\n341102826 248150916 1"]
|
NoteIn the first test case, Alex wins if he receives the card $$$2$$$ (he plays it, and Boris cannot respond). If Alex receives the card $$$1$$$, the game ends in a draw.In the second test case: Alex wins if he receives the cards $$$[3, 4]$$$, $$$[2, 4]$$$ or $$$[1, 4]$$$; Boris wins if Alex receives the cards $$$[1, 2]$$$ or $$$[1, 3]$$$; the game ends in a draw if Alex receives the cards $$$[2, 3]$$$.
|
Java 11
|
standard input
|
[
"combinatorics",
"constructive algorithms",
"dp",
"games"
] |
63fc2fb08d248f8ccdb3f5364c96dac1
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 30$$$) — the number of test cases. Then, $$$t$$$ lines follow. The $$$i$$$-th line contains one even integer $$$n$$$ ($$$2 \le n \le 60$$$).
| 1,500
|
For each test case, print three integers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. Since the answers can be large, print them modulo $$$998244353$$$.
|
standard output
| |
PASSED
|
3aa3f1697544e3c8ec77005b960a8806
|
train_109.jsonl
|
1664462100
|
Consider a game with $$$n$$$ cards ($$$n$$$ is even). Each card has a number written on it, between $$$1$$$ and $$$n$$$. All numbers on the cards are different. We say that a card with number $$$x$$$ is stronger than a card with number $$$y$$$ if $$$x > y$$$.Two players, Alex and Boris, play this game. In the beginning, each of them receives exactly $$$\frac{n}{2}$$$ cards, so each card belongs to exactly one player. Then, they take turns. Alex goes first, then Boris, then Alex again, and so on.On a player's turn, he must play exactly one of his cards. Then, if the opponent doesn't have any cards stronger than the card played, the opponent loses, and the game ends. Otherwise, the opponent has to play a stronger card (exactly one card as well). These two cards are removed from the game, and the turn ends. If there are no cards left, the game ends in a draw; otherwise it's the opponent's turn.Consider all possible ways to distribute the cards between two players, so that each of them receives exactly half of the cards. You have to calculate three numbers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. You may assume that both players play optimally (i. e. if a player can win no matter how his opponent plays, he wins). Two ways to distribute the cards are different if there is at least one card such that, in one of these ways, it is given to Alex, and in the other way, it is given to Boris.For example, suppose $$$n = 4$$$, Alex receives the cards $$$[2, 3]$$$, and Boris receives the cards $$$[1, 4]$$$. Then the game may go as follows: if Alex plays the card $$$2$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$3$$$. So, the game ends in a draw; if Alex plays the card $$$3$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$2$$$. So, the game ends in a draw. So, in this case, the game ends in a draw.
|
512 megabytes
|
import java.io.*;
import java.util.*;
public class Main {
public static int INF = 0x3f3f3f3f, mod = 1000000007, mod9 = 998244353;
public static void main(String args[]){
try {
PrintWriter o = new PrintWriter(System.out);
boolean multiTest = true;
// init
if(multiTest) {
int t = fReader.nextInt(), loop = 0;
while (loop < t) {loop++;solve(o);}
} else solve(o);
o.close();
} catch (Exception e) {e.printStackTrace();}
}
static void solve(PrintWriter o) {
try {
int n = fReader.nextInt();
int k = n/2;
long[][][] dp = new long[k+10][k+10][3];
dp[0][0][2] = 1;
for(int i=0;i<=k;i++) {
for(int j=0;j<=k;j++) {
for(int t=0;t<3;t++) {
int turn = (i+j)%4;
if(turn == 0 || turn == 3) {
dp[i+1][j][t==2?0:t] += dp[i][j][t];
dp[i][j+1][t] += dp[i][j][t];
}
else {
dp[i+1][j][t] += dp[i][j][t];
dp[i][j+1][t==2?1:t] += dp[i][j][t];
}
dp[i][j][t] %= mod9;
}
}
}
o.println(dp[k][k][0] + " " + dp[k][k][1] + " " + dp[k][k][2]);
} catch (Exception e) {
e.printStackTrace();
}
}
public static int upper_bound(List<Integer> a, int val){
int l = 0, r = a.size();
while(l < r){
int mid = l + (r - l) / 2;
if(a.get(mid) <= val) l = mid + 1;
else r = mid;
}
return l;
}
public static int lower_bound(List<Integer> a, int val){
int l = 0, r = a.size();
while(l < r){
int mid = l + (r - l) / 2;
if(a.get(mid) < val) l = mid + 1;
else r = mid;
}
return l;
}
public static long gcd(long a, long b){
return b == 0 ? a : gcd(b, a%b);
}
public static long lcm(long a, long b){
return a / gcd(a,b)*b;
}
public static boolean isPrime(long x){
boolean ok = true;
for(long i=2;i<=Math.sqrt(x);i++){
if(x % i == 0){
ok = false;
break;
}
}
return ok;
}
public static void reverse(int[] array){
reverse(array, 0 , array.length-1);
}
public static void reverse(int[] array, int left, int right) {
if (array != null) {
int i = left;
for(int j = right; j > i; ++i) {
int tmp = array[j];
array[j] = array[i];
array[i] = tmp;
--j;
}
}
}
public static long qpow(long a, long n){
long ret = 1l;
while(n > 0){
if((n & 1) == 1){
ret = ret * a % mod;
}
n >>= 1;
a = a * a % mod;
}
return ret;
}
public static class DSU {
int[] parent;
int[] size;
int n;
public DSU(int n){
this.n = n;
parent = new int[n];
size = new int[n];
for(int i=0;i<n;i++){
parent[i] = i;
size[i] = 1;
}
}
public int find(int p){
while(parent[p] != p){
parent[p] = parent[parent[p]];
p = parent[p];
}
return p;
}
public void union(int p, int q){
int root_p = find(p);
int root_q = find(q);
if(root_p == root_q) return;
if(size[root_p] >= size[root_q]){
parent[root_q] = root_p;
size[root_p] += size[root_q];
size[root_q] = 0;
}
else{
parent[root_p] = root_q;
size[root_q] += size[root_p];
size[root_p] = 0;
}
n--;
}
public int getTotalComNum(){
return n;
}
public int getSize(int i){
return size[find(i)];
}
}
public static class FenWick {
int n;
long[] tree;
public FenWick(int n){
this.n = n;
tree = new long[n+1];
}
private void add(int x, long val){
while(x <= n){
tree[x] += val;
x += x&-x;
}
}
private long query(int x){
long ret = 0l;
while(x > 0){
ret += tree[x];
x -= x&-x;
}
return ret;
}
}
public static class Node implements Comparable<Node> {
Long fst;
Integer snd;
public Node(Long fst, Integer snd) {
this.fst = fst;
this.snd = snd;
}
@Override
public int hashCode() {
int prime = 31, res = 1;
res = prime*res + fst.hashCode();
res = prime*res + snd.hashCode();
return res;
}
@Override
public boolean equals(Object obj) {
if(obj instanceof Node) {
return fst.equals(((Node) obj).fst) && snd.equals(((Node) obj).snd);
}
return false;
}
@Override
public int compareTo(Node node) {
int result = Long.compare(fst, node.fst);
return result == 0 ? Integer.compare(snd, node.snd) : result;
}
}
public static class fReader {
private static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
private static StringTokenizer tokenizer = new StringTokenizer("");
private static String next() throws IOException{
while(!tokenizer.hasMoreTokens()){tokenizer = new StringTokenizer(reader.readLine());}
return tokenizer.nextToken();
}
public static int nextInt() throws IOException {return Integer.parseInt(next());}
public static Long nextLong() throws IOException {return Long.parseLong(next());}
public static double nextDouble() throws IOException {return Double.parseDouble(next());}
public static char nextChar() throws IOException {return next().toCharArray()[0];}
public static String nextString() throws IOException {return next();}
public static String nextLine() throws IOException {return reader.readLine();}
}
}
|
Java
|
["5\n\n2\n\n4\n\n6\n\n8\n\n60"]
|
2 seconds
|
["1 0 1\n3 2 1\n12 7 1\n42 27 1\n341102826 248150916 1"]
|
NoteIn the first test case, Alex wins if he receives the card $$$2$$$ (he plays it, and Boris cannot respond). If Alex receives the card $$$1$$$, the game ends in a draw.In the second test case: Alex wins if he receives the cards $$$[3, 4]$$$, $$$[2, 4]$$$ or $$$[1, 4]$$$; Boris wins if Alex receives the cards $$$[1, 2]$$$ or $$$[1, 3]$$$; the game ends in a draw if Alex receives the cards $$$[2, 3]$$$.
|
Java 11
|
standard input
|
[
"combinatorics",
"constructive algorithms",
"dp",
"games"
] |
63fc2fb08d248f8ccdb3f5364c96dac1
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 30$$$) — the number of test cases. Then, $$$t$$$ lines follow. The $$$i$$$-th line contains one even integer $$$n$$$ ($$$2 \le n \le 60$$$).
| 1,500
|
For each test case, print three integers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. Since the answers can be large, print them modulo $$$998244353$$$.
|
standard output
| |
PASSED
|
d4168d081319beda13353a310a222462
|
train_109.jsonl
|
1664462100
|
Consider a game with $$$n$$$ cards ($$$n$$$ is even). Each card has a number written on it, between $$$1$$$ and $$$n$$$. All numbers on the cards are different. We say that a card with number $$$x$$$ is stronger than a card with number $$$y$$$ if $$$x > y$$$.Two players, Alex and Boris, play this game. In the beginning, each of them receives exactly $$$\frac{n}{2}$$$ cards, so each card belongs to exactly one player. Then, they take turns. Alex goes first, then Boris, then Alex again, and so on.On a player's turn, he must play exactly one of his cards. Then, if the opponent doesn't have any cards stronger than the card played, the opponent loses, and the game ends. Otherwise, the opponent has to play a stronger card (exactly one card as well). These two cards are removed from the game, and the turn ends. If there are no cards left, the game ends in a draw; otherwise it's the opponent's turn.Consider all possible ways to distribute the cards between two players, so that each of them receives exactly half of the cards. You have to calculate three numbers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. You may assume that both players play optimally (i. e. if a player can win no matter how his opponent plays, he wins). Two ways to distribute the cards are different if there is at least one card such that, in one of these ways, it is given to Alex, and in the other way, it is given to Boris.For example, suppose $$$n = 4$$$, Alex receives the cards $$$[2, 3]$$$, and Boris receives the cards $$$[1, 4]$$$. Then the game may go as follows: if Alex plays the card $$$2$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$3$$$. So, the game ends in a draw; if Alex plays the card $$$3$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$2$$$. So, the game ends in a draw. So, in this case, the game ends in a draw.
|
512 megabytes
|
import java.io.*;
import java.util.*;
public class Main {
public static int INF = 0x3f3f3f3f, mod = 1000000007, mod9 = 998244353;
public static void main(String args[]){
try {
PrintWriter o = new PrintWriter(System.out);
boolean multiTest = true;
// init
if(multiTest) {
int t = fReader.nextInt(), loop = 0;
while (loop < t) {loop++;solve(o);}
} else solve(o);
o.close();
} catch (Exception e) {e.printStackTrace();}
}
static void solve(PrintWriter o) {
try {
int n = fReader.nextInt();
long[][] C = new long[n+10][n+10];
for(int i=0;i<=n;i++) {
C[i][0] = 1;
for(int j=1;j<i;j++) C[i][j] = (C[i-1][j] + C[i-1][j-1])%mod9;
C[i][i] = 1;
}
long a = 0, b = 0, c = 1;
for(int i=0;i<n/2;i++) {
if(i%2 == 0) {
a += C[n-2*i-1][n/2-i];
b += C[n-2*i-2][n/2-i];
}
else {
a += C[n-2*i-2][n/2-i];
b += C[n-2*i-1][n/2-i];
}
a %= mod9;
b %= mod9;
}
o.println(a + " " + b + " " + c);
} catch (Exception e) {
e.printStackTrace();
}
}
public static int upper_bound(List<Integer> a, int val){
int l = 0, r = a.size();
while(l < r){
int mid = l + (r - l) / 2;
if(a.get(mid) <= val) l = mid + 1;
else r = mid;
}
return l;
}
public static int lower_bound(List<Integer> a, int val){
int l = 0, r = a.size();
while(l < r){
int mid = l + (r - l) / 2;
if(a.get(mid) < val) l = mid + 1;
else r = mid;
}
return l;
}
public static long gcd(long a, long b){
return b == 0 ? a : gcd(b, a%b);
}
public static long lcm(long a, long b){
return a / gcd(a,b)*b;
}
public static boolean isPrime(long x){
boolean ok = true;
for(long i=2;i<=Math.sqrt(x);i++){
if(x % i == 0){
ok = false;
break;
}
}
return ok;
}
public static void reverse(int[] array){
reverse(array, 0 , array.length-1);
}
public static void reverse(int[] array, int left, int right) {
if (array != null) {
int i = left;
for(int j = right; j > i; ++i) {
int tmp = array[j];
array[j] = array[i];
array[i] = tmp;
--j;
}
}
}
public static long qpow(long a, long n){
long ret = 1l;
while(n > 0){
if((n & 1) == 1){
ret = ret * a % mod;
}
n >>= 1;
a = a * a % mod;
}
return ret;
}
public static class DSU {
int[] parent;
int[] size;
int n;
public DSU(int n){
this.n = n;
parent = new int[n];
size = new int[n];
for(int i=0;i<n;i++){
parent[i] = i;
size[i] = 1;
}
}
public int find(int p){
while(parent[p] != p){
parent[p] = parent[parent[p]];
p = parent[p];
}
return p;
}
public void union(int p, int q){
int root_p = find(p);
int root_q = find(q);
if(root_p == root_q) return;
if(size[root_p] >= size[root_q]){
parent[root_q] = root_p;
size[root_p] += size[root_q];
size[root_q] = 0;
}
else{
parent[root_p] = root_q;
size[root_q] += size[root_p];
size[root_p] = 0;
}
n--;
}
public int getTotalComNum(){
return n;
}
public int getSize(int i){
return size[find(i)];
}
}
public static class FenWick {
int n;
long[] tree;
public FenWick(int n){
this.n = n;
tree = new long[n+1];
}
private void add(int x, long val){
while(x <= n){
tree[x] += val;
x += x&-x;
}
}
private long query(int x){
long ret = 0l;
while(x > 0){
ret += tree[x];
x -= x&-x;
}
return ret;
}
}
public static class Node implements Comparable<Node> {
Long fst;
Integer snd;
public Node(Long fst, Integer snd) {
this.fst = fst;
this.snd = snd;
}
@Override
public int hashCode() {
int prime = 31, res = 1;
res = prime*res + fst.hashCode();
res = prime*res + snd.hashCode();
return res;
}
@Override
public boolean equals(Object obj) {
if(obj instanceof Node) {
return fst.equals(((Node) obj).fst) && snd.equals(((Node) obj).snd);
}
return false;
}
@Override
public int compareTo(Node node) {
int result = Long.compare(fst, node.fst);
return result == 0 ? Integer.compare(snd, node.snd) : result;
}
}
public static class fReader {
private static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
private static StringTokenizer tokenizer = new StringTokenizer("");
private static String next() throws IOException{
while(!tokenizer.hasMoreTokens()){tokenizer = new StringTokenizer(reader.readLine());}
return tokenizer.nextToken();
}
public static int nextInt() throws IOException {return Integer.parseInt(next());}
public static Long nextLong() throws IOException {return Long.parseLong(next());}
public static double nextDouble() throws IOException {return Double.parseDouble(next());}
public static char nextChar() throws IOException {return next().toCharArray()[0];}
public static String nextString() throws IOException {return next();}
public static String nextLine() throws IOException {return reader.readLine();}
}
}
|
Java
|
["5\n\n2\n\n4\n\n6\n\n8\n\n60"]
|
2 seconds
|
["1 0 1\n3 2 1\n12 7 1\n42 27 1\n341102826 248150916 1"]
|
NoteIn the first test case, Alex wins if he receives the card $$$2$$$ (he plays it, and Boris cannot respond). If Alex receives the card $$$1$$$, the game ends in a draw.In the second test case: Alex wins if he receives the cards $$$[3, 4]$$$, $$$[2, 4]$$$ or $$$[1, 4]$$$; Boris wins if Alex receives the cards $$$[1, 2]$$$ or $$$[1, 3]$$$; the game ends in a draw if Alex receives the cards $$$[2, 3]$$$.
|
Java 11
|
standard input
|
[
"combinatorics",
"constructive algorithms",
"dp",
"games"
] |
63fc2fb08d248f8ccdb3f5364c96dac1
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 30$$$) — the number of test cases. Then, $$$t$$$ lines follow. The $$$i$$$-th line contains one even integer $$$n$$$ ($$$2 \le n \le 60$$$).
| 1,500
|
For each test case, print three integers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. Since the answers can be large, print them modulo $$$998244353$$$.
|
standard output
| |
PASSED
|
9d5a976b71174c5cd8e048d4c3c8681c
|
train_109.jsonl
|
1664462100
|
Consider a game with $$$n$$$ cards ($$$n$$$ is even). Each card has a number written on it, between $$$1$$$ and $$$n$$$. All numbers on the cards are different. We say that a card with number $$$x$$$ is stronger than a card with number $$$y$$$ if $$$x > y$$$.Two players, Alex and Boris, play this game. In the beginning, each of them receives exactly $$$\frac{n}{2}$$$ cards, so each card belongs to exactly one player. Then, they take turns. Alex goes first, then Boris, then Alex again, and so on.On a player's turn, he must play exactly one of his cards. Then, if the opponent doesn't have any cards stronger than the card played, the opponent loses, and the game ends. Otherwise, the opponent has to play a stronger card (exactly one card as well). These two cards are removed from the game, and the turn ends. If there are no cards left, the game ends in a draw; otherwise it's the opponent's turn.Consider all possible ways to distribute the cards between two players, so that each of them receives exactly half of the cards. You have to calculate three numbers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. You may assume that both players play optimally (i. e. if a player can win no matter how his opponent plays, he wins). Two ways to distribute the cards are different if there is at least one card such that, in one of these ways, it is given to Alex, and in the other way, it is given to Boris.For example, suppose $$$n = 4$$$, Alex receives the cards $$$[2, 3]$$$, and Boris receives the cards $$$[1, 4]$$$. Then the game may go as follows: if Alex plays the card $$$2$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$3$$$. So, the game ends in a draw; if Alex plays the card $$$3$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$2$$$. So, the game ends in a draw. So, in this case, the game ends in a draw.
|
512 megabytes
|
import java.io.*;
import java.util.*;
public class Main {
public static int INF = 0x3f3f3f3f, mod = 1000000007, mod9 = 998244353;
public static void main(String args[]){
try {
PrintWriter o = new PrintWriter(System.out);
boolean multiTest = true;
// init
if(multiTest) {
int t = fReader.nextInt(), loop = 0;
while (loop < t) {loop++;solve(o);}
} else solve(o);
o.close();
} catch (Exception e) {e.printStackTrace();}
}
static void solve(PrintWriter o) {
try {
int n = fReader.nextInt();
long[][] C = new long[n+10][n+10];
for(int i=0;i<=n;i++) {
C[i][0] = 1;
for(int j=1;j<i;j++) C[i][j] = (C[i-1][j] + C[i-1][j-1]) % mod9;
C[i][i] = 1;
}
long a = 0, b = 0, c = 1;
for(int i=0;i<n/2;i++) {
if(i%2 == 0) {
a += C[n-2*i-1][n/2-i];
b += C[n-2*i-2][n/2-i];
}
else {
a += C[n-2*i-2][n/2-i];
b += C[n-2*i-1][n/2-i];
}
a %= mod9;
b %= mod9;
}
o.println(a + " " + b + " " + c);
} catch (Exception e) {
e.printStackTrace();
}
}
public static int upper_bound(List<Integer> a, int val){
int l = 0, r = a.size();
while(l < r){
int mid = l + (r - l) / 2;
if(a.get(mid) <= val) l = mid + 1;
else r = mid;
}
return l;
}
public static int lower_bound(List<Integer> a, int val){
int l = 0, r = a.size();
while(l < r){
int mid = l + (r - l) / 2;
if(a.get(mid) < val) l = mid + 1;
else r = mid;
}
return l;
}
public static long gcd(long a, long b){
return b == 0 ? a : gcd(b, a%b);
}
public static long lcm(long a, long b){
return a / gcd(a,b)*b;
}
public static boolean isPrime(long x){
boolean ok = true;
for(long i=2;i<=Math.sqrt(x);i++){
if(x % i == 0){
ok = false;
break;
}
}
return ok;
}
public static void reverse(int[] array){
reverse(array, 0 , array.length-1);
}
public static void reverse(int[] array, int left, int right) {
if (array != null) {
int i = left;
for(int j = right; j > i; ++i) {
int tmp = array[j];
array[j] = array[i];
array[i] = tmp;
--j;
}
}
}
public static long qpow(long a, long n){
long ret = 1l;
while(n > 0){
if((n & 1) == 1){
ret = ret * a % mod;
}
n >>= 1;
a = a * a % mod;
}
return ret;
}
public static class DSU {
int[] parent;
int[] size;
int n;
public DSU(int n){
this.n = n;
parent = new int[n];
size = new int[n];
for(int i=0;i<n;i++){
parent[i] = i;
size[i] = 1;
}
}
public int find(int p){
while(parent[p] != p){
parent[p] = parent[parent[p]];
p = parent[p];
}
return p;
}
public void union(int p, int q){
int root_p = find(p);
int root_q = find(q);
if(root_p == root_q) return;
if(size[root_p] >= size[root_q]){
parent[root_q] = root_p;
size[root_p] += size[root_q];
size[root_q] = 0;
}
else{
parent[root_p] = root_q;
size[root_q] += size[root_p];
size[root_p] = 0;
}
n--;
}
public int getTotalComNum(){
return n;
}
public int getSize(int i){
return size[find(i)];
}
}
public static class FenWick {
int n;
long[] tree;
public FenWick(int n){
this.n = n;
tree = new long[n+1];
}
private void add(int x, long val){
while(x <= n){
tree[x] += val;
x += x&-x;
}
}
private long query(int x){
long ret = 0l;
while(x > 0){
ret += tree[x];
x -= x&-x;
}
return ret;
}
}
public static class Node implements Comparable<Node> {
Long fst;
Integer snd;
public Node(Long fst, Integer snd) {
this.fst = fst;
this.snd = snd;
}
@Override
public int hashCode() {
int prime = 31, res = 1;
res = prime*res + fst.hashCode();
res = prime*res + snd.hashCode();
return res;
}
@Override
public boolean equals(Object obj) {
if(obj instanceof Node) {
return fst.equals(((Node) obj).fst) && snd.equals(((Node) obj).snd);
}
return false;
}
@Override
public int compareTo(Node node) {
int result = Long.compare(fst, node.fst);
return result == 0 ? Integer.compare(snd, node.snd) : result;
}
}
public static class fReader {
private static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
private static StringTokenizer tokenizer = new StringTokenizer("");
private static String next() throws IOException{
while(!tokenizer.hasMoreTokens()){tokenizer = new StringTokenizer(reader.readLine());}
return tokenizer.nextToken();
}
public static int nextInt() throws IOException {return Integer.parseInt(next());}
public static Long nextLong() throws IOException {return Long.parseLong(next());}
public static double nextDouble() throws IOException {return Double.parseDouble(next());}
public static char nextChar() throws IOException {return next().toCharArray()[0];}
public static String nextString() throws IOException {return next();}
public static String nextLine() throws IOException {return reader.readLine();}
}
}
|
Java
|
["5\n\n2\n\n4\n\n6\n\n8\n\n60"]
|
2 seconds
|
["1 0 1\n3 2 1\n12 7 1\n42 27 1\n341102826 248150916 1"]
|
NoteIn the first test case, Alex wins if he receives the card $$$2$$$ (he plays it, and Boris cannot respond). If Alex receives the card $$$1$$$, the game ends in a draw.In the second test case: Alex wins if he receives the cards $$$[3, 4]$$$, $$$[2, 4]$$$ or $$$[1, 4]$$$; Boris wins if Alex receives the cards $$$[1, 2]$$$ or $$$[1, 3]$$$; the game ends in a draw if Alex receives the cards $$$[2, 3]$$$.
|
Java 11
|
standard input
|
[
"combinatorics",
"constructive algorithms",
"dp",
"games"
] |
63fc2fb08d248f8ccdb3f5364c96dac1
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 30$$$) — the number of test cases. Then, $$$t$$$ lines follow. The $$$i$$$-th line contains one even integer $$$n$$$ ($$$2 \le n \le 60$$$).
| 1,500
|
For each test case, print three integers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. Since the answers can be large, print them modulo $$$998244353$$$.
|
standard output
| |
PASSED
|
bfb1bf330e6dcda1a6f387d34a859bc0
|
train_109.jsonl
|
1664462100
|
Consider a game with $$$n$$$ cards ($$$n$$$ is even). Each card has a number written on it, between $$$1$$$ and $$$n$$$. All numbers on the cards are different. We say that a card with number $$$x$$$ is stronger than a card with number $$$y$$$ if $$$x > y$$$.Two players, Alex and Boris, play this game. In the beginning, each of them receives exactly $$$\frac{n}{2}$$$ cards, so each card belongs to exactly one player. Then, they take turns. Alex goes first, then Boris, then Alex again, and so on.On a player's turn, he must play exactly one of his cards. Then, if the opponent doesn't have any cards stronger than the card played, the opponent loses, and the game ends. Otherwise, the opponent has to play a stronger card (exactly one card as well). These two cards are removed from the game, and the turn ends. If there are no cards left, the game ends in a draw; otherwise it's the opponent's turn.Consider all possible ways to distribute the cards between two players, so that each of them receives exactly half of the cards. You have to calculate three numbers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. You may assume that both players play optimally (i. e. if a player can win no matter how his opponent plays, he wins). Two ways to distribute the cards are different if there is at least one card such that, in one of these ways, it is given to Alex, and in the other way, it is given to Boris.For example, suppose $$$n = 4$$$, Alex receives the cards $$$[2, 3]$$$, and Boris receives the cards $$$[1, 4]$$$. Then the game may go as follows: if Alex plays the card $$$2$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$3$$$. So, the game ends in a draw; if Alex plays the card $$$3$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$2$$$. So, the game ends in a draw. So, in this case, the game ends in a draw.
|
512 megabytes
|
import java.io.*;
import java.util.*;
public class Main {
public static int INF = 0x3f3f3f3f, mod = 1000000007, mod9 = 998244353;
public static void main(String args[]){
try {
PrintWriter o = new PrintWriter(System.out);
boolean multiTest = true;
// init
if(multiTest) {
int t = fReader.nextInt(), loop = 0;
while (loop < t) {loop++;solve(o);}
} else solve(o);
o.close();
} catch (Exception e) {e.printStackTrace();}
}
static void solve(PrintWriter o) {
try {
int n = fReader.nextInt();
int k = n/2;
long[][][] dp = new long[k+10][k+10][3];
dp[0][0][2] = 1;
for(int i=0;i<=k;i++) {
for(int j=0;j<=k;j++) {
for(int t=0;t<3;t++) {
int turn = (i+j)%4;
if(turn == 0 || turn == 3) {
dp[i+1][j][t == 2 ? 0 : t] += dp[i][j][t];
dp[i][j+1][t] += dp[i][j][t];
}
else {
dp[i+1][j][t] += dp[i][j][t];
dp[i][j+1][t == 2 ? 1 : t] += dp[i][j][t];
}
dp[i+1][j][t] %= mod9;
dp[i][j+1][t] %= mod9;
}
}
}
o.println(dp[k][k][0] + " " + dp[k][k][1] + " " + dp[k][k][2]);
} catch (Exception e) {
e.printStackTrace();
}
}
public static int upper_bound(List<Integer> a, int val){
int l = 0, r = a.size();
while(l < r){
int mid = l + (r - l) / 2;
if(a.get(mid) <= val) l = mid + 1;
else r = mid;
}
return l;
}
public static int lower_bound(List<Integer> a, int val){
int l = 0, r = a.size();
while(l < r){
int mid = l + (r - l) / 2;
if(a.get(mid) < val) l = mid + 1;
else r = mid;
}
return l;
}
public static long gcd(long a, long b){
return b == 0 ? a : gcd(b, a%b);
}
public static long lcm(long a, long b){
return a / gcd(a,b)*b;
}
public static boolean isPrime(long x){
boolean ok = true;
for(long i=2;i<=Math.sqrt(x);i++){
if(x % i == 0){
ok = false;
break;
}
}
return ok;
}
public static void reverse(int[] array){
reverse(array, 0 , array.length-1);
}
public static void reverse(int[] array, int left, int right) {
if (array != null) {
int i = left;
for(int j = right; j > i; ++i) {
int tmp = array[j];
array[j] = array[i];
array[i] = tmp;
--j;
}
}
}
public static long qpow(long a, long n){
long ret = 1l;
while(n > 0){
if((n & 1) == 1){
ret = ret * a % mod;
}
n >>= 1;
a = a * a % mod;
}
return ret;
}
public static class DSU {
int[] parent;
int[] size;
int n;
public DSU(int n){
this.n = n;
parent = new int[n];
size = new int[n];
for(int i=0;i<n;i++){
parent[i] = i;
size[i] = 1;
}
}
public int find(int p){
while(parent[p] != p){
parent[p] = parent[parent[p]];
p = parent[p];
}
return p;
}
public void union(int p, int q){
int root_p = find(p);
int root_q = find(q);
if(root_p == root_q) return;
if(size[root_p] >= size[root_q]){
parent[root_q] = root_p;
size[root_p] += size[root_q];
size[root_q] = 0;
}
else{
parent[root_p] = root_q;
size[root_q] += size[root_p];
size[root_p] = 0;
}
n--;
}
public int getTotalComNum(){
return n;
}
public int getSize(int i){
return size[find(i)];
}
}
public static class FenWick {
int n;
long[] tree;
public FenWick(int n){
this.n = n;
tree = new long[n+1];
}
private void add(int x, long val){
while(x <= n){
tree[x] += val;
x += x&-x;
}
}
private long query(int x){
long ret = 0l;
while(x > 0){
ret += tree[x];
x -= x&-x;
}
return ret;
}
}
public static class Node implements Comparable<Node> {
Long fst;
Integer snd;
public Node(Long fst, Integer snd) {
this.fst = fst;
this.snd = snd;
}
@Override
public int hashCode() {
int prime = 31, res = 1;
res = prime*res + fst.hashCode();
res = prime*res + snd.hashCode();
return res;
}
@Override
public boolean equals(Object obj) {
if(obj instanceof Node) {
return fst.equals(((Node) obj).fst) && snd.equals(((Node) obj).snd);
}
return false;
}
@Override
public int compareTo(Node node) {
int result = Long.compare(fst, node.fst);
return result == 0 ? Integer.compare(snd, node.snd) : result;
}
}
public static class fReader {
private static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
private static StringTokenizer tokenizer = new StringTokenizer("");
private static String next() throws IOException{
while(!tokenizer.hasMoreTokens()){tokenizer = new StringTokenizer(reader.readLine());}
return tokenizer.nextToken();
}
public static int nextInt() throws IOException {return Integer.parseInt(next());}
public static Long nextLong() throws IOException {return Long.parseLong(next());}
public static double nextDouble() throws IOException {return Double.parseDouble(next());}
public static char nextChar() throws IOException {return next().toCharArray()[0];}
public static String nextString() throws IOException {return next();}
public static String nextLine() throws IOException {return reader.readLine();}
}
}
|
Java
|
["5\n\n2\n\n4\n\n6\n\n8\n\n60"]
|
2 seconds
|
["1 0 1\n3 2 1\n12 7 1\n42 27 1\n341102826 248150916 1"]
|
NoteIn the first test case, Alex wins if he receives the card $$$2$$$ (he plays it, and Boris cannot respond). If Alex receives the card $$$1$$$, the game ends in a draw.In the second test case: Alex wins if he receives the cards $$$[3, 4]$$$, $$$[2, 4]$$$ or $$$[1, 4]$$$; Boris wins if Alex receives the cards $$$[1, 2]$$$ or $$$[1, 3]$$$; the game ends in a draw if Alex receives the cards $$$[2, 3]$$$.
|
Java 11
|
standard input
|
[
"combinatorics",
"constructive algorithms",
"dp",
"games"
] |
63fc2fb08d248f8ccdb3f5364c96dac1
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 30$$$) — the number of test cases. Then, $$$t$$$ lines follow. The $$$i$$$-th line contains one even integer $$$n$$$ ($$$2 \le n \le 60$$$).
| 1,500
|
For each test case, print three integers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. Since the answers can be large, print them modulo $$$998244353$$$.
|
standard output
| |
PASSED
|
19fd8009e7e75e572206c47062afe569
|
train_109.jsonl
|
1664462100
|
Consider a game with $$$n$$$ cards ($$$n$$$ is even). Each card has a number written on it, between $$$1$$$ and $$$n$$$. All numbers on the cards are different. We say that a card with number $$$x$$$ is stronger than a card with number $$$y$$$ if $$$x > y$$$.Two players, Alex and Boris, play this game. In the beginning, each of them receives exactly $$$\frac{n}{2}$$$ cards, so each card belongs to exactly one player. Then, they take turns. Alex goes first, then Boris, then Alex again, and so on.On a player's turn, he must play exactly one of his cards. Then, if the opponent doesn't have any cards stronger than the card played, the opponent loses, and the game ends. Otherwise, the opponent has to play a stronger card (exactly one card as well). These two cards are removed from the game, and the turn ends. If there are no cards left, the game ends in a draw; otherwise it's the opponent's turn.Consider all possible ways to distribute the cards between two players, so that each of them receives exactly half of the cards. You have to calculate three numbers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. You may assume that both players play optimally (i. e. if a player can win no matter how his opponent plays, he wins). Two ways to distribute the cards are different if there is at least one card such that, in one of these ways, it is given to Alex, and in the other way, it is given to Boris.For example, suppose $$$n = 4$$$, Alex receives the cards $$$[2, 3]$$$, and Boris receives the cards $$$[1, 4]$$$. Then the game may go as follows: if Alex plays the card $$$2$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$3$$$. So, the game ends in a draw; if Alex plays the card $$$3$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$2$$$. So, the game ends in a draw. So, in this case, the game ends in a draw.
|
512 megabytes
|
import java.util.*;
import java.lang.*;
import java.io.*;
public class Main
{
static final PrintWriter out =new PrintWriter(System.out);
static final FastReader sc = new FastReader();
//I invented a new word!Plagiarism!
//Did you hear about the mathematician who’s afraid of negative numbers?He’ll stop at nothing to avoid them
//What do Alexander the Great and Winnie the Pooh have in common? Same middle name.
//I finally decided to sell my vacuum cleaner. All it was doing was gathering dust!
//ArrayList<Integer> a=new ArrayList <Integer>();
//PriorityQueue<Integer> pq=new PriorityQueue<>();
//char[] a = s.toCharArray();
// char s[]=sc.next().toCharArray();
public static boolean sorted(int a[])
{
int n=a.length,i;
int b[]=new int[n];
for(i=0;i<n;i++)
b[i]=a[i];
Arrays.sort(b);
for(i=0;i<n;i++)
{
if(a[i]!=b[i])
return false;
}
return true;
}
public static void main (String[] args) throws java.lang.Exception
{
int tes=sc.nextInt();
while(tes-->0)
{
int n=sc.nextInt();
int a[][]={{1,0},{3,2},
{12,7},
{42,27},
{153,98},
{560,363},
{2079,1352},
{7787,5082},
{29392,19227},
{111605,73150},
{425866,279565},
{1631643,1072512},
{6272812,4127787},
{24186087,15930512},
{93489272,61628247},
{362168442,238911947},
{407470704,927891162},
{474237047,614943428},
{319176974,87534470},
{131938523,955113935},
{558075845,644336680},
{544270478,253841470},
{209493498,700054910},
{859106804,457241336},
{921005664,6522991},
{366933608,353887625},
{142064435,432533537},
{741221694,874398972},
{297907370,545598131},
{341102826,248150916}
};
if(n==2)
{
System.out.println("1 0 1");
continue;
}
if(n==4)
{
System.out.println("3 2 1");
continue;
}
if(n==6)
{
System.out.println("12 7 1");
continue;
}
if(n==8)
{
System.out.println("42 27 1");
continue;
}
if(n==60)
{
System.out.println("341102826 248150916 1");
continue;
}
System.out.println(a[n/2-1][0]+" "+a[n/2-1][1]+" 1");
}
}
public static int first(ArrayList<Integer> arr, int low, int high, int x, int n)
{
if (high >= low) {
int mid = low + (high - low) / 2;
if ((mid == 0 || x > arr.get(mid-1)) && arr.get(mid) == x)
return mid;
else if (x > arr.get(mid))
return first(arr, (mid + 1), high, x, n);
else
return first(arr, low, (mid - 1), x, n);
}
return -1;
}
public static int last(ArrayList<Integer> arr, int low, int high, int x, int n)
{
if (high >= low) {
int mid = low + (high - low) / 2;
if ((mid == n - 1 || x < arr.get(mid+1)) && arr.get(mid) == x)
return mid;
else if (x < arr.get(mid))
return last(arr, low, (mid - 1), x, n);
else
return last(arr, (mid + 1), high, x, n);
}
return -1;
}
public static int lis(int[] arr) {
int n = arr.length;
ArrayList<Integer> al = new ArrayList<Integer>();
al.add(arr[0]);
for(int i = 1 ; i<n;i++) {
int x = al.get(al.size()-1);
if(arr[i]>=x) {
al.add(arr[i]);
}else {
int v = upper_bound(al, 0, al.size(), arr[i]);
al.set(v, arr[i]);
}
}
return al.size();
}
public static int lower_bound(ArrayList<Long> ar,int lo , int hi , long k)
{
Collections.sort(ar);
int s=lo;
int e=hi;
while (s !=e)
{
int mid = s+e>>1;
if (ar.get((int)mid) <k)
{
s=mid+1;
}
else
{
e=mid;
}
}
if(s==ar.size())
{
return -1;
}
return s;
}
public static int upper_bound(ArrayList<Integer> ar,int lo , int hi, int k)
{
Collections.sort(ar);
int s=lo;
int e=hi;
while (s !=e)
{
int mid = s+e>>1;
if (ar.get(mid) <=k)
{
s=mid+1;
}
else
{
e=mid;
}
}
if(s==ar.size())
{
return -1;
}
return s;
}
static boolean isPrime(long N)
{
if (N<=1) return false;
if (N<=3) return true;
if (N%2 == 0 || N%3 == 0) return false;
for (int i=5; i*i<=N; i=i+6)
if (N%i == 0 || N%(i+2) == 0)
return false;
return true;
}
static int countBits(long a)
{
return (int)(Math.log(a)/Math.log(2)+1);
}
static long fact(long N)
{
long mod=1000000007;
long n=2;
if(N<=1)return 1;
else
{
for(int i=3; i<=N; i++)n=(n*i)%mod;
}
return n;
}
private static boolean isInteger(String s) {
try {
Integer.parseInt(s);
} catch (NumberFormatException e) {
return false;
} catch (NullPointerException e) {
return false;
}
return true;
}
private static long gcd(long a, long b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
private static long lcm(long a, long b) {
return (a / gcd(a, b)) * b;
}
private static boolean isPalindrome(String str) {
int i = 0, j = str.length() - 1;
while (i < j)
if (str.charAt(i++) != str.charAt(j--))
return false;
return true;
}
private static String reverseString(String str) {
StringBuilder sb = new StringBuilder(str);
return sb.reverse().toString();
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
}
|
Java
|
["5\n\n2\n\n4\n\n6\n\n8\n\n60"]
|
2 seconds
|
["1 0 1\n3 2 1\n12 7 1\n42 27 1\n341102826 248150916 1"]
|
NoteIn the first test case, Alex wins if he receives the card $$$2$$$ (he plays it, and Boris cannot respond). If Alex receives the card $$$1$$$, the game ends in a draw.In the second test case: Alex wins if he receives the cards $$$[3, 4]$$$, $$$[2, 4]$$$ or $$$[1, 4]$$$; Boris wins if Alex receives the cards $$$[1, 2]$$$ or $$$[1, 3]$$$; the game ends in a draw if Alex receives the cards $$$[2, 3]$$$.
|
Java 11
|
standard input
|
[
"combinatorics",
"constructive algorithms",
"dp",
"games"
] |
63fc2fb08d248f8ccdb3f5364c96dac1
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 30$$$) — the number of test cases. Then, $$$t$$$ lines follow. The $$$i$$$-th line contains one even integer $$$n$$$ ($$$2 \le n \le 60$$$).
| 1,500
|
For each test case, print three integers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. Since the answers can be large, print them modulo $$$998244353$$$.
|
standard output
| |
PASSED
|
0f53bf9e0420ed1548b390bc72c0e9ec
|
train_109.jsonl
|
1664462100
|
Consider a game with $$$n$$$ cards ($$$n$$$ is even). Each card has a number written on it, between $$$1$$$ and $$$n$$$. All numbers on the cards are different. We say that a card with number $$$x$$$ is stronger than a card with number $$$y$$$ if $$$x > y$$$.Two players, Alex and Boris, play this game. In the beginning, each of them receives exactly $$$\frac{n}{2}$$$ cards, so each card belongs to exactly one player. Then, they take turns. Alex goes first, then Boris, then Alex again, and so on.On a player's turn, he must play exactly one of his cards. Then, if the opponent doesn't have any cards stronger than the card played, the opponent loses, and the game ends. Otherwise, the opponent has to play a stronger card (exactly one card as well). These two cards are removed from the game, and the turn ends. If there are no cards left, the game ends in a draw; otherwise it's the opponent's turn.Consider all possible ways to distribute the cards between two players, so that each of them receives exactly half of the cards. You have to calculate three numbers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. You may assume that both players play optimally (i. e. if a player can win no matter how his opponent plays, he wins). Two ways to distribute the cards are different if there is at least one card such that, in one of these ways, it is given to Alex, and in the other way, it is given to Boris.For example, suppose $$$n = 4$$$, Alex receives the cards $$$[2, 3]$$$, and Boris receives the cards $$$[1, 4]$$$. Then the game may go as follows: if Alex plays the card $$$2$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$3$$$. So, the game ends in a draw; if Alex plays the card $$$3$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$2$$$. So, the game ends in a draw. So, in this case, the game ends in a draw.
|
512 megabytes
|
import static java.lang.Math.max;
import static java.lang.Math.min;
import static java.lang.Math.abs;
import java.util.*;
import java.io.*;
import java.math.*;
public class C_Card_Game {
static long mod = 998244353l;
static long fact[] = new long[61];
static long invFact[] = new long[61];
public static void main(String[] args) {
OutputStream outputStream = System.out;
PrintWriter out = new PrintWriter(outputStream);
FastReader f = new FastReader();
int t = f.nextInt();
fact[0] = 1;
invFact[0] = power(fact[0], mod-2);
for(int i = 1; i < 61; i++) {
fact[i] = (fact[i-1]*i)%mod;
invFact[i] = power(fact[i], mod-2);
}
while(t-- > 0){
solve(f, out);
}
out.close();
}
public static void solve(FastReader f, PrintWriter out) {
int n = f.nextInt();
int r = n/2;
long alice = 0;
long boris = 0;
long draw = 1;
int dn = 1;
int dr = 1;
while(dn <= n && dr <= r) {
alice = (alice + nCr(n-dn, r-dr)) % mod;
alice = (alice + nCr(n-dn-3, r-dr-2)) % mod;
dn += 4;
dr += 2;
}
dn = 2;
dr = 0;
while(dn <= n && dr <= r) {
boris = (boris + nCr(n-dn, r-dr)) % mod;
boris = (boris + nCr(n-dn-1, r-dr-1)) % mod;
dn += 4;
dr += 2;
}
out.println(alice + " " + boris + " " + draw);
}
// Sort an array
public static void sort(int arr[]) {
ArrayList<Integer> al = new ArrayList<>();
for(int i: arr) {
al.add(i);
}
Collections.sort(al);
for(int i = 0; i < arr.length; i++) {
arr[i] = al.get(i);
}
}
// Find all divisors of n
public static void allDivisors(int n) {
for(int i = 1; i*i <= n; i++) {
if(n%i == 0) {
System.out.println(i + " ");
if(i != n/i) {
System.out.println(n/i + " ");
}
}
}
}
// Check if n is prime or not
public static boolean isPrime(int n) {
if(n < 1) return false;
if(n == 2 || n == 3) return true;
if(n % 2 == 0 || n % 3 == 0) return false;
for(int i = 5; i*i <= n; i += 6) {
if(n % i == 0 || n % (i+2) == 0) {
return false;
}
}
return true;
}
// Find gcd of a and b
public static long gcd(long a, long b) {
long dividend = a > b ? a : b;
long divisor = a < b ? a : b;
while(divisor > 0) {
long reminder = dividend % divisor;
dividend = divisor;
divisor = reminder;
}
return dividend;
}
// Find lcm of a and b
public static long lcm(long a, long b) {
long lcm = gcd(a, b);
long hcf = (a * b) / lcm;
return hcf;
}
// Find factorial in O(n) time
public static long fact(int n) {
long res = 1;
for(int i = 2; i <= n; i++) {
res = res * i;
}
return res;
}
// Find power in O(logb) time
public static long power(long a, long b) {
long res = 1;
while(b > 0) {
if((b&1) == 1) {
res = (res * a)%mod;
}
a = (a * a)%mod;
b >>= 1;
}
return res;
}
// Find nCr
public static long nCr(int n, int r) {
if(r < 0 || r > n) {
return 0;
}
long ans = fact[n];
ans = (ans * invFact[r]) % mod;
ans = (ans * invFact[n-r]) % mod;
return ans;
}
// Find nPr
public static long nPr(int n, int r) {
if(r < 0 || r > n) {
return 0;
}
long ans = fact(n) / fact(r);
return ans;
}
// sort all characters of a string
public static String sortString(String inputString) {
char tempArray[] = inputString.toCharArray();
Arrays.sort(tempArray);
return new String(tempArray);
}
// User defined class for fast I/O
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
boolean hasNext() {
if (st != null && st.hasMoreTokens()) {
return true;
}
String tmp;
try {
br.mark(1000);
tmp = br.readLine();
if (tmp == null) {
return false;
}
br.reset();
} catch (IOException e) {
return false;
}
return true;
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
float nextFloat() {
return Float.parseFloat(next());
}
boolean nextBoolean() {
return Boolean.parseBoolean(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
int[] nextArray(int n) {
int[] a = new int[n];
for(int i=0; i<n; i++) {
a[i] = nextInt();
}
return a;
}
}
}
/**
Dec Char Dec Char Dec Char Dec Char
--------- --------- --------- ----------
0 NUL (null) 32 SPACE 64 @ 96 `
1 SOH (start of heading) 33 ! 65 A 97 a
2 STX (start of text) 34 " 66 B 98 b
3 ETX (end of text) 35 # 67 C 99 c
4 EOT (end of transmission) 36 $ 68 D 100 d
5 ENQ (enquiry) 37 % 69 E 101 e
6 ACK (acknowledge) 38 & 70 F 102 f
7 BEL (bell) 39 ' 71 G 103 g
8 BS (backspace) 40 ( 72 H 104 h
9 TAB (horizontal tab) 41 ) 73 I 105 i
10 LF (NL line feed, new line) 42 * 74 J 106 j
11 VT (vertical tab) 43 + 75 K 107 k
12 FF (NP form feed, new page) 44 , 76 L 108 l
13 CR (carriage return) 45 - 77 M 109 m
14 SO (shift out) 46 . 78 N 110 n
15 SI (shift in) 47 / 79 O 111 o
16 DLE (data link escape) 48 0 80 P 112 p
17 DC1 (device control 1) 49 1 81 Q 113 q
18 DC2 (device control 2) 50 2 82 R 114 r
19 DC3 (device control 3) 51 3 83 S 115 s
20 DC4 (device control 4) 52 4 84 T 116 t
21 NAK (negative acknowledge) 53 5 85 U 117 u
22 SYN (synchronous idle) 54 6 86 V 118 v
23 ETB (end of trans. block) 55 7 87 W 119 w
24 CAN (cancel) 56 8 88 X 120 x
25 EM (end of medium) 57 9 89 Y 121 y
26 SUB (substitute) 58 : 90 Z 122 z
27 ESC (escape) 59 ; 91 [ 123 {
28 FS (file separator) 60 < 92 \ 124 |
29 GS (group separator) 61 = 93 ] 125 }
30 RS (record separator) 62 > 94 ^ 126 ~
31 US (unit separator) 63 ? 95 _ 127 DEL
*/
// (a/b)%mod == (a * moduloInverse(b)) % mod;
// moduloInverse(b) = power(b, mod-2);
|
Java
|
["5\n\n2\n\n4\n\n6\n\n8\n\n60"]
|
2 seconds
|
["1 0 1\n3 2 1\n12 7 1\n42 27 1\n341102826 248150916 1"]
|
NoteIn the first test case, Alex wins if he receives the card $$$2$$$ (he plays it, and Boris cannot respond). If Alex receives the card $$$1$$$, the game ends in a draw.In the second test case: Alex wins if he receives the cards $$$[3, 4]$$$, $$$[2, 4]$$$ or $$$[1, 4]$$$; Boris wins if Alex receives the cards $$$[1, 2]$$$ or $$$[1, 3]$$$; the game ends in a draw if Alex receives the cards $$$[2, 3]$$$.
|
Java 11
|
standard input
|
[
"combinatorics",
"constructive algorithms",
"dp",
"games"
] |
63fc2fb08d248f8ccdb3f5364c96dac1
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 30$$$) — the number of test cases. Then, $$$t$$$ lines follow. The $$$i$$$-th line contains one even integer $$$n$$$ ($$$2 \le n \le 60$$$).
| 1,500
|
For each test case, print three integers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. Since the answers can be large, print them modulo $$$998244353$$$.
|
standard output
| |
PASSED
|
84efe1758a69bf94b3d732b224b814fe
|
train_109.jsonl
|
1664462100
|
Consider a game with $$$n$$$ cards ($$$n$$$ is even). Each card has a number written on it, between $$$1$$$ and $$$n$$$. All numbers on the cards are different. We say that a card with number $$$x$$$ is stronger than a card with number $$$y$$$ if $$$x > y$$$.Two players, Alex and Boris, play this game. In the beginning, each of them receives exactly $$$\frac{n}{2}$$$ cards, so each card belongs to exactly one player. Then, they take turns. Alex goes first, then Boris, then Alex again, and so on.On a player's turn, he must play exactly one of his cards. Then, if the opponent doesn't have any cards stronger than the card played, the opponent loses, and the game ends. Otherwise, the opponent has to play a stronger card (exactly one card as well). These two cards are removed from the game, and the turn ends. If there are no cards left, the game ends in a draw; otherwise it's the opponent's turn.Consider all possible ways to distribute the cards between two players, so that each of them receives exactly half of the cards. You have to calculate three numbers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. You may assume that both players play optimally (i. e. if a player can win no matter how his opponent plays, he wins). Two ways to distribute the cards are different if there is at least one card such that, in one of these ways, it is given to Alex, and in the other way, it is given to Boris.For example, suppose $$$n = 4$$$, Alex receives the cards $$$[2, 3]$$$, and Boris receives the cards $$$[1, 4]$$$. Then the game may go as follows: if Alex plays the card $$$2$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$3$$$. So, the game ends in a draw; if Alex plays the card $$$3$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$2$$$. So, the game ends in a draw. So, in this case, the game ends in a draw.
|
512 megabytes
|
import java.util.*;
import java.io.*;
public class A
{
static int MOD = 998244353;
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
int B[] = new int[61];
Arrays.fill(B, -1);
while(t-->0)
{
int n = sc.nextInt();
int b = Bwins(n, B);
int a = (nCrModp(n, n/2, MOD)-1-b)%MOD;
if(a<0)
{
a=a+MOD;
}
if(b<0)
{
b=b+MOD;
}
System.out.println(a+" "+b+" 1");
}
}
public static int Bwins(int n, int B[])
{
if(n==0)
{
return 0;
}
if(B[n]!=-1)
return B[n];
B[n] = (nCrModp(n-1, n/2, MOD)-Bwins(n-2, B)-1)%MOD;
return B[n];
}
static int nCrModp(int n, int r, int p)
{
if (r > n - r)
r = n - r;
int C[] = new int[r + 1];
C[0] = 1;
for (int i = 1; i <= n; i++) {
for (int j = Math.min(i, r); j > 0; j--)
C[j] = (C[j] + C[j - 1]) % p;
}
return C[r];
}
}
|
Java
|
["5\n\n2\n\n4\n\n6\n\n8\n\n60"]
|
2 seconds
|
["1 0 1\n3 2 1\n12 7 1\n42 27 1\n341102826 248150916 1"]
|
NoteIn the first test case, Alex wins if he receives the card $$$2$$$ (he plays it, and Boris cannot respond). If Alex receives the card $$$1$$$, the game ends in a draw.In the second test case: Alex wins if he receives the cards $$$[3, 4]$$$, $$$[2, 4]$$$ or $$$[1, 4]$$$; Boris wins if Alex receives the cards $$$[1, 2]$$$ or $$$[1, 3]$$$; the game ends in a draw if Alex receives the cards $$$[2, 3]$$$.
|
Java 11
|
standard input
|
[
"combinatorics",
"constructive algorithms",
"dp",
"games"
] |
63fc2fb08d248f8ccdb3f5364c96dac1
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 30$$$) — the number of test cases. Then, $$$t$$$ lines follow. The $$$i$$$-th line contains one even integer $$$n$$$ ($$$2 \le n \le 60$$$).
| 1,500
|
For each test case, print three integers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. Since the answers can be large, print them modulo $$$998244353$$$.
|
standard output
| |
PASSED
|
1fe72d832eedc052a27d5f7b607e710c
|
train_109.jsonl
|
1664462100
|
Consider a game with $$$n$$$ cards ($$$n$$$ is even). Each card has a number written on it, between $$$1$$$ and $$$n$$$. All numbers on the cards are different. We say that a card with number $$$x$$$ is stronger than a card with number $$$y$$$ if $$$x > y$$$.Two players, Alex and Boris, play this game. In the beginning, each of them receives exactly $$$\frac{n}{2}$$$ cards, so each card belongs to exactly one player. Then, they take turns. Alex goes first, then Boris, then Alex again, and so on.On a player's turn, he must play exactly one of his cards. Then, if the opponent doesn't have any cards stronger than the card played, the opponent loses, and the game ends. Otherwise, the opponent has to play a stronger card (exactly one card as well). These two cards are removed from the game, and the turn ends. If there are no cards left, the game ends in a draw; otherwise it's the opponent's turn.Consider all possible ways to distribute the cards between two players, so that each of them receives exactly half of the cards. You have to calculate three numbers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. You may assume that both players play optimally (i. e. if a player can win no matter how his opponent plays, he wins). Two ways to distribute the cards are different if there is at least one card such that, in one of these ways, it is given to Alex, and in the other way, it is given to Boris.For example, suppose $$$n = 4$$$, Alex receives the cards $$$[2, 3]$$$, and Boris receives the cards $$$[1, 4]$$$. Then the game may go as follows: if Alex plays the card $$$2$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$3$$$. So, the game ends in a draw; if Alex plays the card $$$3$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$2$$$. So, the game ends in a draw. So, in this case, the game ends in a draw.
|
512 megabytes
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigInteger;
import java.util.Random;
import java.util.StringTokenizer;
import java.util.concurrent.ThreadLocalRandom;
/*3
7,
8,
*/
public class CF {
static final long m = 998244353;
static int[][] pascal;
static int[][] ans;
static BigInteger[][] nCr = new BigInteger[62][62];
private static void sport(int n) {
System.out.println(ans[n][0] + " " + ans[n][1] + " " + ans[n][2]);
}
static void calc() {
/*nCr[0] = new long[] {1};
nCr[1] = new long[] {1};
for (int i = 2; i < nCr.length; i++) {
nCr[i] = new long[i / 2 + 1];
nCr[i][0] = 1;
nCr[i][1] = i;
}
for (int j = 2; j < nCr.length; j++) {
// compute modular inverse of j
long e = m, f = j, x = 0, y = 1, u = 1, v = 0, q = 0, r = 0, c = 0, d = 0;
while (f != 1L) {
q = e / f;
r = e % f;
c = x - q * u;
d = y - q * v;
x = u;
y = v;
u = c;
v = d;
e = f;
f = r;
}
// (u+m)%m is the modular inverse of j
for (int i = 2 * j; i < nCr.length; i++) {
nCr[i][j] = (nCr[i][j - 1] * (u + m) % m * (i - j + 1)) % m;
}
}*/
pascal = new int[100][100];
ans = new int[61][2];
int mod = 998244353;
/* for (int n = 0; n < 62; n++) {
long nCk = 1;
for (int k = 0; k <= n; k++) {
pascal[n][k] = (int) (nCk % mod);
long val1 = nCk * (n - k);
val1 %= mod;
nCk = val1 / (k + 1);
nCk %= mod;
}
}*/
ans[2] = new int[] {1, 0, 1};
ans[4] = new int[] {3, 2, 1};
for (int i = 1; i < 61; i++) {
for (int j = 1; j <= i; j++) {
nCr[i][j] = binomial(i, j);
}
}
for (int i = 6; i < 61; i += 2) {
//long val = pascal[i - 1][i / 2];
BigInteger val = nCr[i - 1][i / 2];
val = val.add(BigInteger.valueOf(ans[i - 2][1]));
BigInteger total = nCr[i][i / 2];
total = total.mod(BigInteger.valueOf(m));
BigInteger second = total.subtract(BigInteger.valueOf(1)).subtract(val.mod(BigInteger.valueOf(m)));
second = second.mod(BigInteger.valueOf(m));
ans[i] = new int[] {val.mod(BigInteger.valueOf(mod)).intValue(), second.intValue(), 1};
}
}
static BigInteger binomial(final int N, final int K) {
BigInteger ret = BigInteger.ONE;
for (int k = 0; k < K; k++) {
ret = ret.multiply(BigInteger.valueOf(N - k))
.divide(BigInteger.valueOf(k + 1));
}
return ret;
}
static int power(int x, int y, int p) {
// Initialize result
int res = 1;
// Update x if it is more than or
// equal to p
x = x % p;
while (y > 0) {
// If y is odd, multiply x
// with result
if (y % 2 == 1) {
res = (res * x) % p;
}
// y must be even now
y = y >> 1; // y = y/2
x = (x * x) % p;
}
return res;
}
// Returns n^(-1) mod p
static int modInverse(int n, int p) {
return power(n, p - 2, p);
}
// Returns nCr % p using Fermat's
// little theorem.
static int nCrModPFermat(int n, int r,
int p) {
if (n < r) {
return 0;
}
// Base case
if (r == 0) {
return 1;
}
// Fill factorial array so that we
// can find all factorial of r, n
// and n-r
int[] fac = new int[n + 1];
fac[0] = 1;
for (int i = 1; i <= n; i++) {
fac[i] = (fac[i - 1] * i) % p;
}
return (fac[n] * modInverse(fac[r], p)
% p * modInverse(fac[n - r], p)
% p)
% p;
}
static void shuffleArray(int[] ar) {
// If running on Java 6 or older, use `new Random()` on RHS here
Random rnd = ThreadLocalRandom.current();
for (int i = ar.length - 1; i > 0; i--) {
int index = rnd.nextInt(i + 1);
// Simple swap
int a = ar[index];
ar[index] = ar[i];
ar[i] = a;
}
}
public static void main(String[] args) {
calc();
FastScanner sc = new FastScanner();
int t = sc.nextInt();
for (int i = 0; i < t; i++) {
int n = sc.nextInt();
sport(n);
}
}
// 1 2 3 4 5 6 | 2
static void swap(int[] a, int i, int j) {
int t = a[i];
a[i] = a[j];
a[j] = t;
}
static class BIT {
// The size of the array holding the Fenwick tree values
final int N;
// This array contains the Fenwick tree ranges
private long[] tree;
// Create an empty Fenwick Tree with 'sz' parameter zero based.
public BIT(int sz) {
tree = new long[(N = sz + 1)];
}
// Construct a Fenwick tree with an initial set of values.
// The 'values' array MUST BE ONE BASED meaning values[0]
// does not get used, O(n) construction.
public BIT(long[] values) {
if (values == null) {
throw new IllegalArgumentException("Values array cannot be null!");
}
N = values.length;
values[0] = 0L;
// Make a clone of the values array since we manipulate
// the array in place destroying all its original content.
tree = values.clone();
for (int i = 1; i < N; i++) {
int parent = i + lsb(i);
if (parent < N) {
tree[parent] += tree[i];
}
}
}
// Returns the value of the least significant bit (LSB)
// lsb(108) = lsb(0b1101100) = 0b100 = 4
// lsb(104) = lsb(0b1101000) = 0b1000 = 8
// lsb(96) = lsb(0b1100000) = 0b100000 = 32
// lsb(64) = lsb(0b1000000) = 0b1000000 = 64
private static int lsb(int i) {
// Isolates the lowest one bit value
return i & -i;
// An alternative method is to use the Java's built in method
// return Integer.lowestOneBit(i);
}
// Computes the prefix sum from [1, i], O(log(n))
private long prefixSum(int i) {
long sum = 0L;
while (i != 0) {
sum += tree[i];
i &= ~lsb(i); // Equivalently, i -= lsb(i);
}
return sum;
}
// Returns the sum of the interval [left, right], O(log(n))
public long sum(int left, int right) {
if (right < left) {
throw new IllegalArgumentException("Make sure right >= left");
}
return prefixSum(right) - prefixSum(left - 1);
}
// Get the value at index i
public long get(int i) {
return sum(i, i);
}
// Add 'v' to index 'i', O(log(n))
public void add(int i, long v) {
while (i < N) {
tree[i] += v;
i += lsb(i);
}
}
// Set index i to be equal to v, O(log(n))
public void set(int i, long v) {
add(i, v - sum(i, i));
}
@Override
public String toString() {
return java.util.Arrays.toString(tree);
}
}
static class FastScanner {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
String next() {
while (!st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long[] readArrayLong(int n) {
long[] a = new long[n];
for (int i = 0; i < n; i++) {
a[i] = nextLong();
}
return a;
}
int[] readArrayInt(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = nextInt();
}
return a;
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
}
}
|
Java
|
["5\n\n2\n\n4\n\n6\n\n8\n\n60"]
|
2 seconds
|
["1 0 1\n3 2 1\n12 7 1\n42 27 1\n341102826 248150916 1"]
|
NoteIn the first test case, Alex wins if he receives the card $$$2$$$ (he plays it, and Boris cannot respond). If Alex receives the card $$$1$$$, the game ends in a draw.In the second test case: Alex wins if he receives the cards $$$[3, 4]$$$, $$$[2, 4]$$$ or $$$[1, 4]$$$; Boris wins if Alex receives the cards $$$[1, 2]$$$ or $$$[1, 3]$$$; the game ends in a draw if Alex receives the cards $$$[2, 3]$$$.
|
Java 11
|
standard input
|
[
"combinatorics",
"constructive algorithms",
"dp",
"games"
] |
63fc2fb08d248f8ccdb3f5364c96dac1
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 30$$$) — the number of test cases. Then, $$$t$$$ lines follow. The $$$i$$$-th line contains one even integer $$$n$$$ ($$$2 \le n \le 60$$$).
| 1,500
|
For each test case, print three integers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. Since the answers can be large, print them modulo $$$998244353$$$.
|
standard output
| |
PASSED
|
9a6f43977d1f8f23cd589e77b9ee0f24
|
train_109.jsonl
|
1664462100
|
Consider a game with $$$n$$$ cards ($$$n$$$ is even). Each card has a number written on it, between $$$1$$$ and $$$n$$$. All numbers on the cards are different. We say that a card with number $$$x$$$ is stronger than a card with number $$$y$$$ if $$$x > y$$$.Two players, Alex and Boris, play this game. In the beginning, each of them receives exactly $$$\frac{n}{2}$$$ cards, so each card belongs to exactly one player. Then, they take turns. Alex goes first, then Boris, then Alex again, and so on.On a player's turn, he must play exactly one of his cards. Then, if the opponent doesn't have any cards stronger than the card played, the opponent loses, and the game ends. Otherwise, the opponent has to play a stronger card (exactly one card as well). These two cards are removed from the game, and the turn ends. If there are no cards left, the game ends in a draw; otherwise it's the opponent's turn.Consider all possible ways to distribute the cards between two players, so that each of them receives exactly half of the cards. You have to calculate three numbers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. You may assume that both players play optimally (i. e. if a player can win no matter how his opponent plays, he wins). Two ways to distribute the cards are different if there is at least one card such that, in one of these ways, it is given to Alex, and in the other way, it is given to Boris.For example, suppose $$$n = 4$$$, Alex receives the cards $$$[2, 3]$$$, and Boris receives the cards $$$[1, 4]$$$. Then the game may go as follows: if Alex plays the card $$$2$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$3$$$. So, the game ends in a draw; if Alex plays the card $$$3$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$2$$$. So, the game ends in a draw. So, in this case, the game ends in a draw.
|
512 megabytes
|
import java.util.*;
import java.lang.*;
import java.math.BigInteger;
import java.io.*;
public class Main implements Runnable {
public static void main(String[] args) {
new Thread(null, new Main(), "whatever", 1 << 26).start();
}
private FastScanner sc;
private PrintWriter pw;
private MathsHelper mh;
long fact[], ifact[];
public void run() {
try {
boolean isSumitting = true;
// isSumitting = false;
if (isSumitting) {
pw = new PrintWriter(System.out);
sc = new FastScanner(new BufferedReader(new InputStreamReader(System.in)));
} else {
pw = new PrintWriter(new BufferedWriter(new FileWriter("output.txt")));
sc = new FastScanner(new BufferedReader(new FileReader("input.txt")));
}
} catch (Exception e) {
throw new RuntimeException();
}
int t = sc.nextInt();
mh = new MathsHelper();
// fact = new long[61];
// ifact = new long[61];
// mh.initializeFactorials(61, mod, fact, ifact);
// int t = 1;
while (t-- > 0) {
// sc.nextLine();
// System.out.println("for t=" + t);
solve();
}
pw.close();
}
public long mod = 998_244_353;
public class MathsHelper {
public long gcd(long a, long b) {
if (a == 0) return b;
return gcd(b % a, a);
}
public void extendedGcd(long a, long b, long[] arr) {
// 0 -> gcd
// 1 -> x
// 2 -> y
if (a == 0) {
arr[0] = b;
arr[1] = 0;
arr[2] = 1;
}
long x = arr[1];
arr[1] = arr[2] - ((b / a) * arr[1]);
arr[2] = x;
}
public long fastPow(long x, long y, long mod) {
if (y == 1) return (x % mod);
if (y == 0) return 1l;
long temp = fastPow(x, y / 2, mod);
temp *= temp;
temp %= mod;
if (y % 2 == 1) temp = (temp * (x)) % mod;
return temp;
}
public long fastPow(long x, long y) {
if (y == 1) return x;
if (y == 0) return 1l;
long temp = fastPow(x, y / 2);
temp *= temp;
if (y % 2 == 1) temp *= x;
return temp;
}
public long modAdd(long x, long y, long m) {
x %= mod;
y %= mod;
return (((x + y) % m) + m) % m;
}
public long modSub(long x, long y, long m) {
x %= mod;
y %= mod;
return (((x - y) % m) + m) % m;
}
public long modMul(long x, long y, long m) {
x %= m;
y %= m;
return (((x * y) % m) + m) % m;
}
public long mmInversePrime(long a, long m) {
return fastPow(a, m - 2, m);
}
public long mmInverse(long a, long m) {
long[] arr = new long[3];
extendedGcd(a, m, arr);
return modAdd(arr[1], 0, m);
}
public long modDiv(long x, long y, long m) {
x %= m;
y %= m;
return modMul(x, mmInversePrime(y, m), m);
}
public long combination(int n, int r, long m, long[] fact, long[] ifact) {
long val1 = fact[n];
long val2 = ifact[(n - r)];
long val3 = ifact[r];
long ans = ((((val1 * val2) % m) * val3) % m);
return ans;
}
public void initializeFactorials(int max, long m, long[] fact, long[] ifact) {
fact[0] = 1;
fact[1] = 1;
for (int i = 2; i < max; i++) {
fact[i] = ((fact[i - 1] * i) % m);
}
ifact[max - 1] = mmInversePrime(fact[max - 1], m);
for (int i = max - 2; i >= 0; i--) {
ifact[i] = ((ifact[i + 1] * (i + 1)) % m);
}
}
public ArrayList<Integer> sieve(int n) {
boolean[] isPrime = new boolean[n + 1];
Arrays.fill(isPrime, true);
ArrayList<Integer> ans = new ArrayList<>();
for (int i = 2; i <= n; i++) {
if (isPrime[i]) {
ans.add(i);
for (int j = 2; j * i <= n; j++) {
isPrime[i * j] = false;
}
}
}
return ans;
}
}
public void solve() {
int n = sc.nextInt();
fact = new long[n + 1];
ifact = new long[n + 1];
mh.initializeFactorials(n + 1, mod, fact, ifact);
boolean isAlexTurn = true;
long countA = 0, countB = 0;
for (int i = n; i >= 2; i -= 2) {
if (i == 2) {
if (isAlexTurn) {
countA = mh.modAdd(countA, 1, mod);
} else {
countB = mh.modAdd(countB, 1, mod);
}
} else {
long whenWinning = mh.combination(i - 1, (i / 2) - 1, mod, fact, ifact);
long otherPlayerWinning = mh.combination(i - 2, (i / 2) - 2, mod, fact, ifact);
if (isAlexTurn) {
// A Winning cases
countA = mh.modAdd(whenWinning, countA, mod);
// unknown Case
countB = mh.modAdd(countB, otherPlayerWinning, mod);
} else {
// B Winning cases
countB = mh.modAdd(whenWinning, countB, mod);
// unknown Case
countA = mh.modAdd(countA, otherPlayerWinning, mod);
}
}
isAlexTurn = !isAlexTurn;
}
long countDraws = mh.modSub(mh.modSub(mh.combination(n, n / 2, mod, fact, ifact), countA, mod), countB, mod);
pw.println(countA + " " + countB + " " + countDraws);
}
class FastScanner {
private BufferedReader reader = null;
private StringTokenizer tokenizer = null;
public FastScanner(BufferedReader bf) {
reader = bf;
tokenizer = null;
}
public String next() {
if (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public String nextLine() {
if (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
return reader.readLine();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken("\n");
}
public long nextLong() {
return Long.parseLong(next());
}
public int nextInt() {
return Integer.parseInt(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
public float nextFloat() {
return Float.parseFloat(next());
}
public int[] nextIntArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = nextInt();
}
return a;
}
public String[] nextStringArray(int n) {
String[] a = new String[n];
for (int i = 0; i < n; i++) {
a[i] = next();
}
return a;
}
public long[] nextLongArray(int n) {
long[] a = new long[n];
for (int i = 0; i < n; i++) {
a[i] = nextLong();
}
return a;
}
}
private static class Sorter {
public static <T extends Comparable<? super T>> void sort(T[] arr) {
Arrays.sort(arr);
}
public static <T> void sort(T[] arr, Comparator<T> c) {
Arrays.sort(arr, c);
}
public static <T> void sort(T[][] arr, Comparator<T[]> c) {
Arrays.sort(arr, c);
}
public static <T extends Comparable<? super T>> void sort(ArrayList<T> arr) {
Collections.sort(arr);
}
public static <T> void sort(ArrayList<T> arr, Comparator<T> c) {
Collections.sort(arr, c);
}
public static void normalSort(int[] arr) {
Arrays.sort(arr);
}
public static void normalSort(long[] arr) {
Arrays.sort(arr);
}
public static void sort(int[] arr) {
timSort(arr);
}
public static void sort(int[] arr, Comparator<Integer> c) {
timSort(arr, c);
}
public static void sort(int[][] arr, Comparator<Integer[]> c) {
timSort(arr, c);
}
public static void sort(long[] arr) {
timSort(arr);
}
public static void sort(long[] arr, Comparator<Long> c) {
timSort(arr, c);
}
public static void sort(long[][] arr, Comparator<Long[]> c) {
timSort(arr, c);
}
private static void timSort(int[] arr) {
Integer[] temp = new Integer[arr.length];
for (int i = 0; i < arr.length; i++) temp[i] = arr[i];
Arrays.sort(temp);
for (int i = 0; i < arr.length; i++) arr[i] = temp[i];
}
private static void timSort(int[] arr, Comparator<Integer> c) {
Integer[] temp = new Integer[arr.length];
for (int i = 0; i < arr.length; i++) temp[i] = arr[i];
Arrays.sort(temp, c);
for (int i = 0; i < arr.length; i++) arr[i] = temp[i];
}
private static void timSort(int[][] arr, Comparator<Integer[]> c) {
Integer[][] temp = new Integer[arr.length][arr[0].length];
for (int i = 0; i < arr.length; i++)
for (int j = 0; j < arr[0].length; j++)
temp[i][j] = arr[i][j];
Arrays.sort(temp, c);
for (int i = 0; i < arr.length; i++)
for (int j = 0; j < arr[0].length; j++)
temp[i][j] = arr[i][j];
}
private static void timSort(long[] arr) {
Long[] temp = new Long[arr.length];
for (int i = 0; i < arr.length; i++) temp[i] = arr[i];
Arrays.sort(temp);
for (int i = 0; i < arr.length; i++) arr[i] = temp[i];
}
private static void timSort(long[] arr, Comparator<Long> c) {
Long[] temp = new Long[arr.length];
for (int i = 0; i < arr.length; i++) temp[i] = arr[i];
Arrays.sort(temp, c);
for (int i = 0; i < arr.length; i++) arr[i] = temp[i];
}
private static void timSort(long[][] arr, Comparator<Long[]> c) {
Long[][] temp = new Long[arr.length][arr[0].length];
for (int i = 0; i < arr.length; i++)
for (int j = 0; j < arr[0].length; j++)
temp[i][j] = arr[i][j];
Arrays.sort(temp, c);
for (int i = 0; i < arr.length; i++)
for (int j = 0; j < arr[0].length; j++)
temp[i][j] = arr[i][j];
}
}
public long fastPow(long x, long y, long mod) {
if (y == 0) return 1;
if (y == 1) return x % mod;
long temp = fastPow(x, y / 2, mod);
long ans = (temp * temp) % mod;
ans = ((y % 2 == 1) ? (ans * (x % mod)) % mod : ans);
return (ans + mod) % mod;
}
public long fastPow(long x, long y) {
if (y == 0) return 1;
if (y == 1) return x;
long temp = fastPow(x, y / 2);
long ans = (temp * temp);
return (y % 2 == 1) ? (ans * x) : ans;
}
}
|
Java
|
["5\n\n2\n\n4\n\n6\n\n8\n\n60"]
|
2 seconds
|
["1 0 1\n3 2 1\n12 7 1\n42 27 1\n341102826 248150916 1"]
|
NoteIn the first test case, Alex wins if he receives the card $$$2$$$ (he plays it, and Boris cannot respond). If Alex receives the card $$$1$$$, the game ends in a draw.In the second test case: Alex wins if he receives the cards $$$[3, 4]$$$, $$$[2, 4]$$$ or $$$[1, 4]$$$; Boris wins if Alex receives the cards $$$[1, 2]$$$ or $$$[1, 3]$$$; the game ends in a draw if Alex receives the cards $$$[2, 3]$$$.
|
Java 11
|
standard input
|
[
"combinatorics",
"constructive algorithms",
"dp",
"games"
] |
63fc2fb08d248f8ccdb3f5364c96dac1
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 30$$$) — the number of test cases. Then, $$$t$$$ lines follow. The $$$i$$$-th line contains one even integer $$$n$$$ ($$$2 \le n \le 60$$$).
| 1,500
|
For each test case, print three integers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. Since the answers can be large, print them modulo $$$998244353$$$.
|
standard output
| |
PASSED
|
041c3f9e55443dbb09bba566b4ed099b
|
train_109.jsonl
|
1664462100
|
Consider a game with $$$n$$$ cards ($$$n$$$ is even). Each card has a number written on it, between $$$1$$$ and $$$n$$$. All numbers on the cards are different. We say that a card with number $$$x$$$ is stronger than a card with number $$$y$$$ if $$$x > y$$$.Two players, Alex and Boris, play this game. In the beginning, each of them receives exactly $$$\frac{n}{2}$$$ cards, so each card belongs to exactly one player. Then, they take turns. Alex goes first, then Boris, then Alex again, and so on.On a player's turn, he must play exactly one of his cards. Then, if the opponent doesn't have any cards stronger than the card played, the opponent loses, and the game ends. Otherwise, the opponent has to play a stronger card (exactly one card as well). These two cards are removed from the game, and the turn ends. If there are no cards left, the game ends in a draw; otherwise it's the opponent's turn.Consider all possible ways to distribute the cards between two players, so that each of them receives exactly half of the cards. You have to calculate three numbers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. You may assume that both players play optimally (i. e. if a player can win no matter how his opponent plays, he wins). Two ways to distribute the cards are different if there is at least one card such that, in one of these ways, it is given to Alex, and in the other way, it is given to Boris.For example, suppose $$$n = 4$$$, Alex receives the cards $$$[2, 3]$$$, and Boris receives the cards $$$[1, 4]$$$. Then the game may go as follows: if Alex plays the card $$$2$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$3$$$. So, the game ends in a draw; if Alex plays the card $$$3$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$2$$$. So, the game ends in a draw. So, in this case, the game ends in a draw.
|
512 megabytes
|
import java.util.Scanner;
public class C {
static long mod=998244353;
public static void main(String[] args) {
Scanner stdin=new Scanner(System.in);
int t=stdin.nextInt();
long[] A=new long[61];
long[] B=new long[61];
long[] D=new long[61];
long[][] nCr=nCrTable(60);
A[2]=1;
B[2]=0;
D[2]=1;
A[4]=3;
B[4]=2;
D[4]=1;
for (int i=6;i<=60;i+=2) {
A[i]=((nCr[i-1][i/2]+A[i-4])%mod+nCr[i-4][i/2-3])%mod;
B[i]=(nCr[i][i/2]-1-A[i]+10*mod)%mod;
D[i]=1;
}
while (t-->0) {
int n=stdin.nextInt();
System.out.println(A[n] + " " + B[n] + " " +D[n]);
}
}
static long[][] nCrTable(int n) {
long[][] t=new long[n+1][];
for (int i=0;i<=n;i++) t[i]=new long[i+1];
t[0][0]=1;
for (int i=1;i<=n;i++) {
t[i][0]=1;
t[i][i]=1;
for (int j=1;j<i;j++) {
t[i][j]=(t[i-1][j-1]+t[i-1][j])%mod;
}
}
return t;
}
}
|
Java
|
["5\n\n2\n\n4\n\n6\n\n8\n\n60"]
|
2 seconds
|
["1 0 1\n3 2 1\n12 7 1\n42 27 1\n341102826 248150916 1"]
|
NoteIn the first test case, Alex wins if he receives the card $$$2$$$ (he plays it, and Boris cannot respond). If Alex receives the card $$$1$$$, the game ends in a draw.In the second test case: Alex wins if he receives the cards $$$[3, 4]$$$, $$$[2, 4]$$$ or $$$[1, 4]$$$; Boris wins if Alex receives the cards $$$[1, 2]$$$ or $$$[1, 3]$$$; the game ends in a draw if Alex receives the cards $$$[2, 3]$$$.
|
Java 11
|
standard input
|
[
"combinatorics",
"constructive algorithms",
"dp",
"games"
] |
63fc2fb08d248f8ccdb3f5364c96dac1
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 30$$$) — the number of test cases. Then, $$$t$$$ lines follow. The $$$i$$$-th line contains one even integer $$$n$$$ ($$$2 \le n \le 60$$$).
| 1,500
|
For each test case, print three integers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. Since the answers can be large, print them modulo $$$998244353$$$.
|
standard output
| |
PASSED
|
32f032bafd41f59fa0d49becd922bbdb
|
train_109.jsonl
|
1664462100
|
Consider a game with $$$n$$$ cards ($$$n$$$ is even). Each card has a number written on it, between $$$1$$$ and $$$n$$$. All numbers on the cards are different. We say that a card with number $$$x$$$ is stronger than a card with number $$$y$$$ if $$$x > y$$$.Two players, Alex and Boris, play this game. In the beginning, each of them receives exactly $$$\frac{n}{2}$$$ cards, so each card belongs to exactly one player. Then, they take turns. Alex goes first, then Boris, then Alex again, and so on.On a player's turn, he must play exactly one of his cards. Then, if the opponent doesn't have any cards stronger than the card played, the opponent loses, and the game ends. Otherwise, the opponent has to play a stronger card (exactly one card as well). These two cards are removed from the game, and the turn ends. If there are no cards left, the game ends in a draw; otherwise it's the opponent's turn.Consider all possible ways to distribute the cards between two players, so that each of them receives exactly half of the cards. You have to calculate three numbers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. You may assume that both players play optimally (i. e. if a player can win no matter how his opponent plays, he wins). Two ways to distribute the cards are different if there is at least one card such that, in one of these ways, it is given to Alex, and in the other way, it is given to Boris.For example, suppose $$$n = 4$$$, Alex receives the cards $$$[2, 3]$$$, and Boris receives the cards $$$[1, 4]$$$. Then the game may go as follows: if Alex plays the card $$$2$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$3$$$. So, the game ends in a draw; if Alex plays the card $$$3$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$2$$$. So, the game ends in a draw. So, in this case, the game ends in a draw.
|
512 megabytes
|
import java.util.*;
import java.io.*;
public class Main {
static long mod = 998244353;
static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
public static void main(String[] args) throws IOException {
FastReader sc = new FastReader();
int t = sc.nextInt();
while( t-- > 0) {
int n = sc.nextInt();
long fst = 0;
long total = nCrModPFermat(n, n/2, mod);
int fixed = 0;
for( int i = 1; i <= n/2 ;i ++) {
if( i %2 != 0) {
fst+=nCrModPFermat(n-1-fixed, (n/2) - (fixed/2)-1, mod);
fst%=mod;
// out.println( i + " " + nCrModPFermat(n-1-fixed, (n/2) - (fixed/2)-1, mod));
fixed+=2;
}
else {
if((n/2)-(fixed/2)-2 < 0 ) {
break;
}
fst+=nCrModPFermat(n-fixed-2, (n/2)-(fixed/2)-2, mod);
fst%=mod;
// out.println( i + " " + nCrModPFermat(n-fixed, (n/2)-(fixed/2)-2, mod));
fixed+=2;
}
}
out.println(fst + " " + (total+mod+ - fst -1)%mod + " " + 1);
}
out.flush();
}
/*
* think of binary search
* look at test cases
* do significant case work
*/
static long power(long x, long y, long p)
{
// Initialize result
long res = 1;
// Update x if it is more than or
// equal to p
x = x % p;
while (y > 0) {
// If y is odd, multiply x
// with result
if (y % 2 == 1)
res = (res * x) % p;
// y must be even now
y = y >> 1; // y = y/2
x = (x * x) % p;
}
return res;
}
// Returns n^(-1) mod p
static long modInverse(long n, long p)
{
return power(n, p - 2, p);
}
// Returns nCr % p using Fermat's
// little theorem.
static long nCrModPFermat(int n, int r,
long p)
{
if (n<r)
return 0L;
// Base case
if (r == 0)
return 1L;
// Fill factorial array so that we
// can find all factorial of r, n
// and n-r
long[] fac = new long[n + 1];
fac[0] = 1;
for (int i = 1; i <= n; i++)
fac[i] = fac[i - 1] * i % p;
return (fac[n] * modInverse(fac[r], p)
% p * modInverse(fac[n - r], p)
% p)
% p;
}
static class DSU{
int n;
int[] leaders,size;
public DSU(int n){
this.n=n;
leaders=new int[n+1];
size=new int[n+1];
for(int i=1;i<=n;i++){
leaders[i]=i;
size[i]=1;
}
}
public int find(int a){
if(leaders[a]==a) return a;
return leaders[a]=find (leaders[a]);
}
public void merge(int a,int b){
a = find(a);
b = find(b);
if (a == b) return;
if (size[a] < size[b]) swap(a, b);
leaders[b] = a;
size[a] += size[b];
}
public void swap(int a,int b){
int temp=a;
a=b;
b=temp;
}
}
public static boolean ifpowof2(long n ) {
return ((n&(n-1)) == 0);
}
static boolean isprime(long x ) {
if( x== 2) {
return true;
}
if( x%2 == 0) {
return false;
}
for( long i = 3 ;i*i <= x ;i+=2) {
if( x%i == 0) {
return false;
}
}
return true;
}
static boolean[] sieveOfEratosthenes(long n) {
boolean prime[] = new boolean[(int)n + 1];
for (int i = 0; i <= n; i++) {
prime[i] = true;
}
for (long p = 2; p * p <= n; p++) {
if (prime[(int)p] == true) {
for (long i = p * p; i <= n; i += p)
prime[(int)i] = false;
}
}
return prime;
}
public List<Integer> goodIndices(int[] nums, int k) {
int n = nums.length;
int fst[] = nextLargerElement(nums, n);
int scnd[] = nextlargerElement(nums, n);
List<Integer> ans = new ArrayList<>();
for( int i = 0 ;i < n; i++) {
if( fst[i] == -1 || scnd[i] == -1) {
continue;
}
if( fst[i]-i >= k && i - scnd[i] >= k) {
ans.add(i);
}
}
return ans;
}
public static int[] nextLargerElement(int[] arr, int n) {
Stack<Integer> stack = new Stack<>();
int rtrn[] = new int[n];
rtrn[n-1] = -1;
stack.push( n-1);
for( int i = n-2 ;i >= 0 ; i--){
int temp = arr[i];
int lol = -1;
while( !stack.isEmpty() && arr[stack.peek()] <= temp){
if(arr[stack.peek()] == temp ) {
lol = stack.peek();
}
stack.pop();
}
if( stack.isEmpty()){
if( lol != -1) {
rtrn[i] = lol;
}
else {
rtrn[i] = -1;
}
}
else{
rtrn[i] = stack.peek();
}
stack.push( i);
}
return rtrn;
}
public static int[] nextlargerElement(int[] arr, int n) {
Stack<Integer> stack = new Stack<>();
int rtrn[] = new int[n];
rtrn[0] = -1;
stack.add( 0);
for( int i = 1 ;i < n ; i++){
int temp = arr[i];
int lol = -1;
while( !stack.isEmpty() && arr[stack.peek()] <= temp){
if(arr[stack.peek()] == temp ) {
lol = stack.peek();
}
stack.pop();
}
if( stack.isEmpty()){
if( lol != -1) {
rtrn[i] = lol;
}
else {
rtrn[i] = -1;
}
}
else{
rtrn[i] = stack.peek();
}
stack.add( i);
}
return rtrn;
}
static void mysort(int[] arr) {
for(int i=0;i<arr.length;i++) {
int rand = (int) (Math.random() * arr.length);
int loc = arr[rand];
arr[rand] = arr[i];
arr[i] = loc;
}
Arrays.sort(arr);
}
static void mySort(long[] arr) {
for(int i=0;i<arr.length;i++) {
int rand = (int) (Math.random() * arr.length);
long loc = arr[rand];
arr[rand] = arr[i];
arr[i] = loc;
}
Arrays.sort(arr);
}
static long gcd(long a, long b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
static long lcm(long a, long b)
{
return (a / gcd(a, b)) * b;
}
static long rightmostsetbit(long n) {
return n&-n;
}
static long leftmostsetbit(long n)
{
long k = (long)(Math.log(n) / Math.log(2));
return k;
}
static HashMap<Long,Long> primefactor( long n){
HashMap<Long ,Long> hm = new HashMap<>();
long temp = 0;
while( n%2 == 0) {
temp++;
n/=2;
}
if( temp!= 0) {
hm.put( 2L, temp);
}
long c = (long)Math.sqrt(n);
for( long i = 3 ; i <= c ; i+=2) {
temp = 0;
while( n% i == 0) {
temp++;
n/=i;
}
if( temp!= 0) {
hm.put( i, temp);
}
}
if( n!= 1) {
hm.put( n , 1L);
}
return hm;
}
static TreeSet<Long> allfactors(long abs) {
HashMap<Long,Integer> hm = new HashMap<>();
TreeSet<Long> rtrn = new TreeSet<>();
rtrn.add(1L);
for( long i = 2 ;i*i <= abs; i++) {
if( abs% i == 0) {
hm.put( i , 0);
hm.put(abs/i, 0);
}
}
for( long x : hm.keySet()) {
rtrn.add(x);
}
if( abs != 0) {
rtrn.add(abs);
}
return rtrn;
}
public static int[][] prefixsum( int n , int m , int arr[][] ){
int prefixsum[][] = new int[n+1][m+1];
for( int i = 1 ;i <= n ;i++) {
for( int j = 1 ; j<= m ; j++) {
int toadd = 0;
if( arr[i-1][j-1] == 1) {
toadd = 1;
}
prefixsum[i][j] = toadd + prefixsum[i][j-1] + prefixsum[i-1][j] - prefixsum[i-1][j-1];
}
}
return prefixsum;
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
}
|
Java
|
["5\n\n2\n\n4\n\n6\n\n8\n\n60"]
|
2 seconds
|
["1 0 1\n3 2 1\n12 7 1\n42 27 1\n341102826 248150916 1"]
|
NoteIn the first test case, Alex wins if he receives the card $$$2$$$ (he plays it, and Boris cannot respond). If Alex receives the card $$$1$$$, the game ends in a draw.In the second test case: Alex wins if he receives the cards $$$[3, 4]$$$, $$$[2, 4]$$$ or $$$[1, 4]$$$; Boris wins if Alex receives the cards $$$[1, 2]$$$ or $$$[1, 3]$$$; the game ends in a draw if Alex receives the cards $$$[2, 3]$$$.
|
Java 11
|
standard input
|
[
"combinatorics",
"constructive algorithms",
"dp",
"games"
] |
63fc2fb08d248f8ccdb3f5364c96dac1
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 30$$$) — the number of test cases. Then, $$$t$$$ lines follow. The $$$i$$$-th line contains one even integer $$$n$$$ ($$$2 \le n \le 60$$$).
| 1,500
|
For each test case, print three integers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. Since the answers can be large, print them modulo $$$998244353$$$.
|
standard output
| |
PASSED
|
29ff183edce917acd5e3a317d8eaa771
|
train_109.jsonl
|
1664462100
|
Consider a game with $$$n$$$ cards ($$$n$$$ is even). Each card has a number written on it, between $$$1$$$ and $$$n$$$. All numbers on the cards are different. We say that a card with number $$$x$$$ is stronger than a card with number $$$y$$$ if $$$x > y$$$.Two players, Alex and Boris, play this game. In the beginning, each of them receives exactly $$$\frac{n}{2}$$$ cards, so each card belongs to exactly one player. Then, they take turns. Alex goes first, then Boris, then Alex again, and so on.On a player's turn, he must play exactly one of his cards. Then, if the opponent doesn't have any cards stronger than the card played, the opponent loses, and the game ends. Otherwise, the opponent has to play a stronger card (exactly one card as well). These two cards are removed from the game, and the turn ends. If there are no cards left, the game ends in a draw; otherwise it's the opponent's turn.Consider all possible ways to distribute the cards between two players, so that each of them receives exactly half of the cards. You have to calculate three numbers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. You may assume that both players play optimally (i. e. if a player can win no matter how his opponent plays, he wins). Two ways to distribute the cards are different if there is at least one card such that, in one of these ways, it is given to Alex, and in the other way, it is given to Boris.For example, suppose $$$n = 4$$$, Alex receives the cards $$$[2, 3]$$$, and Boris receives the cards $$$[1, 4]$$$. Then the game may go as follows: if Alex plays the card $$$2$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$3$$$. So, the game ends in a draw; if Alex plays the card $$$3$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$2$$$. So, the game ends in a draw. So, in this case, the game ends in a draw.
|
512 megabytes
|
import java.io.*;
import java.lang.*;
import java.util.*;
public class ComdeFormces {
static int x[]= {-1,0,0,1,1,1,-1,-1};
static int y[]= {0,1,-1,0,1,-1,1,-1};
static int dp[][][][];
static int seg[];
static class Trie{
Trie a[];
int ind;
public Trie() {
this.a=new Trie[3];
this.ind=-1;
}
}
static long ncr[][];
public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
FastReader sc=new FastReader();
BufferedWriter log = new BufferedWriter(new OutputStreamWriter(System.out));
// OutputStream out = new BufferedOutputStream ( System.out );
int t=sc.nextInt();
int tc=1;
ncr=new long[61][61];
for(int i=1;i<61;i++) {
for(int j=0;j<=i;j++) {
if(i==j || j==0)ncr[i][j]=1;
else ncr[i][j]=((ncr[i-1][j])%m+(ncr[i-1][j-1])%m)%m;
}
}
while(t--!=0) {
int n=sc.nextInt();
long dp[][]=new long[2][n+1];
eval(n,-1,dp);
log.write(dp[0][n]+" "+dp[1][n]+" "+1+"\n");
log.flush();
}
}
static long eval(int n,int i,long dp[][]) {
if(n==0) {
return 0;
}
if(i==1) {
return dp[i][n]=(ncr[n-2][n/2]+eval(n-2,1-i,dp))%m;
}
else if(i==0) {
return dp[i][n]=(ncr[n-1][n/2]+eval(n-2,1-i,dp))%m;
}
else {
dp[1][n]=(ncr[n-2][n/2]+eval(n-2,0,dp))%m;
dp[0][n]=(ncr[n-1][n/2]+eval(n-2,1,dp))%m;
return 1;
}
}
static void insert(Trie proot,int a[],int ind) {
Trie root=proot;
for(int i=0;i<a.length;i++) {
if(root.a[a[i]]==null) {
root.a[a[i]]=new Trie();
root=root.a[a[i]];
}
else root=root.a[a[i]];
}
root.ind=ind;
}
static int ser(Trie proot,int a[],int b[]) {
Trie root=proot;
for(int i=0;i<a.length;i++) {
if(a[i]==b[i]) {
if(root.a[a[i]]==null)return -1;
root=root.a[a[i]];
}
else {
if((a[i]==1 || a[i]==0) && (b[i]==0 || b[i]==1)) {
if(root.a[2]==null)return -1;
root=root.a[2];
}
else if((a[i]==1 || a[i]==2) && (b[i]==1 || b[i]==2)) {
if(root.a[0]==null)return -1;
root=root.a[0];
}
else if((a[i]==2 || a[i]==0) && (b[i]==2 || b[i]==0)) {
if(root.a[1]==null)return -1;
root=root.a[1];
}
}
}
return root.ind;
}
public static int m=(int)(998244353);
public static int mul(int a, int b) {
return ((a%m)*(b%m))%m;
}
public static long mul(long a, long b) {
return ((a%m)*(b%m))%m;
}
public static int add(int a, int b) {
return ((a%m)+(b%m))%m;
}
public static long add(long a, long b) {
return ((a%m)+(b%m))%m;
}
public static long sub(long a,long b) {
return ((a%m)-(b%m)+m)%m;
}
static long gcd(long a,long b) {
if(b==0)return a;
else return gcd(b,a%b);
}
static int gcd(int a,int b) {
if(b==0)return a;
else return gcd(b,a%b);
}
static long ncr(int n, int r){
if(r>n-r)r=n-r;
long ans=1;
long m=998244353;
for(int i=0;i<r;i++){
ans=mul(ans,(n-i));
ans=div(ans,i+1,m);
}
return ans;
}
public static class num{
long v;
}
static long gcd(long a,long b,num x,num y) {
if(b==0) {
x.v=1;
y.v=0;
return a;
}
num x1=new num();
num y1=new num();
long ans=gcd(b,a%b,x1,y1);
x.v=y1.v;
y.v=x1.v-(a/b)*y1.v;
return ans;
}
static long inverse(long b,long m) {
num x=new num();
num y=new num();
long gc=gcd(b,m,x,y);
if(gc!=1) {
return -1;
}
return (x.v%m+m)%m;
}
static long div(long a,long b,long m) {
a%=m;
if(inverse(b,m)==-1)return a/b;
return (inverse(b,m)*a)%m;
}
public static class trip{
int a;
int b;
int c;
public trip(int a,int b,int c) {
this.a=a;
this.b=b;
this.c=c;
}
}
static void mergesort(int[] a,int start,int end) {
if(start>=end)return ;
int mid=start+(end-start)/2;
mergesort(a,start,mid);
mergesort(a,mid+1,end);
merge(a,start,mid,end);
}
static void merge(int[] a, int start,int mid,int end) {
int ptr1=start;
int ptr2=mid+1;
int b[]=new int[end-start+1];
int i=0;
while(ptr1<=mid && ptr2<=end) {
if(a[ptr1]<=a[ptr2]) {
b[i]=a[ptr1];
ptr1++;
i++;
}
else {
b[i]=a[ptr2];
ptr2++;
i++;
}
}
while(ptr1<=mid) {
b[i]=a[ptr1];
ptr1++;
i++;
}
while(ptr2<=end) {
b[i]=a[ptr2];
ptr2++;
i++;
}
for(int j=start;j<=end;j++) {
a[j]=b[j-start];
}
}
public static class FastReader {
BufferedReader b;
StringTokenizer s;
public FastReader() {
b=new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while(s==null ||!s.hasMoreElements()) {
try {
s=new StringTokenizer(b.readLine());
}
catch(IOException e) {
e.printStackTrace();
}
}
return s.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str="";
try {
str=b.readLine();
}
catch(IOException e) {
e.printStackTrace();
}
return str;
}
boolean hasNext() {
if (s != null && s.hasMoreTokens()) {
return true;
}
String tmp;
try {
b.mark(1000);
tmp = b.readLine();
if (tmp == null) {
return false;
}
b.reset();
} catch (IOException e) {
return false;
}
return true;
}
}
public static class pair{
int a;
int b;
public pair(int a,int b) {
this.a=a;
this.b=b;
}
@Override
public String toString() {
return "{"+this.a+" "+this.b+"}";
}
}
static long pow(long a, long pw) {
long temp;
if(pw==0)return 1;
temp=pow(a,pw/2);
if(pw%2==0)return temp*temp;
return a*temp*temp;
}
public static int md=998244353;
static int pow(int a, int pw) {
int temp;
if(pw==0)return 1;
temp=pow(a,pw/2);
if(pw%2==0)return temp*temp;
return a*temp*temp;
}
}
|
Java
|
["5\n\n2\n\n4\n\n6\n\n8\n\n60"]
|
2 seconds
|
["1 0 1\n3 2 1\n12 7 1\n42 27 1\n341102826 248150916 1"]
|
NoteIn the first test case, Alex wins if he receives the card $$$2$$$ (he plays it, and Boris cannot respond). If Alex receives the card $$$1$$$, the game ends in a draw.In the second test case: Alex wins if he receives the cards $$$[3, 4]$$$, $$$[2, 4]$$$ or $$$[1, 4]$$$; Boris wins if Alex receives the cards $$$[1, 2]$$$ or $$$[1, 3]$$$; the game ends in a draw if Alex receives the cards $$$[2, 3]$$$.
|
Java 11
|
standard input
|
[
"combinatorics",
"constructive algorithms",
"dp",
"games"
] |
63fc2fb08d248f8ccdb3f5364c96dac1
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 30$$$) — the number of test cases. Then, $$$t$$$ lines follow. The $$$i$$$-th line contains one even integer $$$n$$$ ($$$2 \le n \le 60$$$).
| 1,500
|
For each test case, print three integers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. Since the answers can be large, print them modulo $$$998244353$$$.
|
standard output
| |
PASSED
|
3c2264736153abc1fb8213c1df5b7cc8
|
train_109.jsonl
|
1664462100
|
Consider a game with $$$n$$$ cards ($$$n$$$ is even). Each card has a number written on it, between $$$1$$$ and $$$n$$$. All numbers on the cards are different. We say that a card with number $$$x$$$ is stronger than a card with number $$$y$$$ if $$$x > y$$$.Two players, Alex and Boris, play this game. In the beginning, each of them receives exactly $$$\frac{n}{2}$$$ cards, so each card belongs to exactly one player. Then, they take turns. Alex goes first, then Boris, then Alex again, and so on.On a player's turn, he must play exactly one of his cards. Then, if the opponent doesn't have any cards stronger than the card played, the opponent loses, and the game ends. Otherwise, the opponent has to play a stronger card (exactly one card as well). These two cards are removed from the game, and the turn ends. If there are no cards left, the game ends in a draw; otherwise it's the opponent's turn.Consider all possible ways to distribute the cards between two players, so that each of them receives exactly half of the cards. You have to calculate three numbers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. You may assume that both players play optimally (i. e. if a player can win no matter how his opponent plays, he wins). Two ways to distribute the cards are different if there is at least one card such that, in one of these ways, it is given to Alex, and in the other way, it is given to Boris.For example, suppose $$$n = 4$$$, Alex receives the cards $$$[2, 3]$$$, and Boris receives the cards $$$[1, 4]$$$. Then the game may go as follows: if Alex plays the card $$$2$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$3$$$. So, the game ends in a draw; if Alex plays the card $$$3$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$2$$$. So, the game ends in a draw. So, in this case, the game ends in a draw.
|
512 megabytes
|
import java.util.*;
public class C_Card_Game {
static long printNcR(int n, int r) {
long p = 1, k = 1;
if (n - r < r) {
r = n - r;
}
if (r != 0) {
while (r > 0) {
p *= n;
k *= r;
long m = __gcd(p, k);
p /= m;
k /= m;
n--;
r--;
}
} else {
p = 1;
}
return p;
}
static long __gcd(long n1, long n2) {
long gcd = 1;
for (int i = 1; i <= n1 && i <= n2; ++i) {
// Checks if i is factor of both integers
if (n1 % i == 0 && n2 % i == 0) {
gcd = i;
}
}
return gcd;
}
static long mod = 998244353;
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int t = scn.nextInt();
long[][] dp = new long[61][2];
for (int i = 0; i < dp.length; i++) {
for (int j = 0; j < dp[0].length; j++) {
dp[i][j] = -1;
}
}
while (t-- > 0) {
int n = scn.nextInt();
if (dp[n][0] != -1) {
System.out.println(dp[n][0] + " " + dp[n][1] + " " + 1);
} else {
long alose = 0;
long blose = 0;
int p = n - 3;
int r = n / 2 - 1;
for (int i = 0; r > 0; i++) {
long sum = 0;
int k = p;
while (k >= r) {
sum += printNcR(k, r);
k--;
}
if (i % 2 == 0)
alose += sum;
else
blose += sum;
r--;
p -= 2;
}
// System.out.println(alose+" "+ blose);
int p1 = n - 1;
int r1 = n / 2;
for (int i = 0; r1 > 0; i++) {
if (i % 2 == 0)
blose += printNcR(p1, r1);
else
alose += printNcR(p1, r1);
p1 -= 2;
r1--;
}
dp[n][0] = blose % mod;
dp[n][1] = alose % mod;
System.out.print(blose % mod + " " + alose % mod + " " + 1);
System.out.println();
}
}
}
}
|
Java
|
["5\n\n2\n\n4\n\n6\n\n8\n\n60"]
|
2 seconds
|
["1 0 1\n3 2 1\n12 7 1\n42 27 1\n341102826 248150916 1"]
|
NoteIn the first test case, Alex wins if he receives the card $$$2$$$ (he plays it, and Boris cannot respond). If Alex receives the card $$$1$$$, the game ends in a draw.In the second test case: Alex wins if he receives the cards $$$[3, 4]$$$, $$$[2, 4]$$$ or $$$[1, 4]$$$; Boris wins if Alex receives the cards $$$[1, 2]$$$ or $$$[1, 3]$$$; the game ends in a draw if Alex receives the cards $$$[2, 3]$$$.
|
Java 11
|
standard input
|
[
"combinatorics",
"constructive algorithms",
"dp",
"games"
] |
63fc2fb08d248f8ccdb3f5364c96dac1
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 30$$$) — the number of test cases. Then, $$$t$$$ lines follow. The $$$i$$$-th line contains one even integer $$$n$$$ ($$$2 \le n \le 60$$$).
| 1,500
|
For each test case, print three integers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. Since the answers can be large, print them modulo $$$998244353$$$.
|
standard output
| |
PASSED
|
a6335387ae4f7606e67471cb0fccda72
|
train_109.jsonl
|
1664462100
|
Consider a game with $$$n$$$ cards ($$$n$$$ is even). Each card has a number written on it, between $$$1$$$ and $$$n$$$. All numbers on the cards are different. We say that a card with number $$$x$$$ is stronger than a card with number $$$y$$$ if $$$x > y$$$.Two players, Alex and Boris, play this game. In the beginning, each of them receives exactly $$$\frac{n}{2}$$$ cards, so each card belongs to exactly one player. Then, they take turns. Alex goes first, then Boris, then Alex again, and so on.On a player's turn, he must play exactly one of his cards. Then, if the opponent doesn't have any cards stronger than the card played, the opponent loses, and the game ends. Otherwise, the opponent has to play a stronger card (exactly one card as well). These two cards are removed from the game, and the turn ends. If there are no cards left, the game ends in a draw; otherwise it's the opponent's turn.Consider all possible ways to distribute the cards between two players, so that each of them receives exactly half of the cards. You have to calculate three numbers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. You may assume that both players play optimally (i. e. if a player can win no matter how his opponent plays, he wins). Two ways to distribute the cards are different if there is at least one card such that, in one of these ways, it is given to Alex, and in the other way, it is given to Boris.For example, suppose $$$n = 4$$$, Alex receives the cards $$$[2, 3]$$$, and Boris receives the cards $$$[1, 4]$$$. Then the game may go as follows: if Alex plays the card $$$2$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$3$$$. So, the game ends in a draw; if Alex plays the card $$$3$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$2$$$. So, the game ends in a draw. So, in this case, the game ends in a draw.
|
512 megabytes
|
import java.util.*;
import java.lang.invoke.MethodHandles;
import java.io.*;
public class C1739C {
static final int MOD = 998244353;
static final Random RAND = new Random();
static Combination COMB = new Combination(60);
static int[] solve(int n) {
int[] ans = new int[3];
boolean alexTurn=true; //Alex gets to play first
//n=2 is the base case here, and the number of wins will reversed for each n-2
for (int i=n;i>=2;i-=2)
{
//Alex will win if he get i, then we find the number of way to distribute n-1 card to Alex
int v1=COMB.choose(i-1,i/2-1);
//if there is 2 card, Alex always win by getting the bigger card, Alex win is 0
//else, the number of way to distribute card for Boris win is: Alex get neither n or n-1, pick the rest for Boris
int v2 = (i==2) ? 0 : COMB.choose(i-2, i/2-2);
if (alexTurn)
{
ans[0]=(ans[0]+v1)%MOD; //if it is Alex turn, we can calculate number of distribute the n
ans[1]=(ans[1]+v2)%MOD;
}
else
{
//if not Alex turn, we have n-2 card left and it Boris turn, Boris will follow the same strategy to get the win-> the number of win is reversed
ans[1]=(ans[1]+v1)%MOD;
ans[0]=(ans[0]+v2)%MOD;
}
alexTurn=!alexTurn;//reversed turn after each play
}
ans[2]++;
return ans;
}
static class Combination {
final int n;
int[] fact;
int[] finv;
public Combination(int n) {
this.n = n;
this.fact = new int[n+1];
this.finv = new int[n+1];
fact[0] = 1;
finv[0] = 1;
for (int i = 1; i <= n; i++) {
fact[i] = (int) (((long) fact[i-1] * i) % MOD);
finv[i] = (int) (((long) finv[i-1] * inverse(i)) % MOD);
}
}
public int choose(int m, int k) {
return (int) ((((long)fact[m] * finv[k] % MOD) * finv[m-k]) % MOD);
}
public static int inverse(long a) {
return power(a, MOD - 2);
}
public static int power(long x, int y) {
if (y == 0) {
return 1;
}
long w = power(x, y / 2);
w = (w * w) % MOD;
return (int) (y % 2 == 0 ? w : (w * x) % MOD);
}
}
static boolean test = false;
static void doTest() {
if (!test) {
return;
}
long t0 = System.currentTimeMillis();
int m = 3;
int n = 2 * m;
int[] cnt = new int[3];
for (int i = 0; i < (1<<n); i++) {
if (Integer.bitCount(i) != m) {
continue;
}
List<Integer> numsa = new ArrayList<>();
List<Integer> numsb = new ArrayList<>();
for (int j = 0; j < n; j++) {
if ((i & (1 << j)) != 0) {
numsa.add(j+1);
} else {
numsb.add(j+1);
}
}
// System.out.format(" A: %s\n", numsa.toString());
// System.out.format(" B: %s\n", numsb.toString());
int w = 2;
for (int j = 0; j < m; j++) {
if (j % 2 == 0) {
// Alex's turn
int ib = 0;
while (ib < numsb.size() && numsb.get(ib) < numsa.get(numsa.size()-1)) {
ib++;
}
if (ib >= numsb.size()) {
w = 0;
break;
}
numsa.remove(numsa.size()-1);
numsb.remove(ib);
} else {
// Bob's turn
int ia = 0;
while (ia < numsa.size() && numsa.get(ia) < numsb.get(numsb.size()-1)) {
ia++;
}
if (ia >= numsa.size()) {
w = 1;
break;
}
numsa.remove(ia);
numsb.remove(numsb.size()-1);
}
}
System.out.format(" %s %d\n", traceBinary(i, n), w);
cnt[w]++;
}
System.out.format("%s\n", Arrays.toString(cnt));
System.out.format("%d msec\n", System.currentTimeMillis() - t0);
System.exit(0);
}
static String traceBinary(int v, int n) {
StringBuilder sb = new StringBuilder();
while (v != 0) {
sb.append((char)('0' + v % 2));
v /= 2;
}
while (sb.length() < n) {
sb.append('0');
}
return sb.reverse().toString();
}
public static void main(String[] args) {
doTest();
MyScanner in = new MyScanner();
int T = in.nextInt();
for (int t = 1; t <= T; t++) {
int n = in.nextInt();
int[] ans = solve(n);
System.out.format("%d %d %d\n", ans[0], ans[1], ans[2]);
}
}
static void output(int[] a) {
StringBuilder sb = new StringBuilder();
for (int v : a) {
sb.append(v);
sb.append(' ');
if (sb.length() > 4000) {
System.out.print(sb.toString());
sb.setLength(0);
}
}
System.out.println(sb.toString());
}
static void myAssert(boolean cond) {
if (!cond) {
throw new RuntimeException("Unexpected");
}
}
static class MyScanner {
BufferedReader br;
StringTokenizer st;
public MyScanner() {
try {
final String USERDIR = System.getProperty("user.dir");
String cname = MethodHandles.lookup().lookupClass().getCanonicalName().replace(".MyScanner", "");
cname = cname.lastIndexOf('.') > 0 ? cname.substring(cname.lastIndexOf('.') + 1) : cname;
final File fin = new File(USERDIR + "/io/c" + cname.substring(1,5) + "/" + cname + ".in");
br = new BufferedReader(new InputStreamReader(fin.exists()
? new FileInputStream(fin) : System.in));
} catch (Exception e) {
br = new BufferedReader(new InputStreamReader(System.in));
}
}
public String next() {
try {
while (st == null || !st.hasMoreElements()) {
st = new StringTokenizer(br.readLine());
}
return st.nextToken();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
}
}
|
Java
|
["5\n\n2\n\n4\n\n6\n\n8\n\n60"]
|
2 seconds
|
["1 0 1\n3 2 1\n12 7 1\n42 27 1\n341102826 248150916 1"]
|
NoteIn the first test case, Alex wins if he receives the card $$$2$$$ (he plays it, and Boris cannot respond). If Alex receives the card $$$1$$$, the game ends in a draw.In the second test case: Alex wins if he receives the cards $$$[3, 4]$$$, $$$[2, 4]$$$ or $$$[1, 4]$$$; Boris wins if Alex receives the cards $$$[1, 2]$$$ or $$$[1, 3]$$$; the game ends in a draw if Alex receives the cards $$$[2, 3]$$$.
|
Java 11
|
standard input
|
[
"combinatorics",
"constructive algorithms",
"dp",
"games"
] |
63fc2fb08d248f8ccdb3f5364c96dac1
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 30$$$) — the number of test cases. Then, $$$t$$$ lines follow. The $$$i$$$-th line contains one even integer $$$n$$$ ($$$2 \le n \le 60$$$).
| 1,500
|
For each test case, print three integers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. Since the answers can be large, print them modulo $$$998244353$$$.
|
standard output
| |
PASSED
|
1a5dfee0fdfdaa9424d274023687fde9
|
train_109.jsonl
|
1664462100
|
Consider a game with $$$n$$$ cards ($$$n$$$ is even). Each card has a number written on it, between $$$1$$$ and $$$n$$$. All numbers on the cards are different. We say that a card with number $$$x$$$ is stronger than a card with number $$$y$$$ if $$$x > y$$$.Two players, Alex and Boris, play this game. In the beginning, each of them receives exactly $$$\frac{n}{2}$$$ cards, so each card belongs to exactly one player. Then, they take turns. Alex goes first, then Boris, then Alex again, and so on.On a player's turn, he must play exactly one of his cards. Then, if the opponent doesn't have any cards stronger than the card played, the opponent loses, and the game ends. Otherwise, the opponent has to play a stronger card (exactly one card as well). These two cards are removed from the game, and the turn ends. If there are no cards left, the game ends in a draw; otherwise it's the opponent's turn.Consider all possible ways to distribute the cards between two players, so that each of them receives exactly half of the cards. You have to calculate three numbers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. You may assume that both players play optimally (i. e. if a player can win no matter how his opponent plays, he wins). Two ways to distribute the cards are different if there is at least one card such that, in one of these ways, it is given to Alex, and in the other way, it is given to Boris.For example, suppose $$$n = 4$$$, Alex receives the cards $$$[2, 3]$$$, and Boris receives the cards $$$[1, 4]$$$. Then the game may go as follows: if Alex plays the card $$$2$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$3$$$. So, the game ends in a draw; if Alex plays the card $$$3$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$2$$$. So, the game ends in a draw. So, in this case, the game ends in a draw.
|
512 megabytes
|
import java.io.*; import java.util.*;
public class Main
{
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter pw = new PrintWriter(System.out);
long[][] arr = new long[61][2];
arr[2][0] = 1; arr[2][1] = 0;
for(int i=4; i<61; i+=2){
long mult = 1;
ArrayList<Integer> u = new ArrayList<>();
for(int j=1; j<=i/2; j++){
u.add(j);
}
int j = i;
while(j>i/2){
mult*=j;
j--;
int f = 0;
ArrayDeque<Integer> r = new ArrayDeque<>();
for(int k: u){
if(mult%k==0){
mult/=k;
r.addFirst(f);
}
f++;
}
for(int l: r){
u.remove(l);
}
}
arr[i][0]=mult/2+arr[i-2][1];
arr[i][1]=mult-arr[i][0]-1;
arr[i][0]%=998244353;
arr[i][1]%=998244353;
}
int t = Integer.parseInt(br.readLine());
while(t-->0){
int n = Integer.parseInt(br.readLine());
pw.println(arr[n][0]+" "+arr[n][1]+" 1");
}
pw.close();
}
}
|
Java
|
["5\n\n2\n\n4\n\n6\n\n8\n\n60"]
|
2 seconds
|
["1 0 1\n3 2 1\n12 7 1\n42 27 1\n341102826 248150916 1"]
|
NoteIn the first test case, Alex wins if he receives the card $$$2$$$ (he plays it, and Boris cannot respond). If Alex receives the card $$$1$$$, the game ends in a draw.In the second test case: Alex wins if he receives the cards $$$[3, 4]$$$, $$$[2, 4]$$$ or $$$[1, 4]$$$; Boris wins if Alex receives the cards $$$[1, 2]$$$ or $$$[1, 3]$$$; the game ends in a draw if Alex receives the cards $$$[2, 3]$$$.
|
Java 11
|
standard input
|
[
"combinatorics",
"constructive algorithms",
"dp",
"games"
] |
63fc2fb08d248f8ccdb3f5364c96dac1
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 30$$$) — the number of test cases. Then, $$$t$$$ lines follow. The $$$i$$$-th line contains one even integer $$$n$$$ ($$$2 \le n \le 60$$$).
| 1,500
|
For each test case, print three integers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. Since the answers can be large, print them modulo $$$998244353$$$.
|
standard output
| |
PASSED
|
4f6bb015ac5a0e7eab5fcf5791ed5d83
|
train_109.jsonl
|
1664462100
|
Consider a game with $$$n$$$ cards ($$$n$$$ is even). Each card has a number written on it, between $$$1$$$ and $$$n$$$. All numbers on the cards are different. We say that a card with number $$$x$$$ is stronger than a card with number $$$y$$$ if $$$x > y$$$.Two players, Alex and Boris, play this game. In the beginning, each of them receives exactly $$$\frac{n}{2}$$$ cards, so each card belongs to exactly one player. Then, they take turns. Alex goes first, then Boris, then Alex again, and so on.On a player's turn, he must play exactly one of his cards. Then, if the opponent doesn't have any cards stronger than the card played, the opponent loses, and the game ends. Otherwise, the opponent has to play a stronger card (exactly one card as well). These two cards are removed from the game, and the turn ends. If there are no cards left, the game ends in a draw; otherwise it's the opponent's turn.Consider all possible ways to distribute the cards between two players, so that each of them receives exactly half of the cards. You have to calculate three numbers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. You may assume that both players play optimally (i. e. if a player can win no matter how his opponent plays, he wins). Two ways to distribute the cards are different if there is at least one card such that, in one of these ways, it is given to Alex, and in the other way, it is given to Boris.For example, suppose $$$n = 4$$$, Alex receives the cards $$$[2, 3]$$$, and Boris receives the cards $$$[1, 4]$$$. Then the game may go as follows: if Alex plays the card $$$2$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$3$$$. So, the game ends in a draw; if Alex plays the card $$$3$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$2$$$. So, the game ends in a draw. So, in this case, the game ends in a draw.
|
512 megabytes
|
import java.io.*; import java.util.*;
public class Main
{
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter pw = new PrintWriter(System.out);
long[][] arr = new long[61][2];
arr[2][0] = 1; arr[2][1] = 0;
for(int i=4; i<61; i+=2){
long mult = 1;
ArrayList<Integer> u = new ArrayList<>();
for(int j=1; j<=i/2; j++){
u.add(j);
}
int j = i;
while(j>i/2){
mult*=j;
j--;
int f = 0;
for(int k: u){
if(mult%k==0){
mult/=k;
u.remove(f);
f--;
break;
}
f++;
}
}
arr[i][0]=mult/2+arr[i-2][1];
arr[i][1]=mult-arr[i][0]-1;
arr[i][0]%=998244353;
arr[i][1]%=998244353;
}
int t = Integer.parseInt(br.readLine());
while(t-->0){
int n = Integer.parseInt(br.readLine());
pw.println(arr[n][0]+" "+arr[n][1]+" 1");
}
pw.close();
}
}
|
Java
|
["5\n\n2\n\n4\n\n6\n\n8\n\n60"]
|
2 seconds
|
["1 0 1\n3 2 1\n12 7 1\n42 27 1\n341102826 248150916 1"]
|
NoteIn the first test case, Alex wins if he receives the card $$$2$$$ (he plays it, and Boris cannot respond). If Alex receives the card $$$1$$$, the game ends in a draw.In the second test case: Alex wins if he receives the cards $$$[3, 4]$$$, $$$[2, 4]$$$ or $$$[1, 4]$$$; Boris wins if Alex receives the cards $$$[1, 2]$$$ or $$$[1, 3]$$$; the game ends in a draw if Alex receives the cards $$$[2, 3]$$$.
|
Java 11
|
standard input
|
[
"combinatorics",
"constructive algorithms",
"dp",
"games"
] |
63fc2fb08d248f8ccdb3f5364c96dac1
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 30$$$) — the number of test cases. Then, $$$t$$$ lines follow. The $$$i$$$-th line contains one even integer $$$n$$$ ($$$2 \le n \le 60$$$).
| 1,500
|
For each test case, print three integers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. Since the answers can be large, print them modulo $$$998244353$$$.
|
standard output
| |
PASSED
|
18170589883f74af5097ec691d585d09
|
train_109.jsonl
|
1664462100
|
Consider a game with $$$n$$$ cards ($$$n$$$ is even). Each card has a number written on it, between $$$1$$$ and $$$n$$$. All numbers on the cards are different. We say that a card with number $$$x$$$ is stronger than a card with number $$$y$$$ if $$$x > y$$$.Two players, Alex and Boris, play this game. In the beginning, each of them receives exactly $$$\frac{n}{2}$$$ cards, so each card belongs to exactly one player. Then, they take turns. Alex goes first, then Boris, then Alex again, and so on.On a player's turn, he must play exactly one of his cards. Then, if the opponent doesn't have any cards stronger than the card played, the opponent loses, and the game ends. Otherwise, the opponent has to play a stronger card (exactly one card as well). These two cards are removed from the game, and the turn ends. If there are no cards left, the game ends in a draw; otherwise it's the opponent's turn.Consider all possible ways to distribute the cards between two players, so that each of them receives exactly half of the cards. You have to calculate three numbers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. You may assume that both players play optimally (i. e. if a player can win no matter how his opponent plays, he wins). Two ways to distribute the cards are different if there is at least one card such that, in one of these ways, it is given to Alex, and in the other way, it is given to Boris.For example, suppose $$$n = 4$$$, Alex receives the cards $$$[2, 3]$$$, and Boris receives the cards $$$[1, 4]$$$. Then the game may go as follows: if Alex plays the card $$$2$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$3$$$. So, the game ends in a draw; if Alex plays the card $$$3$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$2$$$. So, the game ends in a draw. So, in this case, the game ends in a draw.
|
512 megabytes
|
import java.util.*;
import java.lang.*;
import java.io.*;
public class Main
{
static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
static FastReader sc = new FastReader();
static long mod = 998244353;//(int)1e9+7;
static int maxn = 60;
static int[][] C = new int[maxn + 1][maxn + 1];
static long[][] dp;
public static void main (String[] args) throws java.lang.Exception
{
int t = sc.nextInt();
C[0][0] = 1;
for (int n = 1; n <= maxn; ++n) {
C[n][0] = C[n][n] = 1;
for (int k = 1; k < n; ++k)
C[n][k] = (int)(((long)C[n - 1][k - 1] + C[n - 1][k])%mod);
}
dp = new long[61][61];
dp[2][1] = 0;
dp[2][0] = 1;
dp[0][0]=dp[0][1]=0;
for(int i = 4;i<61;i+=2){
dp[i][0] = (C[i-1][i/2-1] + dp[i-2][1])%mod;
dp[i][1] = (C[i][i/2] - 1 - dp[i][0] + mod)%mod ;
}
while(t-->0){
solve();
}
}
public static void solve() {
int n = i();
out.print(dp[n][0] + " " + dp[n][1] + " " + 1);
out.println();
out.flush();
}
static class Pair implements Comparable<Pair>{
int pos; int time;
Pair(int a, int b){
this.pos=a;
this.time=b;
}
public int compareTo(Pair o) {
return this.pos-o.pos;
}
//public String toString(){
//return "{" + a + " " + b + "}" ;
//}
}
static long gcd(long a, long b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
static int csb(long n)
{
int count = 0;
while (n > 0) {
n &= (n - 1);
count++;
}
return count;
}
static void mysort(int[] arr) {
for(int i=0;i<arr.length;i++) {
int rand = (int) (Math.random() * arr.length);
int loc = arr[rand];
arr[rand] = arr[i];
arr[i] = loc;
}
Arrays.sort(arr);
}
static long power(long x, long y)
{
long res = 1;
x = x % mod;
if (x == 0)
return 0;
while (y > 0)
{
if ((y & 1) != 0)
res = (res * x) % mod;
y = y >> 1;
x = (x * x) % mod;
}
return res;
}
static int i() {
return sc.nextInt();
}
static String s() {
return sc.next();
}
static long l() {
return sc.nextLong();
}
static int[] ia(int n){
int[] arr = new int[n];
for(int i = 0;i<n;++i){
arr[i]=i();
}
return arr;
}
static long[] la(int n){
long[] arr = new long[n];
for(int i = 0;i<n;++i){
arr[i]=l();
}
return arr;
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
}
|
Java
|
["5\n\n2\n\n4\n\n6\n\n8\n\n60"]
|
2 seconds
|
["1 0 1\n3 2 1\n12 7 1\n42 27 1\n341102826 248150916 1"]
|
NoteIn the first test case, Alex wins if he receives the card $$$2$$$ (he plays it, and Boris cannot respond). If Alex receives the card $$$1$$$, the game ends in a draw.In the second test case: Alex wins if he receives the cards $$$[3, 4]$$$, $$$[2, 4]$$$ or $$$[1, 4]$$$; Boris wins if Alex receives the cards $$$[1, 2]$$$ or $$$[1, 3]$$$; the game ends in a draw if Alex receives the cards $$$[2, 3]$$$.
|
Java 11
|
standard input
|
[
"combinatorics",
"constructive algorithms",
"dp",
"games"
] |
63fc2fb08d248f8ccdb3f5364c96dac1
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 30$$$) — the number of test cases. Then, $$$t$$$ lines follow. The $$$i$$$-th line contains one even integer $$$n$$$ ($$$2 \le n \le 60$$$).
| 1,500
|
For each test case, print three integers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. Since the answers can be large, print them modulo $$$998244353$$$.
|
standard output
| |
PASSED
|
7e8f5ed76225ae64d06152a748e44c36
|
train_109.jsonl
|
1664462100
|
Consider a game with $$$n$$$ cards ($$$n$$$ is even). Each card has a number written on it, between $$$1$$$ and $$$n$$$. All numbers on the cards are different. We say that a card with number $$$x$$$ is stronger than a card with number $$$y$$$ if $$$x > y$$$.Two players, Alex and Boris, play this game. In the beginning, each of them receives exactly $$$\frac{n}{2}$$$ cards, so each card belongs to exactly one player. Then, they take turns. Alex goes first, then Boris, then Alex again, and so on.On a player's turn, he must play exactly one of his cards. Then, if the opponent doesn't have any cards stronger than the card played, the opponent loses, and the game ends. Otherwise, the opponent has to play a stronger card (exactly one card as well). These two cards are removed from the game, and the turn ends. If there are no cards left, the game ends in a draw; otherwise it's the opponent's turn.Consider all possible ways to distribute the cards between two players, so that each of them receives exactly half of the cards. You have to calculate three numbers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. You may assume that both players play optimally (i. e. if a player can win no matter how his opponent plays, he wins). Two ways to distribute the cards are different if there is at least one card such that, in one of these ways, it is given to Alex, and in the other way, it is given to Boris.For example, suppose $$$n = 4$$$, Alex receives the cards $$$[2, 3]$$$, and Boris receives the cards $$$[1, 4]$$$. Then the game may go as follows: if Alex plays the card $$$2$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$3$$$. So, the game ends in a draw; if Alex plays the card $$$3$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$2$$$. So, the game ends in a draw. So, in this case, the game ends in a draw.
|
512 megabytes
|
import java.io.*;
public class Main2 {
public static long MOD = 998244353;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int testcase = Integer.parseInt(br.readLine());
int[][] pascal = new int[61][61];
pascal[0][0] = 1;
for (int i=1;i<61;i++) {
pascal[i][0] = pascal[i][i] = 1;
for (int j=1;j<i;j++) {
pascal[i][j] = pascal[i-1][j-1] + pascal[i-1][j];
pascal[i][j] %= MOD;
}
}
long[] alex = new long[61];
long[] bori = new long[61];
alex[2] = 1;
for (int i=4;i<=60;i+=2) {
bori[i] = alex[i-2];
alex[i] = bori[i-2];
bori[i] += pascal[i-2][i/2];
alex[i] += pascal[i-1][i/2];
alex[i] %= MOD;
bori[i] %= MOD;
}
C: while(testcase-->0) {
int n = Integer.parseInt(br.readLine());
bw.write(alex[n]+" "+bori[n]+" 1\n");
}
bw.flush();
}
}
|
Java
|
["5\n\n2\n\n4\n\n6\n\n8\n\n60"]
|
2 seconds
|
["1 0 1\n3 2 1\n12 7 1\n42 27 1\n341102826 248150916 1"]
|
NoteIn the first test case, Alex wins if he receives the card $$$2$$$ (he plays it, and Boris cannot respond). If Alex receives the card $$$1$$$, the game ends in a draw.In the second test case: Alex wins if he receives the cards $$$[3, 4]$$$, $$$[2, 4]$$$ or $$$[1, 4]$$$; Boris wins if Alex receives the cards $$$[1, 2]$$$ or $$$[1, 3]$$$; the game ends in a draw if Alex receives the cards $$$[2, 3]$$$.
|
Java 11
|
standard input
|
[
"combinatorics",
"constructive algorithms",
"dp",
"games"
] |
63fc2fb08d248f8ccdb3f5364c96dac1
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 30$$$) — the number of test cases. Then, $$$t$$$ lines follow. The $$$i$$$-th line contains one even integer $$$n$$$ ($$$2 \le n \le 60$$$).
| 1,500
|
For each test case, print three integers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. Since the answers can be large, print them modulo $$$998244353$$$.
|
standard output
| |
PASSED
|
9d3ae59a92d72cd26be6fb5f997b6a7e
|
train_109.jsonl
|
1664462100
|
Consider a game with $$$n$$$ cards ($$$n$$$ is even). Each card has a number written on it, between $$$1$$$ and $$$n$$$. All numbers on the cards are different. We say that a card with number $$$x$$$ is stronger than a card with number $$$y$$$ if $$$x > y$$$.Two players, Alex and Boris, play this game. In the beginning, each of them receives exactly $$$\frac{n}{2}$$$ cards, so each card belongs to exactly one player. Then, they take turns. Alex goes first, then Boris, then Alex again, and so on.On a player's turn, he must play exactly one of his cards. Then, if the opponent doesn't have any cards stronger than the card played, the opponent loses, and the game ends. Otherwise, the opponent has to play a stronger card (exactly one card as well). These two cards are removed from the game, and the turn ends. If there are no cards left, the game ends in a draw; otherwise it's the opponent's turn.Consider all possible ways to distribute the cards between two players, so that each of them receives exactly half of the cards. You have to calculate three numbers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. You may assume that both players play optimally (i. e. if a player can win no matter how his opponent plays, he wins). Two ways to distribute the cards are different if there is at least one card such that, in one of these ways, it is given to Alex, and in the other way, it is given to Boris.For example, suppose $$$n = 4$$$, Alex receives the cards $$$[2, 3]$$$, and Boris receives the cards $$$[1, 4]$$$. Then the game may go as follows: if Alex plays the card $$$2$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$3$$$. So, the game ends in a draw; if Alex plays the card $$$3$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$2$$$. So, the game ends in a draw. So, in this case, the game ends in a draw.
|
512 megabytes
|
import java.io.*;
import java.util.StringTokenizer;
public class C {
private final static String inputFileName = null;
private final static String outputFileName = null;
public static void main(String[] args) {
try (Solver solver = new Solver(inputFileName, outputFileName))
{
solver.solve();
}
}
private static class Solver implements AutoCloseable {
final long MOD = 998244353;
public void solve(){
int mx = 65;
long[][] choose = new long[mx][mx];
for (int i = 0; i < mx; i++)
for (int j = 0; j <= i; j++)
choose[i][j] = (i == 0 || j == 0) ? 1 : choose[i-1][j] + choose[i-1][j-1];
int t = reader.readInt();
while (t-- > 0)
{
int n = reader.readInt();
long a = 0, b = 0;
for (int i = 0; i < n; i++) {
if (i % 4 == 0 || i % 4 == 3)
a += choose[n - i - 1][n / 2 - i / 2];
else
b += choose[n - i - 1][n / 2 - i / 2];
}
writer.writeLine(a % MOD + " " + b % MOD + " 1");
}
}
private final FastReader reader;
private final FastWriter writer;
private Solver(String inputFileName, String outputFileName) {
this.reader = inputFileName == null ? new FastReader() : new FastReader(inputFileName);
this.writer = outputFileName == null ? new FastWriter() : new FastWriter(outputFileName);
}
@Override
public void close() {
reader.close();
writer.close();
}
}
private static class FastReader implements Closeable {
private final BufferedReader bufferedReader;
private StringTokenizer stringTokenizer;
public FastReader() {
bufferedReader = new BufferedReader(new InputStreamReader(System.in));
}
public FastReader(String fileName) {
try {
bufferedReader = new BufferedReader(new FileReader(fileName));
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
}
}
public String readLine() {
try {
return bufferedReader.readLine();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public String read() {
while (stringTokenizer == null || !stringTokenizer.hasMoreTokens())
stringTokenizer = new StringTokenizer(readLine());
return stringTokenizer.nextToken();
}
public int readInt() {
return Integer.parseInt(read());
}
public long readLong() {
return Long.parseLong(read());
}
public double readDouble() {
return Double.parseDouble(read());
}
@Override
public void close() {
try {
bufferedReader.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
private static class FastWriter implements Closeable {
private final BufferedWriter bufferedWriter;
public FastWriter() {
bufferedWriter = new BufferedWriter(new OutputStreamWriter(System.out));
}
public FastWriter(String fileName) {
try {
bufferedWriter = new BufferedWriter(new FileWriter(fileName));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public void newLine() {
try {
bufferedWriter.newLine();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public void write(Object object) {
try {
bufferedWriter.write(object.toString());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public void writeLine(Object object) {
write(object);
newLine();
}
public void flush() {
try {
bufferedWriter.flush();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
@Override
public void close() {
try {
bufferedWriter.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
|
Java
|
["5\n\n2\n\n4\n\n6\n\n8\n\n60"]
|
2 seconds
|
["1 0 1\n3 2 1\n12 7 1\n42 27 1\n341102826 248150916 1"]
|
NoteIn the first test case, Alex wins if he receives the card $$$2$$$ (he plays it, and Boris cannot respond). If Alex receives the card $$$1$$$, the game ends in a draw.In the second test case: Alex wins if he receives the cards $$$[3, 4]$$$, $$$[2, 4]$$$ or $$$[1, 4]$$$; Boris wins if Alex receives the cards $$$[1, 2]$$$ or $$$[1, 3]$$$; the game ends in a draw if Alex receives the cards $$$[2, 3]$$$.
|
Java 11
|
standard input
|
[
"combinatorics",
"constructive algorithms",
"dp",
"games"
] |
63fc2fb08d248f8ccdb3f5364c96dac1
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 30$$$) — the number of test cases. Then, $$$t$$$ lines follow. The $$$i$$$-th line contains one even integer $$$n$$$ ($$$2 \le n \le 60$$$).
| 1,500
|
For each test case, print three integers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. Since the answers can be large, print them modulo $$$998244353$$$.
|
standard output
| |
PASSED
|
a49fa952375e616127ed95532a7c12fe
|
train_109.jsonl
|
1664462100
|
Consider a game with $$$n$$$ cards ($$$n$$$ is even). Each card has a number written on it, between $$$1$$$ and $$$n$$$. All numbers on the cards are different. We say that a card with number $$$x$$$ is stronger than a card with number $$$y$$$ if $$$x > y$$$.Two players, Alex and Boris, play this game. In the beginning, each of them receives exactly $$$\frac{n}{2}$$$ cards, so each card belongs to exactly one player. Then, they take turns. Alex goes first, then Boris, then Alex again, and so on.On a player's turn, he must play exactly one of his cards. Then, if the opponent doesn't have any cards stronger than the card played, the opponent loses, and the game ends. Otherwise, the opponent has to play a stronger card (exactly one card as well). These two cards are removed from the game, and the turn ends. If there are no cards left, the game ends in a draw; otherwise it's the opponent's turn.Consider all possible ways to distribute the cards between two players, so that each of them receives exactly half of the cards. You have to calculate three numbers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. You may assume that both players play optimally (i. e. if a player can win no matter how his opponent plays, he wins). Two ways to distribute the cards are different if there is at least one card such that, in one of these ways, it is given to Alex, and in the other way, it is given to Boris.For example, suppose $$$n = 4$$$, Alex receives the cards $$$[2, 3]$$$, and Boris receives the cards $$$[1, 4]$$$. Then the game may go as follows: if Alex plays the card $$$2$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$3$$$. So, the game ends in a draw; if Alex plays the card $$$3$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$2$$$. So, the game ends in a draw. So, in this case, the game ends in a draw.
|
512 megabytes
|
import java.io.*;
import java.util.StringTokenizer;
public class C {
private final static String inputFileName = null;
private final static String outputFileName = null;
public static void main(String[] args) {
try (Solver solver = new Solver(inputFileName, outputFileName))
{
solver.solve();
}
}
private static class Solver implements AutoCloseable {
final long MOD = 998244353;
public void solve(){
int mx = 65;
long[][] choose = new long[mx][mx];
for (int i = 0; i < mx; i++)
for (int j = 0; j <= i; j++)
choose[i][j] = (i == 0 || j == 0) ? 1 : (choose[i-1][j] + choose[i-1][j-1]) % MOD;
long[] a = new long[mx];
long[] b = new long[mx];
for (int i = 2; i < mx; i++)
{
a[i] = (b[i - 2] + choose[i - 1][i / 2]) % MOD;
b[i] = (a[i - 2] + choose[i - 2][i / 2]) % MOD;
}
int t = reader.readInt();
while (t-- > 0)
{
int n = reader.readInt();
writer.writeLine(a[n] + " " + b[n] + " 1");
}
}
private final FastReader reader;
private final FastWriter writer;
private Solver(String inputFileName, String outputFileName) {
this.reader = inputFileName == null ? new FastReader() : new FastReader(inputFileName);
this.writer = outputFileName == null ? new FastWriter() : new FastWriter(outputFileName);
}
@Override
public void close() {
reader.close();
writer.close();
}
}
private static class FastReader implements Closeable {
private final BufferedReader bufferedReader;
private StringTokenizer stringTokenizer;
public FastReader() {
bufferedReader = new BufferedReader(new InputStreamReader(System.in));
}
public FastReader(String fileName) {
try {
bufferedReader = new BufferedReader(new FileReader(fileName));
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
}
}
public String readLine() {
try {
return bufferedReader.readLine();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public String read() {
while (stringTokenizer == null || !stringTokenizer.hasMoreTokens())
stringTokenizer = new StringTokenizer(readLine());
return stringTokenizer.nextToken();
}
public int readInt() {
return Integer.parseInt(read());
}
public long readLong() {
return Long.parseLong(read());
}
public double readDouble() {
return Double.parseDouble(read());
}
@Override
public void close() {
try {
bufferedReader.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
private static class FastWriter implements Closeable {
private final BufferedWriter bufferedWriter;
public FastWriter() {
bufferedWriter = new BufferedWriter(new OutputStreamWriter(System.out));
}
public FastWriter(String fileName) {
try {
bufferedWriter = new BufferedWriter(new FileWriter(fileName));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public void newLine() {
try {
bufferedWriter.newLine();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public void write(Object object) {
try {
bufferedWriter.write(object.toString());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public void writeLine(Object object) {
write(object);
newLine();
}
public void flush() {
try {
bufferedWriter.flush();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
@Override
public void close() {
try {
bufferedWriter.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
|
Java
|
["5\n\n2\n\n4\n\n6\n\n8\n\n60"]
|
2 seconds
|
["1 0 1\n3 2 1\n12 7 1\n42 27 1\n341102826 248150916 1"]
|
NoteIn the first test case, Alex wins if he receives the card $$$2$$$ (he plays it, and Boris cannot respond). If Alex receives the card $$$1$$$, the game ends in a draw.In the second test case: Alex wins if he receives the cards $$$[3, 4]$$$, $$$[2, 4]$$$ or $$$[1, 4]$$$; Boris wins if Alex receives the cards $$$[1, 2]$$$ or $$$[1, 3]$$$; the game ends in a draw if Alex receives the cards $$$[2, 3]$$$.
|
Java 11
|
standard input
|
[
"combinatorics",
"constructive algorithms",
"dp",
"games"
] |
63fc2fb08d248f8ccdb3f5364c96dac1
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 30$$$) — the number of test cases. Then, $$$t$$$ lines follow. The $$$i$$$-th line contains one even integer $$$n$$$ ($$$2 \le n \le 60$$$).
| 1,500
|
For each test case, print three integers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. Since the answers can be large, print them modulo $$$998244353$$$.
|
standard output
| |
PASSED
|
c17c6b1c1fafe32cd9ede38db81a5c09
|
train_109.jsonl
|
1664462100
|
Consider a game with $$$n$$$ cards ($$$n$$$ is even). Each card has a number written on it, between $$$1$$$ and $$$n$$$. All numbers on the cards are different. We say that a card with number $$$x$$$ is stronger than a card with number $$$y$$$ if $$$x > y$$$.Two players, Alex and Boris, play this game. In the beginning, each of them receives exactly $$$\frac{n}{2}$$$ cards, so each card belongs to exactly one player. Then, they take turns. Alex goes first, then Boris, then Alex again, and so on.On a player's turn, he must play exactly one of his cards. Then, if the opponent doesn't have any cards stronger than the card played, the opponent loses, and the game ends. Otherwise, the opponent has to play a stronger card (exactly one card as well). These two cards are removed from the game, and the turn ends. If there are no cards left, the game ends in a draw; otherwise it's the opponent's turn.Consider all possible ways to distribute the cards between two players, so that each of them receives exactly half of the cards. You have to calculate three numbers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. You may assume that both players play optimally (i. e. if a player can win no matter how his opponent plays, he wins). Two ways to distribute the cards are different if there is at least one card such that, in one of these ways, it is given to Alex, and in the other way, it is given to Boris.For example, suppose $$$n = 4$$$, Alex receives the cards $$$[2, 3]$$$, and Boris receives the cards $$$[1, 4]$$$. Then the game may go as follows: if Alex plays the card $$$2$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$3$$$. So, the game ends in a draw; if Alex plays the card $$$3$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$2$$$. So, the game ends in a draw. So, in this case, the game ends in a draw.
|
512 megabytes
|
import java.io.*;
import java.util.*;
public class C {
static int mod = 998244353;
public static void main(String[] args) throws IOException{
FastScanner fs = new FastScanner();
printNcR(32, 16);
int tt = fs.nextInt();
outer: while(tt-- > 0) {
int n = fs.nextInt();
long wins = 0;
long[] dp = new long[61];
long[] winss = new long[61];
winss[2] = 1;
dp[2] = 0l;
for(int i = 4; i <= 60; i+=2) {
wins = (printNcR(i-1, (i/2)-1) + dp[i-2])%mod;
dp[i] = ((printNcR(i, i/2)%mod)%mod - 1 - (wins));
winss[i] = wins;
}
System.out.println((winss[n]+mod)%mod + " " + (dp[n]+mod)%mod + " " + 1);
}
}
static long printNcR(long n, long r)
{
long p = 1l, k = 1l;
if (n - r < r) {
r = n - r;
}
if (r != 0) {
while (r > 0) {
p *= n;
k *= r;
long m = __gcd(p, k);
p /= m;
k /= m;
n--;
r--;
}
}
else {
p = 1;
}
// System.out.println(p);
return p%mod;
}
static long __gcd(long n1, long n2)
{
long gcd = 1;
for (int i = 1; i <= n1 && i <= n2; ++i) {
if (n1 % i == 0 && n2 % i == 0) {
gcd = i;
}
}
return gcd;
}
public static int gcd(int a, int b) {
if(b == 0) return a;
return gcd(b, a%b);
}
static void sort(int[] a) {
ArrayList<Integer> l=new ArrayList<>();
for (int i:a) l.add(i);
Collections.sort(l);
for (int i=0; i<a.length; i++) a[i]=l.get(i);
}
static class FastScanner {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st=new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
int[] readArray(int n) {
int[] a=new int[n];
for (int i=0; i<n; i++) a[i]=nextInt();
return a;
}
long[] readArrayLong(int n) {
long[] a = new long[n];
for(int i = 0; i < n; i++) a[i] = nextLong();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
}
}
|
Java
|
["5\n\n2\n\n4\n\n6\n\n8\n\n60"]
|
2 seconds
|
["1 0 1\n3 2 1\n12 7 1\n42 27 1\n341102826 248150916 1"]
|
NoteIn the first test case, Alex wins if he receives the card $$$2$$$ (he plays it, and Boris cannot respond). If Alex receives the card $$$1$$$, the game ends in a draw.In the second test case: Alex wins if he receives the cards $$$[3, 4]$$$, $$$[2, 4]$$$ or $$$[1, 4]$$$; Boris wins if Alex receives the cards $$$[1, 2]$$$ or $$$[1, 3]$$$; the game ends in a draw if Alex receives the cards $$$[2, 3]$$$.
|
Java 11
|
standard input
|
[
"combinatorics",
"constructive algorithms",
"dp",
"games"
] |
63fc2fb08d248f8ccdb3f5364c96dac1
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 30$$$) — the number of test cases. Then, $$$t$$$ lines follow. The $$$i$$$-th line contains one even integer $$$n$$$ ($$$2 \le n \le 60$$$).
| 1,500
|
For each test case, print three integers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. Since the answers can be large, print them modulo $$$998244353$$$.
|
standard output
| |
PASSED
|
87ce92a1fa432a16054e9f2d51024bfc
|
train_109.jsonl
|
1664462100
|
Consider a game with $$$n$$$ cards ($$$n$$$ is even). Each card has a number written on it, between $$$1$$$ and $$$n$$$. All numbers on the cards are different. We say that a card with number $$$x$$$ is stronger than a card with number $$$y$$$ if $$$x > y$$$.Two players, Alex and Boris, play this game. In the beginning, each of them receives exactly $$$\frac{n}{2}$$$ cards, so each card belongs to exactly one player. Then, they take turns. Alex goes first, then Boris, then Alex again, and so on.On a player's turn, he must play exactly one of his cards. Then, if the opponent doesn't have any cards stronger than the card played, the opponent loses, and the game ends. Otherwise, the opponent has to play a stronger card (exactly one card as well). These two cards are removed from the game, and the turn ends. If there are no cards left, the game ends in a draw; otherwise it's the opponent's turn.Consider all possible ways to distribute the cards between two players, so that each of them receives exactly half of the cards. You have to calculate three numbers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. You may assume that both players play optimally (i. e. if a player can win no matter how his opponent plays, he wins). Two ways to distribute the cards are different if there is at least one card such that, in one of these ways, it is given to Alex, and in the other way, it is given to Boris.For example, suppose $$$n = 4$$$, Alex receives the cards $$$[2, 3]$$$, and Boris receives the cards $$$[1, 4]$$$. Then the game may go as follows: if Alex plays the card $$$2$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$3$$$. So, the game ends in a draw; if Alex plays the card $$$3$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$2$$$. So, the game ends in a draw. So, in this case, the game ends in a draw.
|
512 megabytes
|
import java.util.*;
public class Solution {
static Scanner scn = new Scanner(System.in);
public static void main(String[] args) {
int t = scn.nextInt();
int temp = t;
long mod = 998244353;
long[] fact = new long[61];
fact[0] = 1;
fact[1] = 1;
for(int i = 2 ; i<=60 ; i++){
fact[i] = (i%mod*fact[i-1]%mod)%mod;
//System.out.println(fact[i]);
}
long[] wins = new long[61];
long[] loses = new long[61];
wins[2] = 1;
loses[2] = 0;
for(int i = 4 ; i<=60 ; i+=2){
wins[i] = ((fact[i-1] * modInverse(fact[(i/2) - 1], mod) % mod * modInverse(fact[i/2], mod) % mod) % mod + loses[i-2]%mod)%mod;
loses[i] = ((fact[i] * modInverse(fact[i/2], mod) % mod * modInverse(fact[i/2], mod) % mod)- wins[i]%mod - 1%mod + mod)%mod;
//System.out.println(wins[i] + " " + loses[i]);
}
while (t-->0) {
int n = scn.nextInt();
System.out.println(wins[n] +" " + loses[n] + " "+1);
//System.out.println();
}
}
static long power(long x, long y, long p)
{
// Initialize result
long res = 1;
// Update x if it is more than or
// equal to p
x = x % p;
while (y > 0) {
// If y is odd, multiply x
// with result
if (y % 2 == 1)
res = (res * x) % p;
// y must be even now
y = y >> 1; // y = y/2
x = (x * x) % p;
}
return res;
}
// Returns n^(-1) mod p
static long modInverse(long n, long p)
{
return power(n, p - 2, p);
}
// Returns nCr % p using Fermat's
// little theorem.
}
class Node{
int val;
int days;
Node(int _val , int _days){
val = _val;
days = _days;
}
public int getDays() {
return days;
}
public int getVal() {
return val;
}
}
/*
4 3
1 3 5
2
the end of the array will be in decreasing order
we have to perform a binary search on the last elements of all the arraylists and find
*/
|
Java
|
["5\n\n2\n\n4\n\n6\n\n8\n\n60"]
|
2 seconds
|
["1 0 1\n3 2 1\n12 7 1\n42 27 1\n341102826 248150916 1"]
|
NoteIn the first test case, Alex wins if he receives the card $$$2$$$ (he plays it, and Boris cannot respond). If Alex receives the card $$$1$$$, the game ends in a draw.In the second test case: Alex wins if he receives the cards $$$[3, 4]$$$, $$$[2, 4]$$$ or $$$[1, 4]$$$; Boris wins if Alex receives the cards $$$[1, 2]$$$ or $$$[1, 3]$$$; the game ends in a draw if Alex receives the cards $$$[2, 3]$$$.
|
Java 11
|
standard input
|
[
"combinatorics",
"constructive algorithms",
"dp",
"games"
] |
63fc2fb08d248f8ccdb3f5364c96dac1
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 30$$$) — the number of test cases. Then, $$$t$$$ lines follow. The $$$i$$$-th line contains one even integer $$$n$$$ ($$$2 \le n \le 60$$$).
| 1,500
|
For each test case, print three integers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. Since the answers can be large, print them modulo $$$998244353$$$.
|
standard output
| |
PASSED
|
fd4975fdc302870853952e73a60ba281
|
train_109.jsonl
|
1664462100
|
Consider a game with $$$n$$$ cards ($$$n$$$ is even). Each card has a number written on it, between $$$1$$$ and $$$n$$$. All numbers on the cards are different. We say that a card with number $$$x$$$ is stronger than a card with number $$$y$$$ if $$$x > y$$$.Two players, Alex and Boris, play this game. In the beginning, each of them receives exactly $$$\frac{n}{2}$$$ cards, so each card belongs to exactly one player. Then, they take turns. Alex goes first, then Boris, then Alex again, and so on.On a player's turn, he must play exactly one of his cards. Then, if the opponent doesn't have any cards stronger than the card played, the opponent loses, and the game ends. Otherwise, the opponent has to play a stronger card (exactly one card as well). These two cards are removed from the game, and the turn ends. If there are no cards left, the game ends in a draw; otherwise it's the opponent's turn.Consider all possible ways to distribute the cards between two players, so that each of them receives exactly half of the cards. You have to calculate three numbers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. You may assume that both players play optimally (i. e. if a player can win no matter how his opponent plays, he wins). Two ways to distribute the cards are different if there is at least one card such that, in one of these ways, it is given to Alex, and in the other way, it is given to Boris.For example, suppose $$$n = 4$$$, Alex receives the cards $$$[2, 3]$$$, and Boris receives the cards $$$[1, 4]$$$. Then the game may go as follows: if Alex plays the card $$$2$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$3$$$. So, the game ends in a draw; if Alex plays the card $$$3$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$2$$$. So, the game ends in a draw. So, in this case, the game ends in a draw.
|
512 megabytes
|
import java.io.*;
import java.math.*;
import java.util.*;
public class Main {
static final int INF = 0x3f3f3f3f;
static final long LNF = 0x3f3f3f3f3f3f3f3fL;
static BigInteger mod=new BigInteger("998244353");
static BigInteger yi=new BigInteger("1");
public static void main(String[] args) throws IOException {
initReader();
int t=nextInt();
BigInteger[]an1=new BigInteger[62];
BigInteger[]an2=new BigInteger[62];
an2[2]=new BigInteger("0");
an1[2]=new BigInteger("1");
for(int i=4;i<=60;i+=2){
an1[i]=new BigInteger("0");
an2[i]=new BigInteger("0");
long b=i/2;
BigInteger sum=ans(BigInteger.valueOf(i),BigInteger.valueOf(b));
sum=sum.subtract(yi);
BigInteger o=ans(BigInteger.valueOf(i-1),BigInteger.valueOf(b-1));
// o%=mod;
// an1[i]=((o%mod)+(an2[i-2]%mod))%mod;
// an1[i]%=mod;
an1[i]=an1[i].add(o).add(an2[i-2]);
an2[i]=an2[i].add(sum).subtract(an1[i]);
}
while (t--!=0){
int n=nextInt();
pw.println(an1[n].mod(mod)+" "+an2[n].mod(mod)+" 1");
}
pw.close();
}
static BigInteger ans(BigInteger a,BigInteger b){
BigInteger p=a.subtract(b).add(yi);
BigInteger ans1=new BigInteger("1");
while (a.compareTo(p)>=0){
//ans1=((ans1%mod)*(a%mod))%mod;
ans1=ans1.multiply(a);
//ans1%=mod;
a=a.subtract(yi);
}
BigInteger ans2=new BigInteger("1");
while (b.compareTo(yi)>=0){
//ans2=((ans2%mod)*(b%mod))%mod;
ans2=ans2.multiply(b);
//ans2%=mod;
b=b.subtract(yi);
}
return ans1.divide(ans2);
}
/***************************************************************************************/
static BufferedReader reader;
static StringTokenizer tokenizer;
static PrintWriter pw;
public static void initReader() throws IOException {
reader = new BufferedReader(new InputStreamReader(System.in));
tokenizer = new StringTokenizer("");
pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
// 从文件读写
// reader = new BufferedReader(new FileReader("test.in"));
// tokenizer = new StringTokenizer("");
// pw = new PrintWriter(new BufferedWriter(new FileWriter("test1.out")));
}
public static boolean hasNext() {
try {
while (!tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(reader.readLine());
}
} catch (Exception e) {
return false;
}
return true;
}
public static String next() throws IOException {
while (!tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(reader.readLine());
}
return tokenizer.nextToken();
}
public static String nextLine() {
try {
return reader.readLine();
} catch (Exception e) {
return null;
}
}
public static int nextInt() throws IOException {
return Integer.parseInt(next());
}
public static long nextLong() throws IOException {
return Long.parseLong(next());
}
public static double nextDouble() throws IOException {
return Double.parseDouble(next());
}
public static char nextChar() throws IOException {
return next().charAt(0);
}
}
|
Java
|
["5\n\n2\n\n4\n\n6\n\n8\n\n60"]
|
2 seconds
|
["1 0 1\n3 2 1\n12 7 1\n42 27 1\n341102826 248150916 1"]
|
NoteIn the first test case, Alex wins if he receives the card $$$2$$$ (he plays it, and Boris cannot respond). If Alex receives the card $$$1$$$, the game ends in a draw.In the second test case: Alex wins if he receives the cards $$$[3, 4]$$$, $$$[2, 4]$$$ or $$$[1, 4]$$$; Boris wins if Alex receives the cards $$$[1, 2]$$$ or $$$[1, 3]$$$; the game ends in a draw if Alex receives the cards $$$[2, 3]$$$.
|
Java 11
|
standard input
|
[
"combinatorics",
"constructive algorithms",
"dp",
"games"
] |
63fc2fb08d248f8ccdb3f5364c96dac1
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 30$$$) — the number of test cases. Then, $$$t$$$ lines follow. The $$$i$$$-th line contains one even integer $$$n$$$ ($$$2 \le n \le 60$$$).
| 1,500
|
For each test case, print three integers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. Since the answers can be large, print them modulo $$$998244353$$$.
|
standard output
| |
PASSED
|
fb9006129a069dc839322e399e40dbd0
|
train_109.jsonl
|
1664462100
|
Consider a game with $$$n$$$ cards ($$$n$$$ is even). Each card has a number written on it, between $$$1$$$ and $$$n$$$. All numbers on the cards are different. We say that a card with number $$$x$$$ is stronger than a card with number $$$y$$$ if $$$x > y$$$.Two players, Alex and Boris, play this game. In the beginning, each of them receives exactly $$$\frac{n}{2}$$$ cards, so each card belongs to exactly one player. Then, they take turns. Alex goes first, then Boris, then Alex again, and so on.On a player's turn, he must play exactly one of his cards. Then, if the opponent doesn't have any cards stronger than the card played, the opponent loses, and the game ends. Otherwise, the opponent has to play a stronger card (exactly one card as well). These two cards are removed from the game, and the turn ends. If there are no cards left, the game ends in a draw; otherwise it's the opponent's turn.Consider all possible ways to distribute the cards between two players, so that each of them receives exactly half of the cards. You have to calculate three numbers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. You may assume that both players play optimally (i. e. if a player can win no matter how his opponent plays, he wins). Two ways to distribute the cards are different if there is at least one card such that, in one of these ways, it is given to Alex, and in the other way, it is given to Boris.For example, suppose $$$n = 4$$$, Alex receives the cards $$$[2, 3]$$$, and Boris receives the cards $$$[1, 4]$$$. Then the game may go as follows: if Alex plays the card $$$2$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$3$$$. So, the game ends in a draw; if Alex plays the card $$$3$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$2$$$. So, the game ends in a draw. So, in this case, the game ends in a draw.
|
512 megabytes
|
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class CardGame {
static class FastScanner {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st = new StringTokenizer(br.readLine());
} catch (IOException ignored) {
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
}
public static void main(String[] args) {
FastScanner sc = new FastScanner();
int t = sc.nextInt();
long mod = 998244353;
long[] d = new long[33];
long[] dp = new long[33];
long p;
int n;
for (int i = 1; i < 31; i++) {
n = i*2;
p = 1;
for (int j = 0; j <= n/2 - 1; j++) {
p = p * (n-j);
p = p/(j + 1);
}
d[i] = 1;
for (int j = 0; j <= n/2-1; j++) {
d[i] = d[i] * (n - 1 - j);
d[i] = d[i] /(j + 1);
}
d[i] = (int) ((d[i] + dp[i - 1]) % mod);
dp[i] = (int) ((p - d[i] - 1) % mod);
}
while (t-- > 0) {
int in = sc.nextInt();
System.out.println(d[in/2] + " " + dp[in/2] + " " + 1);
}
}
}
|
Java
|
["5\n\n2\n\n4\n\n6\n\n8\n\n60"]
|
2 seconds
|
["1 0 1\n3 2 1\n12 7 1\n42 27 1\n341102826 248150916 1"]
|
NoteIn the first test case, Alex wins if he receives the card $$$2$$$ (he plays it, and Boris cannot respond). If Alex receives the card $$$1$$$, the game ends in a draw.In the second test case: Alex wins if he receives the cards $$$[3, 4]$$$, $$$[2, 4]$$$ or $$$[1, 4]$$$; Boris wins if Alex receives the cards $$$[1, 2]$$$ or $$$[1, 3]$$$; the game ends in a draw if Alex receives the cards $$$[2, 3]$$$.
|
Java 11
|
standard input
|
[
"combinatorics",
"constructive algorithms",
"dp",
"games"
] |
63fc2fb08d248f8ccdb3f5364c96dac1
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 30$$$) — the number of test cases. Then, $$$t$$$ lines follow. The $$$i$$$-th line contains one even integer $$$n$$$ ($$$2 \le n \le 60$$$).
| 1,500
|
For each test case, print three integers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. Since the answers can be large, print them modulo $$$998244353$$$.
|
standard output
| |
PASSED
|
253afb2c81d26103d20e8a15c81d6802
|
train_109.jsonl
|
1664462100
|
Consider a game with $$$n$$$ cards ($$$n$$$ is even). Each card has a number written on it, between $$$1$$$ and $$$n$$$. All numbers on the cards are different. We say that a card with number $$$x$$$ is stronger than a card with number $$$y$$$ if $$$x > y$$$.Two players, Alex and Boris, play this game. In the beginning, each of them receives exactly $$$\frac{n}{2}$$$ cards, so each card belongs to exactly one player. Then, they take turns. Alex goes first, then Boris, then Alex again, and so on.On a player's turn, he must play exactly one of his cards. Then, if the opponent doesn't have any cards stronger than the card played, the opponent loses, and the game ends. Otherwise, the opponent has to play a stronger card (exactly one card as well). These two cards are removed from the game, and the turn ends. If there are no cards left, the game ends in a draw; otherwise it's the opponent's turn.Consider all possible ways to distribute the cards between two players, so that each of them receives exactly half of the cards. You have to calculate three numbers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. You may assume that both players play optimally (i. e. if a player can win no matter how his opponent plays, he wins). Two ways to distribute the cards are different if there is at least one card such that, in one of these ways, it is given to Alex, and in the other way, it is given to Boris.For example, suppose $$$n = 4$$$, Alex receives the cards $$$[2, 3]$$$, and Boris receives the cards $$$[1, 4]$$$. Then the game may go as follows: if Alex plays the card $$$2$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$3$$$. So, the game ends in a draw; if Alex plays the card $$$3$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$2$$$. So, the game ends in a draw. So, in this case, the game ends in a draw.
|
512 megabytes
|
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class CardGame {
static class FastScanner {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st = new StringTokenizer(br.readLine());
} catch (IOException ignored) {
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
}
public static void main(String[] args) {
FastScanner sc = new FastScanner();
int t = sc.nextInt();
long mod = 998244353;
long[] d = new long[33];
long[] dp = new long[33];
long perm;
int n;
for (int i = 1; i < 31; i++) {
n = i*2;
perm = 1;
for (int j = 0; j <= n/2 - 1; j++) {
perm = perm * (n-j);
perm = perm/(j + 1);
}
d[i] = 1;
for (int j = 0; j <= n/2-1; j++) {
d[i] = d[i] * (n - 1 - j);
d[i] = d[i] /(j + 1);
}
d[i] = (int) ((d[i] + dp[i - 1]) % mod);
dp[i] = (int) ((perm - d[i] - 1) % mod);
}
while (t-- > 0) {
int in = sc.nextInt();
System.out.println(d[in/2] + " " + dp[in/2] + " " + 1);
}
}
}
|
Java
|
["5\n\n2\n\n4\n\n6\n\n8\n\n60"]
|
2 seconds
|
["1 0 1\n3 2 1\n12 7 1\n42 27 1\n341102826 248150916 1"]
|
NoteIn the first test case, Alex wins if he receives the card $$$2$$$ (he plays it, and Boris cannot respond). If Alex receives the card $$$1$$$, the game ends in a draw.In the second test case: Alex wins if he receives the cards $$$[3, 4]$$$, $$$[2, 4]$$$ or $$$[1, 4]$$$; Boris wins if Alex receives the cards $$$[1, 2]$$$ or $$$[1, 3]$$$; the game ends in a draw if Alex receives the cards $$$[2, 3]$$$.
|
Java 11
|
standard input
|
[
"combinatorics",
"constructive algorithms",
"dp",
"games"
] |
63fc2fb08d248f8ccdb3f5364c96dac1
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 30$$$) — the number of test cases. Then, $$$t$$$ lines follow. The $$$i$$$-th line contains one even integer $$$n$$$ ($$$2 \le n \le 60$$$).
| 1,500
|
For each test case, print three integers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. Since the answers can be large, print them modulo $$$998244353$$$.
|
standard output
| |
PASSED
|
f31901146e32a0c587b8e8abd0b3d0d8
|
train_109.jsonl
|
1664462100
|
Consider a game with $$$n$$$ cards ($$$n$$$ is even). Each card has a number written on it, between $$$1$$$ and $$$n$$$. All numbers on the cards are different. We say that a card with number $$$x$$$ is stronger than a card with number $$$y$$$ if $$$x > y$$$.Two players, Alex and Boris, play this game. In the beginning, each of them receives exactly $$$\frac{n}{2}$$$ cards, so each card belongs to exactly one player. Then, they take turns. Alex goes first, then Boris, then Alex again, and so on.On a player's turn, he must play exactly one of his cards. Then, if the opponent doesn't have any cards stronger than the card played, the opponent loses, and the game ends. Otherwise, the opponent has to play a stronger card (exactly one card as well). These two cards are removed from the game, and the turn ends. If there are no cards left, the game ends in a draw; otherwise it's the opponent's turn.Consider all possible ways to distribute the cards between two players, so that each of them receives exactly half of the cards. You have to calculate three numbers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. You may assume that both players play optimally (i. e. if a player can win no matter how his opponent plays, he wins). Two ways to distribute the cards are different if there is at least one card such that, in one of these ways, it is given to Alex, and in the other way, it is given to Boris.For example, suppose $$$n = 4$$$, Alex receives the cards $$$[2, 3]$$$, and Boris receives the cards $$$[1, 4]$$$. Then the game may go as follows: if Alex plays the card $$$2$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$3$$$. So, the game ends in a draw; if Alex plays the card $$$3$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$2$$$. So, the game ends in a draw. So, in this case, the game ends in a draw.
|
512 megabytes
|
import java.io.*;
import java.lang.reflect.WildcardType;
import java.util.ArrayList;
import java.util.Collections;
import java.util.InputMismatchException;
public class E1739C {
static int MOD = 998244353;
public static void main(String[] args) {
FastIO io = new FastIO();
int t = io.nextInt();
while (t-- > 0) {
int n = io.nextInt();
long winCount = 0;
long lostCount = 0;
boolean parity = true;
for (int i = n; i > 0; i -= 2) {
if (parity) {
winCount = (winCount + nCk(i - 1, i / 2) + 10l * MOD) % MOD;
lostCount = (lostCount + nCk(i - 1, i / 2) - nCk(i - 2, (i - 2) / 2) + 10l * MOD) % MOD;
} else {
lostCount = (lostCount + nCk(i - 1, i / 2) + 10l * MOD) % MOD;
winCount = (winCount + nCk(i - 1, i / 2) - nCk(i - 2, (i - 2) / 2) + 10l * MOD) % MOD;
}
parity = !parity;
}
long drawCount = 1;
winCount %= MOD;
lostCount %= MOD;
io.println(winCount + " " + lostCount + " " + drawCount);
}
io.close();
}
static long dumbFac(int n) {
long ans = 1;
for (int i = 1; i <= n; i++) {
ans = (ans * i) % MOD;
}
return ans;
}
static long nCk(int n, int k) {
long facN = dumbFac(n);
long facK = dumbFac(k);
long facNSubK = dumbFac(n - k);
long dem = (facK * facNSubK) % MOD;
dem = binpow(dem, MOD - 2);
return (facN * dem) % MOD;
}
public static long binpow(long x, long n) {
//inverse modulo= binpow(2, MOD - 2, MOD);
assert (n >= 0);
x %= MOD; // note: m * m must be less than 2^63 to avoid ll overflow
long res = 1;
while (n > 0) {
if (n % 2 == 1) // if n is odd
res = res * x % MOD;
x = x * x % MOD;
n /= 2; // divide by two
}
return res;
}
static void sort(int[] a) {
ArrayList<Integer> l = new ArrayList<>();
for (int i : a) l.add(i);
Collections.sort(l);
for (int i = 0; i < a.length; i++) a[i] = l.get(i);
}
private static class FastIO extends PrintWriter {
private final InputStream stream;
private final byte[] buf = new byte[1 << 16];
private int curChar, numChars;
// standard input
public FastIO() {
this(System.in, System.out);
}
public FastIO(InputStream i, OutputStream o) {
super(o);
stream = i;
}
// file input
public FastIO(String i, String o) throws IOException {
super(new FileWriter(o));
stream = new FileInputStream(i);
}
// throws InputMismatchException() if previously detected end of file
private int nextByte() {
if (numChars == -1) throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars == -1) return -1; // end of file
}
return buf[curChar++];
}
// to read in entire lines, replace c <= ' '
// with a function that checks whether c is a line break
public String next() {
int c;
do {
c = nextByte();
} while (c <= ' ');
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = nextByte();
} while (c > ' ');
return res.toString();
}
public int nextInt() { // nextLong() would be implemented similarly
int c;
do {
c = nextByte();
} while (c <= ' ');
int sgn = 1;
if (c == '-') {
sgn = -1;
c = nextByte();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res = 10 * res + c - '0';
c = nextByte();
} while (c > ' ');
return res * sgn;
}
public long nextLong() { // nextLong() would be implemented similarly
int c;
do {
c = nextByte();
} while (c <= ' ');
int sgn = 1;
if (c == '-') {
sgn = -1;
c = nextByte();
}
long res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res = 10 * res + c - '0';
c = nextByte();
} while (c > ' ');
return res * sgn;
}
public double nextDouble() {
return Double.parseDouble(next());
}
}
}
|
Java
|
["5\n\n2\n\n4\n\n6\n\n8\n\n60"]
|
2 seconds
|
["1 0 1\n3 2 1\n12 7 1\n42 27 1\n341102826 248150916 1"]
|
NoteIn the first test case, Alex wins if he receives the card $$$2$$$ (he plays it, and Boris cannot respond). If Alex receives the card $$$1$$$, the game ends in a draw.In the second test case: Alex wins if he receives the cards $$$[3, 4]$$$, $$$[2, 4]$$$ or $$$[1, 4]$$$; Boris wins if Alex receives the cards $$$[1, 2]$$$ or $$$[1, 3]$$$; the game ends in a draw if Alex receives the cards $$$[2, 3]$$$.
|
Java 11
|
standard input
|
[
"combinatorics",
"constructive algorithms",
"dp",
"games"
] |
63fc2fb08d248f8ccdb3f5364c96dac1
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 30$$$) — the number of test cases. Then, $$$t$$$ lines follow. The $$$i$$$-th line contains one even integer $$$n$$$ ($$$2 \le n \le 60$$$).
| 1,500
|
For each test case, print three integers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. Since the answers can be large, print them modulo $$$998244353$$$.
|
standard output
| |
PASSED
|
2a438023ae9ac4694e33b72c2d1e641c
|
train_109.jsonl
|
1664462100
|
Consider a game with $$$n$$$ cards ($$$n$$$ is even). Each card has a number written on it, between $$$1$$$ and $$$n$$$. All numbers on the cards are different. We say that a card with number $$$x$$$ is stronger than a card with number $$$y$$$ if $$$x > y$$$.Two players, Alex and Boris, play this game. In the beginning, each of them receives exactly $$$\frac{n}{2}$$$ cards, so each card belongs to exactly one player. Then, they take turns. Alex goes first, then Boris, then Alex again, and so on.On a player's turn, he must play exactly one of his cards. Then, if the opponent doesn't have any cards stronger than the card played, the opponent loses, and the game ends. Otherwise, the opponent has to play a stronger card (exactly one card as well). These two cards are removed from the game, and the turn ends. If there are no cards left, the game ends in a draw; otherwise it's the opponent's turn.Consider all possible ways to distribute the cards between two players, so that each of them receives exactly half of the cards. You have to calculate three numbers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. You may assume that both players play optimally (i. e. if a player can win no matter how his opponent plays, he wins). Two ways to distribute the cards are different if there is at least one card such that, in one of these ways, it is given to Alex, and in the other way, it is given to Boris.For example, suppose $$$n = 4$$$, Alex receives the cards $$$[2, 3]$$$, and Boris receives the cards $$$[1, 4]$$$. Then the game may go as follows: if Alex plays the card $$$2$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$3$$$. So, the game ends in a draw; if Alex plays the card $$$3$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$2$$$. So, the game ends in a draw. So, in this case, the game ends in a draw.
|
512 megabytes
|
import java.io.*;
import java.util.*;
public class Main {
final static long mod=998244353;
static long[][] dp=new long[1000][1000];
public static void main(String[] args) throws IOException {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
long[][] ka=new long[62][3];
ka[2][0]=1; ka[2][1]=0; ka[2][2]=1;
for(int i=4; i<62; i+=2){
long n=rec(i-1,(i-2)/2+1);
ka[i][0]=(n+ka[i-2][1])%mod; ka[i][2]=1; ka[i][1]=(mod+(2*n-ka[i][0]-1)%mod)%mod;
}
int g = Integer.parseInt(bf.readLine());
while (g-- > 0) {
int a=Integer.parseInt(bf.readLine());
System.out.println(ka[a][0]+" "+ka[a][1]+" "+ka[a][2]);
}
}
static long rec(int a,int b){
if(a==b || b==0) return 1;
if(dp[a][b]!=0) return dp[a][b];
return dp[a][b]=(rec(a-1,b)+rec(a-1,b-1))%mod;
}
}
|
Java
|
["5\n\n2\n\n4\n\n6\n\n8\n\n60"]
|
2 seconds
|
["1 0 1\n3 2 1\n12 7 1\n42 27 1\n341102826 248150916 1"]
|
NoteIn the first test case, Alex wins if he receives the card $$$2$$$ (he plays it, and Boris cannot respond). If Alex receives the card $$$1$$$, the game ends in a draw.In the second test case: Alex wins if he receives the cards $$$[3, 4]$$$, $$$[2, 4]$$$ or $$$[1, 4]$$$; Boris wins if Alex receives the cards $$$[1, 2]$$$ or $$$[1, 3]$$$; the game ends in a draw if Alex receives the cards $$$[2, 3]$$$.
|
Java 11
|
standard input
|
[
"combinatorics",
"constructive algorithms",
"dp",
"games"
] |
63fc2fb08d248f8ccdb3f5364c96dac1
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 30$$$) — the number of test cases. Then, $$$t$$$ lines follow. The $$$i$$$-th line contains one even integer $$$n$$$ ($$$2 \le n \le 60$$$).
| 1,500
|
For each test case, print three integers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. Since the answers can be large, print them modulo $$$998244353$$$.
|
standard output
| |
PASSED
|
e8d854c5b13cd5de4a6b7f1bf16eff64
|
train_109.jsonl
|
1664462100
|
Consider a game with $$$n$$$ cards ($$$n$$$ is even). Each card has a number written on it, between $$$1$$$ and $$$n$$$. All numbers on the cards are different. We say that a card with number $$$x$$$ is stronger than a card with number $$$y$$$ if $$$x > y$$$.Two players, Alex and Boris, play this game. In the beginning, each of them receives exactly $$$\frac{n}{2}$$$ cards, so each card belongs to exactly one player. Then, they take turns. Alex goes first, then Boris, then Alex again, and so on.On a player's turn, he must play exactly one of his cards. Then, if the opponent doesn't have any cards stronger than the card played, the opponent loses, and the game ends. Otherwise, the opponent has to play a stronger card (exactly one card as well). These two cards are removed from the game, and the turn ends. If there are no cards left, the game ends in a draw; otherwise it's the opponent's turn.Consider all possible ways to distribute the cards between two players, so that each of them receives exactly half of the cards. You have to calculate three numbers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. You may assume that both players play optimally (i. e. if a player can win no matter how his opponent plays, he wins). Two ways to distribute the cards are different if there is at least one card such that, in one of these ways, it is given to Alex, and in the other way, it is given to Boris.For example, suppose $$$n = 4$$$, Alex receives the cards $$$[2, 3]$$$, and Boris receives the cards $$$[1, 4]$$$. Then the game may go as follows: if Alex plays the card $$$2$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$3$$$. So, the game ends in a draw; if Alex plays the card $$$3$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$2$$$. So, the game ends in a draw. So, in this case, the game ends in a draw.
|
512 megabytes
|
import java.util.*;
import java.io.*;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
import java.util.StringTokenizer;
import java.math.BigInteger;
public class Main{
static class Pair{
int key , value;
Pair(int key , int value) {
this.key = key;
this.value = value;
}
public int hashCode() {
return 10*key + value*11;
}
public boolean equals(Object o) {
if (this == o) return true;
if (o == null) return false;
if (this.getClass() != o.getClass()) return false;
Pair other = (Pair)o;
if (this.key != other.key || this.value != other.value) return false;
return true;
}
}
static class P {
String g;
int x , y;
P(String g,int x ,int y) {
this.x = x;
this.y = y;
this.g = g;
}
}
static class Node {
long sum, pre;
Node(long a, long b) {
this.sum = a;
this.pre = b;
}
}
static class SegmentTree {
int l , r; // range responsible for
SegmentTree left , right;
// List<Integer> list;
int val;
SegmentTree(int l,int r,int a[]) {
this.l = l;
this.r = r;
if(l == r) {
// list.add(a[l]);
val = a[l];
// this.val = a[l];
return;
}
int mid = l + (r-l)/2;
this.left = new SegmentTree(l ,mid , a);
this.right = new SegmentTree(mid + 1 , r,a);
this.val = Math.max(this.left.val , this.right.val);
}
public void merge(List<Integer> A , List<Integer> B ,List<Integer>C) {
int i = 0 ,j = 0;
while(i < B.size() && j < C.size()) {
if(B.get(i) <= C.get(j)) A.add(B.get(i++));
else A.add(C.get(j++));
}
while(i < B.size()) A.add(B.get(i++));
while(j < C.size()) A.add(C.get(j++));
}
public int query(int left ,int right) {
if(this.l > right || this.r < left) return 0;
if(this.l >= left && this.r <= right) {
return this.val;
}
return Math.max(this.left.query(left , right) , this.right.query(left , right));
}
// public void pointUpdate(int index ,int val) {
// // System.out.println("At " + index);
// if(this.l > index || this.r < index) return;
// if(this.l == this.r && this.l == index) {
// this.val = val;
// return ;
// }
// this.left.pointUpdate(index ,val );
// this.right.pointUpdate(index , val);
// this.val = Math.max(this.left.val , this.right.val);
// }
// public void rangeUpdate(int left , int right) {
// if(this.l > right || this.r < left) return;
// if(this.l >= left && this.r <= right) {
// this.val += this.r-this.l + 1;
// System.out.println(" now " + this.val);
// return ;
// }
// this.left.rangeUpdate(left , right );
// this.right.rangeUpdate(left , right );
// this.val = this.left.val + this.right.val;
// }
// public long valueAtK(int k) {
// if(this.l > k || this.r < k) return 0;
// if(this.l == this.r && this.l == k) {
// return this.val;
// }
// return join(this.left.valueAtK(k) , this.right.valueAtK(k));
// }
public int join(int a ,int b) {
return a + b;
}
}
static class SegmentTree2 {
int l , r; // range responsible for
SegmentTree2 left , right;
// List<Integer> list;
int val;
SegmentTree2(int l,int r,int a[]) {
// System.out.println("at " + l + " " + r);
this.l = l;
this.r = r;
if(l == r) {
// list.add(a[l]);
this.val = a[l];
return;
}
int mid = l + (r-l)/2;
this.left = new SegmentTree2(l ,mid , a);
this.right = new SegmentTree2(mid + 1 , r,a);
this.val = Math.min(this.left.val ,this.right.val);
}
// public void merge(List<Integer> A , List<Integer> B ,List<Integer>C) {
// int i = 0 ,j = 0;
// while(i < B.size() && j < C.size()) {
// if(B.get(i) <= C.get(j)) A.add(B.get(i++));
// else A.add(C.get(j++));
// }
// while(i < B.size()) A.add(B.get(i++));
// while(j < C.size()) A.add(C.get(j++));
// }
public int query(int left ,int right) {
if(this.l > right || this.r < left) return Integer.MAX_VALUE;
if(this.l >= left && this.r <= right) {
return this.val;
}
return Math.min(this.left.query(left , right) , this.right.query(left , right));
}
public void pointUpdate(int index ,int val) {
if(this.l > index || this.r < index) return;
if(this.l == this.r && this.l == index) {
this.val = val;
return ;
}
this.left.pointUpdate(index ,val );
this.right.pointUpdate(index , val);
this.val = Math.min(this.left.val , this.right.val);
}
// public void rangeUpdate(int left , int right) {
// if(this.l > right || this.r < left) return;
// if(this.l >= left && this.r <= right) {
// this.val += this.r-this.l + 1;
// System.out.println(" now " + this.val);
// return ;
// }
// this.left.rangeUpdate(left , right );
// this.right.rangeUpdate(left , right );
// this.val = this.left.val + this.right.val;
// }
// public long valueAtK(int k) {
// if(this.l > k || this.r < k) return 0;
// if(this.l == this.r && this.l == k) {
// return this.val;
// }
// return join(this.left.valueAtK(k) , this.right.valueAtK(k));
// }
public int join(int a ,int b) {
return a + b;
}
}
static class Hash {
long hash[] ,mod = (long)1e9 + 7 , powT[] , prime , inverse[];
Hash(char []s) {
prime = 131;
int n = s.length;
powT = new long[n];
hash = new long[n];
inverse = new long[n];
powT[0] = 1;
inverse[n-1] = pow(pow(prime , n-1 ,mod), mod-2 , mod);
for(int i = 1;i < n; i++ ) {
powT[i] = (powT[i-1]*prime)%mod;
}
for(int i = n-2; i>= 0;i-=1) {
inverse[i] = (inverse[i+1]*prime)%mod;
}
hash[0] = (s[0] - '0' + 1);
for(int i = 1; i < n;i++ ) {
hash[i] = hash[i-1] + ((s[i]-'0' + 1)*powT[i])%mod;
hash[i] %= mod;
}
}
public long hashValue(int l , int r) {
if(l == 0) return hash[r]%mod;
long ans = hash[r] - hash[l-1] +mod;
ans %= mod;
// System.out.println(inverse[l] + " " + pow(powT[l], mod- 2 , mod));
ans *= inverse[l];
ans %= mod;
return ans;
}
}
static class ConvexHull {
Stack<Integer>stack;
Stack<Integer>stack1;
int n;
Point arr[];
ConvexHull(Point arr[]) {
n = arr.length;
this.arr = arr;
Arrays.sort(arr , (a , b)-> {
if(a.x == b.x) return (int)(b.y-a.y);
return (int)(a.x-b.x);
});
Point min = arr[0];
stack = new Stack<>();
stack1 = new Stack<>();
stack.push(0);
stack1.push(0);
Point ob = new Point(2,2);
for(int i =1;i < n;i++) {
if(stack.size() < 2) stack.push(i);
else {
while(stack.size() >= 2) {
int a = stack.pop() , b = stack.pop() ,c = i;
int dir = ob.cross(arr[b] , arr[a] , arr[c]);
if(dir < 0) {
stack.push(b);
stack.push(a);
stack.push(c);
break;
}
stack.push(b);
}
if(stack.size() < 2) {
stack.push(i);
}
}
}
for(int i =1;i < n;i++) {
if(stack1.size() < 2) stack1.push(i);
else {
while(stack1.size() >= 2) {
int a = stack1.pop() , b = stack1.pop() ,c = i;
int dir = ob.cross(arr[b] , arr[a] , arr[c]);
if(dir > 0) {
stack1.push(b);
stack1.push(a);
stack1.push(c);
break;
}
stack1.push(b);
}
if(stack1.size() < 2) {
stack1.push(i);
}
}
}
}
public List<Point> getPoints() {
boolean vis[] = new boolean[n];
List<Point> list = new ArrayList<>();
// for(int x : stack) {
// list.add(arr[x]);
// vis[x] = true;
// }
for(int x : stack1) {
// if(vis[x]) continue;
list.add(arr[x]);
}
return list;
}
}
public static class Suffix implements Comparable<Suffix> {
int index;
int rank;
int next;
public Suffix(int ind, int r, int nr) {
index = ind;
rank = r;
next = nr;
}
public int compareTo(Suffix s) {
if (rank != s.rank) return Integer.compare(rank, s.rank);
return Integer.compare(next, s.next);
}
}
static class Point {
long x , y;
Point(long x , long y) {
this.x = x;
this.y = y;
}
public Point sub(Point a,Point b) {
return new Point(a.x - b.x , a.y-b.y);
}
public int cross(Point a ,Point b , Point c) {
Point g = sub(b,a) , l = sub(c, b);
long ans = g.y*l.x - g.x*l.y;
if(ans == 0) return 0;
if(ans < 0) return -1;
return 1;
}
}
static class Kattio extends PrintWriter {
private BufferedReader r;
private StringTokenizer st;
// standard input
public Kattio() { this(System.in, System.out); }
public Kattio(InputStream i, OutputStream o) {
super(o);
r = new BufferedReader(new InputStreamReader(i));
}
// USACO-style file input
public Kattio(String problemName) throws IOException {
super(new FileWriter(problemName + ".out"));
r = new BufferedReader(new FileReader(problemName + ".in"));
}
// returns null if no more input
public String next() {
try {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(r.readLine());
return st.nextToken();
} catch (Exception e) { }
return null;
}
public int nextInt() { return Integer.parseInt(next()); }
public double nextDouble() { return Double.parseDouble(next()); }
public long nextLong() { return Long.parseLong(next()); }
}
static HashMap<Integer ,List<Integer>> graph;
static Kattio sc = new Kattio();
static long mod = (long)1e9 + 7;
static String endl = "\n" , gap = " ";
static HashMap<Integer , Long> value;
static int size[];
static int parent[];
static long fac[];
static long inv[];
static boolean vis[];
static int answer;
static HashSet<String> guess;
static long primePow[];
static int N;
// static int dis[];
static int height[];
static long p[];
static int endTime[];
static int time;
static Long dp[][];
static HashMap<String , Long> dmap;
static long dirpair[][];
static HashSet<String> not;
static SegmentTree tree;
// static long dis[];
static List<Integer> list;
public static void main(String[] args)throws IOException {
long t = ri();
// int max = 200;
// fac = new long[max];
// dis = new long[max];
// long mod = (long)1e9 + 7;
// fac[0] = 1;
// dis[1] = 0;
// dis[2] = 1;
// for(int i = 3;i < max;i++) {
// dis[i] = (i-1)*1l*((dis[i-1] + dis[i-2])%mod);
// dis[i]%=mod;
// }
// // inv = new long[max];
// // inv[0]= inverse(fac[0] ,mod);
// long mod = (long)1e9 + 7;
// for(int i = 1;i < max;i++) {
// fac[i] = i*1l*fac[i-1];
// fac[i] %= mod;
// }
// List<Integer> list = new ArrayList<>();
// int MAX = (int)4e4;
// for(int i =1;i<=MAX;i++) {
// if(isPalindrome(i + "")) list.add(i);
// }
// // System.out.println(list);
// long dp[] = new long[MAX +1];
// dp[0] = 1;
// long mod = (long)(1e9+7);
// for(int x : list) {
// for(int i =1;i<=MAX;i++) {
// if(i >= x) {
// dp[i] += dp[i-x];
// dp[i] %= mod;
// }
// }
// }
// int MAK = (int)1e5 + 1;
// boolean seive[] = new boolean[MAK];
// Arrays.fill(seive , true);
// seive[1] = false;
// seive[0] = false;
// for (int i = 1; i < MAK; i++) {
// if(seive[i]) {
// for (int j = i+i; j < MAK; j+=i) {
// seive[j] = false;
// }
// }
// }
// int T[] = new int[MAK];
// for(int i = 2;i < MAK;i++) {
// if(seive[i]) T[i] = 1;
// }
// for(int i = 1;i <MAK;i++) {
// T[i] += T[i-1];
// }
// for(int i =0;i <MAK;i++) {
// if(T[i]%2 == 0) T[i] = 1;
// else T[i] = 0;
// }
// T[0] = 0;
// tree = new SegmentTree(0 ,MAK-1 , T);
// // TreeSet<Long>primeSet = new TreeSet<>();
// // for(long i = 2;i <= (long)1e6;i++) {
// // if(seive[(int)i])primeSet.add(i);
// // }
// int MAX = (int)1e5 + 1;
// ArrayList<Integer> a[] = new ArrayList[MAX];
// for(int i =0;i < a.length;i++) a[i] = new ArrayList<>();
// boolean notPrime[] = new boolean[MAX];
// for(int i = 2;i<MAX;i++){
// if(notPrime[i]) continue;
// for(int j = i;j<MAX;j+=i){
// notPrime[j] = true;
// a[j].add(i);
// }
// }
int test_case = 1;
while(t-->0) {
// sc.print("Case #"+(test_case++)+": ");
solve();
}
sc.close();
}
// static SegmentTree tree;
static long cn = 0 , cm = 0;
static int total = 0;
static class T {
int val;
T p;
T(int val) {
this.val = val;
}
}
static long inf = Long.MAX_VALUE;
static int dis[];
public static void solve() throws IOException {
long n = rl();
long a[] = solve(n);
System.out.println(a[0] + " "+ a[1] + " " + a[2]);
}
public static long[] solve(long n) {
if(n == 2) return new long[]{1l,0l,1l};
long a[] = solve(n-2);
long mod = 998244353L;
long ans[] = new long[3];
ans[0] = a[1] + Ncr(n-1 , n/2 ,mod);
ans[1] = Ncr(n-2 , n/2,mod) + a[0];
ans[2] = 1;
for(int i =0;i < 3;i++) ans[i] %= mod;
return ans;
}
public static long Ncr(long a ,long b,long mod) {
long ans = 1;
// System.out.println(a + " " + b);
for(int i = 1;i <= a;i++) {
ans *= i;
ans %= mod;
}
// System.out.println(ans);
for(int i = 1;i <= b;i++) {
// ans /= i;
ans *= inverse(i ,mod);
ans %= mod;
}
for(int i = 1;i <= a-b;i++) {
// ans /= i;
ans *= inverse(i ,mod);
ans %= mod;
}
return ans;
}
public static int solve(int s , int last , int a[]) {
int ans = 0;
for(int i = 1;i < a.length;i++) {
if(s == 0) {
if(last == 1 && a[i] == 1) {
last = a[i];
continue;
}
if(last == 0) {
last = a[i];
continue;
}
s ^= 1;
ans++;
last = 1;
}
else {
if(last == 0) {
if(a[i] == 0) {
last = 1;
continue;
}
last = 0;
continue;
}
if(last == 1) {
if(a[i] == 0) {
last = 1;
continue;
}
ans++;
last = 1;
s ^= 1;
}
}
}
// System.out.private intln("ans " + ans);
return ans;
}
public static long get(long sum ,String x , int N,int f) {
if(N >= x.length()) return sum == 0?1:0;
long ans = 0;
for(int i = 0;i <= 9;i++) {
if(i < x.charAt(N)-'0') ans += get(sum - i , x , N + 1 , 0);
if(f == 1 && i >= x.charAt(N)-'0') continue;
ans += get(sum -i , x , N + 1 ,f);
}
return ans;
}
public static String or(String a , String b) {
int i = a.length() -1, j = b.length()-1;
StringBuilder ans = new StringBuilder();
while(i >=0 && j >= 0) {
if(a.charAt(i) == '1' ||b.charAt(j) == '1') {
ans.insert(0 , '1');
}
else ans.insert(0, 0);
i--;
j--;
}
while(i >= 0) ans.insert(0 , a.charAt(i--));
while(j >= 0) ans.insert(0 , b.charAt(j--));
// while(i >= 0) {ans.insert(0 , '1');i--;}
return ans.toString();
}
public static boolean check(String a , String b) {
// System.out.println(a + " " + b);
for(int i = 0;i < a.length();i++) {
if(a.charAt(i) == '1' && b.charAt(i) == '0') return false;
if(a.charAt(i) == '0' && b.charAt(i) == '1') return true;
}
return false;
}
public static int solve( int j, HashSet<Integer> b) {
if(j == 4) {
return 1;
}
int ans = 0;
for(int i = 0;i <= 9;i++) {
if(b.contains(i)) continue;
ans += solve(j + 1 , b);
}
return ans;
}
public static long lcm(long x , long y) {
return x/gcd(x , y)*y;
}
public static long ncr(int a , int b,long mod) {
if(a == b) return 1l;
return (((fac[a]*inv[b])%mod)*inv[a-b])%mod;
}
static long turnOffK(long n, long k) {
return (n & ~(1l << (k)));
}
public static void swap(int i,int j,int arr[]) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
public static long max(long ...a) {
return maxArray(a);
}
public static void swap(int i,int j,long arr[]) {
long temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
public static void swap(int i,int j,char arr[]) {
char temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
public static String slope(Point a , Point b) {
if((a.x-b.x) == 0) return "inf";
long n = a.y- b.y;
if(n == 0) return "0";
long m = a.x-b.x;
boolean neg = (n*m < 0?true:false);
n = Math.abs(n);
m = Math.abs(m);
long g = gcd(Math.abs(n),Math.abs(m));
n /= g;
m /= g;
String ans = n+"/"+m;
if(neg) ans = "-" + ans;
return ans;
}
public static int lis(int A[], int size) {
int[] tailTable = new int[size];
int len;
tailTable[0] = A[0];
len = 1;
for (int i = 1; i < size; i++) {
if (A[i] < tailTable[0]) tailTable[0] = A[i];
else if (A[i] > tailTable[len - 1]) tailTable[len++] = A[i];
else tailTable[CeilIndex(tailTable, -1, len - 1, A[i])] = A[i];
}
return len;
}
public static int CeilIndex(int A[], int l, int r, int key) {
while (r - l > 1) {
int m = l + (r - l) / 2;
if (A[m] >= key) r = m;
else l = m;
}
return r;
}
public static int lcs(char a[] , char b[]) {
int n = a.length , m = b.length;
int dp[][] = new int[n + 1][m + 1];
for(int i =1;i <= n;i++) {
for(int j =1;j <= m;j++) {
if(a[i-1] == b[j-1]) dp[i][j] = 1 + dp[i-1][j-1];
else dp[i][j] = Math.max(dp[i-1][j] , dp[i][j-1]);
}
}
return dp[n][m];
}
public static int find(int node) {
if(node == parent[node]) return node;
return parent[node] = find(parent[node]);
}
public static void merge(int a ,int b ) {
a = find(a);
b = find(b);
if(a == b) return;
if(size[a] >= size[b]) {
parent[b] = a;
size[a] += size[b];
// primePow[a] += primePow[b];
// primePow[b] = 0;
}
else {
parent[a] = b;
size[b] += size[a];
// primePow[b] += primePow[a];
// primePow[a] = 0;
}
}
public static void processPowerOfP(long arr[]) {
int n = arr.length;
arr[0] = 1;
long mod = (long)1e9 + 7;
for(int i =1;i<n;i++) {
arr[i] = arr[i-1]*51;
arr[i] %= mod;
}
}
public static long hashValue(char s[]) {
int n = s.length;
long powerOfP[] = new long[n];
processPowerOfP(powerOfP);
long ans =0;
long mod = (long)1e9 + 7;
for(int i =0;i<n;i++) {
ans += (s[i]-'a'+1)*powerOfP[i];
ans %= mod;
}
return ans;
}
public static void dfs(int r,int c,char arr[][]) {
int n = arr.length , m = arr[0].length;
arr[r][c] = '#';
for(int i =0;i<4;i++) {
int nr = r + colx[i] , nc = c + coly[i];
if(nr < 0 || nc < 0 || nc >= m || nr>=n) continue;
if(arr[nr][nc] == '#') continue;
dfs(nr,nc,arr);
}
}
public static double getSlope(int a , int b,int x,int y) {
if(a-x == 0) return Double.MAX_VALUE;
if(b-y == 0) return 0.0;
return ((double)b-(double)y)/((double)a-(double)x);
}
public static boolean collinearr(long a[] , long b[] , long c[]) {
return (b[1]-a[1])*(b[0]-c[0]) == (b[0]-a[0])*(b[1]-c[1]);
}
public static boolean isSquare(long sum) {
long root = (int)Math.sqrt(sum);
return root*root == sum;
}
public static int[] suffixArray(String s) {
int n = s.length();
Suffix[] su = new Suffix[n];
for (int i = 0; i < n; i++) {
su[i] = new Suffix(i, s.charAt(i) - '$', 0);
}
for (int i = 0; i < n; i++)
su[i].next = (i + 1 < n ? su[i + 1].rank : -1);
Arrays.sort(su);
int[] ind = new int[n];
for (int length = 4; length < 2 * n; length <<= 1) {
int rank = 0, prev = su[0].rank;
su[0].rank = rank;
ind[su[0].index] = 0;
for (int i = 1; i < n; i++) {
if (su[i].rank == prev && su[i].next == su[i - 1].next) {
prev = su[i].rank;
su[i].rank = rank;
}
else {
prev = su[i].rank;
su[i].rank = ++rank;
}
ind[su[i].index] = i;
}
for (int i = 0; i < n; i++) {
int nextP = su[i].index + length / 2;
su[i].next = nextP < n ?
su[ind[nextP]].rank : -1;
}
Arrays.sort(su);
}
int[] suf = new int[n];
for (int i = 0; i < n; i++)
suf[i] = su[i].index;
return suf;
}
public static boolean isPalindrome(String s) {
int i =0 , j = s.length() -1;
while(i <= j && s.charAt(i) == s.charAt(j)) {
i++;
j--;
}
return i>j;
}
// digit dp hint;
public static long callfun(String num , int N, int last ,int secondLast ,int bound ,long dp[][][][]) {
if(N == 1) {
if(last == -1 || secondLast == -1) return 0;
long answer = 0;
int max = (bound==1)?(num.charAt(num.length()-N)-'0') : 9;
for(int i = 0;i<=max;i++) {
if(last > secondLast && last > i) {
answer++;
}
if(last < secondLast && last < i) {
answer++;
}
}
return answer;
}
if(secondLast == -1 || last == -1 ){
long answer = 0l;
int max = (bound==1)?(num.charAt(num.length()-N)-'0') : 9;
for(int i =0;i<=max;i++) {
int nl , nsl , newbound = bound==0?0:i==max?1:0;
if(last == - 1&& secondLast == -1 && i == 0) {
nl = -1 ; nsl = -1;
}
else {
nl = i;nsl = last;
}
long temp = callfun(num , N-1 , nl , nsl ,newbound, dp);
answer += temp;
if(last != -1 && secondLast != -1 &&((last > secondLast && last > i)||(last < secondLast && last < i))) answer++;
}
return answer;
}
if(dp[N][last][secondLast][bound] != -1) return dp[N][last][secondLast][bound];
long answer = 0l;
int max = (bound==1)?(num.charAt(num.length()-N)-'0') : 9;
for(int i =0;i<=max;i++) {
int nl , nsl , newbound = bound==0?0:i==max?1:0;
if(last == - 1&& secondLast == -1 && i == 0) {
nl = -1 ; nsl = -1;
}
else {
nl = i;nsl = last;
}
long temp = callfun(num , N-1 , nl , nsl ,newbound,dp);
answer += temp;
if(last != -1 && secondLast != -1 &&((last > secondLast && last > i)||(last < secondLast && last < i))) answer++;
}
return dp[N][last][secondLast][bound] = answer;
}
public static Long callfun(int index ,int pair,int arr[],Long dp[][]) {
long mod = 998244353l;
if(index >= arr.length) return 1l;
if(dp[index][pair] != null) return dp[index][pair];
Long sum = 0l , ans = 0l;
if(arr[index]%2 == pair) {
return dp[index][pair] = callfun(index + 1,pair^1 , arr,dp)%mod + callfun(index + 1 ,pair , arr , dp)%mod;
}
else {
return dp[index][pair] = callfun(index + 1,pair , arr,dp)%mod;
}
// for(int i =index;i<arr.length;i++) {
// sum += arr[i];
// if(sum%2 == pair) {
// ans = ans + callfun(i + 1,pair^1,arr , dp)%mod;
// ans%=mod;
// }
// }
// return dp[index][pair] = ans;
}
public static boolean callfun(int index , int n,int neg , int pos , String s) {
if(neg < 0 || pos < 0) return false;
if(index >= n) return true;
if(s.charAt(0) == 'P') {
if(neg <= 0) return false;
return callfun(index + 1,n , neg-1 , pos , s.charAt(1) + "N");
}
else {
if(pos <= 0) return false;
return callfun(index + 1 , n , neg , pos-1 , s.charAt(1) + "P");
}
}
public static void getPerm(int n , char arr[] , String s ,List<String>list) {
if(n == 0) {
list.add(s);
return;
}
for(char ch : arr) {
getPerm(n-1 , arr , s+ ch,list);
}
}
public static int getLen(int i ,int j , char s[]) {
while(i >= 0 && j < s.length && s[i] == s[j]) {
i--;
j++;
}
i++;
j--;
if(i>j) return 0;
return j-i + 1;
}
public static int getMaxCount(String x) {
char s[] = x.toCharArray();
int max = 0;
int n = s.length;
for(int i =0;i<n;i++) {
max = Math.max(max,Math.max(getLen(i , i,s) , getLen(i ,i+1,s)));
}
return max;
}
public static double getDis(int arr[][] , int x, int y) {
double ans = 0.0;
for(int a[] : arr) {
int x1 = a[0] , y1 = a[1];
ans += Math.sqrt((x-x1)*(x-x1) + (y-y1)*(y-y1));
}
return ans;
}
public static boolean valid(String x ) {
if(x.length() == 0) return true;
if(x.length() == 1) return false;
char s[] = x.toCharArray();
if(x.length() == 2) {
if(s[0] == s[1]) {
return false;
}
return true;
}
int r = 0 , b = 0;
for(char ch : x.toCharArray()) {
if(ch == 'R') r++;
else b++;
}
return (r >0 && b >0);
}
public static void primeDivisor(HashMap<Long , Long >cnt , long num) {
for(long i = 2;i*i<=num;i++) {
while(num%i == 0) {
cnt.put(i ,(cnt.getOrDefault(i,0l) + 1));
num /= i;
}
}
if(num > 2) {
cnt.put(num ,(cnt.getOrDefault(num,0l) + 1));
}
}
public static boolean isSubsequene(char a[], char b[] ) {
int i =0 , j = 0;
while(i < a.length && j <b.length) {
if(a[i] == b[j]) {
j++;
}
i++;
}
return j >= b.length;
}
public static long fib(int n ,long M) {
if (n == 0) {
return 0;
} else if (n == 1) {
return 1;
} else {
long[][] mat = {{1, 1}, {1, 0}};
mat = pow(mat, n-1 , M);
return mat[0][0];
}
}
public static long[][] pow(long[][] mat, int n ,long M) {
if (n == 1) return mat;
else if (n % 2 == 0) return pow(mul(mat, mat , M), n/2 , M);
else return mul(pow(mul(mat, mat,M), n/2,M), mat , M);
}
static long[][] mul(long[][] p, long[][] q,long M) {
long a = (p[0][0]*q[0][0] + p[0][1]*q[1][0])%M;
long b = (p[0][0]*q[0][1] + p[0][1]*q[1][1])%M;
long c = (p[1][0]*q[0][0] + p[1][1]*q[1][0])%M;
long d = (p[1][0]*q[0][1] + p[1][1]*q[1][1])%M;
return new long[][] {{a, b}, {c, d}};
}
public static long[] kdane(long arr[]) {
int n = arr.length;
long dp[] = new long[n];
dp[0] = arr[0];
long ans = dp[0];
for(int i = 1;i<n;i++) {
dp[i] = Math.max(dp[i-1] + arr[i] , arr[i]);
ans = Math.max(ans , dp[i]);
}
return dp;
}
public static void update(int low , int high , int l , int r, int val , int treeIndex ,int tree[]) {
if(low > r || high < l || high < low) return;
if(l <= low && high <= r) {
System.out.println("At " +low + " and " + high + " ans ttreeIndex " + treeIndex);
tree[treeIndex] += val;
return;
}
int mid = low + (high - low)/2;
update(low , mid , l , r , val , treeIndex*2 + 1, tree);
update(mid + 1 , high , l , r , val , treeIndex*2 + 2 , tree);
}
// static int colx[] = {1 ,1, -1,-1 , 2,2,-2,-2};
// static int coly[] = {-2 ,2, 2,-2,1,-1,1,-1};
static int colx[] = {1 ,-1, 0,0 , 1,1,-1,-1};
static int coly[] = {0 ,0, 1,-1,1,-1,1,-1};
public static void reverse(char arr[]) {
int i =0 , j = arr.length-1;
while(i < j) {
swap(i , j , arr);
i++;
j--;
}
}
public static void reverse(long arr[]) {
int i =0 , j = arr.length-1;
while(i < j) {
swap(i , j , arr);
i++;
j--;
}
}
public static void reverse(int arr[]) {
int i =0 , j = arr.length-1;
while(i < j) {
swap(i , j , arr);
i++;
j--;
}
}
public static long inverse(long x , long mod) {
return pow(x , mod -2 , mod);
}
public static int maxArray(int arr[]) {
int ans = arr[0] , n = arr.length;
for(int i =1;i<n;i++) {
ans = Math.max(ans , arr[i]);
}
return ans;
}
public static long maxArray(long arr[]) {
long ans = arr[0];
int n = arr.length;
for(int i =1;i<n;i++) {
ans = Math.max(ans , arr[i]);
}
return ans;
}
public static int minArray(int arr[]) {
int ans = arr[0] , n = arr.length;
for(int i =0;i<n;i++ ) {
ans = Math.min(ans ,arr[i]);
}
return ans;
}
public static long minArray(long arr[]) {
long ans = arr[0];
int n = arr.length;
for(int i =0;i<n;i++ ) {
ans = Math.min(ans ,arr[i]);
}
return ans;
}
public static int sumArray(int arr[]) {
int ans = 0;
for(int x : arr) {
ans += x;
}
return ans;
}
public static long sumArray(long arr[]) {
long ans = 0;
for(long x : arr) {
ans += x;
}
return ans;
}
public static long rl() {
return sc.nextLong();
}
public static char[] rac() {
return sc.next().toCharArray();
}
public static String rs() {
return sc.next();
}
public static char rc() {
return sc.next().charAt(0);
}
public static int [] rai(int n) {
int ans[] = new int[n];
for(int i =0;i<n;i++) {
ans[i] = sc.nextInt();
}
return ans;
}
public static long [] ral(int n) {
long ans[] = new long[n];
for(int i =0;i<n;i++) {
ans[i] = sc.nextLong();
}
return ans;
}
public static int ri() {
return sc.nextInt();
}
public static int getValue(int num ) {
int ans = 0;
while(num > 0) {
ans++;
num = num&(num-1);
}
return ans;
}
public static boolean isValid(int x ,int y , int n,char arr[][],boolean visited[][][][]) {
return x>=0 && x<n && y>=0 && y <n && !(arr[x][y] == '#');
}
// public static Pair join(Pair a , Pair b) {
// Pair res = new Pair(Math.min(a.min , b.min) , Math.max(a.max , b.max) , a.count + b.count);
// return res;
// }
// segment tree query over range
// public static int query(int node,int l , int r,int a,int b ,Pair tree[] ) {
// if(tree[node].max < a || tree[node].min > b) return 0;
// if(l > r) return 0;
// if(tree[node].min >= a && tree[node].max <= b) {
// return tree[node].count;
// }
// int mid = l + (r-l)/2;
// int ans = query(node*2 ,l , mid ,a , b , tree) + query(node*2 +1,mid + 1, r , a , b, tree);
// return ans;
// }
// // segment tree update over range
// public static void update(int node, int i , int j ,int l , int r,long value, long arr[] ) {
// if(l >= i && j >= r) {
// arr[node] += value;
// return;
// }
// if(j < l|| r < i) return;
// int mid = l + (r-l)/2;
// update(node*2 ,i ,j ,l,mid,value, arr);
// update(node*2 +1,i ,j ,mid + 1,r, value , arr);
// }c
public static long pow(long a , long b , long mod) {
if(b == 1) return a;
if(b == 0) return 1;
long ans = pow(a , b/2 , mod)%mod;
if(b%2 == 0) {
return (ans*ans)%mod;
}
else {
return ((ans*ans)%mod*a)%mod;
}
}
public static long pow(long a , long b ) {
if(b == 1) return a;
if(b == 0) return 1;
long ans = pow(a , b/2);
if(b%2 == 0) {
return (ans*ans);
}
else {
return ((ans*ans)*a);
}
}
public static boolean isVowel(char ch) {
if(ch == 'a' || ch == 'e'||ch == 'i' || ch == 'o' || ch == 'u') return true;
if((ch == 'A' || ch == 'E'||ch == 'I' || ch == 'O' || ch == 'U')) return true;
return false;
}
// public static int getFactor(int num) {
// if(num==1) return 1;
// int ans = 2;
// int k = num/2;
// for(int i = 2;i<=k;i++) {
// if(num%i==0) ans++;
// }
// return Math.abs(ans);
// }
public static int[] readarr()throws IOException {
int n = sc.nextInt();
int arr[] = new int[n];
for(int i =0;i<n;i++) {
arr[i] = sc.nextInt();
}
return arr;
}
public static boolean isPowerOfTwo (long x) {
return x!=0 && ((x&(x-1)) == 0);
}
public static boolean isPrime(long num) {
if(num==1) return false;
if(num<=3) return true;
if(num%2==0||num%3==0) return false;
for(long i =5;i*i<=num;i+=6) {
if(num%i==0 || num%(i+2) == 0) return false;
}
return true;
}
public static boolean isPrime(int num) {
// System.out.println("At pr " + num);
if(num==1) return false;
if(num<=3) return true;
if(num%2==0||num%3==0) return false;
for(int i =5;i*i<=num;i+=6) {
if(num%i==0 || num%(i+2) == 0) return false;
}
return true;
}
// public static boolean isPrime(long num) {
// if(num==1) return false;
// if(num<=3) return true;
// if(num%2==0||num%3==0) return false;
// for(int i =5;i*i<=num;i+=6) {
// if(num%i==0) return false;
// }
// return true;
// }
public static void allMultiple() {
// int MAX = 0 , n = nums.length;
// for(int x : nums) MAX = Math.max(MAX ,x);
// int cnt[] = new int[MAX + 1];
// int ans[] = new int[MAX + 1];
// for (int i = 0; i < n; ++i) cnt[nums[i]]++;
// for (int i = 1; i <= MAX; ++i) {
// for (int j = i; j <= MAX; j += i) ans[i] += cnt[j];
// }
}
public static long gcd(long a , long b) {
if (b == 0) return a;
return gcd(b, a % b);
}
public static int gcd(int a , int b) {
if (b == 0) return a;
return gcd(b, a % b);
}
public static int get_gcd(int a , int b) {
if (b == 0) return a;
return gcd(b, a % b);
}
public static long get_gcd(long a , long b) {
if (b == 0) return a;
return gcd(b, a % b);
}
// public static long fac(long num) {
// long ans = 1;
// int mod = (int)1e9+7;
// for(long i = 2;i<=num;i++) {
// ans = (ans*i)%mod;
// }
// return ans;
// }
}
|
Java
|
["5\n\n2\n\n4\n\n6\n\n8\n\n60"]
|
2 seconds
|
["1 0 1\n3 2 1\n12 7 1\n42 27 1\n341102826 248150916 1"]
|
NoteIn the first test case, Alex wins if he receives the card $$$2$$$ (he plays it, and Boris cannot respond). If Alex receives the card $$$1$$$, the game ends in a draw.In the second test case: Alex wins if he receives the cards $$$[3, 4]$$$, $$$[2, 4]$$$ or $$$[1, 4]$$$; Boris wins if Alex receives the cards $$$[1, 2]$$$ or $$$[1, 3]$$$; the game ends in a draw if Alex receives the cards $$$[2, 3]$$$.
|
Java 11
|
standard input
|
[
"combinatorics",
"constructive algorithms",
"dp",
"games"
] |
63fc2fb08d248f8ccdb3f5364c96dac1
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 30$$$) — the number of test cases. Then, $$$t$$$ lines follow. The $$$i$$$-th line contains one even integer $$$n$$$ ($$$2 \le n \le 60$$$).
| 1,500
|
For each test case, print three integers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. Since the answers can be large, print them modulo $$$998244353$$$.
|
standard output
| |
PASSED
|
84ca2bd8725dc44bddc0d28c1ccd8366
|
train_109.jsonl
|
1664462100
|
Consider a game with $$$n$$$ cards ($$$n$$$ is even). Each card has a number written on it, between $$$1$$$ and $$$n$$$. All numbers on the cards are different. We say that a card with number $$$x$$$ is stronger than a card with number $$$y$$$ if $$$x > y$$$.Two players, Alex and Boris, play this game. In the beginning, each of them receives exactly $$$\frac{n}{2}$$$ cards, so each card belongs to exactly one player. Then, they take turns. Alex goes first, then Boris, then Alex again, and so on.On a player's turn, he must play exactly one of his cards. Then, if the opponent doesn't have any cards stronger than the card played, the opponent loses, and the game ends. Otherwise, the opponent has to play a stronger card (exactly one card as well). These two cards are removed from the game, and the turn ends. If there are no cards left, the game ends in a draw; otherwise it's the opponent's turn.Consider all possible ways to distribute the cards between two players, so that each of them receives exactly half of the cards. You have to calculate three numbers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. You may assume that both players play optimally (i. e. if a player can win no matter how his opponent plays, he wins). Two ways to distribute the cards are different if there is at least one card such that, in one of these ways, it is given to Alex, and in the other way, it is given to Boris.For example, suppose $$$n = 4$$$, Alex receives the cards $$$[2, 3]$$$, and Boris receives the cards $$$[1, 4]$$$. Then the game may go as follows: if Alex plays the card $$$2$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$3$$$. So, the game ends in a draw; if Alex plays the card $$$3$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$2$$$. So, the game ends in a draw. So, in this case, the game ends in a draw.
|
512 megabytes
|
import java.util.*;
import java.lang.*;
import java.io.*;
public class Main
{
PrintWriter out = new PrintWriter(System.out);
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer tok = new StringTokenizer("");
String next() throws IOException {
if (!tok.hasMoreTokens()) { tok = new StringTokenizer(in.readLine()); }
return tok.nextToken();
}
int ni() throws IOException { return Integer.parseInt(next()); }
long nl() throws IOException { return Long.parseLong(next()); }
int[] na(int n) throws IOException { int[]A=new int[n]; for (int i=0;i<n;i++) A[i]=ni(); return A; }
long mod=998244353;
void solve() throws IOException {
long[]F=new long[61];
F[0]=1;
for (int i=1;i<=60;i++) F[i]=(F[i-1]*i)%mod;
for (int tc=ni();tc>0;tc--) {
int n=ni();
long a=0;
long b=0;
int p=0;
for (int i=n;i>2;i-=2) {
long f1=(F[i-2]*mp((F[i/2]*F[i-2-i/2])%mod,mod-2))%mod;
long f2=(F[i-2]*mp((F[i/2-1]*F[i-1-i/2])%mod,mod-2))%mod;
if (p==0) { a=(a+f1+f2)%mod; b=(b+f1)%mod; }
else { b=(b+f1+f2)%mod; a=(a+f1)%mod; }
p=1-p;
}
if (p==0) a++;
else b++;
out.println(a+" "+b+" 1");
}
out.flush();
}
int gcd(int a,int b) { return(b==0?a:gcd(b,a%b)); }
long gcd(long a,long b) { return(b==0?a:gcd(b,a%b)); }
long mp(long a,long p) { long r=1; while(p>0) { if ((p&1)==1) r=(r*a)%mod; p>>=1; a=(a*a)%mod; } return r; }
public static void main(String[] args) throws IOException {
new Main().solve();
}
}
|
Java
|
["5\n\n2\n\n4\n\n6\n\n8\n\n60"]
|
2 seconds
|
["1 0 1\n3 2 1\n12 7 1\n42 27 1\n341102826 248150916 1"]
|
NoteIn the first test case, Alex wins if he receives the card $$$2$$$ (he plays it, and Boris cannot respond). If Alex receives the card $$$1$$$, the game ends in a draw.In the second test case: Alex wins if he receives the cards $$$[3, 4]$$$, $$$[2, 4]$$$ or $$$[1, 4]$$$; Boris wins if Alex receives the cards $$$[1, 2]$$$ or $$$[1, 3]$$$; the game ends in a draw if Alex receives the cards $$$[2, 3]$$$.
|
Java 11
|
standard input
|
[
"combinatorics",
"constructive algorithms",
"dp",
"games"
] |
63fc2fb08d248f8ccdb3f5364c96dac1
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 30$$$) — the number of test cases. Then, $$$t$$$ lines follow. The $$$i$$$-th line contains one even integer $$$n$$$ ($$$2 \le n \le 60$$$).
| 1,500
|
For each test case, print three integers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. Since the answers can be large, print them modulo $$$998244353$$$.
|
standard output
| |
PASSED
|
1c337e5c9340f9612f3bc2d9fb07e23d
|
train_109.jsonl
|
1664462100
|
Consider a game with $$$n$$$ cards ($$$n$$$ is even). Each card has a number written on it, between $$$1$$$ and $$$n$$$. All numbers on the cards are different. We say that a card with number $$$x$$$ is stronger than a card with number $$$y$$$ if $$$x > y$$$.Two players, Alex and Boris, play this game. In the beginning, each of them receives exactly $$$\frac{n}{2}$$$ cards, so each card belongs to exactly one player. Then, they take turns. Alex goes first, then Boris, then Alex again, and so on.On a player's turn, he must play exactly one of his cards. Then, if the opponent doesn't have any cards stronger than the card played, the opponent loses, and the game ends. Otherwise, the opponent has to play a stronger card (exactly one card as well). These two cards are removed from the game, and the turn ends. If there are no cards left, the game ends in a draw; otherwise it's the opponent's turn.Consider all possible ways to distribute the cards between two players, so that each of them receives exactly half of the cards. You have to calculate three numbers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. You may assume that both players play optimally (i. e. if a player can win no matter how his opponent plays, he wins). Two ways to distribute the cards are different if there is at least one card such that, in one of these ways, it is given to Alex, and in the other way, it is given to Boris.For example, suppose $$$n = 4$$$, Alex receives the cards $$$[2, 3]$$$, and Boris receives the cards $$$[1, 4]$$$. Then the game may go as follows: if Alex plays the card $$$2$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$3$$$. So, the game ends in a draw; if Alex plays the card $$$3$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$2$$$. So, the game ends in a draw. So, in this case, the game ends in a draw.
|
512 megabytes
|
import java.util.*;
public class q184 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int t= sc.nextInt();
while (t-->0){
int n = sc.nextInt();
cal(n+1);
dp = new long[n+1][3];
dp[2][0] = 1;
dp[2][1] = 0;
dp[2][2] = 1;
solve(n);
System.out.println(dp[n][0] + " " + dp[n][1] + " " + dp[n][2]);
// System.out.println();
}
}
private static void solve(int n) {
if(n == 2) return;
solve(n-2);
dp[n][0] = (NCR(n-1,n/2)+dp[n-2][1])%mod;
dp[n][1] = (NCR(n-2,n/2)+dp[n-2][0])%mod;
dp[n][2] = (dp[n-2][2])%mod;
}
static long dp[][];
static int mod = 998244353;
static long fact[],invfact[];
public static void cal(int n){
fact= new long[n];
invfact = new long[n];
fact[0] = 1;
invfact[0] = 1;
for(int i =1; i < n; i++){
fact[i] = (fact[i-1]*i)%mod;
invfact[i] = (modInverse(fact[i]));
}
}
public static long modInverse(long n){
return powMod(n,mod-2);
}
public static long powMod(long x, long y){
long res = 1;
x = x%mod;
while(y > 0){
if((y&1) == 1){
res = (res*x)%mod;
}
y = y>>1;
x = (x*x)%mod;
}
return res;
}
public static long NCR(int n, int r){
if(r < 0 || n < 0){
return -1;
}
if(n < r) return 0;
if(n == 0 || r == n) return 1;
return (fact[n] * (invfact[r]) % mod) * invfact[n-r]%mod;
}
}
|
Java
|
["5\n\n2\n\n4\n\n6\n\n8\n\n60"]
|
2 seconds
|
["1 0 1\n3 2 1\n12 7 1\n42 27 1\n341102826 248150916 1"]
|
NoteIn the first test case, Alex wins if he receives the card $$$2$$$ (he plays it, and Boris cannot respond). If Alex receives the card $$$1$$$, the game ends in a draw.In the second test case: Alex wins if he receives the cards $$$[3, 4]$$$, $$$[2, 4]$$$ or $$$[1, 4]$$$; Boris wins if Alex receives the cards $$$[1, 2]$$$ or $$$[1, 3]$$$; the game ends in a draw if Alex receives the cards $$$[2, 3]$$$.
|
Java 11
|
standard input
|
[
"combinatorics",
"constructive algorithms",
"dp",
"games"
] |
63fc2fb08d248f8ccdb3f5364c96dac1
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 30$$$) — the number of test cases. Then, $$$t$$$ lines follow. The $$$i$$$-th line contains one even integer $$$n$$$ ($$$2 \le n \le 60$$$).
| 1,500
|
For each test case, print three integers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. Since the answers can be large, print them modulo $$$998244353$$$.
|
standard output
| |
PASSED
|
a36a7be0575af595c5109a0a09f60161
|
train_109.jsonl
|
1664462100
|
Consider a game with $$$n$$$ cards ($$$n$$$ is even). Each card has a number written on it, between $$$1$$$ and $$$n$$$. All numbers on the cards are different. We say that a card with number $$$x$$$ is stronger than a card with number $$$y$$$ if $$$x > y$$$.Two players, Alex and Boris, play this game. In the beginning, each of them receives exactly $$$\frac{n}{2}$$$ cards, so each card belongs to exactly one player. Then, they take turns. Alex goes first, then Boris, then Alex again, and so on.On a player's turn, he must play exactly one of his cards. Then, if the opponent doesn't have any cards stronger than the card played, the opponent loses, and the game ends. Otherwise, the opponent has to play a stronger card (exactly one card as well). These two cards are removed from the game, and the turn ends. If there are no cards left, the game ends in a draw; otherwise it's the opponent's turn.Consider all possible ways to distribute the cards between two players, so that each of them receives exactly half of the cards. You have to calculate three numbers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. You may assume that both players play optimally (i. e. if a player can win no matter how his opponent plays, he wins). Two ways to distribute the cards are different if there is at least one card such that, in one of these ways, it is given to Alex, and in the other way, it is given to Boris.For example, suppose $$$n = 4$$$, Alex receives the cards $$$[2, 3]$$$, and Boris receives the cards $$$[1, 4]$$$. Then the game may go as follows: if Alex plays the card $$$2$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$3$$$. So, the game ends in a draw; if Alex plays the card $$$3$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$2$$$. So, the game ends in a draw. So, in this case, the game ends in a draw.
|
512 megabytes
|
import java.util.*;
public class q184 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int t= sc.nextInt();
while (t-->0){
int n = sc.nextInt();
cal(n+1);
dp = new long[n+1][3];
dp[2][0] = 1;
dp[2][1] = 0;
dp[2][2] = 1;
solve(n);
System.out.println(dp[n][0] + " " + dp[n][1] + " " + dp[n][2]);
// System.out.println();
}
}
private static void solve(int n) {
if(n == 2) return;
solve(n-2);
dp[n][0] = (NCR(n-1,n/2)+dp[n-2][1])%mod;
dp[n][1] = (NCR(n-2,n/2)+dp[n-2][0])%mod;
dp[n][2] = (dp[n-2][2])%mod;
}
static long dp[][];
static int mod = 998244353;
static long fact[],invfact[];
public static void cal(int n){
fact= new long[n];
invfact = new long[n];
fact[0] = 1;
invfact[0] = 1;
for(int i =1; i < n; i++){
fact[i] = (fact[i-1]*i)%mod;
invfact[i] = (modInverse(fact[i]));
}
}
public static long modInverse(long n){
return powMod(n,mod-2);
}
public static long powMod(long x, long y){
long res = 1;
x = x%mod;
while(y > 0){
if((y&1) == 1){
res = (res*x)%mod;
}
y = y>>1;
x = (x*x)%mod;
}
return res;
}
public static long NCR(int n, int r){
if(r < 0 || n < 0){
return -1;
}
if(n < r) return 0;
if(n == 0 || r == n) return 1;
return (fact[n] * (invfact[r]) % mod) * invfact[n-r]%mod;
}
}
|
Java
|
["5\n\n2\n\n4\n\n6\n\n8\n\n60"]
|
2 seconds
|
["1 0 1\n3 2 1\n12 7 1\n42 27 1\n341102826 248150916 1"]
|
NoteIn the first test case, Alex wins if he receives the card $$$2$$$ (he plays it, and Boris cannot respond). If Alex receives the card $$$1$$$, the game ends in a draw.In the second test case: Alex wins if he receives the cards $$$[3, 4]$$$, $$$[2, 4]$$$ or $$$[1, 4]$$$; Boris wins if Alex receives the cards $$$[1, 2]$$$ or $$$[1, 3]$$$; the game ends in a draw if Alex receives the cards $$$[2, 3]$$$.
|
Java 11
|
standard input
|
[
"combinatorics",
"constructive algorithms",
"dp",
"games"
] |
63fc2fb08d248f8ccdb3f5364c96dac1
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 30$$$) — the number of test cases. Then, $$$t$$$ lines follow. The $$$i$$$-th line contains one even integer $$$n$$$ ($$$2 \le n \le 60$$$).
| 1,500
|
For each test case, print three integers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. Since the answers can be large, print them modulo $$$998244353$$$.
|
standard output
| |
PASSED
|
b745f003e631960207209a5b446185f2
|
train_109.jsonl
|
1664462100
|
Consider a game with $$$n$$$ cards ($$$n$$$ is even). Each card has a number written on it, between $$$1$$$ and $$$n$$$. All numbers on the cards are different. We say that a card with number $$$x$$$ is stronger than a card with number $$$y$$$ if $$$x > y$$$.Two players, Alex and Boris, play this game. In the beginning, each of them receives exactly $$$\frac{n}{2}$$$ cards, so each card belongs to exactly one player. Then, they take turns. Alex goes first, then Boris, then Alex again, and so on.On a player's turn, he must play exactly one of his cards. Then, if the opponent doesn't have any cards stronger than the card played, the opponent loses, and the game ends. Otherwise, the opponent has to play a stronger card (exactly one card as well). These two cards are removed from the game, and the turn ends. If there are no cards left, the game ends in a draw; otherwise it's the opponent's turn.Consider all possible ways to distribute the cards between two players, so that each of them receives exactly half of the cards. You have to calculate three numbers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. You may assume that both players play optimally (i. e. if a player can win no matter how his opponent plays, he wins). Two ways to distribute the cards are different if there is at least one card such that, in one of these ways, it is given to Alex, and in the other way, it is given to Boris.For example, suppose $$$n = 4$$$, Alex receives the cards $$$[2, 3]$$$, and Boris receives the cards $$$[1, 4]$$$. Then the game may go as follows: if Alex plays the card $$$2$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$3$$$. So, the game ends in a draw; if Alex plays the card $$$3$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$2$$$. So, the game ends in a draw. So, in this case, the game ends in a draw.
|
512 megabytes
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.*;
import static java.lang.Math.*;
import static java.util.Arrays.sort;
public class Codeforces {
static long dp[][];
public static void main(String[] args) {
FastReader fastReader = new FastReader();
PrintWriter out = new PrintWriter(System.out);
// int tt = 1;
int tt = fastReader.nextInt();
while(tt -- > 0) {
int n = fastReader.nextInt();
cal(n+1);
dp = new long[n+1][3];
dp[2][0] = 1;
dp[2][1] = 0;
dp[2][2] = 1;
solve(n);
out.println(dp[n][0] + " " + dp[n][1] + " " + dp[n][2]);
}
out.close();
}
private static void solve(int n) {
if(n == 2) return;
solve(n-2);
dp[n][0] = (NCR(n-1,n/2)+dp[n-2][1])%mod;
dp[n][1] = (NCR(n-2,n/2)+dp[n-2][0])%mod;
dp[n][2] = (dp[n-2][2])%mod;
}
static int mod = 998244353;
static long fact[],invfact[];
public static void cal(int n){
fact= new long[n];
invfact = new long[n];
fact[0] = 1;
invfact[0] = 1;
for(int i =1; i < n; i++){
fact[i] = (fact[i-1]*i)%mod;
invfact[i] = (modInverse(fact[i]));
}
}
public static long NCR(int n, int r){
if(r < 0 || n < 0){
return -1;
}
if(n < r) return 0;
if(n == 0 || r == n) return 1;
return (fact[n] * (invfact[r]) % mod) * invfact[n-r]%mod;
}
public static long modInverse(long n){
return powMod(n,mod-2);
}
public static long powMod(long x, long y){
long res = 1;
x = x%mod;
while(y > 0){
if((y&1) == 1){
res = (res*x)%mod;
}
y = y>>1;
x = (x*x)%mod;
}
return res;
}
static class Pair{
int x;
int y;
Pair(int x, int y){
this.x = x;
this.y = y;
}
}
// constants
static final int IBIG = 1000000007;
static final int IMAX = 2147483647;
static final long LMAX = 9223372036854775807L;
static Random __r = new Random();
static int[][] direction = new int[][]{{-1,-1}, {-1,0}, {-1,1}, {0,1}, {1,1}, {1,0}, {1,-1}, {0, -1}};
// math util
static int minof(int a, int b, int c) {
return min(a, min(b, c));
}
static int minof(int... x) {
if (x.length == 1)
return x[0];
if (x.length == 2)
return min(x[0], x[1]);
if (x.length == 3)
return min(x[0], min(x[1], x[2]));
int min = x[0];
for (int i = 1; i < x.length; ++i)
if (x[i] < min)
min = x[i];
return min;
}
static long minof(long a, long b, long c) {
return min(a, min(b, c));
}
static long minof(long... x) {
if (x.length == 1)
return x[0];
if (x.length == 2)
return min(x[0], x[1]);
if (x.length == 3)
return min(x[0], min(x[1], x[2]));
long min = x[0];
for (int i = 1; i < x.length; ++i)
if (x[i] < min)
min = x[i];
return min;
}
static int maxof(int a, int b, int c) {
return max(a, max(b, c));
}
static int maxof(int... x) {
if (x.length == 1)
return x[0];
if (x.length == 2)
return max(x[0], x[1]);
if (x.length == 3)
return max(x[0], max(x[1], x[2]));
int max = x[0];
for (int i = 1; i < x.length; ++i)
if (x[i] > max)
max = x[i];
return max;
}
static long maxof(long a, long b, long c) {
return max(a, max(b, c));
}
static long maxof(long... x) {
if (x.length == 1)
return x[0];
if (x.length == 2)
return max(x[0], x[1]);
if (x.length == 3)
return max(x[0], max(x[1], x[2]));
long max = x[0];
for (int i = 1; i < x.length; ++i)
if (x[i] > max)
max = x[i];
return max;
}
static int powi(int a, int b) {
if (a == 0)
return 0;
int ans = 1;
while (b > 0) {
if ((b & 1) > 0)
ans *= a;
a *= a;
b >>= 1;
}
return ans;
}
static long powl(long a, int b) {
if (a == 0)
return 0;
long ans = 1;
while (b > 0) {
if ((b & 1) > 0)
ans *= a;
a *= a;
b >>= 1;
}
return ans;
}
static int fli(double d) {
return (int) d;
}
static int cei(double d) {
return (int) ceil(d);
}
static long fll(double d) {
return (long) d;
}
static long cel(double d) {
return (long) ceil(d);
}
static int gcd(int a, int b) {
return b == 0 ? a : gcd(b, a % b);
}
static int lcm(int a, int b) {
return (a / gcd(a, b)) * b;
}
static long gcd(long a, long b) {
return b == 0 ? a : gcd(b, a % b);
}
static long lcm(long a, long b) {
return (a / gcd(a, b)) * b;
}
static int[] exgcd(int a, int b) {
if (b == 0)
return new int[] { 1, 0 };
int[] y = exgcd(b, a % b);
return new int[] { y[1], y[0] - y[1] * (a / b) };
}
static long[] exgcd(long a, long b) {
if (b == 0)
return new long[] { 1, 0 };
long[] y = exgcd(b, a % b);
return new long[] { y[1], y[0] - y[1] * (a / b) };
}
static int randInt(int min, int max) {
return __r.nextInt(max - min + 1) + min;
}
static long mix(long x) {
x += 0x9e3779b97f4a7c15L;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9L;
x = (x ^ (x >> 27)) * 0x94d049bb133111ebL;
return x ^ (x >> 31);
}
public static boolean[] findPrimes(int limit) {
assert limit >= 2;
final boolean[] nonPrimes = new boolean[limit];
nonPrimes[0] = true;
nonPrimes[1] = true;
int sqrt = (int) Math.sqrt(limit);
for (int i = 2; i <= sqrt; i++) {
if (nonPrimes[i])
continue;
for (int j = i; j < limit; j += i) {
if (!nonPrimes[j] && i != j)
nonPrimes[j] = true;
}
}
return nonPrimes;
}
// array util
static void reverse(int[] a) {
for (int i = 0, n = a.length, half = n / 2; i < half; ++i) {
int swap = a[i];
a[i] = a[n - i - 1];
a[n - i - 1] = swap;
}
}
static void reverse(long[] a) {
for (int i = 0, n = a.length, half = n / 2; i < half; ++i) {
long swap = a[i];
a[i] = a[n - i - 1];
a[n - i - 1] = swap;
}
}
static void reverse(double[] a) {
for (int i = 0, n = a.length, half = n / 2; i < half; ++i) {
double swap = a[i];
a[i] = a[n - i - 1];
a[n - i - 1] = swap;
}
}
static void reverse(char[] a) {
for (int i = 0, n = a.length, half = n / 2; i < half; ++i) {
char swap = a[i];
a[i] = a[n - i - 1];
a[n - i - 1] = swap;
}
}
static void shuffle(int[] a) {
int n = a.length - 1;
for (int i = 0; i < n; ++i) {
int ind = randInt(i, n);
int swap = a[i];
a[i] = a[ind];
a[ind] = swap;
}
}
static void shuffle(long[] a) {
int n = a.length - 1;
for (int i = 0; i < n; ++i) {
int ind = randInt(i, n);
long swap = a[i];
a[i] = a[ind];
a[ind] = swap;
}
}
static void shuffle(double[] a) {
int n = a.length - 1;
for (int i = 0; i < n; ++i) {
int ind = randInt(i, n);
double swap = a[i];
a[i] = a[ind];
a[ind] = swap;
}
}
static void rsort(int[] a) {
shuffle(a);
sort(a);
}
static void rsort(long[] a) {
shuffle(a);
sort(a);
}
static void rsort(double[] a) {
shuffle(a);
sort(a);
}
static int[] copy(int[] a) {
int[] ans = new int[a.length];
for (int i = 0; i < a.length; ++i)
ans[i] = a[i];
return ans;
}
static long[] copy(long[] a) {
long[] ans = new long[a.length];
for (int i = 0; i < a.length; ++i)
ans[i] = a[i];
return ans;
}
static double[] copy(double[] a) {
double[] ans = new double[a.length];
for (int i = 0; i < a.length; ++i)
ans[i] = a[i];
return ans;
}
static char[] copy(char[] a) {
char[] ans = new char[a.length];
for (int i = 0; i < a.length; ++i)
ans[i] = a[i];
return ans;
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
int[] ria(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = Integer.parseInt(next());
return a;
}
long nextLong() {
return Long.parseLong(next());
}
long[] rla(int n) {
long[] a = new long[n];
for (int i = 0; i < n; i++)
a[i] = Long.parseLong(next());
return a;
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
}
|
Java
|
["5\n\n2\n\n4\n\n6\n\n8\n\n60"]
|
2 seconds
|
["1 0 1\n3 2 1\n12 7 1\n42 27 1\n341102826 248150916 1"]
|
NoteIn the first test case, Alex wins if he receives the card $$$2$$$ (he plays it, and Boris cannot respond). If Alex receives the card $$$1$$$, the game ends in a draw.In the second test case: Alex wins if he receives the cards $$$[3, 4]$$$, $$$[2, 4]$$$ or $$$[1, 4]$$$; Boris wins if Alex receives the cards $$$[1, 2]$$$ or $$$[1, 3]$$$; the game ends in a draw if Alex receives the cards $$$[2, 3]$$$.
|
Java 11
|
standard input
|
[
"combinatorics",
"constructive algorithms",
"dp",
"games"
] |
63fc2fb08d248f8ccdb3f5364c96dac1
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 30$$$) — the number of test cases. Then, $$$t$$$ lines follow. The $$$i$$$-th line contains one even integer $$$n$$$ ($$$2 \le n \le 60$$$).
| 1,500
|
For each test case, print three integers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. Since the answers can be large, print them modulo $$$998244353$$$.
|
standard output
| |
PASSED
|
bc73a906db587166014547a84eb796ee
|
train_109.jsonl
|
1664462100
|
Consider a game with $$$n$$$ cards ($$$n$$$ is even). Each card has a number written on it, between $$$1$$$ and $$$n$$$. All numbers on the cards are different. We say that a card with number $$$x$$$ is stronger than a card with number $$$y$$$ if $$$x > y$$$.Two players, Alex and Boris, play this game. In the beginning, each of them receives exactly $$$\frac{n}{2}$$$ cards, so each card belongs to exactly one player. Then, they take turns. Alex goes first, then Boris, then Alex again, and so on.On a player's turn, he must play exactly one of his cards. Then, if the opponent doesn't have any cards stronger than the card played, the opponent loses, and the game ends. Otherwise, the opponent has to play a stronger card (exactly one card as well). These two cards are removed from the game, and the turn ends. If there are no cards left, the game ends in a draw; otherwise it's the opponent's turn.Consider all possible ways to distribute the cards between two players, so that each of them receives exactly half of the cards. You have to calculate three numbers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. You may assume that both players play optimally (i. e. if a player can win no matter how his opponent plays, he wins). Two ways to distribute the cards are different if there is at least one card such that, in one of these ways, it is given to Alex, and in the other way, it is given to Boris.For example, suppose $$$n = 4$$$, Alex receives the cards $$$[2, 3]$$$, and Boris receives the cards $$$[1, 4]$$$. Then the game may go as follows: if Alex plays the card $$$2$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$3$$$. So, the game ends in a draw; if Alex plays the card $$$3$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$2$$$. So, the game ends in a draw. So, in this case, the game ends in a draw.
|
512 megabytes
|
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int t = scn.nextInt();
combinations=new HashMap<>();
while(t-->0){
int n = scn.nextInt();
long a=0,b=0;
int an = 1, bn = 2;
for(int i=0;i<n/2;i++){
a+=combinations(n-an,n/2-i);
a%=998244353;
b+=combinations(n-bn,n/2-i);
b%=998244353;
if(i%2==0){
an+=3;
bn++;
}else{
an++;
bn+=3;
}
}
System.out.println(a+" "+b+" "+1);
}
}
public static HashMap<String,Long> combinations;
public static long combinations(int n,int r){
if(n<r){
return 0;
}
if(n==r||r==0){
return 1;
}
if(combinations.containsKey(""+n+"C"+r)){
return combinations.get(""+n+"C"+r);
}
combinations.put(""+n+"C"+r,(combinations(n-1,r)+combinations(n-1,r-1))%998244353);
return combinations.get(""+n+"C"+r);
}
}
|
Java
|
["5\n\n2\n\n4\n\n6\n\n8\n\n60"]
|
2 seconds
|
["1 0 1\n3 2 1\n12 7 1\n42 27 1\n341102826 248150916 1"]
|
NoteIn the first test case, Alex wins if he receives the card $$$2$$$ (he plays it, and Boris cannot respond). If Alex receives the card $$$1$$$, the game ends in a draw.In the second test case: Alex wins if he receives the cards $$$[3, 4]$$$, $$$[2, 4]$$$ or $$$[1, 4]$$$; Boris wins if Alex receives the cards $$$[1, 2]$$$ or $$$[1, 3]$$$; the game ends in a draw if Alex receives the cards $$$[2, 3]$$$.
|
Java 11
|
standard input
|
[
"combinatorics",
"constructive algorithms",
"dp",
"games"
] |
63fc2fb08d248f8ccdb3f5364c96dac1
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 30$$$) — the number of test cases. Then, $$$t$$$ lines follow. The $$$i$$$-th line contains one even integer $$$n$$$ ($$$2 \le n \le 60$$$).
| 1,500
|
For each test case, print three integers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. Since the answers can be large, print them modulo $$$998244353$$$.
|
standard output
| |
PASSED
|
22af247912e1737f9cad21ccfc5e4513
|
train_109.jsonl
|
1664462100
|
Consider a game with $$$n$$$ cards ($$$n$$$ is even). Each card has a number written on it, between $$$1$$$ and $$$n$$$. All numbers on the cards are different. We say that a card with number $$$x$$$ is stronger than a card with number $$$y$$$ if $$$x > y$$$.Two players, Alex and Boris, play this game. In the beginning, each of them receives exactly $$$\frac{n}{2}$$$ cards, so each card belongs to exactly one player. Then, they take turns. Alex goes first, then Boris, then Alex again, and so on.On a player's turn, he must play exactly one of his cards. Then, if the opponent doesn't have any cards stronger than the card played, the opponent loses, and the game ends. Otherwise, the opponent has to play a stronger card (exactly one card as well). These two cards are removed from the game, and the turn ends. If there are no cards left, the game ends in a draw; otherwise it's the opponent's turn.Consider all possible ways to distribute the cards between two players, so that each of them receives exactly half of the cards. You have to calculate three numbers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. You may assume that both players play optimally (i. e. if a player can win no matter how his opponent plays, he wins). Two ways to distribute the cards are different if there is at least one card such that, in one of these ways, it is given to Alex, and in the other way, it is given to Boris.For example, suppose $$$n = 4$$$, Alex receives the cards $$$[2, 3]$$$, and Boris receives the cards $$$[1, 4]$$$. Then the game may go as follows: if Alex plays the card $$$2$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$3$$$. So, the game ends in a draw; if Alex plays the card $$$3$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$2$$$. So, the game ends in a draw. So, in this case, the game ends in a draw.
|
512 megabytes
|
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.PrintWriter;
import java.util.Locale;
import java.util.Scanner;
public class Solution {
private static final int MOD = 998_244_353;
private static int[][] bin = new int[61][61];
static {
for (int i = 0; i < bin.length; i++) bin[i][0] = 1;
for (int k = 1; k < bin.length; k++) {
for (int ni = 1; ni < bin.length; ni++) {
bin[ni][k] = (bin[ni - 1][k - 1] + bin[ni - 1][k]) % MOD;
}
}
}
public static void main(String[] args) throws Exception {
try (BufferedInputStream in = new BufferedInputStream(System.in);
PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out))) {
Scanner sc = new Scanner(in).useLocale(Locale.US);
int T = sc.nextInt();
for (int t = 0; t < T; t++) {
int n = sc.nextInt();
int[] ret = solve(n);
out.println(String.format("%s %s %s", ret[0], ret[1], ret[2]));
}
}
}
private static int[] solve(int n) {
if (n == 2) return new int[]{1, 0, 1};
int[] ret = new int[3];
ret[0] += bin[n-1][n/2-1];
ret[0] %= MOD;
ret[1] += bin[n-2][n/2-2];
ret[1] %= MOD;
int[] sub = solve(n - 2);
ret[1] += sub[0];
ret[1] %= MOD;
ret[0] += sub[1];
ret[0] %= MOD;
ret[2] += sub[2];
ret[2] %= MOD;
return ret;
}
}
|
Java
|
["5\n\n2\n\n4\n\n6\n\n8\n\n60"]
|
2 seconds
|
["1 0 1\n3 2 1\n12 7 1\n42 27 1\n341102826 248150916 1"]
|
NoteIn the first test case, Alex wins if he receives the card $$$2$$$ (he plays it, and Boris cannot respond). If Alex receives the card $$$1$$$, the game ends in a draw.In the second test case: Alex wins if he receives the cards $$$[3, 4]$$$, $$$[2, 4]$$$ or $$$[1, 4]$$$; Boris wins if Alex receives the cards $$$[1, 2]$$$ or $$$[1, 3]$$$; the game ends in a draw if Alex receives the cards $$$[2, 3]$$$.
|
Java 11
|
standard input
|
[
"combinatorics",
"constructive algorithms",
"dp",
"games"
] |
63fc2fb08d248f8ccdb3f5364c96dac1
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 30$$$) — the number of test cases. Then, $$$t$$$ lines follow. The $$$i$$$-th line contains one even integer $$$n$$$ ($$$2 \le n \le 60$$$).
| 1,500
|
For each test case, print three integers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. Since the answers can be large, print them modulo $$$998244353$$$.
|
standard output
| |
PASSED
|
845d7ad6282a05ed61dc7ce4526cf3af
|
train_109.jsonl
|
1664462100
|
Consider a game with $$$n$$$ cards ($$$n$$$ is even). Each card has a number written on it, between $$$1$$$ and $$$n$$$. All numbers on the cards are different. We say that a card with number $$$x$$$ is stronger than a card with number $$$y$$$ if $$$x > y$$$.Two players, Alex and Boris, play this game. In the beginning, each of them receives exactly $$$\frac{n}{2}$$$ cards, so each card belongs to exactly one player. Then, they take turns. Alex goes first, then Boris, then Alex again, and so on.On a player's turn, he must play exactly one of his cards. Then, if the opponent doesn't have any cards stronger than the card played, the opponent loses, and the game ends. Otherwise, the opponent has to play a stronger card (exactly one card as well). These two cards are removed from the game, and the turn ends. If there are no cards left, the game ends in a draw; otherwise it's the opponent's turn.Consider all possible ways to distribute the cards between two players, so that each of them receives exactly half of the cards. You have to calculate three numbers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. You may assume that both players play optimally (i. e. if a player can win no matter how his opponent plays, he wins). Two ways to distribute the cards are different if there is at least one card such that, in one of these ways, it is given to Alex, and in the other way, it is given to Boris.For example, suppose $$$n = 4$$$, Alex receives the cards $$$[2, 3]$$$, and Boris receives the cards $$$[1, 4]$$$. Then the game may go as follows: if Alex plays the card $$$2$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$3$$$. So, the game ends in a draw; if Alex plays the card $$$3$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$2$$$. So, the game ends in a draw. So, in this case, the game ends in a draw.
|
512 megabytes
|
import java.util.*;
import java.util.Map.Entry;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.lang.reflect.Array;
import java.math.*;
public class Simple{
public static class Node{
int v;
int val;
public Node(int v,int val){
this.val = val;
this.v = v;
}
}
static final Random random=new Random();
static class FastReader{
BufferedReader br;
StringTokenizer st;
public FastReader(){
br=new BufferedReader(new InputStreamReader(System.in));
}
String next(){
while(st==null || !st.hasMoreTokens()){
try {
st=new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt(){
return Integer.parseInt(next());
}
long nextLong(){
return Long.parseLong(next());
}
double nextDouble(){
return Double.parseDouble(next());
}
String nextLine(){
String str="";
try {
str=br.readLine().trim();
} catch (Exception e) {
e.printStackTrace();
}
return str;
}
}
static class FastWriter {
private final BufferedWriter bw;
public FastWriter() {
this.bw = new BufferedWriter(new OutputStreamWriter(System.out));
}
public void print(Object object) throws IOException {
bw.append("" + object);
}
public void println(Object object) throws IOException {
print(object);
bw.append("\n");
}
public void close() throws IOException {
bw.close();
}
}
static void ruffleSort(int[] a) {
int n=a.length;
for (int i=0; i<n; i++) {
int oi=random.nextInt(n), temp=a[oi];
a[oi]=a[i]; a[i]=temp;
}
Arrays.sort(a);
}
static void ruffleSort(long[] a) {
int n=a.length;
for (int i=0; i<n; i++) {
long oi=random.nextInt(n), temp=a[(int)oi];
a[(int)oi]=a[i]; a[i]=temp;
}
Arrays.sort(a);
}
public static class Pair implements Comparable<Pair>{
int x;
int y;
public Pair(int x,int y){
this.x = x;
this.y = y;
}
public int compareTo(Pair other){
return this.y - other.y;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + x;
result = prime * result + y;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Pair other = (Pair) obj;
if (x != other.x)
return false;
if (y != other.y)
return false;
return true;
}
// public boolean equals(Pair other){
// if(this.x == other.x && this.y == other.y)return true;
// return false;
// }
// public int hashCode(){
// return 31*x + y;
// }
// @Override
// public int compareTo(Simple.Pair o) {
// // TODO Auto-generated method stub
// return 0;
// }
}
static long power(long x, long y, long p)
{
// Initialize result
long res = 1;
// Update x if it is more than or
// equal to p
x = x % p;
while (y > 0) {
// If y is odd, multiply x
// with result
if (y % 2 == 1)
res = (res * x) % p;
// y must be even now
y = y >> 1; // y = y/2
x = (x * x) % p;
}
return res;
}
// Returns n^(-1) mod p
static long modInverse(long n, long p)
{
return power(n, p - 2, p);
}
static long[] fac = new long[100000 + 1];
// Returns nCr % p using Fermat's
// little theorem.
static long nCrModPFermat(long n, long r,
long p)
{
if (n<r)
return 0;
// Base case
if (r == 0)
return 1;
// Fill factorial array so that we
// can find all factorial of r, n
// and n-r
return (fac[(int)n] * modInverse(fac[(int)r], p)
% p * modInverse(fac[(int)n - (int)r], p)
% p)
% p;
}
static int nCrModp(int n, int r, int p)
{
if (r > n - r)
r = n - r;
// The array C is going to store last
// row of pascal triangle at the end.
// And last entry of last row is nCr
int C[] = new int[r + 1];
C[0] = 1; // Top row of Pascal Triangle
// One by constructs remaining rows of Pascal
// Triangle from top to bottom
for (int i = 1; i <= n; i++) {
// Fill entries of current row using previous
// row values
for (int j = Math.min(i, r); j > 0; j--)
// nCj = (n-1)Cj + (n-1)C(j-1);
C[j] = (C[j] + C[j - 1]) % p;
}
return C[r];
}
static int gcd(int a, int b)
{
if(a == 0)return a;
if (b == 0)
return a;
return gcd(b, a % b);
}
static long gcd_long(long a, long b)
{
// Everything divides 0
if (a == 0)
return b;
if (b == 0)
return a;
// base case
if (a == b)
return a;
// a is greater
if (a > b)
return gcd_long(a%b, b);
return gcd_long(a, b%a);
}
public static class DSU{
int n;
int par[];
int rank[];
public DSU(int n){
this.n = n;
par = new int[n+1];
rank = new int[n+1];
for(int i=1;i<=n;i++){
par[i] = i ;
rank[i] = 0;
}
}
public int findPar(int node){
if(node==par[node]){
return node;
}
return par[node] = findPar(par[node]);//path compression
}
public void union(int u,int v){
u = findPar(u);
v = findPar(v);
if(rank[u]<rank[v]){
par[u] = v;
}
else if(rank[u]>rank[v]){
par[v] = u;
}
else{
par[v] = u;
rank[u]++;
}
}
}
static final int MAXN = 100001;
// stores smallest prime factor for every number
static int spf[] = new int[MAXN];
// Calculating SPF (Smallest Prime Factor) for every
// number till MAXN.
// Time Complexity : O(nloglogn)
static void sieve()
{
spf[1] = 1;
for (int i=2; i<MAXN; i++)
// marking smallest prime factor for every
// number to be itself.
spf[i] = i;
// separately marking spf for every even
// number as 2
for (int i=4; i<MAXN; i+=2)
spf[i] = 2;
for (int i=3; i*i<MAXN; i++)
{
// checking if i is prime
if (spf[i] == i)
{
// marking SPF for all numbers divisible by i
for (int j=i*i; j<MAXN; j+=i)
// marking spf[j] if it is not
// previously marked
if (spf[j]==j)
spf[j] = i;
}
}
}
// A O(log n) function returning primefactorization
// by dividing by smallest prime factor at every step
static Vector<Integer> getFactorization(int x)
{
Vector<Integer> ret = new Vector<>();
while (x != 1)
{
ret.add(spf[x]);
x = x / spf[x];
}
return ret;
}
// A utility function to get the
// middle index of given range.
static int getMid(int s, int e)
{
return s + (e - s) / 2;
}
/*
* A recursive function to get the sum
of values in given range of the array.
* The following are parameters
for this function.
*
* st -> Pointer to segment tree
* node -> Index of current node in
* the segment tree.
* ss & se -> Starting and ending indexes
* of the segment represented
* by current node, i.e., st[node]
* l & r -> Starting and ending indexes
* of range query
*/
static int MaxUtil(int[] st, int ss,
int se, int l,
int r, int node)
{
// If segment of this node is completely
// part of given range, then return
// the max of segment
if (l <= ss && r >= se)
return st[node];
// If segment of this node does not
// belong to given range
if (se < l || ss > r)
return -1;
// If segment of this node is partially
// the part of given range
int mid = getMid(ss, se);
return Math.max(
MaxUtil(st, ss, mid, l, r,
2 * node + 1),
MaxUtil(st, mid + 1, se, l, r,
2 * node + 2));
}
/*
* A recursive function to update the
nodes which have the given index in their
* range. The following are parameters
st, ss and se are same as defined above
* index -> index of the element to be updated.
*/
static void updateValue(int arr[], int[]
st, int ss,
int se, int index,
int value,
int node)
{
if (index < ss || index > se) {
System.out.println("Invalid Input");
return;
}
if (ss == se) {
// update value in array and in
// segment tree
arr[index] = value;
st[node] = value;
}
else {
int mid = getMid(ss, se);
if (index >= ss && index <= mid)
updateValue(arr, st, ss, mid,
index, value,
2 * node + 1);
else
updateValue(arr, st, mid + 1, se, index,
value, 2 * node + 2);
st[node] = Math.max(st[2 * node + 1],
st[2 * node + 2]);
}
return;
}
// Return max of elements in range from
// index l (query start) to r (query end).
static int getMax(int[] st, int n, int l, int r)
{
// Check for erroneous input values
if (l < 0 || r > n - 1 || l > r) {
System.out.printf("Invalid Input\n");
return -1;
}
return MaxUtil(st, 0, n - 1, l, r, 0);
}
// A recursive function that constructs Segment
// Tree for array[ss..se]. si is index of
// current node in segment tree st
static int constructSTUtil(int arr[],
int ss, int se,
int[] st, int si)
{
// If there is one element in array, store
// it in current node of segment tree and return
if (ss == se) {
st[si] = arr[ss];
return arr[ss];
}
// If there are more than one elements, then
// recur for left and right subtrees and
// store the max of values in this node
int mid = getMid(ss, se);
st[si] = Math.max(
constructSTUtil(arr, ss, mid,
st, si * 2 + 1),
constructSTUtil(arr, mid + 1,
se, st,
si * 2 + 2));
return st[si];
}
/*
* Function to construct segment tree from
given array. This function allocates
* memory for segment tree.
*/
static int[] constructST(int arr[], int n)
{
// Height of segment tree
int x = (int)Math.ceil(Math.log(n) / Math.log(2));
// Maximum size of segment tree
int max_size = 2 * (int)Math.pow(2, x) - 1;
// Allocate memory
int[] st = new int[max_size];
// Fill the allocated memory st
constructSTUtil(arr, 0, n - 1, st, 0);
// Return the constructed segment tree
return st;
}
public static void main(String args[]){
try {
FastReader s=new FastReader();
// FastWriter out = new FastWriter();
// Scanner s = new Scanner(System.in);
int testCases= s.nextInt();
long dp1[] = new long[61];
long dp2[] = new long[61];
dp1[2] = 1;
dp2[2] = 0;
long mod = 998244353 ;
for(int i = 4; i <= 60; i+=2) {
// game ends in first move of alex
long firstmove = nCrModp(i - 1, i /2 , (int)mod);
dp1[i] = (dp2[i - 2] + firstmove)%mod;
//game end in second move by bob
long secondMove = nCrModp(i - 2, i/2, (int)mod);
dp2[i] = (dp1[i - 2] + secondMove)%mod;
// System.out.println(dp1[i] +" "+dp2[i]+" "+firstmove +" "+secondMove);
}
for(int t = 1; t <= testCases; t++){
int n = s.nextInt();
System.out.println(dp1[n] +" "+dp2[n] +" "+ 1);
}
}
catch (Exception e) {
System.out.println(e.toString());
// System.ouintt.println("Eh");
return;
}
}
}
/*
1 3 4 5 2
2 4 3 5 1
*/
|
Java
|
["5\n\n2\n\n4\n\n6\n\n8\n\n60"]
|
2 seconds
|
["1 0 1\n3 2 1\n12 7 1\n42 27 1\n341102826 248150916 1"]
|
NoteIn the first test case, Alex wins if he receives the card $$$2$$$ (he plays it, and Boris cannot respond). If Alex receives the card $$$1$$$, the game ends in a draw.In the second test case: Alex wins if he receives the cards $$$[3, 4]$$$, $$$[2, 4]$$$ or $$$[1, 4]$$$; Boris wins if Alex receives the cards $$$[1, 2]$$$ or $$$[1, 3]$$$; the game ends in a draw if Alex receives the cards $$$[2, 3]$$$.
|
Java 11
|
standard input
|
[
"combinatorics",
"constructive algorithms",
"dp",
"games"
] |
63fc2fb08d248f8ccdb3f5364c96dac1
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 30$$$) — the number of test cases. Then, $$$t$$$ lines follow. The $$$i$$$-th line contains one even integer $$$n$$$ ($$$2 \le n \le 60$$$).
| 1,500
|
For each test case, print three integers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. Since the answers can be large, print them modulo $$$998244353$$$.
|
standard output
| |
PASSED
|
3b7e851a5017596de61c213e8aa7c03a
|
train_109.jsonl
|
1664462100
|
Consider a game with $$$n$$$ cards ($$$n$$$ is even). Each card has a number written on it, between $$$1$$$ and $$$n$$$. All numbers on the cards are different. We say that a card with number $$$x$$$ is stronger than a card with number $$$y$$$ if $$$x > y$$$.Two players, Alex and Boris, play this game. In the beginning, each of them receives exactly $$$\frac{n}{2}$$$ cards, so each card belongs to exactly one player. Then, they take turns. Alex goes first, then Boris, then Alex again, and so on.On a player's turn, he must play exactly one of his cards. Then, if the opponent doesn't have any cards stronger than the card played, the opponent loses, and the game ends. Otherwise, the opponent has to play a stronger card (exactly one card as well). These two cards are removed from the game, and the turn ends. If there are no cards left, the game ends in a draw; otherwise it's the opponent's turn.Consider all possible ways to distribute the cards between two players, so that each of them receives exactly half of the cards. You have to calculate three numbers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. You may assume that both players play optimally (i. e. if a player can win no matter how his opponent plays, he wins). Two ways to distribute the cards are different if there is at least one card such that, in one of these ways, it is given to Alex, and in the other way, it is given to Boris.For example, suppose $$$n = 4$$$, Alex receives the cards $$$[2, 3]$$$, and Boris receives the cards $$$[1, 4]$$$. Then the game may go as follows: if Alex plays the card $$$2$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$3$$$. So, the game ends in a draw; if Alex plays the card $$$3$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$2$$$. So, the game ends in a draw. So, in this case, the game ends in a draw.
|
512 megabytes
|
import java.io.*;
import java.util.*;
public class CodeForces {
class FastReader{
BufferedReader br;
StringTokenizer st;
FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
try{
while(st == null || !st.hasMoreElements()){
st = new StringTokenizer(br.readLine());
}
}
catch (IOException e){
e.printStackTrace();
}
return st.nextToken();
}
String nextLine()
{
String str = "";
try {
if(st!=null && st.hasMoreTokens()){
str = st.nextToken("\n");
}
else{
str = br.readLine();
}
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
int nextInt(){
return Integer.parseInt(next());
}
long nextLong(){return Long.parseLong(next()); }
double nextDouble(){return Double.parseDouble(next()); }
int[] readIntArray(int size){
int[] arr = new int[size];
for(int i=0;i<size;i++){
arr[i] = nextInt();
}
return arr;
}
long[] readLongArray(int size){
long[] arr = new long[size];
for(int i=0;i<size;i++){
arr[i] = nextLong();
}
return arr;
}
}
class Utilities {
int mod = (int)1e9 + 7;
long[] fact;
public Utilities(){}
public Utilities(int mod){
this.mod = mod;
}
public long add(long a,long b){
return ((a%mod)+(b%mod))%mod;
}
public long mul(long a,long b){
long res = a%mod;
res *= (b%mod);
return (int)(res%mod);
}
public long pow_mod(long a,long b){
long res = 1;
while(b>0){
if((b&1)!=0){
res = mul(res,a);
}
a = mul(a,a);
b>>=1;
}
return res;
}
public long div(long a,long b){
long inv = pow_mod(b,mod - 2);
return mul(a,inv);
}
public int max(int... nums){
int res = nums[0];
for(int i=1;i<nums.length;i++){
res = Math.max(res,nums[i]);
}
return res;
}
public long max(long... nums){
long res = nums[0];
for(int i=1;i<nums.length;i++){
res = Math.max(res,nums[i]);
}
return res;
}
public int min(int... nums){
int res = nums[0];
for(int i=1;i<nums.length;i++){
res = Math.min(res,nums[i]);
}
return res;
}
public long min(long... nums){
long res = nums[0];
for(int i=1;i<nums.length;i++){
res = Math.min(res,nums[i]);
}
return res;
}
public int gcd(int a,int b){
if(a<b){
int temp = a;
a = b;
b = temp;
}
if(b==0)
return a;
return gcd(b,a%b);
}
public void genFact(int limit){
fact = new long[limit+1];
fact[0]=1;
for(int i=1;i<=limit;i++){
fact[i] = mul(i,fact[i-1]);
}
}
public long ncr(int n,int r){
if(r>n)return 0;
long num = fact[n],den = mul(fact[r],fact[n-r]);
return div(num,den);
}
}
public void solve() throws IOException{
FastReader reader = new FastReader();
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
Utilities util = new Utilities(998244353);
int t = reader.nextInt();
long[][] dp = new long[61][2];
dp[2] = new long[]{1,0};
dp[4] = new long[]{3,2};
util.genFact(60);
for(int i=6;i<61;i+=2){
dp[i][0] = util.add(util.ncr(i-1,i/2),dp[i-2][1]);
dp[i][1] = util.add(util.ncr(i-2,i/2),dp[i-2][0]);
}
while(t-->0){
int n = reader.nextInt();
bw.write(dp[n][0] + " " + dp[n][1] + " 1\n");
}
bw.flush();
}
public static void main(String[] args){
CodeForces solver = new CodeForces();
try{
solver.solve();
}
catch (IOException e){
e.printStackTrace();
}
}
}
|
Java
|
["5\n\n2\n\n4\n\n6\n\n8\n\n60"]
|
2 seconds
|
["1 0 1\n3 2 1\n12 7 1\n42 27 1\n341102826 248150916 1"]
|
NoteIn the first test case, Alex wins if he receives the card $$$2$$$ (he plays it, and Boris cannot respond). If Alex receives the card $$$1$$$, the game ends in a draw.In the second test case: Alex wins if he receives the cards $$$[3, 4]$$$, $$$[2, 4]$$$ or $$$[1, 4]$$$; Boris wins if Alex receives the cards $$$[1, 2]$$$ or $$$[1, 3]$$$; the game ends in a draw if Alex receives the cards $$$[2, 3]$$$.
|
Java 11
|
standard input
|
[
"combinatorics",
"constructive algorithms",
"dp",
"games"
] |
63fc2fb08d248f8ccdb3f5364c96dac1
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 30$$$) — the number of test cases. Then, $$$t$$$ lines follow. The $$$i$$$-th line contains one even integer $$$n$$$ ($$$2 \le n \le 60$$$).
| 1,500
|
For each test case, print three integers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. Since the answers can be large, print them modulo $$$998244353$$$.
|
standard output
| |
PASSED
|
317b3a288b1058a7f1a80bc8c5ef792a
|
train_109.jsonl
|
1664462100
|
Consider a game with $$$n$$$ cards ($$$n$$$ is even). Each card has a number written on it, between $$$1$$$ and $$$n$$$. All numbers on the cards are different. We say that a card with number $$$x$$$ is stronger than a card with number $$$y$$$ if $$$x > y$$$.Two players, Alex and Boris, play this game. In the beginning, each of them receives exactly $$$\frac{n}{2}$$$ cards, so each card belongs to exactly one player. Then, they take turns. Alex goes first, then Boris, then Alex again, and so on.On a player's turn, he must play exactly one of his cards. Then, if the opponent doesn't have any cards stronger than the card played, the opponent loses, and the game ends. Otherwise, the opponent has to play a stronger card (exactly one card as well). These two cards are removed from the game, and the turn ends. If there are no cards left, the game ends in a draw; otherwise it's the opponent's turn.Consider all possible ways to distribute the cards between two players, so that each of them receives exactly half of the cards. You have to calculate three numbers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. You may assume that both players play optimally (i. e. if a player can win no matter how his opponent plays, he wins). Two ways to distribute the cards are different if there is at least one card such that, in one of these ways, it is given to Alex, and in the other way, it is given to Boris.For example, suppose $$$n = 4$$$, Alex receives the cards $$$[2, 3]$$$, and Boris receives the cards $$$[1, 4]$$$. Then the game may go as follows: if Alex plays the card $$$2$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$3$$$. So, the game ends in a draw; if Alex plays the card $$$3$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$2$$$. So, the game ends in a draw. So, in this case, the game ends in a draw.
|
512 megabytes
|
import java.io.*;
import java.util.*;
public class Codeforces
{
public static void main(String args[])throws Exception
{
BufferedReader bu=new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb=new StringBuilder();
int t=Integer.parseInt(bu.readLine()),i,N=60;
f=new long[N+1];
f[0]=1;
for(i=1;i<=N;i++) f[i]=f[i-1]*i%M;
while(t-->0)
{
int n=Integer.parseInt(bu.readLine());
long alex=0,tot=ncr(n,n/2),draw=1;
for(i=n;i-4>=0;i-=4)
{
alex=(alex+ncr(i-1,n/2-((n-i)/2)))%M; //place an A in the end
alex=(alex+ncr(i-4,n/2-((n-i)/2)-1))%M; //AAA in beginning
}
if(i>0) alex=(alex+1)%M;
long bob=(tot-alex-draw+M+M)%M;
sb.append(alex+" "+bob+" "+draw+"\n");
}
System.out.print(sb);
}
static long f[],M=998244353;
static long ncr(int n,int r)
{
if(r>n) return 0;
return f[n]*power(f[r],M-2)%M*power(f[n-r],M-2)%M;
}
static long power(long a,long b)
{
long res=1;
while(b!=0)
{
if(b%2==1) res=res*a%M;
a=a*a%M;
b>>=1;
}
return res;
}
}
|
Java
|
["5\n\n2\n\n4\n\n6\n\n8\n\n60"]
|
2 seconds
|
["1 0 1\n3 2 1\n12 7 1\n42 27 1\n341102826 248150916 1"]
|
NoteIn the first test case, Alex wins if he receives the card $$$2$$$ (he plays it, and Boris cannot respond). If Alex receives the card $$$1$$$, the game ends in a draw.In the second test case: Alex wins if he receives the cards $$$[3, 4]$$$, $$$[2, 4]$$$ or $$$[1, 4]$$$; Boris wins if Alex receives the cards $$$[1, 2]$$$ or $$$[1, 3]$$$; the game ends in a draw if Alex receives the cards $$$[2, 3]$$$.
|
Java 11
|
standard input
|
[
"combinatorics",
"constructive algorithms",
"dp",
"games"
] |
63fc2fb08d248f8ccdb3f5364c96dac1
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 30$$$) — the number of test cases. Then, $$$t$$$ lines follow. The $$$i$$$-th line contains one even integer $$$n$$$ ($$$2 \le n \le 60$$$).
| 1,500
|
For each test case, print three integers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. Since the answers can be large, print them modulo $$$998244353$$$.
|
standard output
| |
PASSED
|
7e9f0ce68611c1f46c78931491592b27
|
train_109.jsonl
|
1664462100
|
Consider a game with $$$n$$$ cards ($$$n$$$ is even). Each card has a number written on it, between $$$1$$$ and $$$n$$$. All numbers on the cards are different. We say that a card with number $$$x$$$ is stronger than a card with number $$$y$$$ if $$$x > y$$$.Two players, Alex and Boris, play this game. In the beginning, each of them receives exactly $$$\frac{n}{2}$$$ cards, so each card belongs to exactly one player. Then, they take turns. Alex goes first, then Boris, then Alex again, and so on.On a player's turn, he must play exactly one of his cards. Then, if the opponent doesn't have any cards stronger than the card played, the opponent loses, and the game ends. Otherwise, the opponent has to play a stronger card (exactly one card as well). These two cards are removed from the game, and the turn ends. If there are no cards left, the game ends in a draw; otherwise it's the opponent's turn.Consider all possible ways to distribute the cards between two players, so that each of them receives exactly half of the cards. You have to calculate three numbers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. You may assume that both players play optimally (i. e. if a player can win no matter how his opponent plays, he wins). Two ways to distribute the cards are different if there is at least one card such that, in one of these ways, it is given to Alex, and in the other way, it is given to Boris.For example, suppose $$$n = 4$$$, Alex receives the cards $$$[2, 3]$$$, and Boris receives the cards $$$[1, 4]$$$. Then the game may go as follows: if Alex plays the card $$$2$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$3$$$. So, the game ends in a draw; if Alex plays the card $$$3$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$2$$$. So, the game ends in a draw. So, in this case, the game ends in a draw.
|
512 megabytes
|
import com.sun.source.tree.Tree;
import java.io.*;
import java.util.*;
public class Main {
// private static int[][] dirs = {{-1,-1}, {1, 1}, {-1, 1}, {1, -1}};
private static int[][] dirs = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
private static long inf = (long) 1e13;
private static long div = 998_244_353L;
// private static long div = ((long)1e9) + 7;
private static void swap(int[] arr, int i, int j) {
int tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
private static void swap(char[] arr, int i, int j) {
char tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
private static void perms(int offset, List<List<Integer>> perms, int[] arr) {
if (offset == arr.length) {
List<Integer> perm = new ArrayList<>();
for (int j : arr) perm.add(j);
perms.add(perm);
return;
}
for(int i = offset; i < arr.length; ++i) {
swap(arr, offset, i);
perms(offset + 1, perms, arr);
swap(arr, offset, i);
}
}
private static List<List<Integer>> perms(int[] arr) {
List<List<Integer>> perms = new ArrayList<>();
perms(0, perms, arr);
return perms;
}
private static long pow(long num, long pow, long div) {
if (pow == 0) return 1L;
long res = pow(num, pow/2, div);
long ret = 1;
if (pow % 2 != 0) ret = num % div;
ret = (ret * res) % div;
ret = (ret * res) % div;
return ret;
}
private static boolean[] visited = new boolean[61];
private static long[][] cache = new long[61][3];
private static long[] solve(int n, long[] fact, long[] ifact, boolean[] visited, long[][] cache) {
if (n == 2) {
return new long[] {1, 0, 1};
}
if (visited[n]) {
return cache[n];
}
visited[n] = true;
int h = n / 2;
long wins = ( (fact[n - 1] * ifact[h - 1]) % div * ifact[n - h] ) % div;
long losses = ( (fact[n - 2] * ifact[h - 2]) % div * ifact[n - h] )% div;
long[] prev = solve(n - 2, fact, ifact, visited, cache);
wins = (wins + prev[1]) % div;
losses = (losses + prev[0]) % div;
cache[n][0] = wins;
cache[n][1] = losses;
cache[n][2] = prev[2];
return cache[n];
}
public static void main(String[] commands) throws Exception {
BufferedReader rd = new BufferedReader(new InputStreamReader(System.in));
String[] parts = rd.readLine().split(" ");
int Q = Integer.parseInt(parts[0]);
long[] fact = new long[61];
fact[0] = 1;
for(int i = 1; i <= 60; ++i) {
fact[i] = (fact[i - 1] * i) % div;
}
long[] ifact = new long[61];
ifact[0] = 1L;
for(int i = 1;i <= 60; ++i) {
ifact[i] = pow(fact[i], div - 2, div);
}
BufferedOutputStream bfo = new BufferedOutputStream(System.out);
for(int q = 0;q < Q; ++q) {
int L = Integer.parseInt(rd.readLine());
long[] ans = solve(L, fact, ifact, visited, cache);
bfo.write((ans[0] + " " + ans[1] + " " + ans[2] + "\n").getBytes());
}
bfo.flush();
}
}
|
Java
|
["5\n\n2\n\n4\n\n6\n\n8\n\n60"]
|
2 seconds
|
["1 0 1\n3 2 1\n12 7 1\n42 27 1\n341102826 248150916 1"]
|
NoteIn the first test case, Alex wins if he receives the card $$$2$$$ (he plays it, and Boris cannot respond). If Alex receives the card $$$1$$$, the game ends in a draw.In the second test case: Alex wins if he receives the cards $$$[3, 4]$$$, $$$[2, 4]$$$ or $$$[1, 4]$$$; Boris wins if Alex receives the cards $$$[1, 2]$$$ or $$$[1, 3]$$$; the game ends in a draw if Alex receives the cards $$$[2, 3]$$$.
|
Java 11
|
standard input
|
[
"combinatorics",
"constructive algorithms",
"dp",
"games"
] |
63fc2fb08d248f8ccdb3f5364c96dac1
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 30$$$) — the number of test cases. Then, $$$t$$$ lines follow. The $$$i$$$-th line contains one even integer $$$n$$$ ($$$2 \le n \le 60$$$).
| 1,500
|
For each test case, print three integers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. Since the answers can be large, print them modulo $$$998244353$$$.
|
standard output
| |
PASSED
|
0368ded85c3ff29d026c8ebc2a01c67b
|
train_109.jsonl
|
1664462100
|
Consider a game with $$$n$$$ cards ($$$n$$$ is even). Each card has a number written on it, between $$$1$$$ and $$$n$$$. All numbers on the cards are different. We say that a card with number $$$x$$$ is stronger than a card with number $$$y$$$ if $$$x > y$$$.Two players, Alex and Boris, play this game. In the beginning, each of them receives exactly $$$\frac{n}{2}$$$ cards, so each card belongs to exactly one player. Then, they take turns. Alex goes first, then Boris, then Alex again, and so on.On a player's turn, he must play exactly one of his cards. Then, if the opponent doesn't have any cards stronger than the card played, the opponent loses, and the game ends. Otherwise, the opponent has to play a stronger card (exactly one card as well). These two cards are removed from the game, and the turn ends. If there are no cards left, the game ends in a draw; otherwise it's the opponent's turn.Consider all possible ways to distribute the cards between two players, so that each of them receives exactly half of the cards. You have to calculate three numbers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. You may assume that both players play optimally (i. e. if a player can win no matter how his opponent plays, he wins). Two ways to distribute the cards are different if there is at least one card such that, in one of these ways, it is given to Alex, and in the other way, it is given to Boris.For example, suppose $$$n = 4$$$, Alex receives the cards $$$[2, 3]$$$, and Boris receives the cards $$$[1, 4]$$$. Then the game may go as follows: if Alex plays the card $$$2$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$3$$$. So, the game ends in a draw; if Alex plays the card $$$3$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$2$$$. So, the game ends in a draw. So, in this case, the game ends in a draw.
|
512 megabytes
|
/**
* @author Nitin Bhakar
*
*/
import java.io.*;
import java.util.*;
public class Codeforces{
static long mod = 1000000007L;
static long m = 998244353;
static int ninf = Integer.MIN_VALUE;
static int inf = Integer.MAX_VALUE;
static void swap(int[] a,int i,int j) {int temp = a[i]; a[i] = a[j]; a[j] = temp;}
static void priArr(int[] a) {for(int i=0;i<a.length;i++) out.print(a[i] + " ");out.println();}
static long gcd(long a,long b){if(b==0) return a; return gcd(b,a%b);}
static int gcd(int a,int b){ if(b==0) return a;return gcd(b,a%b);}
static void sieve(boolean[] a, int n) {a[1] = false; for(int i=2;i*i<=n;i++) { if(a[i]) { for(int j=i*i;j<=n;j+=i) a[j] = false;}}}
static int getRanNum(int lo, int hi) {return (int)Math.random()*(hi-lo+1)+lo;}
private static void sort(int[] arr) {List<Integer> list = new ArrayList<>();for (int i=0; i<arr.length; i++)list.add(arr[i]);Collections.sort(list);for (int i = 0; i < arr.length; i++)arr[i] = list.get(i);}
private static long power(int n, int m) {long res = 1;while(m>0) {if(m%2 != 0) {res *= n; m--;}n *=n;m/=2;}return res;}
static long modInv(long x,long mod) {return expo(x,mod-2,mod)%mod;}
static long expo(long x,long k,long mod) {long res = 1;while(k>0) {if(k%2 != 0) {res = (res*x)%mod;k--;}x = (x*x)%mod;k /= 2;}return res%mod;}
//map.put(a[i],map.getOrDefault(a[i],0)+1);
// map.putIfAbsent;
// StringBuilder ans = new StringBuilder();
static MyScanner sc = new MyScanner();
static int[][] dp;
//<----------------------------------------------WRITE HERE------------------------------------------->
static void solve(){
int n = sc.nextInt();
long[][] dp =new long[n+1][3];
_ncr_precompute(60);
dp[2][0] = 1;
dp[2][1] = 0;
dp[2][2] = 1;
recurse(n,dp);
out.println(dp[n][0]+" "+dp[n][1]+" "+dp[n][2]);
}
static void recurse(int n, long[][] dp) {
if(n == 2) return;
recurse(n-2,dp);
dp[n][0] = (_ncr(n-1,n/2)+dp[n-2][1])%m;
dp[n][1] = (_ncr(n-2,n/2)+dp[n-2][0])%m;
dp[n][2] = dp[n-2][2];
}
public static long[] factorial , inverseFact;
public static void _ncr_precompute(int n){
factorial = new long[n+1];
inverseFact = new long[n+1];
factorial[0] = inverseFact[0] = 1;
for (int i = 1; i <=n; i++) {
factorial[i] = (factorial[i - 1] * i) % m;
inverseFact[i] = modInv(factorial[i], m);
}
}
public static int _ncr(int n,int r){
if(r > n)return 0;
return (int)(factorial[n] * inverseFact[r] % m * inverseFact[n - r] % m);
}
public static int _npr(int n,int r){
if(r > n)return 0;
return (int)(factorial[n] * inverseFact[n - r] % mod);
}
//<----------------------------------------------WRITE HERE------------------------------------------->
static class Pair implements Comparable<Pair> {
int v1;
int v2;
Pair(){}
Pair(int v,int f){
v1 = v;
v2 = f;
}
public int compareTo(Pair p){
if(this.v1 == p.v1) return this.v2-p.v2;
return this.v1-p.v1;
}
}
public static void main(String[] args) {
out = new PrintWriter(new BufferedOutputStream(System.out));
int t = sc.nextInt();
// int t= 1;
while(t-- >0){
solve();
}
// Stop writing your solution here. -------------------------------------
out.close();
}
//-----------PrintWriter for faster output---------------------------------
public static PrintWriter out;
//-----------MyScanner class for faster input----------
public static class MyScanner {
BufferedReader br;
StringTokenizer st;
public MyScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
public int[] readIntArray(int n){
int arr[] = new int[n];
for(int i = 0;i<n;i++){
arr[i] = Integer.parseInt(next());
}
return arr;
}
int[] reverse(int arr[]){
int n= arr.length;
int i = 0;
int j = n-1;
while(i<j){
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
j--;i++;
}
return arr;
}
long[] readLongArray(int n){
long arr[] = new long[n];
for(int i = 0;i<n;i++){
arr[i] = Long.parseLong(next());
}
return arr;
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
}
|
Java
|
["5\n\n2\n\n4\n\n6\n\n8\n\n60"]
|
2 seconds
|
["1 0 1\n3 2 1\n12 7 1\n42 27 1\n341102826 248150916 1"]
|
NoteIn the first test case, Alex wins if he receives the card $$$2$$$ (he plays it, and Boris cannot respond). If Alex receives the card $$$1$$$, the game ends in a draw.In the second test case: Alex wins if he receives the cards $$$[3, 4]$$$, $$$[2, 4]$$$ or $$$[1, 4]$$$; Boris wins if Alex receives the cards $$$[1, 2]$$$ or $$$[1, 3]$$$; the game ends in a draw if Alex receives the cards $$$[2, 3]$$$.
|
Java 11
|
standard input
|
[
"combinatorics",
"constructive algorithms",
"dp",
"games"
] |
63fc2fb08d248f8ccdb3f5364c96dac1
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 30$$$) — the number of test cases. Then, $$$t$$$ lines follow. The $$$i$$$-th line contains one even integer $$$n$$$ ($$$2 \le n \le 60$$$).
| 1,500
|
For each test case, print three integers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. Since the answers can be large, print them modulo $$$998244353$$$.
|
standard output
| |
PASSED
|
7d57e70c96a0e74c597c0145266e7b9d
|
train_109.jsonl
|
1664462100
|
Consider a game with $$$n$$$ cards ($$$n$$$ is even). Each card has a number written on it, between $$$1$$$ and $$$n$$$. All numbers on the cards are different. We say that a card with number $$$x$$$ is stronger than a card with number $$$y$$$ if $$$x > y$$$.Two players, Alex and Boris, play this game. In the beginning, each of them receives exactly $$$\frac{n}{2}$$$ cards, so each card belongs to exactly one player. Then, they take turns. Alex goes first, then Boris, then Alex again, and so on.On a player's turn, he must play exactly one of his cards. Then, if the opponent doesn't have any cards stronger than the card played, the opponent loses, and the game ends. Otherwise, the opponent has to play a stronger card (exactly one card as well). These two cards are removed from the game, and the turn ends. If there are no cards left, the game ends in a draw; otherwise it's the opponent's turn.Consider all possible ways to distribute the cards between two players, so that each of them receives exactly half of the cards. You have to calculate three numbers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. You may assume that both players play optimally (i. e. if a player can win no matter how his opponent plays, he wins). Two ways to distribute the cards are different if there is at least one card such that, in one of these ways, it is given to Alex, and in the other way, it is given to Boris.For example, suppose $$$n = 4$$$, Alex receives the cards $$$[2, 3]$$$, and Boris receives the cards $$$[1, 4]$$$. Then the game may go as follows: if Alex plays the card $$$2$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$3$$$. So, the game ends in a draw; if Alex plays the card $$$3$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$2$$$. So, the game ends in a draw. So, in this case, the game ends in a draw.
|
512 megabytes
|
import java.io.*;
import java.lang.*;
import java.lang.reflect.Array;
import java.util.*;
public class Main {
public static void main(String[] args) {
solve();
}
private static void solve() {
FastReader sc = new FastReader();
int t = sc.nextInt();
while (t-- > 0) {
// write any code from here//
int n = sc.nextInt();
final long MOD = 998244353;
long dp[][][] = new long[n / 2 + 1][n / 2 + 1][3];
// 0->alex //1->bob //2->draw
dp[0][0][2] = 1;
for (int i = 0; i <= n / 2; i++) {
for (int j = 0; j <= n / 2; j++) {
for (int k = 0; k <= 2; k++) {
int x = (i + j) % 4;
// BAABBAABBAAB...
if (x == 0 | x == 3) {
if (i + 1 <= n / 2) {
dp[i + 1][j][k == 2 ? 0 : k] += dp[i][j][k];
}
if (j + 1 <= n / 2) {
dp[i][j + 1][k] += dp[i][j][k];
}
}
if (x == 1 | x == 2) {
if (i + 1 <= n / 2) {
dp[i + 1][j][k] += dp[i][j][k];
}
if (j + 1 <= n / 2) {
dp[i][j + 1][k == 2 ? 1 : k] += dp[i][j][k];
}
}
}
}
}
for (int k = 0; k <= 2; k++)
System.out.print((dp[n / 2][n / 2][k]) % MOD + " ");
System.out.println();
}
}
static int findDepth(int cur, int p[][]) {
if (p[cur][1] != -1)
return p[cur][1];
return p[cur][1] = findDepth(p[cur][0], p) + 1;
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
if (st.hasMoreTokens()) {
str = st.nextToken("\n");
} else {
str = br.readLine();
}
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
}
|
Java
|
["5\n\n2\n\n4\n\n6\n\n8\n\n60"]
|
2 seconds
|
["1 0 1\n3 2 1\n12 7 1\n42 27 1\n341102826 248150916 1"]
|
NoteIn the first test case, Alex wins if he receives the card $$$2$$$ (he plays it, and Boris cannot respond). If Alex receives the card $$$1$$$, the game ends in a draw.In the second test case: Alex wins if he receives the cards $$$[3, 4]$$$, $$$[2, 4]$$$ or $$$[1, 4]$$$; Boris wins if Alex receives the cards $$$[1, 2]$$$ or $$$[1, 3]$$$; the game ends in a draw if Alex receives the cards $$$[2, 3]$$$.
|
Java 11
|
standard input
|
[
"combinatorics",
"constructive algorithms",
"dp",
"games"
] |
63fc2fb08d248f8ccdb3f5364c96dac1
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 30$$$) — the number of test cases. Then, $$$t$$$ lines follow. The $$$i$$$-th line contains one even integer $$$n$$$ ($$$2 \le n \le 60$$$).
| 1,500
|
For each test case, print three integers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. Since the answers can be large, print them modulo $$$998244353$$$.
|
standard output
| |
PASSED
|
af41afa725cf80705aafafc464e4bee6
|
train_109.jsonl
|
1664462100
|
Consider a game with $$$n$$$ cards ($$$n$$$ is even). Each card has a number written on it, between $$$1$$$ and $$$n$$$. All numbers on the cards are different. We say that a card with number $$$x$$$ is stronger than a card with number $$$y$$$ if $$$x > y$$$.Two players, Alex and Boris, play this game. In the beginning, each of them receives exactly $$$\frac{n}{2}$$$ cards, so each card belongs to exactly one player. Then, they take turns. Alex goes first, then Boris, then Alex again, and so on.On a player's turn, he must play exactly one of his cards. Then, if the opponent doesn't have any cards stronger than the card played, the opponent loses, and the game ends. Otherwise, the opponent has to play a stronger card (exactly one card as well). These two cards are removed from the game, and the turn ends. If there are no cards left, the game ends in a draw; otherwise it's the opponent's turn.Consider all possible ways to distribute the cards between two players, so that each of them receives exactly half of the cards. You have to calculate three numbers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. You may assume that both players play optimally (i. e. if a player can win no matter how his opponent plays, he wins). Two ways to distribute the cards are different if there is at least one card such that, in one of these ways, it is given to Alex, and in the other way, it is given to Boris.For example, suppose $$$n = 4$$$, Alex receives the cards $$$[2, 3]$$$, and Boris receives the cards $$$[1, 4]$$$. Then the game may go as follows: if Alex plays the card $$$2$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$3$$$. So, the game ends in a draw; if Alex plays the card $$$3$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$2$$$. So, the game ends in a draw. So, in this case, the game ends in a draw.
|
512 megabytes
|
import java.util.*;
import java.io.*;
import java.math.BigInteger;
public class Main {
static ContestScanner sc = new ContestScanner(System.in);
static ContestPrinter pw = new ContestPrinter(System.out);
static StringBuilder sb = new StringBuilder();
static long mod = 998244353;
static Combination comb = new Combination(61);
public static void main(String[] args) throws Exception {
int T = sc.nextInt();
for(int i = 0; i < T; i++)solve();
//solve();
pw.flush();
}
public static void solve() {
int n = sc.nextInt();
long ans = 0;
long ans2 = 0;
int num = n/2;
int num2 = n/2;
for(int i = 1; i < n; i++){
if(i % 4 <= 1){
ans += comb.comb(num+num2-1,num-1);
ans %= mod;
num2--;
}else{
ans2 += comb.comb(num+num2-1,num2-1);
ans2 %= mod;
num--;
}
}
pw.println(ans + " " + ans2 + " " + 1);
}
private static long rep2(long b, long n, long mod){
if(n == 0) return 1;
long bn = rep2(b,n/2,mod);
if(n % 2 == 0){
return (bn*bn)%mod;
}else{
return (bn*bn)%mod*b%mod;
}
}
static long modinv(long a, long mod) {
long x = 1, u = 0, s = a, t = mod;
long k = 0, tmp = 0;
while (t > 0) {
k = s / t;
s -= k * t;
{
tmp = s;
s = t;
t = tmp;
}
x -= k * u;
{
tmp = x;
x = u;
u = tmp;
}
}
x %= mod;
if (x < 0)
x += mod;
return x;
}
/**
* n 人を区別のない k 個以下のグループに分ける場合の数なんだそうな
* verified - https://judge.u-aizu.ac.jp/onlinejudge/review.jsp?rid=6293335#1
* Math.max(n,k)無いと動かないので注意
*/
static class Bell {
static long[] pre_sum, fact_inv, inv;
/*
* init_bell :前処理
* 計算量:O(n)
*/
public Bell(int SIZE) {
// 階乗の逆元を計算
fact_inv = new long[SIZE + 5];
inv = new long[SIZE + 5];
fact_inv[0] = fact_inv[1] = 1;
inv[1] = 1;
for (int i = 2; i < SIZE + 5; i++) {
inv[i] = mod - inv[(int) (mod % i)] * (mod / i) % mod;
fact_inv[i] = fact_inv[i - 1] * inv[i] % mod;
}
// 目的の値を前計算
pre_sum = new long[SIZE + 5];
pre_sum[0] = 1;
for (int i = 1; i < SIZE + 5; i++) {
if (i % 2 == 1) {
pre_sum[i] = (pre_sum[i - 1] + (mod - fact_inv[i])) % mod;
} else {
pre_sum[i] = (pre_sum[i - 1] + fact_inv[i]) % mod;
}
}
}
// べき乗
static long pow(long x, long n) {
long ret = 1;
while (n > 0) {
if ((n & 1) == 1)
ret = ret * x % mod;
x = x * x % mod;
n >>= 1;
}
return ret;
}
/*
* ベル数を求める(事前にinit_bell の実行が必要)
* 計算量:O(K log N)
*/
static long bell(int N, int K) {
long ret = 0;
for (int i = 0; i <= K; i++) {
ret += (pow(i, N) * fact_inv[i]) % mod * pre_sum[K - i] % mod;
ret %= mod;
}
return ret;
}
}
static class GeekInteger {
public static void save_sort(int[] array) {
shuffle(array);
Arrays.sort(array);
}
public static int[] shuffle(int[] array) {
int n = array.length;
Random random = new Random();
for (int i = 0, j; i < n; i++) {
j = i + random.nextInt(n - i);
int randomElement = array[j];
array[j] = array[i];
array[i] = randomElement;
}
return array;
}
public static void save_sort(long[] array) {
shuffle(array);
Arrays.sort(array);
}
public static long[] shuffle(long[] array) {
int n = array.length;
Random random = new Random();
for (int i = 0, j; i < n; i++) {
j = i + random.nextInt(n - i);
long randomElement = array[j];
array[j] = array[i];
array[i] = randomElement;
}
return array;
}
}
}
class Combination {
final static long mod = 998244353;
private static long[] fact, ifact;
public Combination(int n) {
fact = new long[n + 1];
ifact = new long[n + 1];
fact[0] = 1;
long ln = n;
for (long i = 1; i <= ln; ++i) {
int ii = (int) i;
fact[ii] = fact[ii - 1] % mod * i % mod;
}
ifact[n] = pow(fact[n], this.mod - 2);
for (int i = n; i >= 1; --i) {
int ii = (int) i;
ifact[ii - 1] = ifact[ii] % mod * i % mod;
}
}
public static long comb(int n, int k) {
if (k < 0 || k > n)
return 0;
return fact[n] % mod * ifact[k] % mod * ifact[n - k] % mod;
}
public static long perm(int n, int k) {
return comb(n, k) * fact[k] % mod;
}
public static long pow(long a, long b) {
long ret = 1;
long tmp = a;
while (b > 0) {
if ((b & 1) == 1) {
ret = (ret * tmp) % mod;
}
tmp = (tmp * tmp) % mod;
b = b >> 1;
}
return ret;
}
}
/**
* refercence : https://github.com/NASU41/AtCoderLibraryForJava/blob/master/ContestIO/ContestScanner.java
*/
class ContestScanner {
private final java.io.InputStream in;
private final byte[] buffer = new byte[1024];
private int ptr = 0;
private int buflen = 0;
private static final long LONG_MAX_TENTHS = 922337203685477580L;
private static final int LONG_MAX_LAST_DIGIT = 7;
private static final int LONG_MIN_LAST_DIGIT = 8;
public ContestScanner(java.io.InputStream in){
this.in = in;
}
public ContestScanner(java.io.File file) throws java.io.FileNotFoundException {
this(new java.io.BufferedInputStream(new java.io.FileInputStream(file)));
}
public ContestScanner(){
this(System.in);
}
private boolean hasNextByte() {
if (ptr < buflen) {
return true;
}else{
ptr = 0;
try {
buflen = in.read(buffer);
} catch (java.io.IOException e) {
e.printStackTrace();
}
if (buflen <= 0) {
return false;
}
}
return true;
}
private int readByte() {
if (hasNextByte()) return buffer[ptr++]; else return -1;
}
private static boolean isPrintableChar(int c) {
return 33 <= c && c <= 126;
}
public boolean hasNext() {
while(hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++;
return hasNextByte();
}
public String next() {
if (!hasNext()) throw new java.util.NoSuchElementException();
StringBuilder sb = new StringBuilder();
int b = readByte();
while(isPrintableChar(b)) {
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
public long nextLong() {
if (!hasNext()) throw new java.util.NoSuchElementException();
long n = 0;
boolean minus = false;
int b = readByte();
if (b == '-') {
minus = true;
b = readByte();
}
if (b < '0' || '9' < b) {
throw new NumberFormatException();
}
while (true) {
if ('0' <= b && b <= '9') {
int digit = b - '0';
if (n >= LONG_MAX_TENTHS) {
if (n == LONG_MAX_TENTHS) {
if (minus) {
if (digit <= LONG_MIN_LAST_DIGIT) {
n = -n * 10 - digit;
b = readByte();
if (!isPrintableChar(b)) {
return n;
} else if (b < '0' || '9' < b) {
throw new NumberFormatException(
String.format("%d%s... is not number", n, Character.toString(b))
);
}
}
} else {
if (digit <= LONG_MAX_LAST_DIGIT) {
n = n * 10 + digit;
b = readByte();
if (!isPrintableChar(b)) {
return n;
} else if (b < '0' || '9' < b) {
throw new NumberFormatException(
String.format("%d%s... is not number", n, Character.toString(b))
);
}
}
}
}
throw new ArithmeticException(
String.format("%s%d%d... overflows long.", minus ? "-" : "", n, digit)
);
}
n = n * 10 + digit;
}else if(b == -1 || !isPrintableChar(b)){
return minus ? -n : n;
}else{
throw new NumberFormatException();
}
b = readByte();
}
}
public int nextInt() {
long nl = nextLong();
if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE) throw new NumberFormatException();
return (int) nl;
}
public double nextDouble() {
return Double.parseDouble(next());
}
public long[] nextLongArray(int length){
long[] array = new long[length];
for(int i=0; i<length; i++) array[i] = this.nextLong();
return array;
}
public long[] nextLongArray(int length, java.util.function.LongUnaryOperator map){
long[] array = new long[length];
for(int i=0; i<length; i++) array[i] = map.applyAsLong(this.nextLong());
return array;
}
public int[] nextIntArray(int length){
int[] array = new int[length];
for(int i=0; i<length; i++) array[i] = this.nextInt();
return array;
}
public int[] nextIntArray(int length, java.util.function.IntUnaryOperator map){
int[] array = new int[length];
for(int i=0; i<length; i++) array[i] = map.applyAsInt(this.nextInt());
return array;
}
public double[] nextDoubleArray(int length){
double[] array = new double[length];
for(int i=0; i<length; i++) array[i] = this.nextDouble();
return array;
}
public double[] nextDoubleArray(int length, java.util.function.DoubleUnaryOperator map){
double[] array = new double[length];
for(int i=0; i<length; i++) array[i] = map.applyAsDouble(this.nextDouble());
return array;
}
public long[][] nextLongMatrix(int height, int width){
long[][] mat = new long[height][width];
for(int h=0; h<height; h++) for(int w=0; w<width; w++){
mat[h][w] = this.nextLong();
}
return mat;
}
public int[][] nextIntMatrix(int height, int width){
int[][] mat = new int[height][width];
for(int h=0; h<height; h++) for(int w=0; w<width; w++){
mat[h][w] = this.nextInt();
}
return mat;
}
public double[][] nextDoubleMatrix(int height, int width){
double[][] mat = new double[height][width];
for(int h=0; h<height; h++) for(int w=0; w<width; w++){
mat[h][w] = this.nextDouble();
}
return mat;
}
public char[][] nextCharMatrix(int height, int width){
char[][] mat = new char[height][width];
for(int h=0; h<height; h++){
String s = this.next();
for(int w=0; w<width; w++){
mat[h][w] = s.charAt(w);
}
}
return mat;
}
}
class ContestPrinter extends PrintWriter {
public ContestPrinter(PrintStream stream) {
super(stream);
}
public ContestPrinter() {
super(System.out);
}
private static String dtos(double x, int n) {
StringBuilder sb = new StringBuilder();
if (x < 0) {
sb.append('-');
x = -x;
}
x += Math.pow(10, -n) / 2;
sb.append((long) x);
sb.append(".");
x -= (long) x;
for (int i = 0; i < n; i++) {
x *= 10;
sb.append((int) x);
x -= (int) x;
}
return sb.toString();
}
@Override
public void print(float f) {
super.print(dtos(f, 20));
}
@Override
public void println(float f) {
super.println(dtos(f, 20));
}
@Override
public void print(double d) {
super.print(dtos(d, 20));
}
@Override
public void println(double d) {
super.println(dtos(d, 20));
}
public void printlnArray(String[] array) {
for (String i : array)
super.println(i);
}
public void printSpace(Object... o) {
int n = o.length - 1;
for (int i = 0; i < n; i++) {
super.print(o[i]);
super.print(" ");
}
super.println(o[n]);
}
public void println(Object... o) {
int n = o.length - 1;
for (int i = 0; i < n; i++)
super.print(o[i]);
super.println(o[n]);
}
public void printYN(boolean o) {
super.println(o ? "Yes" : "No");
}
public void print(Object... o) {
int n = o.length - 1;
for (int i = 0; i < n; i++)
super.print(o[i]);
super.print(o[n]);
}
public void printArray(Object[] array) {
int n = array.length - 1;
for (int i = 0; i < n; i++) {
super.print(array[i]);
super.print(" ");
}
super.println(array[n]);
}
public void printlnArray(Object[] array) {
for (Object i : array)
super.println(i);
}
public void printArray(int[] array, String separator) {
int n = array.length - 1;
if (n == -1)
return;
for (int i = 0; i < n; i++) {
super.print(array[i]);
super.print(separator);
}
super.println(array[n]);
}
public void printArray(int[] array) {
this.printArray(array, " ");
}
public void printArray(Integer[] array) {
this.printArray(array, " ");
}
public void printArray(Integer[] array, String separator) {
int n = array.length - 1;
for (int i = 0; i < n; i++) {
super.print(array[i]);
super.print(separator);
}
super.println(array[n]);
}
public void printlnArray(int[] array) {
for (int i : array)
super.println(i);
}
public void printArray(int[] array, String separator, java.util.function.IntUnaryOperator map) {
int n = array.length - 1;
for (int i = 0; i < n; i++) {
super.print(map.applyAsInt(array[i]));
super.print(separator);
}
super.println(map.applyAsInt(array[n]));
}
public void printlnArray(int[] array, java.util.function.IntUnaryOperator map) {
for (int i : array)
super.println(map.applyAsInt(i));
}
public void printlnArray(long[] array, java.util.function.LongUnaryOperator map) {
for (long i : array)
super.println(map.applyAsLong(i));
}
public void printArray(int[] array, java.util.function.IntUnaryOperator map) {
this.printArray(array, " ", map);
}
public void printArray(long[] array, String separator) {
int n = array.length - 1;
for (int i = 0; i < n; i++) {
super.print(array[i]);
super.print(separator);
}
super.println(array[n]);
}
public void printArray(long[] array) {
this.printArray(array, " ");
}
public void printlnArray(long[] array) {
for (long i : array)
super.println(i);
}
public void printArray(double[] array) {
printArray(array, " ");
}
public void printArray(double[] array, String separator) {
int n = array.length - 1;
for (int i = 0; i < n; i++) {
super.print(array[i]);
super.print(separator);
}
super.println(array[n]);
}
public void printlnArray(double[] array) {
for (double i : array)
super.println(i);
}
public void printArray(boolean[] array, String a, String b) {
int n = array.length - 1;
for (int i = 0; i < n; i++)
super.print((array[i] ? a : b) + " ");
super.println(array[n] ? a : b);
}
public void printArray(boolean[] array) {
this.printArray(array, "Y", "N");
}
public void printArray(char[] array) {
for (char c : array)
this.print(c);
this.println();
}
public void printArray(long[] array, String separator, java.util.function.LongUnaryOperator map) {
int n = array.length - 1;
for (int i = 0; i < n; i++) {
super.print(map.applyAsLong(array[i]));
super.print(separator);
}
super.println(map.applyAsLong(array[n]));
}
public void printArray(long[] array, java.util.function.LongUnaryOperator map) {
this.printArray(array, " ", map);
}
public void printArray(ArrayList<?> array) {
this.printArray(array, " ");
}
public void printArray(ArrayList<?> array, String separator) {
int n = array.size() - 1;
if (n == -1)
return;
for (int i = 0; i < n; i++) {
super.print(array.get(i).toString());
super.print(separator);
}
super.println(array.get(n).toString());
}
public void printlnArray(ArrayList<?> array) {
int n = array.size();
for (int i = 0; i < n; i++)
super.println(array.get(i).toString());
}
public void printlnArray(ArrayList<Integer> array, java.util.function.IntUnaryOperator map) {
int n = array.size();
for (int i = 0; i < n; i++)
super.println(map.applyAsInt(array.get(i)));
}
public void printlnArray(ArrayList<Long> array, java.util.function.LongUnaryOperator map) {
int n = array.size();
for (int i = 0; i < n; i++)
super.println(map.applyAsLong(array.get(i)));
}
public void printArray(int[][] array) {
for (int[] a : array)
this.printArray(a);
}
public void printArray(int[][] array, java.util.function.IntUnaryOperator map) {
for (int[] a : array)
this.printArray(a, map);
}
public void printArray(long[][] array) {
int n = array.length;
if (n == 0)
return;
int m = array[0].length - 1;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++)
super.print(array[i][j] + " ");
super.println(array[i][m]);
}
}
public void printArray(long[][] array, java.util.function.LongUnaryOperator map) {
int n = array.length;
if (n == 0)
return;
int m = array[0].length - 1;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
super.print(map.applyAsLong(array[i][j]));
super.print(" ");
}
super.println(map.applyAsLong(array[i][m]));
}
}
public void printArray(boolean[][] array) {
int n = array.length;
if (n == 0)
return;
int m = array[0].length - 1;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++)
super.print(array[i][j] ? "○ " : "× ");
super.println(array[i][m] ? "○" : "×");
}
}
public void printArray(char[][] array) {
int n = array.length;
if (n == 0)
return;
int m = array[0].length;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++)
super.print(array[i][j]);
super.println();
}
}
}
|
Java
|
["5\n\n2\n\n4\n\n6\n\n8\n\n60"]
|
2 seconds
|
["1 0 1\n3 2 1\n12 7 1\n42 27 1\n341102826 248150916 1"]
|
NoteIn the first test case, Alex wins if he receives the card $$$2$$$ (he plays it, and Boris cannot respond). If Alex receives the card $$$1$$$, the game ends in a draw.In the second test case: Alex wins if he receives the cards $$$[3, 4]$$$, $$$[2, 4]$$$ or $$$[1, 4]$$$; Boris wins if Alex receives the cards $$$[1, 2]$$$ or $$$[1, 3]$$$; the game ends in a draw if Alex receives the cards $$$[2, 3]$$$.
|
Java 11
|
standard input
|
[
"combinatorics",
"constructive algorithms",
"dp",
"games"
] |
63fc2fb08d248f8ccdb3f5364c96dac1
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 30$$$) — the number of test cases. Then, $$$t$$$ lines follow. The $$$i$$$-th line contains one even integer $$$n$$$ ($$$2 \le n \le 60$$$).
| 1,500
|
For each test case, print three integers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. Since the answers can be large, print them modulo $$$998244353$$$.
|
standard output
| |
PASSED
|
273d09aa2067de17a1d547176730b327
|
train_109.jsonl
|
1664462100
|
Consider a game with $$$n$$$ cards ($$$n$$$ is even). Each card has a number written on it, between $$$1$$$ and $$$n$$$. All numbers on the cards are different. We say that a card with number $$$x$$$ is stronger than a card with number $$$y$$$ if $$$x > y$$$.Two players, Alex and Boris, play this game. In the beginning, each of them receives exactly $$$\frac{n}{2}$$$ cards, so each card belongs to exactly one player. Then, they take turns. Alex goes first, then Boris, then Alex again, and so on.On a player's turn, he must play exactly one of his cards. Then, if the opponent doesn't have any cards stronger than the card played, the opponent loses, and the game ends. Otherwise, the opponent has to play a stronger card (exactly one card as well). These two cards are removed from the game, and the turn ends. If there are no cards left, the game ends in a draw; otherwise it's the opponent's turn.Consider all possible ways to distribute the cards between two players, so that each of them receives exactly half of the cards. You have to calculate three numbers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. You may assume that both players play optimally (i. e. if a player can win no matter how his opponent plays, he wins). Two ways to distribute the cards are different if there is at least one card such that, in one of these ways, it is given to Alex, and in the other way, it is given to Boris.For example, suppose $$$n = 4$$$, Alex receives the cards $$$[2, 3]$$$, and Boris receives the cards $$$[1, 4]$$$. Then the game may go as follows: if Alex plays the card $$$2$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$3$$$. So, the game ends in a draw; if Alex plays the card $$$3$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$2$$$. So, the game ends in a draw. So, in this case, the game ends in a draw.
|
512 megabytes
|
import java.util.*;
public class C {
public static final int MOD998 = 998244353;
public static final int MOD100 = 1000000007;
public static void main(String[] args) throws Exception {
ContestScanner sc = new ContestScanner();
ContestPrinter cp = new ContestPrinter();
int T = sc.nextInt();
ModIntFactory mf = new ModIntFactory(MOD998);
long[][] dp = new long[30][3];
dp[0][0] = 1;
dp[0][2] = 1;
for (int n = 1; n < 30; n++) {
dp[n][0] += mf.combination((n + 1) * 2, n + 1).div(2).value();
dp[n][1] += mf.combination(n * 2, n - 1).value();
dp[n][0] += dp[n - 1][1];
dp[n][1] += dp[n - 1][0];
dp[n][2] += dp[n - 1][2];
dp[n][0] %= MOD998;
dp[n][1] %= MOD998;
}
for (int t = 0; t < T; t++) {
int x = sc.nextInt() / 2 - 1;
cp.printArray(dp[x]);
}
cp.close();
}
//////////////////
// My Library //
//////////////////
public static int zeroOneBFS(int[][][] weighted_graph, int start, int goal) {
int[] dist = new int[weighted_graph.length];
Arrays.fill(dist, Integer.MAX_VALUE);
dist[start] = 0;
LinkedList<Integer> queue = new LinkedList<>();
queue.add(start);
while (!queue.isEmpty()) {
int now = queue.poll();
if (now == goal) {
return dist[goal];
}
for (int[] info : weighted_graph[now]) {
if (dist[info[0]] > dist[now] + info[1]) {
dist[info[0]] = dist[now] + info[1];
if (info[1] == 0) {
queue.addFirst(info[0]);
} else {
queue.addLast(info[0]);
}
}
}
}
return -1;
}
public static int[] zeroOneBFSAll(int[][][] weighted_graph, int start) {
int[] dist = new int[weighted_graph.length];
Arrays.fill(dist, Integer.MAX_VALUE);
dist[start] = 0;
LinkedList<Integer> queue = new LinkedList<>();
queue.add(start);
while (!queue.isEmpty()) {
int now = queue.poll();
for (int[] info : weighted_graph[now]) {
if (dist[info[0]] > dist[now] + info[1]) {
dist[info[0]] = dist[now] + info[1];
if (info[1] == 0) {
queue.addFirst(info[0]);
} else {
queue.addLast(info[0]);
}
}
}
}
return dist;
}
public static long dijkstra(int[][][] weighted_graph, int start, int goal) {
long[] dist = new long[weighted_graph.length];
Arrays.fill(dist, 0, dist.length, Long.MAX_VALUE);
dist[start] = 0;
PriorityQueue<Pair<Integer, Long>> unsettled = new PriorityQueue<>((u, v) -> (int) (u.cdr - v.cdr));
unsettled.offer(new Pair<Integer, Long>(start, 0L));
while (!unsettled.isEmpty()) {
Pair<Integer, Long> pair = unsettled.poll();
int now = pair.car;
if (now == goal) {
return dist[goal];
}
if (dist[now] < pair.cdr) {
continue;
}
for (int[] info : weighted_graph[now]) {
if (dist[info[0]] > dist[now] + info[1]) {
dist[info[0]] = dist[now] + info[1];
unsettled.offer(new Pair<Integer, Long>(info[0], dist[info[0]]));
}
}
}
return -1;
}
public static long[] dijkstraAll(int[][][] weighted_graph, int start) {
long[] dist = new long[weighted_graph.length];
Arrays.fill(dist, 0, dist.length, Long.MAX_VALUE);
dist[start] = 0;
PriorityQueue<Pair<Integer, Long>> unsettled = new PriorityQueue<>((u, v) -> (int) (u.cdr - v.cdr));
unsettled.offer(new Pair<Integer, Long>(start, 0L));
while (!unsettled.isEmpty()) {
Pair<Integer, Long> pair = unsettled.poll();
int now = pair.car;
if (dist[now] < pair.cdr) {
continue;
}
for (int[] info : weighted_graph[now]) {
if (dist[info[0]] > dist[now] + info[1]) {
dist[info[0]] = dist[now] + info[1];
unsettled.offer(new Pair<Integer, Long>(info[0], dist[info[0]]));
}
}
}
return dist;
}
public static class Pair<A, B> {
public final A car;
public final B cdr;
public Pair(A car_, B cdr_) {
car = car_;
cdr = cdr_;
}
private static boolean eq(Object o1, Object o2) {
return o1 == null ? o2 == null : o1.equals(o2);
}
private static int hc(Object o) {
return o == null ? 0 : o.hashCode();
}
@Override
public boolean equals(Object o) {
if (!(o instanceof Pair))
return false;
Pair<?, ?> rhs = (Pair<?, ?>) o;
return eq(car, rhs.car) && eq(cdr, rhs.cdr);
}
@Override
public int hashCode() {
return hc(car) ^ hc(cdr);
}
}
public static class Tuple1<A> extends Pair<A, Object> {
public Tuple1(A a) {
super(a, null);
}
}
public static class Tuple2<A, B> extends Pair<A, Tuple1<B>> {
public Tuple2(A a, B b) {
super(a, new Tuple1<>(b));
}
}
public static class Tuple3<A, B, C> extends Pair<A, Tuple2<B, C>> {
public Tuple3(A a, B b, C c) {
super(a, new Tuple2<>(b, c));
}
}
public static class Tuple4<A, B, C, D> extends Pair<A, Tuple3<B, C, D>> {
public Tuple4(A a, B b, C c, D d) {
super(a, new Tuple3<>(b, c, d));
}
}
public static class Tuple5<A, B, C, D, E> extends Pair<A, Tuple4<B, C, D, E>> {
public Tuple5(A a, B b, C c, D d, E e) {
super(a, new Tuple4<>(b, c, d, e));
}
}
public static class PriorityQueueLogTime<T> {
private PriorityQueue<T> queue;
private Multiset<T> total;
private int size = 0;
public PriorityQueueLogTime() {
queue = new PriorityQueue<>();
total = new Multiset<>();
}
public PriorityQueueLogTime(Comparator<T> c) {
queue = new PriorityQueue<>(c);
total = new Multiset<>();
}
public void clear() {
queue.clear();
total.clear();
size = 0;
}
public boolean contains(T e) {
return total.count(e) > 0;
}
public boolean isEmpty() {
return size == 0;
}
public boolean offer(T e) {
total.addOne(e);
size++;
return queue.offer(e);
}
public T peek() {
if (total.isEmpty()) {
return null;
}
simplify();
return queue.peek();
}
public T poll() {
if (total.isEmpty()) {
return null;
}
simplify();
size--;
T res = queue.poll();
total.removeOne(res);
return res;
}
public void remove(T e) {
total.removeOne(e);
size--;
}
public int size() {
return size;
}
private void simplify() {
while (total.count(queue.peek()) == 0) {
queue.poll();
}
}
}
static int[][] scanGraphOneIndexed(ContestScanner sc, int node, int edge, boolean undirected) {
int[][] arr = sc.nextIntArrayMulti(edge, 2);
for (int n = 0; n < edge; n++) {
arr[0][n]--;
arr[1][n]--;
}
return GraphBuilder.makeGraph(node, edge, arr[0], arr[1], undirected);
}
static int[][][] scanWeightedGraphOneIndexed(ContestScanner sc, int node, int edge, boolean undirected) {
int[][] arr = sc.nextIntArrayMulti(edge, 3);
for (int n = 0; n < edge; n++) {
arr[0][n]--;
arr[1][n]--;
}
return GraphBuilder.makeGraphWithWeight(node, edge, arr[0], arr[1], arr[2], undirected);
}
static class EdgeData {
private int capacity;
private int[] from, to, weight;
private int p = 0;
private boolean weighted;
public EdgeData(boolean weighted) {
this(weighted, 500000);
}
public EdgeData(boolean weighted, int initial_capacity) {
capacity = initial_capacity;
from = new int[capacity];
to = new int[capacity];
weight = new int[capacity];
this.weighted = weighted;
}
public void addEdge(int u, int v) {
if (weighted) {
System.err.println("The graph is weighted!");
return;
}
if (p == capacity) {
int[] newfrom = new int[capacity * 2];
int[] newto = new int[capacity * 2];
System.arraycopy(from, 0, newfrom, 0, capacity);
System.arraycopy(to, 0, newto, 0, capacity);
from = newfrom;
to = newto;
capacity *= 2;
}
from[p] = u;
to[p] = v;
p++;
}
public void addEdge(int u, int v, int w) {
if (!weighted) {
System.err.println("The graph is NOT weighted!");
return;
}
if (p == capacity) {
int[] newfrom = new int[capacity * 2];
int[] newto = new int[capacity * 2];
int[] newweight = new int[capacity * 2];
System.arraycopy(from, 0, newfrom, 0, capacity);
System.arraycopy(to, 0, newto, 0, capacity);
System.arraycopy(weight, 0, newweight, 0, capacity);
from = newfrom;
to = newto;
weight = newweight;
capacity *= 2;
}
from[p] = u;
to[p] = v;
weight[p] = w;
p++;
}
public int[] getFrom() {
int[] result = new int[p];
System.arraycopy(from, 0, result, 0, p);
return result;
}
public int[] getTo() {
int[] result = new int[p];
System.arraycopy(to, 0, result, 0, p);
return result;
}
public int[] getWeight() {
int[] result = new int[p];
System.arraycopy(weight, 0, result, 0, p);
return result;
}
public int size() {
return p;
}
}
////////////////////////////////
// Atcoder Library for Java //
////////////////////////////////
static class MathLib {
private static long safe_mod(long x, long m) {
x %= m;
if (x < 0)
x += m;
return x;
}
private static long[] inv_gcd(long a, long b) {
a = safe_mod(a, b);
if (a == 0)
return new long[] { b, 0 };
long s = b, t = a;
long m0 = 0, m1 = 1;
while (t > 0) {
long u = s / t;
s -= t * u;
m0 -= m1 * u;
long tmp = s;
s = t;
t = tmp;
tmp = m0;
m0 = m1;
m1 = tmp;
}
if (m0 < 0)
m0 += b / s;
return new long[] { s, m0 };
}
public static long gcd(long a, long b) {
a = java.lang.Math.abs(a);
b = java.lang.Math.abs(b);
return inv_gcd(a, b)[0];
}
public static long lcm(long a, long b) {
a = java.lang.Math.abs(a);
b = java.lang.Math.abs(b);
return a / gcd(a, b) * b;
}
public static long pow_mod(long x, long n, int m) {
assert n >= 0;
assert m >= 1;
if (m == 1)
return 0L;
x = safe_mod(x, m);
long ans = 1L;
while (n > 0) {
if ((n & 1) == 1)
ans = (ans * x) % m;
x = (x * x) % m;
n >>>= 1;
}
return ans;
}
public static long[] crt(long[] r, long[] m) {
assert (r.length == m.length);
int n = r.length;
long r0 = 0, m0 = 1;
for (int i = 0; i < n; i++) {
assert (1 <= m[i]);
long r1 = safe_mod(r[i], m[i]), m1 = m[i];
if (m0 < m1) {
long tmp = r0;
r0 = r1;
r1 = tmp;
tmp = m0;
m0 = m1;
m1 = tmp;
}
if (m0 % m1 == 0) {
if (r0 % m1 != r1)
return new long[] { 0, 0 };
continue;
}
long[] ig = inv_gcd(m0, m1);
long g = ig[0], im = ig[1];
long u1 = m1 / g;
if ((r1 - r0) % g != 0)
return new long[] { 0, 0 };
long x = (r1 - r0) / g % u1 * im % u1;
r0 += x * m0;
m0 *= u1;
if (r0 < 0)
r0 += m0;
// System.err.printf("%d %d\n", r0, m0);
}
return new long[] { r0, m0 };
}
public static long floor_sum(long n, long m, long a, long b) {
long ans = 0;
if (a >= m) {
ans += (n - 1) * n * (a / m) / 2;
a %= m;
}
if (b >= m) {
ans += n * (b / m);
b %= m;
}
long y_max = (a * n + b) / m;
long x_max = y_max * m - b;
if (y_max == 0)
return ans;
ans += (n - (x_max + a - 1) / a) * y_max;
ans += floor_sum(y_max, a, m, (a - x_max % a) % a);
return ans;
}
public static java.util.ArrayList<Long> divisors(long n) {
java.util.ArrayList<Long> divisors = new ArrayList<>();
java.util.ArrayList<Long> large = new ArrayList<>();
for (long i = 1; i * i <= n; i++)
if (n % i == 0) {
divisors.add(i);
if (i * i < n)
large.add(n / i);
}
for (int p = large.size() - 1; p >= 0; p--) {
divisors.add(large.get(p));
}
return divisors;
}
}
static class Multiset<T> extends java.util.TreeMap<T, Long> {
public Multiset() {
super();
}
public Multiset(java.util.List<T> list) {
super();
for (T e : list)
this.addOne(e);
}
public long count(Object elm) {
return getOrDefault(elm, 0L);
}
public void add(T elm, long amount) {
if (!this.containsKey(elm))
put(elm, amount);
else
replace(elm, get(elm) + amount);
if (this.count(elm) == 0)
this.remove(elm);
}
public void addOne(T elm) {
this.add(elm, 1);
}
public void removeOne(T elm) {
this.add(elm, -1);
}
public void removeAll(T elm) {
this.add(elm, -this.count(elm));
}
public static <T> Multiset<T> merge(Multiset<T> a, Multiset<T> b) {
Multiset<T> c = new Multiset<>();
for (T x : a.keySet())
c.add(x, a.count(x));
for (T y : b.keySet())
c.add(y, b.count(y));
return c;
}
}
static class GraphBuilder {
public static int[][] makeGraph(int NumberOfNodes, int NumberOfEdges, int[] from, int[] to,
boolean undirected) {
int[][] graph = new int[NumberOfNodes][];
int[] outdegree = new int[NumberOfNodes];
for (int i = 0; i < NumberOfEdges; i++) {
outdegree[from[i]]++;
if (undirected)
outdegree[to[i]]++;
}
for (int i = 0; i < NumberOfNodes; i++)
graph[i] = new int[outdegree[i]];
for (int i = 0; i < NumberOfEdges; i++) {
graph[from[i]][--outdegree[from[i]]] = to[i];
if (undirected)
graph[to[i]][--outdegree[to[i]]] = from[i];
}
return graph;
}
public static int[][][] makeGraphWithWeight(int NumberOfNodes, int NumberOfEdges, int[] from, int[] to,
int[] weight, boolean undirected) {
int[][][] graph = new int[NumberOfNodes][][];
int[] outdegree = new int[NumberOfNodes];
for (int i = 0; i < NumberOfEdges; i++) {
outdegree[from[i]]++;
if (undirected)
outdegree[to[i]]++;
}
for (int i = 0; i < NumberOfNodes; i++)
graph[i] = new int[outdegree[i]][];
for (int i = 0; i < NumberOfEdges; i++) {
graph[from[i]][--outdegree[from[i]]] = new int[] { to[i], weight[i] };
if (undirected)
graph[to[i]][--outdegree[to[i]]] = new int[] { from[i], weight[i] };
}
return graph;
}
public static int[][][] makeGraphWithEdgeInfo(int NumberOfNodes, int NumberOfEdges, int[] from, int[] to,
boolean undirected) {
int[][][] graph = new int[NumberOfNodes][][];
int[] outdegree = new int[NumberOfNodes];
for (int i = 0; i < NumberOfEdges; i++) {
outdegree[from[i]]++;
if (undirected)
outdegree[to[i]]++;
}
for (int i = 0; i < NumberOfNodes; i++)
graph[i] = new int[outdegree[i]][];
for (int i = 0; i < NumberOfEdges; i++) {
graph[from[i]][--outdegree[from[i]]] = new int[] { to[i], i, 0 };
if (undirected)
graph[to[i]][--outdegree[to[i]]] = new int[] { from[i], i, 1 };
}
return graph;
}
public static int[][][] makeGraphWithWeightAndEdgeInfo(int NumberOfNodes, int NumberOfEdges, int[] from,
int[] to, int[] weight, boolean undirected) {
int[][][] graph = new int[NumberOfNodes][][];
int[] outdegree = new int[NumberOfNodes];
for (int i = 0; i < NumberOfEdges; i++) {
outdegree[from[i]]++;
if (undirected)
outdegree[to[i]]++;
}
for (int i = 0; i < NumberOfNodes; i++)
graph[i] = new int[outdegree[i]][];
for (int i = 0; i < NumberOfEdges; i++) {
graph[from[i]][--outdegree[from[i]]] = new int[] { to[i], weight[i], i, 0 };
if (undirected)
graph[to[i]][--outdegree[to[i]]] = new int[] { from[i], weight[i], i, 1 };
}
return graph;
}
}
static class DSU {
private int n;
private int[] parentOrSize;
public DSU(int n) {
this.n = n;
this.parentOrSize = new int[n];
java.util.Arrays.fill(parentOrSize, -1);
}
int merge(int a, int b) {
if (!(0 <= a && a < n))
throw new IndexOutOfBoundsException("a=" + a);
if (!(0 <= b && b < n))
throw new IndexOutOfBoundsException("b=" + b);
int x = leader(a);
int y = leader(b);
if (x == y)
return x;
if (-parentOrSize[x] < -parentOrSize[y]) {
int tmp = x;
x = y;
y = tmp;
}
parentOrSize[x] += parentOrSize[y];
parentOrSize[y] = x;
return x;
}
boolean same(int a, int b) {
if (!(0 <= a && a < n))
throw new IndexOutOfBoundsException("a=" + a);
if (!(0 <= b && b < n))
throw new IndexOutOfBoundsException("b=" + b);
return leader(a) == leader(b);
}
int leader(int a) {
if (parentOrSize[a] < 0) {
return a;
} else {
parentOrSize[a] = leader(parentOrSize[a]);
return parentOrSize[a];
}
}
int size(int a) {
if (!(0 <= a && a < n))
throw new IndexOutOfBoundsException("" + a);
return -parentOrSize[leader(a)];
}
java.util.ArrayList<java.util.ArrayList<Integer>> groups() {
int[] leaderBuf = new int[n];
int[] groupSize = new int[n];
for (int i = 0; i < n; i++) {
leaderBuf[i] = leader(i);
groupSize[leaderBuf[i]]++;
}
java.util.ArrayList<java.util.ArrayList<Integer>> result = new java.util.ArrayList<>(n);
for (int i = 0; i < n; i++) {
result.add(new java.util.ArrayList<>(groupSize[i]));
}
for (int i = 0; i < n; i++) {
result.get(leaderBuf[i]).add(i);
}
result.removeIf(java.util.ArrayList::isEmpty);
return result;
}
}
static class ModIntFactory {
private final ModArithmetic ma;
private final int mod;
private final boolean usesMontgomery;
private final ModArithmetic.ModArithmeticMontgomery maMontgomery;
private ArrayList<Integer> factorial;
private ArrayList<Integer> factorial_inversion;
public ModIntFactory(int mod) {
this.ma = ModArithmetic.of(mod);
this.usesMontgomery = ma instanceof ModArithmetic.ModArithmeticMontgomery;
this.maMontgomery = usesMontgomery ? (ModArithmetic.ModArithmeticMontgomery) ma : null;
this.mod = mod;
this.factorial = new ArrayList<>();
this.factorial_inversion = new ArrayList<>();
}
public ModInt create(long value) {
if ((value %= mod) < 0)
value += mod;
if (usesMontgomery) {
return new ModInt(maMontgomery.generate(value));
} else {
return new ModInt((int) value);
}
}
private void prepareFactorial(int max) {
factorial.ensureCapacity(max + 1);
if (factorial.size() == 0)
factorial.add(1);
for (int i = factorial.size(); i <= max; i++) {
factorial.add(ma.mul(factorial.get(i - 1), i));
}
}
public ModInt factorial(int i) {
prepareFactorial(i);
return create(factorial.get(i));
}
public ModInt permutation(int n, int r) {
if (n < 0 || r < 0 || n < r)
return create(0);
prepareFactorial(n);
if (factorial_inversion.size() > n) {
return create(ma.mul(factorial.get(n), factorial_inversion.get(n - r)));
}
return create(ma.div(factorial.get(n), factorial.get(n - r)));
}
public ModInt combination(int n, int r) {
if (n < 0 || r < 0 || n < r)
return create(0);
prepareFactorial(n);
if (factorial_inversion.size() > n) {
return create(
ma.mul(factorial.get(n), ma.mul(factorial_inversion.get(n - r), factorial_inversion.get(r))));
}
return create(ma.div(factorial.get(n), ma.mul(factorial.get(r), factorial.get(n - r))));
}
public void prepareFactorialInv(int max) {
prepareFactorial(max);
factorial_inversion.ensureCapacity(max + 1);
for (int i = factorial_inversion.size(); i <= max; i++) {
factorial_inversion.add(ma.inv(factorial.get(i)));
}
}
public int getMod() {
return mod;
}
public class ModInt {
private int value;
private ModInt(int value) {
this.value = value;
}
public int mod() {
return mod;
}
public int value() {
if (ma instanceof ModArithmetic.ModArithmeticMontgomery) {
return ((ModArithmetic.ModArithmeticMontgomery) ma).reduce(value);
}
return value;
}
public ModInt add(ModInt mi) {
return new ModInt(ma.add(value, mi.value));
}
public ModInt add(ModInt mi1, ModInt mi2) {
return new ModInt(ma.add(value, mi1.value)).addAsg(mi2);
}
public ModInt add(ModInt mi1, ModInt mi2, ModInt mi3) {
return new ModInt(ma.add(value, mi1.value)).addAsg(mi2).addAsg(mi3);
}
public ModInt add(ModInt mi1, ModInt mi2, ModInt mi3, ModInt mi4) {
return new ModInt(ma.add(value, mi1.value)).addAsg(mi2).addAsg(mi3).addAsg(mi4);
}
public ModInt add(ModInt mi1, ModInt... mis) {
ModInt mi = add(mi1);
for (ModInt m : mis)
mi.addAsg(m);
return mi;
}
public ModInt add(long mi) {
return new ModInt(ma.add(value, ma.remainder(mi)));
}
public ModInt sub(ModInt mi) {
return new ModInt(ma.sub(value, mi.value));
}
public ModInt sub(long mi) {
return new ModInt(ma.sub(value, ma.remainder(mi)));
}
public ModInt mul(ModInt mi) {
return new ModInt(ma.mul(value, mi.value));
}
public ModInt mul(ModInt mi1, ModInt mi2) {
return new ModInt(ma.mul(value, mi1.value)).mulAsg(mi2);
}
public ModInt mul(ModInt mi1, ModInt mi2, ModInt mi3) {
return new ModInt(ma.mul(value, mi1.value)).mulAsg(mi2).mulAsg(mi3);
}
public ModInt mul(ModInt mi1, ModInt mi2, ModInt mi3, ModInt mi4) {
return new ModInt(ma.mul(value, mi1.value)).mulAsg(mi2).mulAsg(mi3).mulAsg(mi4);
}
public ModInt mul(ModInt mi1, ModInt... mis) {
ModInt mi = mul(mi1);
for (ModInt m : mis)
mi.mulAsg(m);
return mi;
}
public ModInt mul(long mi) {
return new ModInt(ma.mul(value, ma.remainder(mi)));
}
public ModInt div(ModInt mi) {
return new ModInt(ma.div(value, mi.value));
}
public ModInt div(long mi) {
return new ModInt(ma.div(value, ma.remainder(mi)));
}
public ModInt inv() {
return new ModInt(ma.inv(value));
}
public ModInt pow(long b) {
return new ModInt(ma.pow(value, b));
}
public ModInt addAsg(ModInt mi) {
this.value = ma.add(value, mi.value);
return this;
}
public ModInt addAsg(ModInt mi1, ModInt mi2) {
return addAsg(mi1).addAsg(mi2);
}
public ModInt addAsg(ModInt mi1, ModInt mi2, ModInt mi3) {
return addAsg(mi1).addAsg(mi2).addAsg(mi3);
}
public ModInt addAsg(ModInt mi1, ModInt mi2, ModInt mi3, ModInt mi4) {
return addAsg(mi1).addAsg(mi2).addAsg(mi3).addAsg(mi4);
}
public ModInt addAsg(ModInt... mis) {
for (ModInt m : mis)
addAsg(m);
return this;
}
public ModInt addAsg(long mi) {
this.value = ma.add(value, ma.remainder(mi));
return this;
}
public ModInt subAsg(ModInt mi) {
this.value = ma.sub(value, mi.value);
return this;
}
public ModInt subAsg(long mi) {
this.value = ma.sub(value, ma.remainder(mi));
return this;
}
public ModInt mulAsg(ModInt mi) {
this.value = ma.mul(value, mi.value);
return this;
}
public ModInt mulAsg(ModInt mi1, ModInt mi2) {
return mulAsg(mi1).mulAsg(mi2);
}
public ModInt mulAsg(ModInt mi1, ModInt mi2, ModInt mi3) {
return mulAsg(mi1).mulAsg(mi2).mulAsg(mi3);
}
public ModInt mulAsg(ModInt mi1, ModInt mi2, ModInt mi3, ModInt mi4) {
return mulAsg(mi1).mulAsg(mi2).mulAsg(mi3).mulAsg(mi4);
}
public ModInt mulAsg(ModInt... mis) {
for (ModInt m : mis)
mulAsg(m);
return this;
}
public ModInt mulAsg(long mi) {
this.value = ma.mul(value, ma.remainder(mi));
return this;
}
public ModInt divAsg(ModInt mi) {
this.value = ma.div(value, mi.value);
return this;
}
public ModInt divAsg(long mi) {
this.value = ma.div(value, ma.remainder(mi));
return this;
}
@Override
public String toString() {
return String.valueOf(value());
}
@Override
public boolean equals(Object o) {
if (o instanceof ModInt) {
ModInt mi = (ModInt) o;
return mod() == mi.mod() && value() == mi.value();
}
return false;
}
@Override
public int hashCode() {
return (1 * 37 + mod()) * 37 + value();
}
}
private static abstract class ModArithmetic {
abstract int mod();
abstract int remainder(long value);
abstract int add(int a, int b);
abstract int sub(int a, int b);
abstract int mul(int a, int b);
int div(int a, int b) {
return mul(a, inv(b));
}
int inv(int a) {
int b = mod();
if (b == 1)
return 0;
long u = 1, v = 0;
while (b >= 1) {
int t = a / b;
a -= t * b;
int tmp1 = a;
a = b;
b = tmp1;
u -= t * v;
long tmp2 = u;
u = v;
v = tmp2;
}
if (a != 1) {
throw new ArithmeticException("divide by zero");
}
return remainder(u);
}
int pow(int a, long b) {
if (b < 0)
throw new ArithmeticException("negative power");
int r = 1;
int x = a;
while (b > 0) {
if ((b & 1) == 1)
r = mul(r, x);
x = mul(x, x);
b >>= 1;
}
return r;
}
static ModArithmetic of(int mod) {
if (mod <= 0) {
throw new IllegalArgumentException();
} else if (mod == 1) {
return new ModArithmetic1();
} else if (mod == 2) {
return new ModArithmetic2();
} else if (mod == 998244353) {
return new ModArithmetic998244353();
} else if (mod == 1000000007) {
return new ModArithmetic1000000007();
} else if ((mod & 1) == 1) {
return new ModArithmeticMontgomery(mod);
} else {
return new ModArithmeticBarrett(mod);
}
}
private static final class ModArithmetic1 extends ModArithmetic {
int mod() {
return 1;
}
int remainder(long value) {
return 0;
}
int add(int a, int b) {
return 0;
}
int sub(int a, int b) {
return 0;
}
int mul(int a, int b) {
return 0;
}
int pow(int a, long b) {
return 0;
}
}
private static final class ModArithmetic2 extends ModArithmetic {
int mod() {
return 2;
}
int remainder(long value) {
return (int) (value & 1);
}
int add(int a, int b) {
return a ^ b;
}
int sub(int a, int b) {
return a ^ b;
}
int mul(int a, int b) {
return a & b;
}
}
private static final class ModArithmetic998244353 extends ModArithmetic {
private final int mod = 998244353;
int mod() {
return mod;
}
int remainder(long value) {
return (int) ((value %= mod) < 0 ? value + mod : value);
}
int add(int a, int b) {
int res = a + b;
return res >= mod ? res - mod : res;
}
int sub(int a, int b) {
int res = a - b;
return res < 0 ? res + mod : res;
}
int mul(int a, int b) {
return (int) (((long) a * b) % mod);
}
}
private static final class ModArithmetic1000000007 extends ModArithmetic {
private final int mod = 1000000007;
int mod() {
return mod;
}
int remainder(long value) {
return (int) ((value %= mod) < 0 ? value + mod : value);
}
int add(int a, int b) {
int res = a + b;
return res >= mod ? res - mod : res;
}
int sub(int a, int b) {
int res = a - b;
return res < 0 ? res + mod : res;
}
int mul(int a, int b) {
return (int) (((long) a * b) % mod);
}
}
private static final class ModArithmeticMontgomery extends ModArithmeticDynamic {
private final long negInv;
private final long r2;
private ModArithmeticMontgomery(int mod) {
super(mod);
long inv = 0;
long s = 1, t = 0;
for (int i = 0; i < 32; i++) {
if ((t & 1) == 0) {
t += mod;
inv += s;
}
t >>= 1;
s <<= 1;
}
long r = (1l << 32) % mod;
this.negInv = inv;
this.r2 = (r * r) % mod;
}
private int generate(long x) {
return reduce(x * r2);
}
private int reduce(long x) {
x = (x + ((x * negInv) & 0xffff_ffffl) * mod) >>> 32;
return (int) (x < mod ? x : x - mod);
}
@Override
int remainder(long value) {
return generate((value %= mod) < 0 ? value + mod : value);
}
@Override
int mul(int a, int b) {
return reduce((long) a * b);
}
@Override
int inv(int a) {
return super.inv(reduce(a));
}
@Override
int pow(int a, long b) {
return generate(super.pow(a, b));
}
}
private static final class ModArithmeticBarrett extends ModArithmeticDynamic {
private static final long mask = 0xffff_ffffl;
private final long mh;
private final long ml;
private ModArithmeticBarrett(int mod) {
super(mod);
/**
* m = floor(2^64/mod) 2^64 = p*mod + q, 2^32 = a*mod + b => (a*mod + b)^2 =
* p*mod + q => p = mod*a^2 + 2ab + floor(b^2/mod)
*/
long a = (1l << 32) / mod;
long b = (1l << 32) % mod;
long m = a * a * mod + 2 * a * b + (b * b) / mod;
mh = m >>> 32;
ml = m & mask;
}
private int reduce(long x) {
long z = (x & mask) * ml;
z = (x & mask) * mh + (x >>> 32) * ml + (z >>> 32);
z = (x >>> 32) * mh + (z >>> 32);
x -= z * mod;
return (int) (x < mod ? x : x - mod);
}
@Override
int remainder(long value) {
return (int) ((value %= mod) < 0 ? value + mod : value);
}
@Override
int mul(int a, int b) {
return reduce((long) a * b);
}
}
private static class ModArithmeticDynamic extends ModArithmetic {
final int mod;
ModArithmeticDynamic(int mod) {
this.mod = mod;
}
int mod() {
return mod;
}
int remainder(long value) {
return (int) ((value %= mod) < 0 ? value + mod : value);
}
int add(int a, int b) {
int sum = a + b;
return sum >= mod ? sum - mod : sum;
}
int sub(int a, int b) {
int sum = a - b;
return sum < 0 ? sum + mod : sum;
}
int mul(int a, int b) {
return (int) (((long) a * b) % mod);
}
}
}
}
static class Convolution {
/**
* Find a primitive root.
*
* @param m A prime number.
* @return Primitive root.
*/
private static int primitiveRoot(int m) {
if (m == 2)
return 1;
if (m == 167772161)
return 3;
if (m == 469762049)
return 3;
if (m == 754974721)
return 11;
if (m == 998244353)
return 3;
int[] divs = new int[20];
divs[0] = 2;
int cnt = 1;
int x = (m - 1) / 2;
while (x % 2 == 0)
x /= 2;
for (int i = 3; (long) (i) * i <= x; i += 2) {
if (x % i == 0) {
divs[cnt++] = i;
while (x % i == 0) {
x /= i;
}
}
}
if (x > 1) {
divs[cnt++] = x;
}
for (int g = 2;; g++) {
boolean ok = true;
for (int i = 0; i < cnt; i++) {
if (pow(g, (m - 1) / divs[i], m) == 1) {
ok = false;
break;
}
}
if (ok)
return g;
}
}
/**
* Power.
*
* @param x Parameter x.
* @param n Parameter n.
* @param m Mod.
* @return n-th power of x mod m.
*/
private static long pow(long x, long n, int m) {
if (m == 1)
return 0;
long r = 1;
long y = x % m;
while (n > 0) {
if ((n & 1) != 0)
r = (r * y) % m;
y = (y * y) % m;
n >>= 1;
}
return r;
}
/**
* Ceil of power 2.
*
* @param n Value.
* @return Ceil of power 2.
*/
private static int ceilPow2(int n) {
int x = 0;
while ((1L << x) < n)
x++;
return x;
}
/**
* Garner's algorithm.
*
* @param c Mod convolution results.
* @param mods Mods.
* @return Result.
*/
private static long garner(long[] c, int[] mods) {
int n = c.length + 1;
long[] cnst = new long[n];
long[] coef = new long[n];
java.util.Arrays.fill(coef, 1);
for (int i = 0; i < n - 1; i++) {
int m1 = mods[i];
long v = (c[i] - cnst[i] + m1) % m1;
v = v * pow(coef[i], m1 - 2, m1) % m1;
for (int j = i + 1; j < n; j++) {
long m2 = mods[j];
cnst[j] = (cnst[j] + coef[j] * v) % m2;
coef[j] = (coef[j] * m1) % m2;
}
}
return cnst[n - 1];
}
/**
* Pre-calculation for NTT.
*
* @param mod NTT Prime.
* @param g Primitive root of mod.
* @return Pre-calculation table.
*/
private static long[] sumE(int mod, int g) {
long[] sum_e = new long[30];
long[] es = new long[30];
long[] ies = new long[30];
int cnt2 = Integer.numberOfTrailingZeros(mod - 1);
long e = pow(g, (mod - 1) >> cnt2, mod);
long ie = pow(e, mod - 2, mod);
for (int i = cnt2; i >= 2; i--) {
es[i - 2] = e;
ies[i - 2] = ie;
e = e * e % mod;
ie = ie * ie % mod;
}
long now = 1;
for (int i = 0; i < cnt2 - 2; i++) {
sum_e[i] = es[i] * now % mod;
now = now * ies[i] % mod;
}
return sum_e;
}
/**
* Pre-calculation for inverse NTT.
*
* @param mod Mod.
* @param g Primitive root of mod.
* @return Pre-calculation table.
*/
private static long[] sumIE(int mod, int g) {
long[] sum_ie = new long[30];
long[] es = new long[30];
long[] ies = new long[30];
int cnt2 = Integer.numberOfTrailingZeros(mod - 1);
long e = pow(g, (mod - 1) >> cnt2, mod);
long ie = pow(e, mod - 2, mod);
for (int i = cnt2; i >= 2; i--) {
es[i - 2] = e;
ies[i - 2] = ie;
e = e * e % mod;
ie = ie * ie % mod;
}
long now = 1;
for (int i = 0; i < cnt2 - 2; i++) {
sum_ie[i] = ies[i] * now % mod;
now = now * es[i] % mod;
}
return sum_ie;
}
/**
* Inverse NTT.
*
* @param a Target array.
* @param sumIE Pre-calculation table.
* @param mod NTT Prime.
*/
private static void butterflyInv(long[] a, long[] sumIE, int mod) {
int n = a.length;
int h = ceilPow2(n);
for (int ph = h; ph >= 1; ph--) {
int w = 1 << (ph - 1), p = 1 << (h - ph);
long inow = 1;
for (int s = 0; s < w; s++) {
int offset = s << (h - ph + 1);
for (int i = 0; i < p; i++) {
long l = a[i + offset];
long r = a[i + offset + p];
a[i + offset] = (l + r) % mod;
a[i + offset + p] = (mod + l - r) * inow % mod;
}
int x = Integer.numberOfTrailingZeros(~s);
inow = inow * sumIE[x] % mod;
}
}
}
/**
* Inverse NTT.
*
* @param a Target array.
* @param sumE Pre-calculation table.
* @param mod NTT Prime.
*/
private static void butterfly(long[] a, long[] sumE, int mod) {
int n = a.length;
int h = ceilPow2(n);
for (int ph = 1; ph <= h; ph++) {
int w = 1 << (ph - 1), p = 1 << (h - ph);
long now = 1;
for (int s = 0; s < w; s++) {
int offset = s << (h - ph + 1);
for (int i = 0; i < p; i++) {
long l = a[i + offset];
long r = a[i + offset + p] * now % mod;
a[i + offset] = (l + r) % mod;
a[i + offset + p] = (l - r + mod) % mod;
}
int x = Integer.numberOfTrailingZeros(~s);
now = now * sumE[x] % mod;
}
}
}
/**
* Convolution.
*
* @param a Target array 1.
* @param b Target array 2.
* @param mod NTT Prime.
* @return Answer.
*/
public static long[] convolution(long[] a, long[] b, int mod) {
int n = a.length;
int m = b.length;
if (n == 0 || m == 0)
return new long[0];
int z = 1 << ceilPow2(n + m - 1);
{
long[] na = new long[z];
long[] nb = new long[z];
System.arraycopy(a, 0, na, 0, n);
System.arraycopy(b, 0, nb, 0, m);
a = na;
b = nb;
}
int g = primitiveRoot(mod);
long[] sume = sumE(mod, g);
long[] sumie = sumIE(mod, g);
butterfly(a, sume, mod);
butterfly(b, sume, mod);
for (int i = 0; i < z; i++) {
a[i] = a[i] * b[i] % mod;
}
butterflyInv(a, sumie, mod);
a = java.util.Arrays.copyOf(a, n + m - 1);
long iz = pow(z, mod - 2, mod);
for (int i = 0; i < n + m - 1; i++)
a[i] = a[i] * iz % mod;
return a;
}
/**
* Convolution.
*
* @param a Target array 1.
* @param b Target array 2.
* @param mod Any mod.
* @return Answer.
*/
public static long[] convolutionLL(long[] a, long[] b, int mod) {
int n = a.length;
int m = b.length;
if (n == 0 || m == 0)
return new long[0];
int mod1 = 754974721;
int mod2 = 167772161;
int mod3 = 469762049;
long[] c1 = convolution(a, b, mod1);
long[] c2 = convolution(a, b, mod2);
long[] c3 = convolution(a, b, mod3);
int retSize = c1.length;
long[] ret = new long[retSize];
int[] mods = { mod1, mod2, mod3, mod };
for (int i = 0; i < retSize; ++i) {
ret[i] = garner(new long[] { c1[i], c2[i], c3[i] }, mods);
}
return ret;
}
/**
* Convolution by ModInt.
*
* @param a Target array 1.
* @param b Target array 2.
* @return Answer.
*/
public static java.util.List<ModIntFactory.ModInt> convolution(java.util.List<ModIntFactory.ModInt> a,
java.util.List<ModIntFactory.ModInt> b) {
int mod = a.get(0).mod();
long[] va = a.stream().mapToLong(ModIntFactory.ModInt::value).toArray();
long[] vb = b.stream().mapToLong(ModIntFactory.ModInt::value).toArray();
long[] c = convolutionLL(va, vb, mod);
ModIntFactory factory = new ModIntFactory(mod);
return java.util.Arrays.stream(c).mapToObj(factory::create).collect(java.util.stream.Collectors.toList());
}
/**
* Naive convolution. (Complexity is O(N^2)!!)
*
* @param a Target array 1.
* @param b Target array 2.
* @param mod Mod.
* @return Answer.
*/
public static long[] convolutionNaive(long[] a, long[] b, int mod) {
int n = a.length;
int m = b.length;
int k = n + m - 1;
long[] ret = new long[k];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
ret[i + j] += a[i] * b[j] % mod;
ret[i + j] %= mod;
}
}
return ret;
}
}
static class ContestScanner {
private final java.io.InputStream in;
private final byte[] buffer = new byte[1024];
private int ptr = 0;
private int buflen = 0;
private static final long LONG_MAX_TENTHS = 922337203685477580L;
private static final int LONG_MAX_LAST_DIGIT = 7;
private static final int LONG_MIN_LAST_DIGIT = 8;
public ContestScanner(java.io.InputStream in) {
this.in = in;
}
public ContestScanner() {
this(System.in);
}
private boolean hasNextByte() {
if (ptr < buflen) {
return true;
} else {
ptr = 0;
try {
buflen = in.read(buffer);
} catch (java.io.IOException e) {
e.printStackTrace();
}
if (buflen <= 0) {
return false;
}
}
return true;
}
private int readByte() {
if (hasNextByte())
return buffer[ptr++];
else
return -1;
}
private static boolean isPrintableChar(int c) {
return 33 <= c && c <= 126;
}
public boolean hasNext() {
while (hasNextByte() && !isPrintableChar(buffer[ptr]))
ptr++;
return hasNextByte();
}
public String next() {
if (!hasNext())
throw new java.util.NoSuchElementException();
StringBuilder sb = new StringBuilder();
int b = readByte();
while (isPrintableChar(b)) {
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
public long nextLong() {
if (!hasNext())
throw new java.util.NoSuchElementException();
long n = 0;
boolean minus = false;
int b = readByte();
if (b == '-') {
minus = true;
b = readByte();
}
if (b < '0' || '9' < b) {
throw new NumberFormatException();
}
while (true) {
if ('0' <= b && b <= '9') {
int digit = b - '0';
if (n >= LONG_MAX_TENTHS) {
if (n == LONG_MAX_TENTHS) {
if (minus) {
if (digit <= LONG_MIN_LAST_DIGIT) {
n = -n * 10 - digit;
b = readByte();
if (!isPrintableChar(b)) {
return n;
} else if (b < '0' || '9' < b) {
throw new NumberFormatException(
String.format("%d%s... is not number", n, Character.toString(b)));
}
}
} else {
if (digit <= LONG_MAX_LAST_DIGIT) {
n = n * 10 + digit;
b = readByte();
if (!isPrintableChar(b)) {
return n;
} else if (b < '0' || '9' < b) {
throw new NumberFormatException(
String.format("%d%s... is not number", n, Character.toString(b)));
}
}
}
}
throw new ArithmeticException(
String.format("%s%d%d... overflows long.", minus ? "-" : "", n, digit));
}
n = n * 10 + digit;
} else if (b == -1 || !isPrintableChar(b)) {
return minus ? -n : n;
} else {
throw new NumberFormatException();
}
b = readByte();
}
}
public int nextInt() {
long nl = nextLong();
if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE)
throw new NumberFormatException();
return (int) nl;
}
public double nextDouble() {
return Double.parseDouble(next());
}
public long[] nextLongArray(int length) {
long[] array = new long[length];
for (int i = 0; i < length; i++)
array[i] = this.nextLong();
return array;
}
public long[] nextLongArray(int length, java.util.function.LongUnaryOperator map) {
long[] array = new long[length];
for (int i = 0; i < length; i++)
array[i] = map.applyAsLong(this.nextLong());
return array;
}
public int[] nextIntArray(int length) {
int[] array = new int[length];
for (int i = 0; i < length; i++)
array[i] = this.nextInt();
return array;
}
public int[][] nextIntArrayMulti(int length, int width) {
int[][] arrays = new int[width][length];
for (int i = 0; i < length; i++) {
for (int j = 0; j < width; j++)
arrays[j][i] = this.nextInt();
}
return arrays;
}
public int[] nextIntArray(int length, java.util.function.IntUnaryOperator map) {
int[] array = new int[length];
for (int i = 0; i < length; i++)
array[i] = map.applyAsInt(this.nextInt());
return array;
}
public double[] nextDoubleArray(int length) {
double[] array = new double[length];
for (int i = 0; i < length; i++)
array[i] = this.nextDouble();
return array;
}
public double[] nextDoubleArray(int length, java.util.function.DoubleUnaryOperator map) {
double[] array = new double[length];
for (int i = 0; i < length; i++)
array[i] = map.applyAsDouble(this.nextDouble());
return array;
}
public long[][] nextLongMatrix(int height, int width) {
long[][] mat = new long[height][width];
for (int h = 0; h < height; h++)
for (int w = 0; w < width; w++) {
mat[h][w] = this.nextLong();
}
return mat;
}
public int[][] nextIntMatrix(int height, int width) {
int[][] mat = new int[height][width];
for (int h = 0; h < height; h++)
for (int w = 0; w < width; w++) {
mat[h][w] = this.nextInt();
}
return mat;
}
public double[][] nextDoubleMatrix(int height, int width) {
double[][] mat = new double[height][width];
for (int h = 0; h < height; h++)
for (int w = 0; w < width; w++) {
mat[h][w] = this.nextDouble();
}
return mat;
}
public char[][] nextCharMatrix(int height, int width) {
char[][] mat = new char[height][width];
for (int h = 0; h < height; h++) {
String s = this.next();
for (int w = 0; w < width; w++) {
mat[h][w] = s.charAt(w);
}
}
return mat;
}
}
static class ContestPrinter extends java.io.PrintWriter {
public ContestPrinter(java.io.PrintStream stream) {
super(stream);
}
public ContestPrinter() {
super(System.out);
}
private static String dtos(double x, int n) {
StringBuilder sb = new StringBuilder();
if (x < 0) {
sb.append('-');
x = -x;
}
x += Math.pow(10, -n) / 2;
sb.append((long) x);
sb.append(".");
x -= (long) x;
for (int i = 0; i < n; i++) {
x *= 10;
sb.append((int) x);
x -= (int) x;
}
return sb.toString();
}
@Override
public void print(float f) {
super.print(dtos(f, 20));
}
@Override
public void println(float f) {
super.println(dtos(f, 20));
}
@Override
public void print(double d) {
super.print(dtos(d, 20));
}
@Override
public void println(double d) {
super.println(dtos(d, 20));
}
public void printArray(int[] array, String separator) {
int n = array.length;
for (int i = 0; i < n - 1; i++) {
super.print(array[i]);
super.print(separator);
}
super.println(array[n - 1]);
}
public void printArray(int[] array) {
this.printArray(array, " ");
}
public void printArray(int[] array, String separator, java.util.function.IntUnaryOperator map) {
int n = array.length;
for (int i = 0; i < n - 1; i++) {
super.print(map.applyAsInt(array[i]));
super.print(separator);
}
super.println(map.applyAsInt(array[n - 1]));
}
public void printArray(int[] array, java.util.function.IntUnaryOperator map) {
this.printArray(array, " ", map);
}
public void printArray(long[] array, String separator) {
int n = array.length;
for (int i = 0; i < n - 1; i++) {
super.print(array[i]);
super.print(separator);
}
super.println(array[n - 1]);
}
public void printArray(long[] array) {
this.printArray(array, " ");
}
public void printArray(long[] array, String separator, java.util.function.LongUnaryOperator map) {
int n = array.length;
for (int i = 0; i < n - 1; i++) {
super.print(map.applyAsLong(array[i]));
super.print(separator);
}
super.println(map.applyAsLong(array[n - 1]));
}
public void printArray(long[] array, java.util.function.LongUnaryOperator map) {
this.printArray(array, " ", map);
}
}
public static void safeSort(int[] array) {
Integer[] temp = new Integer[array.length];
for (int n = 0; n < array.length; n++) {
temp[n] = array[n];
}
Arrays.sort(temp);
for (int n = 0; n < array.length; n++) {
array[n] = temp[n];
}
}
public static void safeSort(long[] array) {
Long[] temp = new Long[array.length];
for (int n = 0; n < array.length; n++) {
temp[n] = array[n];
}
Arrays.sort(temp);
for (int n = 0; n < array.length; n++) {
array[n] = temp[n];
}
}
public static void safeSort(double[] array) {
Double[] temp = new Double[array.length];
for (int n = 0; n < array.length; n++) {
temp[n] = array[n];
}
Arrays.sort(temp);
for (int n = 0; n < array.length; n++) {
array[n] = temp[n];
}
}
}
|
Java
|
["5\n\n2\n\n4\n\n6\n\n8\n\n60"]
|
2 seconds
|
["1 0 1\n3 2 1\n12 7 1\n42 27 1\n341102826 248150916 1"]
|
NoteIn the first test case, Alex wins if he receives the card $$$2$$$ (he plays it, and Boris cannot respond). If Alex receives the card $$$1$$$, the game ends in a draw.In the second test case: Alex wins if he receives the cards $$$[3, 4]$$$, $$$[2, 4]$$$ or $$$[1, 4]$$$; Boris wins if Alex receives the cards $$$[1, 2]$$$ or $$$[1, 3]$$$; the game ends in a draw if Alex receives the cards $$$[2, 3]$$$.
|
Java 11
|
standard input
|
[
"combinatorics",
"constructive algorithms",
"dp",
"games"
] |
63fc2fb08d248f8ccdb3f5364c96dac1
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 30$$$) — the number of test cases. Then, $$$t$$$ lines follow. The $$$i$$$-th line contains one even integer $$$n$$$ ($$$2 \le n \le 60$$$).
| 1,500
|
For each test case, print three integers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. Since the answers can be large, print them modulo $$$998244353$$$.
|
standard output
| |
PASSED
|
a378a5dc1fdb2827a7887cb9f4bb5203
|
train_109.jsonl
|
1664462100
|
Consider a game with $$$n$$$ cards ($$$n$$$ is even). Each card has a number written on it, between $$$1$$$ and $$$n$$$. All numbers on the cards are different. We say that a card with number $$$x$$$ is stronger than a card with number $$$y$$$ if $$$x > y$$$.Two players, Alex and Boris, play this game. In the beginning, each of them receives exactly $$$\frac{n}{2}$$$ cards, so each card belongs to exactly one player. Then, they take turns. Alex goes first, then Boris, then Alex again, and so on.On a player's turn, he must play exactly one of his cards. Then, if the opponent doesn't have any cards stronger than the card played, the opponent loses, and the game ends. Otherwise, the opponent has to play a stronger card (exactly one card as well). These two cards are removed from the game, and the turn ends. If there are no cards left, the game ends in a draw; otherwise it's the opponent's turn.Consider all possible ways to distribute the cards between two players, so that each of them receives exactly half of the cards. You have to calculate three numbers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. You may assume that both players play optimally (i. e. if a player can win no matter how his opponent plays, he wins). Two ways to distribute the cards are different if there is at least one card such that, in one of these ways, it is given to Alex, and in the other way, it is given to Boris.For example, suppose $$$n = 4$$$, Alex receives the cards $$$[2, 3]$$$, and Boris receives the cards $$$[1, 4]$$$. Then the game may go as follows: if Alex plays the card $$$2$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$3$$$. So, the game ends in a draw; if Alex plays the card $$$3$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$2$$$. So, the game ends in a draw. So, in this case, the game ends in a draw.
|
512 megabytes
|
import java.io.*;
import java.util.*;
public class Main {
static Scanner sc;
static long mod = 998244353;
static long fact[];
static long inverse[];
public static void main (String[] args){
sc = new Scanner(System.in);
fact = new long[65];
inverse = new long[65];
fact[0] = 1;
inverse[0] = 1;
for(int i = 1;i<fact.length;i++){
fact[i] = (fact[i-1] * i)%mod;
inverse[i] = binaryExpo(fact[i], mod-2);
}
int t = sc.nextInt();
while(t-->0){
solve();
}
}
public static long nck(int n,int k){
return ((fact[n] * (inverse[n-k])%mod) * inverse[k])%mod;
}
public static long binaryExpo(long base,long expo){
if(expo == 0)return 1;
long val;
if(expo%2 == 1){
val = (binaryExpo(base, expo/2));
val = (val * val)%mod;
val = (val * base)%mod;
}
else{
val = binaryExpo(base, expo/2);
val = (val*val)%mod;
}
return (val%mod);
}
public static void solve(){
int n = sc.nextInt();
long[]winner = new long[61];
long[]loser = new long[61];
for(int i = 2;i<=60;i+=2){
long total = (nck(i,i/2));
long alex = (nck(i-1,i/2));
alex = (alex + loser[i-2])%mod;
winner[i] = alex;
loser[i] = (((total - ((alex + 1)%mod)) + mod)%mod);
}
System.out.println(winner[n] + " " + loser[n] + " " + (1));
}
}
|
Java
|
["5\n\n2\n\n4\n\n6\n\n8\n\n60"]
|
2 seconds
|
["1 0 1\n3 2 1\n12 7 1\n42 27 1\n341102826 248150916 1"]
|
NoteIn the first test case, Alex wins if he receives the card $$$2$$$ (he plays it, and Boris cannot respond). If Alex receives the card $$$1$$$, the game ends in a draw.In the second test case: Alex wins if he receives the cards $$$[3, 4]$$$, $$$[2, 4]$$$ or $$$[1, 4]$$$; Boris wins if Alex receives the cards $$$[1, 2]$$$ or $$$[1, 3]$$$; the game ends in a draw if Alex receives the cards $$$[2, 3]$$$.
|
Java 11
|
standard input
|
[
"combinatorics",
"constructive algorithms",
"dp",
"games"
] |
63fc2fb08d248f8ccdb3f5364c96dac1
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 30$$$) — the number of test cases. Then, $$$t$$$ lines follow. The $$$i$$$-th line contains one even integer $$$n$$$ ($$$2 \le n \le 60$$$).
| 1,500
|
For each test case, print three integers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. Since the answers can be large, print them modulo $$$998244353$$$.
|
standard output
| |
PASSED
|
8a9cd04ef542a93cdb2f532196659ca3
|
train_109.jsonl
|
1664462100
|
Consider a game with $$$n$$$ cards ($$$n$$$ is even). Each card has a number written on it, between $$$1$$$ and $$$n$$$. All numbers on the cards are different. We say that a card with number $$$x$$$ is stronger than a card with number $$$y$$$ if $$$x > y$$$.Two players, Alex and Boris, play this game. In the beginning, each of them receives exactly $$$\frac{n}{2}$$$ cards, so each card belongs to exactly one player. Then, they take turns. Alex goes first, then Boris, then Alex again, and so on.On a player's turn, he must play exactly one of his cards. Then, if the opponent doesn't have any cards stronger than the card played, the opponent loses, and the game ends. Otherwise, the opponent has to play a stronger card (exactly one card as well). These two cards are removed from the game, and the turn ends. If there are no cards left, the game ends in a draw; otherwise it's the opponent's turn.Consider all possible ways to distribute the cards between two players, so that each of them receives exactly half of the cards. You have to calculate three numbers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. You may assume that both players play optimally (i. e. if a player can win no matter how his opponent plays, he wins). Two ways to distribute the cards are different if there is at least one card such that, in one of these ways, it is given to Alex, and in the other way, it is given to Boris.For example, suppose $$$n = 4$$$, Alex receives the cards $$$[2, 3]$$$, and Boris receives the cards $$$[1, 4]$$$. Then the game may go as follows: if Alex plays the card $$$2$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$3$$$. So, the game ends in a draw; if Alex plays the card $$$3$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$2$$$. So, the game ends in a draw. So, in this case, the game ends in a draw.
|
512 megabytes
|
/*
"Everything in the universe is balanced. Every disappointment
you face in life will be balanced by something good for you!
Keep going, never give up."
Just have Patience + 1...
*/
import java.util.*;
import java.lang.*;
import java.io.*;
public class SolutionC {
static int MAX = 65, MOD = 998244353;
static long[] factorial = new long[MAX];
public static void main(String[] args) throws Exception {
out = new PrintWriter(new BufferedOutputStream(System.out));
sc = new FastReader();
factorial[0] = 1;
for (int i = 1; i < MAX; i++) {
factorial[i] = factorial[i - 1] * i;
factorial[i] %= MOD;
}
int test = sc.nextInt();
for (int t = 1; t <= test; t++) {
solve(t);
}
out.close();
}
private static void solve(int t) {
int n = sc.nextInt();
long[] wins = playCardGame(n, 0);
out.println(wins[0] + " " + wins[1] + " " + 1);
}
private static long[] playCardGame(int n, int player) {
long[] wins = new long[2];
if (n == 0) {
return wins;
}
wins[player] = nChooseK(n - 1, n / 2 - 1); // choose card with max value n, and then any distribution will make player win
long[] next = playCardGame(n - 2, 1 - player);
wins[player] = addMod(wins[player], next[player]);
wins[1 - player] = subMod(subMod(nChooseK(n, n / 2), wins[player]), 1); // matches won by opponent = (total matches - matches won by player - total draw matches).
return wins;
}
private static long nChooseK(int n, int k) {
if (n < 0 || k < 0) {
return 0;
}
if (n < k) {
return 0;
}
long p = factorial[n];
long q = (factorial[n - k]) % MOD;
long q2 = (factorial[k]) % MOD;
long q3 = mulMod(q, q2);
p = mulMod(p, power(q3, MOD - 2));
return p;
}
private static long power(long a, int b) {
long ans = 1;
a %= MOD;
while (b > 0) {
if ((b & 1) == 1) {
ans *= a;
ans %= MOD;
}
b >>= 1;
a = (a * a) % MOD;
}
return ans;
}
private static long addMod(long a, long b) {
return (a % MOD + b % MOD) % MOD;
}
private static long subMod(long a, long b) {
return (a % MOD - b % MOD + MOD) % MOD;
}
private static long mulMod(long a, long b) {
return ((a % MOD) * (b % MOD)) % MOD;
}
private static long divMod(long a, long b) {
return mulMod(a, power(b, MOD - 2));
}
public static FastReader sc;
public static PrintWriter out;
static class FastReader
{
BufferedReader br;
StringTokenizer str;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (str == null || !str.hasMoreElements())
{
try
{
str = new StringTokenizer(br.readLine());
}
catch (IOException lastMonthOfVacation)
{
lastMonthOfVacation.printStackTrace();
}
}
return str.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException lastMonthOfVacation)
{
lastMonthOfVacation.printStackTrace();
}
return str;
}
}
}
|
Java
|
["5\n\n2\n\n4\n\n6\n\n8\n\n60"]
|
2 seconds
|
["1 0 1\n3 2 1\n12 7 1\n42 27 1\n341102826 248150916 1"]
|
NoteIn the first test case, Alex wins if he receives the card $$$2$$$ (he plays it, and Boris cannot respond). If Alex receives the card $$$1$$$, the game ends in a draw.In the second test case: Alex wins if he receives the cards $$$[3, 4]$$$, $$$[2, 4]$$$ or $$$[1, 4]$$$; Boris wins if Alex receives the cards $$$[1, 2]$$$ or $$$[1, 3]$$$; the game ends in a draw if Alex receives the cards $$$[2, 3]$$$.
|
Java 11
|
standard input
|
[
"combinatorics",
"constructive algorithms",
"dp",
"games"
] |
63fc2fb08d248f8ccdb3f5364c96dac1
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 30$$$) — the number of test cases. Then, $$$t$$$ lines follow. The $$$i$$$-th line contains one even integer $$$n$$$ ($$$2 \le n \le 60$$$).
| 1,500
|
For each test case, print three integers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. Since the answers can be large, print them modulo $$$998244353$$$.
|
standard output
| |
PASSED
|
091e7551c3b075fb381880c043bbd6c7
|
train_109.jsonl
|
1664462100
|
Consider a game with $$$n$$$ cards ($$$n$$$ is even). Each card has a number written on it, between $$$1$$$ and $$$n$$$. All numbers on the cards are different. We say that a card with number $$$x$$$ is stronger than a card with number $$$y$$$ if $$$x > y$$$.Two players, Alex and Boris, play this game. In the beginning, each of them receives exactly $$$\frac{n}{2}$$$ cards, so each card belongs to exactly one player. Then, they take turns. Alex goes first, then Boris, then Alex again, and so on.On a player's turn, he must play exactly one of his cards. Then, if the opponent doesn't have any cards stronger than the card played, the opponent loses, and the game ends. Otherwise, the opponent has to play a stronger card (exactly one card as well). These two cards are removed from the game, and the turn ends. If there are no cards left, the game ends in a draw; otherwise it's the opponent's turn.Consider all possible ways to distribute the cards between two players, so that each of them receives exactly half of the cards. You have to calculate three numbers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. You may assume that both players play optimally (i. e. if a player can win no matter how his opponent plays, he wins). Two ways to distribute the cards are different if there is at least one card such that, in one of these ways, it is given to Alex, and in the other way, it is given to Boris.For example, suppose $$$n = 4$$$, Alex receives the cards $$$[2, 3]$$$, and Boris receives the cards $$$[1, 4]$$$. Then the game may go as follows: if Alex plays the card $$$2$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$3$$$. So, the game ends in a draw; if Alex plays the card $$$3$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$2$$$. So, the game ends in a draw. So, in this case, the game ends in a draw.
|
512 megabytes
|
import java.io.*;
import java.util.*;
public class Main {
static Scanner sc;
static long mod = 998244353;
static long fact[];
static long inverse[];
public static void main (String[] args){
sc = new Scanner(System.in);
fact = new long[65];
inverse = new long[65];
fact[0] = 1;
inverse[0] = 1;
for(int i = 1;i<fact.length;i++){
fact[i] = (fact[i-1] * i)%mod;
inverse[i] = binaryExpo(fact[i], mod-2);
}
int t = sc.nextInt();
while(t-->0){
solve();
}
}
public static long nck(int n,int k){
return ((fact[n] * (inverse[n-k])%mod) * inverse[k])%mod;
}
public static long binaryExpo(long base,long expo){
if(expo == 0)return 1;
long val;
if(expo%2 == 1){
val = (binaryExpo(base, expo/2));
val = (val * val)%mod;
val = (val * base)%mod;
}
else{
val = binaryExpo(base, expo/2);
val = (val*val)%mod;
}
return (val%mod);
}
public static void solve(){
int n = sc.nextInt();
long[]winner = new long[61];
long[]loser = new long[61];
for(int i = 2;i<=60;i+=2){
long total = (nck(i,i/2));
long alex = (nck(i-1,i/2));
alex = (alex + loser[i-2])%mod;
winner[i] = alex;
loser[i] = (((total - ((alex + 1)%mod)) + mod)%mod);
}
System.out.println(winner[n] + " " + loser[n] + " " + (1));
}
}
|
Java
|
["5\n\n2\n\n4\n\n6\n\n8\n\n60"]
|
2 seconds
|
["1 0 1\n3 2 1\n12 7 1\n42 27 1\n341102826 248150916 1"]
|
NoteIn the first test case, Alex wins if he receives the card $$$2$$$ (he plays it, and Boris cannot respond). If Alex receives the card $$$1$$$, the game ends in a draw.In the second test case: Alex wins if he receives the cards $$$[3, 4]$$$, $$$[2, 4]$$$ or $$$[1, 4]$$$; Boris wins if Alex receives the cards $$$[1, 2]$$$ or $$$[1, 3]$$$; the game ends in a draw if Alex receives the cards $$$[2, 3]$$$.
|
Java 11
|
standard input
|
[
"combinatorics",
"constructive algorithms",
"dp",
"games"
] |
63fc2fb08d248f8ccdb3f5364c96dac1
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 30$$$) — the number of test cases. Then, $$$t$$$ lines follow. The $$$i$$$-th line contains one even integer $$$n$$$ ($$$2 \le n \le 60$$$).
| 1,500
|
For each test case, print three integers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. Since the answers can be large, print them modulo $$$998244353$$$.
|
standard output
| |
PASSED
|
3a8ec51677361e07a2f3b67ca47b2870
|
train_109.jsonl
|
1664462100
|
Consider a game with $$$n$$$ cards ($$$n$$$ is even). Each card has a number written on it, between $$$1$$$ and $$$n$$$. All numbers on the cards are different. We say that a card with number $$$x$$$ is stronger than a card with number $$$y$$$ if $$$x > y$$$.Two players, Alex and Boris, play this game. In the beginning, each of them receives exactly $$$\frac{n}{2}$$$ cards, so each card belongs to exactly one player. Then, they take turns. Alex goes first, then Boris, then Alex again, and so on.On a player's turn, he must play exactly one of his cards. Then, if the opponent doesn't have any cards stronger than the card played, the opponent loses, and the game ends. Otherwise, the opponent has to play a stronger card (exactly one card as well). These two cards are removed from the game, and the turn ends. If there are no cards left, the game ends in a draw; otherwise it's the opponent's turn.Consider all possible ways to distribute the cards between two players, so that each of them receives exactly half of the cards. You have to calculate three numbers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. You may assume that both players play optimally (i. e. if a player can win no matter how his opponent plays, he wins). Two ways to distribute the cards are different if there is at least one card such that, in one of these ways, it is given to Alex, and in the other way, it is given to Boris.For example, suppose $$$n = 4$$$, Alex receives the cards $$$[2, 3]$$$, and Boris receives the cards $$$[1, 4]$$$. Then the game may go as follows: if Alex plays the card $$$2$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$3$$$. So, the game ends in a draw; if Alex plays the card $$$3$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$2$$$. So, the game ends in a draw. So, in this case, the game ends in a draw.
|
512 megabytes
|
import java.util.*;
import java.io.*;
public class Main{
static int mod = 998244353;
static long fact[] = new long[65];
static long inv[] = new long [65];
static Scanner sc = new Scanner (System.in);
static BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
static StreamTokenizer st =new StreamTokenizer(bf);
static PrintWriter pw = new PrintWriter(new OutputStreamWriter(System.out));
public static void main(String[]args){
int t = sc.nextInt();
pre();
while(t-->0)
solve();
pw.flush();
}
static void solve(){
int n = sc.nextInt();
long a=0,b=0;
boolean f = true;
for (int i = n ; i >=2 ; i-=2) {
long x = C(i-1,i/2-1);
long y = (i == 2)? 0:C(i-2,i/2-2);
if(f) {
a = (a+x)%mod;
b = (b+y)%mod;
f = false;
}
else {
a = (a+y)%mod;
b = (b+x)%mod;
f = true;
}
}
pw.println(a+" "+b+" 1");
}
static long C(int a,int b) {
return fact[a]*inv[b]%mod*inv[a-b]%mod;
}
static void pre(){
fact[0] = 1;inv[0] = 1;
for(int i = 1 ; i <=60 ; i++) {
fact[i] = fact[i-1]*i%mod;
inv[i] = (fpow(i)*inv[i-1])%mod;
}
}
static long fpow(int aa) {
long res = 1;
long a = aa%mod;
int b = mod-2;
while(b>0) {
if(b%2 == 1) res = res*a%mod;
a = a*a%mod;
b /=2;
}
return res;
}
}
|
Java
|
["5\n\n2\n\n4\n\n6\n\n8\n\n60"]
|
2 seconds
|
["1 0 1\n3 2 1\n12 7 1\n42 27 1\n341102826 248150916 1"]
|
NoteIn the first test case, Alex wins if he receives the card $$$2$$$ (he plays it, and Boris cannot respond). If Alex receives the card $$$1$$$, the game ends in a draw.In the second test case: Alex wins if he receives the cards $$$[3, 4]$$$, $$$[2, 4]$$$ or $$$[1, 4]$$$; Boris wins if Alex receives the cards $$$[1, 2]$$$ or $$$[1, 3]$$$; the game ends in a draw if Alex receives the cards $$$[2, 3]$$$.
|
Java 11
|
standard input
|
[
"combinatorics",
"constructive algorithms",
"dp",
"games"
] |
63fc2fb08d248f8ccdb3f5364c96dac1
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 30$$$) — the number of test cases. Then, $$$t$$$ lines follow. The $$$i$$$-th line contains one even integer $$$n$$$ ($$$2 \le n \le 60$$$).
| 1,500
|
For each test case, print three integers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. Since the answers can be large, print them modulo $$$998244353$$$.
|
standard output
| |
PASSED
|
efcc6856b7970032afc258ca182d6b55
|
train_109.jsonl
|
1664462100
|
Consider a game with $$$n$$$ cards ($$$n$$$ is even). Each card has a number written on it, between $$$1$$$ and $$$n$$$. All numbers on the cards are different. We say that a card with number $$$x$$$ is stronger than a card with number $$$y$$$ if $$$x > y$$$.Two players, Alex and Boris, play this game. In the beginning, each of them receives exactly $$$\frac{n}{2}$$$ cards, so each card belongs to exactly one player. Then, they take turns. Alex goes first, then Boris, then Alex again, and so on.On a player's turn, he must play exactly one of his cards. Then, if the opponent doesn't have any cards stronger than the card played, the opponent loses, and the game ends. Otherwise, the opponent has to play a stronger card (exactly one card as well). These two cards are removed from the game, and the turn ends. If there are no cards left, the game ends in a draw; otherwise it's the opponent's turn.Consider all possible ways to distribute the cards between two players, so that each of them receives exactly half of the cards. You have to calculate three numbers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. You may assume that both players play optimally (i. e. if a player can win no matter how his opponent plays, he wins). Two ways to distribute the cards are different if there is at least one card such that, in one of these ways, it is given to Alex, and in the other way, it is given to Boris.For example, suppose $$$n = 4$$$, Alex receives the cards $$$[2, 3]$$$, and Boris receives the cards $$$[1, 4]$$$. Then the game may go as follows: if Alex plays the card $$$2$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$3$$$. So, the game ends in a draw; if Alex plays the card $$$3$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$2$$$. So, the game ends in a draw. So, in this case, the game ends in a draw.
|
512 megabytes
|
/**
* 09/29/22 morning
* https://codeforces.com/contest/1739/problem/B
*/
// package codeforce.ecf.r136;
import java.util.*;
import java.io.*;
public class C {
static PrintWriter pw;
final int mod = 998244353;
long[] fact, ifact, inv;
int N = 70;
void comb_init() {
fact = new long[N];
ifact = new long[N];
inv = new long[N];
fact[0] = ifact[0] = inv[1] = 1;
for (int i = 2; i < N; i++) inv[i] = (mod - mod / i) * inv[mod % i] % mod;
for (int i = 1; i < N; i++) {
fact[i] = fact[i - 1] * i % mod;
ifact[i] = ifact[i - 1] * inv[i] % mod;
}
}
long comb(int n, int k) {
if (n < k || k < 0) return 0;
return fact[n] * ifact[k] % mod * ifact[n - k] % mod;
}
// C(n-1, n/2)
/*
[1, 2, 6] [1, 3, 6] [1, 4, 6] [1, 5, 6]
[2, 3, 6] [2, 4, 6] [2, 5, 6]
[3, 4, 6] [3, 5, 6]
[4, 5, 6]
*/
void solve(int n) {
// tr(n, n / 2 - 1);
// long tot = comb(n, n / 2);
// long A = comb(n - 1, n / 2 - 1), B = tot - 1 - A;
// tr("tot", tot, "A", A, "B", B);
long A = 0, B = 0;
for (int i = 0; i < n / 2; i++) {
int rest = n - i * 2;
A += comb(rest - 2, rest / 2);
A %= mod;
B += comb(rest - 2, rest / 2);
B %= mod;
if (i % 2 == 0) {
A += comb(rest - 2, rest / 2 - 1);
A %= mod;
} else {
B += comb(rest - 2, rest / 2 - 1);
B %= mod;
}
}
pr(A + " " + B + " " + 1);
}
private void run() {
// read_write_file(); // comment this before submission
FastScanner fs = new FastScanner();
comb_init();
int t = fs.nextInt();
while (t-- > 0) {
int n = fs.nextInt();
solve(n);
}
}
private final String INPUT = "input.txt";
private final String OUTPUT = "output.txt";
void read_write_file() {
FileInputStream instream = null;
PrintStream outstream = null;
try {
instream = new FileInputStream(INPUT);
outstream = new PrintStream(new FileOutputStream(OUTPUT));
System.setIn(instream);
System.setOut(outstream);
} catch (Exception e) {
}
}
public static void main(String[] args) {
pw = new PrintWriter(System.out);
new C().run();
pw.close();
}
<T> void pr(T t) {
pw.println(t);
}
class FastScanner {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
int[] readArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++) a[i] = nextInt();
return a;
}
}
void tr(Object... o) {
pw.println(Arrays.deepToString(o));
}
}
|
Java
|
["5\n\n2\n\n4\n\n6\n\n8\n\n60"]
|
2 seconds
|
["1 0 1\n3 2 1\n12 7 1\n42 27 1\n341102826 248150916 1"]
|
NoteIn the first test case, Alex wins if he receives the card $$$2$$$ (he plays it, and Boris cannot respond). If Alex receives the card $$$1$$$, the game ends in a draw.In the second test case: Alex wins if he receives the cards $$$[3, 4]$$$, $$$[2, 4]$$$ or $$$[1, 4]$$$; Boris wins if Alex receives the cards $$$[1, 2]$$$ or $$$[1, 3]$$$; the game ends in a draw if Alex receives the cards $$$[2, 3]$$$.
|
Java 11
|
standard input
|
[
"combinatorics",
"constructive algorithms",
"dp",
"games"
] |
63fc2fb08d248f8ccdb3f5364c96dac1
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 30$$$) — the number of test cases. Then, $$$t$$$ lines follow. The $$$i$$$-th line contains one even integer $$$n$$$ ($$$2 \le n \le 60$$$).
| 1,500
|
For each test case, print three integers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. Since the answers can be large, print them modulo $$$998244353$$$.
|
standard output
| |
PASSED
|
0049dfd9627c38d1a2292e0a1df4ac68
|
train_109.jsonl
|
1664462100
|
Consider a game with $$$n$$$ cards ($$$n$$$ is even). Each card has a number written on it, between $$$1$$$ and $$$n$$$. All numbers on the cards are different. We say that a card with number $$$x$$$ is stronger than a card with number $$$y$$$ if $$$x > y$$$.Two players, Alex and Boris, play this game. In the beginning, each of them receives exactly $$$\frac{n}{2}$$$ cards, so each card belongs to exactly one player. Then, they take turns. Alex goes first, then Boris, then Alex again, and so on.On a player's turn, he must play exactly one of his cards. Then, if the opponent doesn't have any cards stronger than the card played, the opponent loses, and the game ends. Otherwise, the opponent has to play a stronger card (exactly one card as well). These two cards are removed from the game, and the turn ends. If there are no cards left, the game ends in a draw; otherwise it's the opponent's turn.Consider all possible ways to distribute the cards between two players, so that each of them receives exactly half of the cards. You have to calculate three numbers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. You may assume that both players play optimally (i. e. if a player can win no matter how his opponent plays, he wins). Two ways to distribute the cards are different if there is at least one card such that, in one of these ways, it is given to Alex, and in the other way, it is given to Boris.For example, suppose $$$n = 4$$$, Alex receives the cards $$$[2, 3]$$$, and Boris receives the cards $$$[1, 4]$$$. Then the game may go as follows: if Alex plays the card $$$2$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$3$$$. So, the game ends in a draw; if Alex plays the card $$$3$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$2$$$. So, the game ends in a draw. So, in this case, the game ends in a draw.
|
512 megabytes
|
/*
Author: Spidey2182
#: 1739C
*/
import java.util.*;
public class CardGame implements Runnable {
static ContestScanner sc = new ContestScanner();
static ContestPrinter out = new ContestPrinter();
public static void main(String[] args) {
new Thread(null, new CardGame(), "main", 1 << 28).start();
}
public void run() {
fact = new long[61];
invFact = new long[61];
fact[0] = 1;
for (int i = 1; i < 61; i++)
fact[i] = fact[i - 1] % MOD9 * i % MOD9;
invFact[60] = pow(fact[60], MOD9 - 2);
for (int i = 60; i >= 1; i--)
invFact[i - 1] = invFact[i] % MOD9 * i % MOD9;
int testCases=sc.nextInt();
for(int testCase=1; testCase<=testCases; testCase++) {
int n=sc.nextInt();
long a=0, b=0, d=1;
for(int i=n, j=1; i>0; i-=4, j+=2) {
a = (a + nCr(i - 1, (n >> 1) - j)) % MOD9;
b = (b + nCr(i - 2, (n >> 1) - j - 1)) % MOD9;
b = (b + nCr(i - 3, (n >> 1) - j)) % MOD9;
a = (a + nCr(i - 4, (n >> 1) - j)) % MOD9;
}
out.printf("%d %d %d\n", a, b, d);
}
out.flush();
out.close();
}
static long[] fact;
static long[] invFact;
static long nCr(int n, int r) {
if (r < 0 || r > n)
return 0;
return fact[n] % MOD9 * invFact[r] % MOD9 * invFact[n - r] % MOD9;
}
static long pow(long a, long b) {
long ret = 1;
long tmp = a;
while (b > 0) {
if ((b & 1) == 1) {
ret = (ret * tmp) % MOD9;
}
tmp = (tmp * tmp) % MOD9;
b = b >> 1;
}
return ret;
}
static final int MOD = (int) 1e9 + 7;
static final int MOD9 = 998244353;
static int gcd(int a, int b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
static long gcd(long a, long b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
}
//Credits: https://github.com/NASU41/AtCoderLibraryForJava/tree/master/ContestIO
class ContestScanner {
private final java.io.InputStream in;
private final byte[] buffer = new byte[1024];
private int ptr = 0;
private int buflen = 0;
private static final long LONG_MAX_TENTHS = 922337203685477580L;
private static final int LONG_MAX_LAST_DIGIT = 7;
private static final int LONG_MIN_LAST_DIGIT = 8;
public ContestScanner(java.io.InputStream in) {
this.in = in;
}
public ContestScanner(java.io.File file) throws java.io.FileNotFoundException {
this(new java.io.BufferedInputStream(new java.io.FileInputStream(file)));
}
public ContestScanner() {
this(System.in);
}
private boolean hasNextByte() {
if (ptr < buflen) {
return true;
} else {
ptr = 0;
try {
buflen = in.read(buffer);
} catch (java.io.IOException e) {
e.printStackTrace();
}
if (buflen <= 0) {
return false;
}
}
return true;
}
private int readByte() {
if (hasNextByte()) return buffer[ptr++];
else return -1;
}
private static boolean isPrintableChar(int c) {
return 33 <= c && c <= 126;
}
public boolean hasNext() {
while (hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++;
return hasNextByte();
}
public String next() {
if (!hasNext()) throw new NoSuchElementException();
StringBuilder sb = new StringBuilder();
int b = readByte();
while (isPrintableChar(b)) {
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
public long nextLong() {
if (!hasNext()) throw new NoSuchElementException();
long n = 0;
boolean minus = false;
int b = readByte();
if (b == '-') {
minus = true;
b = readByte();
}
if (b < '0' || '9' < b) {
throw new NumberFormatException();
}
while (true) {
if ('0' <= b && b <= '9') {
int digit = b - '0';
if (n >= LONG_MAX_TENTHS) {
if (n == LONG_MAX_TENTHS) {
if (minus) {
if (digit <= LONG_MIN_LAST_DIGIT) {
n = -n * 10 - digit;
b = readByte();
if (!isPrintableChar(b)) {
return n;
} else if (b < '0' || '9' < b) {
throw new NumberFormatException(
String.format("%d%s... is not number", n, Character.toString(b))
);
}
}
} else {
if (digit <= LONG_MAX_LAST_DIGIT) {
n = n * 10 + digit;
b = readByte();
if (!isPrintableChar(b)) {
return n;
} else if (b < '0' || '9' < b) {
throw new NumberFormatException(
String.format("%d%s... is not number", n, Character.toString(b))
);
}
}
}
}
throw new ArithmeticException(
String.format("%s%d%d... overflows long.", minus ? "-" : "", n, digit)
);
}
n = n * 10 + digit;
} else if (b == -1 || !isPrintableChar(b)) {
return minus ? -n : n;
} else {
throw new NumberFormatException();
}
b = readByte();
}
}
public int nextInt() {
long nl = nextLong();
if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE) throw new NumberFormatException();
return (int) nl;
}
public double nextDouble() {
return Double.parseDouble(next());
}
public long[] nextLongArray(int length) {
long[] array = new long[length];
for (int i = 0; i < length; i++) array[i] = this.nextLong();
return array;
}
public long[] nextLongArray(int length, java.util.function.LongUnaryOperator map) {
long[] array = new long[length];
for (int i = 0; i < length; i++) array[i] = map.applyAsLong(this.nextLong());
return array;
}
public int[] nextIntArray(int length) {
int[] array = new int[length];
for (int i = 0; i < length; i++) array[i] = this.nextInt();
return array;
}
public int[] nextIntArray(int length, java.util.function.IntUnaryOperator map) {
int[] array = new int[length];
for (int i = 0; i < length; i++) array[i] = map.applyAsInt(this.nextInt());
return array;
}
public double[] nextDoubleArray(int length) {
double[] array = new double[length];
for (int i = 0; i < length; i++) array[i] = this.nextDouble();
return array;
}
public double[] nextDoubleArray(int length, java.util.function.DoubleUnaryOperator map) {
double[] array = new double[length];
for (int i = 0; i < length; i++) array[i] = map.applyAsDouble(this.nextDouble());
return array;
}
public long[][] nextLongMatrix(int height, int width) {
long[][] mat = new long[height][width];
for (int h = 0; h < height; h++)
for (int w = 0; w < width; w++) {
mat[h][w] = this.nextLong();
}
return mat;
}
public int[][] nextIntMatrix(int height, int width) {
int[][] mat = new int[height][width];
for (int h = 0; h < height; h++)
for (int w = 0; w < width; w++) {
mat[h][w] = this.nextInt();
}
return mat;
}
public double[][] nextDoubleMatrix(int height, int width) {
double[][] mat = new double[height][width];
for (int h = 0; h < height; h++)
for (int w = 0; w < width; w++) {
mat[h][w] = this.nextDouble();
}
return mat;
}
public char[][] nextCharMatrix(int height, int width) {
char[][] mat = new char[height][width];
for (int h = 0; h < height; h++) {
String s = this.next();
for (int w = 0; w < width; w++) {
mat[h][w] = s.charAt(w);
}
}
return mat;
}
}
class ContestPrinter extends java.io.PrintWriter {
public ContestPrinter(java.io.PrintStream stream) {
super(stream);
}
public ContestPrinter(java.io.File file) throws java.io.FileNotFoundException {
super(new java.io.PrintStream(file));
}
public ContestPrinter() {
super(System.out);
}
private static String dtos(double x, int n) {
StringBuilder sb = new StringBuilder();
if (x < 0) {
sb.append('-');
x = -x;
}
x += Math.pow(10, -n) / 2;
sb.append((long) x);
sb.append(".");
x -= (long) x;
for (int i = 0; i < n; i++) {
x *= 10;
sb.append((int) x);
x -= (int) x;
}
return sb.toString();
}
@Override
public void print(float f) {
super.print(dtos(f, 20));
}
@Override
public void println(float f) {
super.println(dtos(f, 20));
}
@Override
public void print(double d) {
super.print(dtos(d, 20));
}
@Override
public void println(double d) {
super.println(dtos(d, 20));
}
public void printArray(int[] array, String separator) {
int n = array.length;
if (n == 0) {
super.println();
return;
}
for (int i = 0; i < n - 1; i++) {
super.print(array[i]);
super.print(separator);
}
super.println(array[n - 1]);
}
public void printArray(int[] array) {
this.printArray(array, " ");
}
public void printArray(int[] array, String separator, java.util.function.IntUnaryOperator map) {
int n = array.length;
if (n == 0) {
super.println();
return;
}
for (int i = 0; i < n - 1; i++) {
super.print(map.applyAsInt(array[i]));
super.print(separator);
}
super.println(map.applyAsInt(array[n - 1]));
}
public void printArray(int[] array, java.util.function.IntUnaryOperator map) {
this.printArray(array, " ", map);
}
public void printArray(long[] array, String separator) {
int n = array.length;
if (n == 0) {
super.println();
return;
}
for (int i = 0; i < n - 1; i++) {
super.print(array[i]);
super.print(separator);
}
super.println(array[n - 1]);
}
public void printArray(long[] array) {
this.printArray(array, " ");
}
public void printArray(long[] array, String separator, java.util.function.LongUnaryOperator map) {
int n = array.length;
if (n == 0) {
super.println();
return;
}
for (int i = 0; i < n - 1; i++) {
super.print(map.applyAsLong(array[i]));
super.print(separator);
}
super.println(map.applyAsLong(array[n - 1]));
}
public void printArray(long[] array, java.util.function.LongUnaryOperator map) {
this.printArray(array, " ", map);
}
public <T> void printArray(T[] array, String separator) {
int n = array.length;
if (n == 0) {
super.println();
return;
}
for (int i = 0; i < n - 1; i++) {
super.print(array[i]);
super.print(separator);
}
super.println(array[n - 1]);
}
public <T> void printArray(T[] array) {
this.printArray(array, " ");
}
public <T> void printArray(T[] array, String separator, java.util.function.UnaryOperator<T> map) {
int n = array.length;
if (n == 0) {
super.println();
return;
}
for (int i = 0; i < n - 1; i++) {
super.print(map.apply(array[i]));
super.print(separator);
}
super.println(map.apply(array[n - 1]));
}
public <T> void printArray(T[] array, java.util.function.UnaryOperator<T> map) {
this.printArray(array, " ", map);
}
}
|
Java
|
["5\n\n2\n\n4\n\n6\n\n8\n\n60"]
|
2 seconds
|
["1 0 1\n3 2 1\n12 7 1\n42 27 1\n341102826 248150916 1"]
|
NoteIn the first test case, Alex wins if he receives the card $$$2$$$ (he plays it, and Boris cannot respond). If Alex receives the card $$$1$$$, the game ends in a draw.In the second test case: Alex wins if he receives the cards $$$[3, 4]$$$, $$$[2, 4]$$$ or $$$[1, 4]$$$; Boris wins if Alex receives the cards $$$[1, 2]$$$ or $$$[1, 3]$$$; the game ends in a draw if Alex receives the cards $$$[2, 3]$$$.
|
Java 11
|
standard input
|
[
"combinatorics",
"constructive algorithms",
"dp",
"games"
] |
63fc2fb08d248f8ccdb3f5364c96dac1
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 30$$$) — the number of test cases. Then, $$$t$$$ lines follow. The $$$i$$$-th line contains one even integer $$$n$$$ ($$$2 \le n \le 60$$$).
| 1,500
|
For each test case, print three integers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. Since the answers can be large, print them modulo $$$998244353$$$.
|
standard output
| |
PASSED
|
b7ab004ba33f4691761e0558090781a6
|
train_109.jsonl
|
1664462100
|
Consider a game with $$$n$$$ cards ($$$n$$$ is even). Each card has a number written on it, between $$$1$$$ and $$$n$$$. All numbers on the cards are different. We say that a card with number $$$x$$$ is stronger than a card with number $$$y$$$ if $$$x > y$$$.Two players, Alex and Boris, play this game. In the beginning, each of them receives exactly $$$\frac{n}{2}$$$ cards, so each card belongs to exactly one player. Then, they take turns. Alex goes first, then Boris, then Alex again, and so on.On a player's turn, he must play exactly one of his cards. Then, if the opponent doesn't have any cards stronger than the card played, the opponent loses, and the game ends. Otherwise, the opponent has to play a stronger card (exactly one card as well). These two cards are removed from the game, and the turn ends. If there are no cards left, the game ends in a draw; otherwise it's the opponent's turn.Consider all possible ways to distribute the cards between two players, so that each of them receives exactly half of the cards. You have to calculate three numbers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. You may assume that both players play optimally (i. e. if a player can win no matter how his opponent plays, he wins). Two ways to distribute the cards are different if there is at least one card such that, in one of these ways, it is given to Alex, and in the other way, it is given to Boris.For example, suppose $$$n = 4$$$, Alex receives the cards $$$[2, 3]$$$, and Boris receives the cards $$$[1, 4]$$$. Then the game may go as follows: if Alex plays the card $$$2$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$3$$$. So, the game ends in a draw; if Alex plays the card $$$3$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$2$$$. So, the game ends in a draw. So, in this case, the game ends in a draw.
|
512 megabytes
|
// Working program with FastReader
import java.io.*;
import java.util.*;
public class Main {
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try {
if(st.hasMoreTokens()){
str = st.nextToken("\n");
}
else{
str = br.readLine();
}
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
public static long mod=998244353l;
public static void main(String[] args)
{
FastReader fs = new FastReader();
long t,n,i,x,y,k,ans,tot;
t=fs.nextInt();
while(t > 0){
t--;
n=fs.nextInt();
tot = ncr(n,n/2);
n--;
k=n/2;
ans=ncr(n,k);
//System.out.println(ans);
n-=3;
k-=2;
while(k>=0){
ans=(ans+ncr(n,k))%mod;
n--;
ans=(ans+ncr(n,k))%mod;
n-=3;
k-=2;
}
System.out.println(ans+" "+(tot-ans-1+mod)%mod+" "+1);
}
}
static long ncr(long n,long k){
long ans=1l;
if(k==0)
return ans;
for(int i=1;i<=k;i++,n--){
ans*=n;
ans/=i;
}
return (ans%mod);
}
}
|
Java
|
["5\n\n2\n\n4\n\n6\n\n8\n\n60"]
|
2 seconds
|
["1 0 1\n3 2 1\n12 7 1\n42 27 1\n341102826 248150916 1"]
|
NoteIn the first test case, Alex wins if he receives the card $$$2$$$ (he plays it, and Boris cannot respond). If Alex receives the card $$$1$$$, the game ends in a draw.In the second test case: Alex wins if he receives the cards $$$[3, 4]$$$, $$$[2, 4]$$$ or $$$[1, 4]$$$; Boris wins if Alex receives the cards $$$[1, 2]$$$ or $$$[1, 3]$$$; the game ends in a draw if Alex receives the cards $$$[2, 3]$$$.
|
Java 11
|
standard input
|
[
"combinatorics",
"constructive algorithms",
"dp",
"games"
] |
63fc2fb08d248f8ccdb3f5364c96dac1
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 30$$$) — the number of test cases. Then, $$$t$$$ lines follow. The $$$i$$$-th line contains one even integer $$$n$$$ ($$$2 \le n \le 60$$$).
| 1,500
|
For each test case, print three integers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. Since the answers can be large, print them modulo $$$998244353$$$.
|
standard output
| |
PASSED
|
d7b003d071a3471c6883b2f6337a9b52
|
train_109.jsonl
|
1664462100
|
Consider a game with $$$n$$$ cards ($$$n$$$ is even). Each card has a number written on it, between $$$1$$$ and $$$n$$$. All numbers on the cards are different. We say that a card with number $$$x$$$ is stronger than a card with number $$$y$$$ if $$$x > y$$$.Two players, Alex and Boris, play this game. In the beginning, each of them receives exactly $$$\frac{n}{2}$$$ cards, so each card belongs to exactly one player. Then, they take turns. Alex goes first, then Boris, then Alex again, and so on.On a player's turn, he must play exactly one of his cards. Then, if the opponent doesn't have any cards stronger than the card played, the opponent loses, and the game ends. Otherwise, the opponent has to play a stronger card (exactly one card as well). These two cards are removed from the game, and the turn ends. If there are no cards left, the game ends in a draw; otherwise it's the opponent's turn.Consider all possible ways to distribute the cards between two players, so that each of them receives exactly half of the cards. You have to calculate three numbers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. You may assume that both players play optimally (i. e. if a player can win no matter how his opponent plays, he wins). Two ways to distribute the cards are different if there is at least one card such that, in one of these ways, it is given to Alex, and in the other way, it is given to Boris.For example, suppose $$$n = 4$$$, Alex receives the cards $$$[2, 3]$$$, and Boris receives the cards $$$[1, 4]$$$. Then the game may go as follows: if Alex plays the card $$$2$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$3$$$. So, the game ends in a draw; if Alex plays the card $$$3$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$2$$$. So, the game ends in a draw. So, in this case, the game ends in a draw.
|
512 megabytes
|
import java.util.*;
public class Solution {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
long mod = 998244353;
long [] alex = new long[31];
long [] boris = new long[31];
alex[1] = 1;
boris[1] = 0;
for(int i = 2; i<31; i++){
int temp = i*2;
long tot = printNcR(temp, i);
long req = printNcR(temp-1, i-1);
alex[i] = (req + boris[i-1])%mod;
boris[i] = (tot-alex[i]-1)%mod;
}
int T = scan.nextInt();
while(T-->0){
int n = scan.nextInt();
System.out.println(alex[n/2] + " " + boris[n/2] + " 1");
}
}
static long printNcR(int n, int r)
{
// p holds the value of n*(n-1)*(n-2)...,
// k holds the value of r*(r-1)...
long p = 1, k = 1;
// C(n, r) == C(n, n-r),
// choosing the smaller value
if (n - r < r) {
r = n - r;
}
if (r != 0) {
while (r > 0) {
p *= n;
k *= r;
// gcd of p, k
long m = __gcd(p, k);
// dividing by gcd, to simplify
// product division by their gcd
// saves from the overflow
p /= m;
k /= m;
n--;
r--;
}
// k should be simplified to 1
// as C(n, r) is a natural number
// (denominator should be 1 ) .
}
else {
p = 1;
}
// if our approach is correct p = ans and k =1
return p;
}
static long __gcd(long n1, long n2)
{
long gcd = 1;
for (int i = 1; i <= n1 && i <= n2; ++i) {
// Checks if i is factor of both integers
if (n1 % i == 0 && n2 % i == 0) {
gcd = i;
}
}
return gcd;
}
}
|
Java
|
["5\n\n2\n\n4\n\n6\n\n8\n\n60"]
|
2 seconds
|
["1 0 1\n3 2 1\n12 7 1\n42 27 1\n341102826 248150916 1"]
|
NoteIn the first test case, Alex wins if he receives the card $$$2$$$ (he plays it, and Boris cannot respond). If Alex receives the card $$$1$$$, the game ends in a draw.In the second test case: Alex wins if he receives the cards $$$[3, 4]$$$, $$$[2, 4]$$$ or $$$[1, 4]$$$; Boris wins if Alex receives the cards $$$[1, 2]$$$ or $$$[1, 3]$$$; the game ends in a draw if Alex receives the cards $$$[2, 3]$$$.
|
Java 11
|
standard input
|
[
"combinatorics",
"constructive algorithms",
"dp",
"games"
] |
63fc2fb08d248f8ccdb3f5364c96dac1
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 30$$$) — the number of test cases. Then, $$$t$$$ lines follow. The $$$i$$$-th line contains one even integer $$$n$$$ ($$$2 \le n \le 60$$$).
| 1,500
|
For each test case, print three integers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. Since the answers can be large, print them modulo $$$998244353$$$.
|
standard output
| |
PASSED
|
ab2b1a42043e90b426c6c8daf2a4fe95
|
train_109.jsonl
|
1664462100
|
Consider a game with $$$n$$$ cards ($$$n$$$ is even). Each card has a number written on it, between $$$1$$$ and $$$n$$$. All numbers on the cards are different. We say that a card with number $$$x$$$ is stronger than a card with number $$$y$$$ if $$$x > y$$$.Two players, Alex and Boris, play this game. In the beginning, each of them receives exactly $$$\frac{n}{2}$$$ cards, so each card belongs to exactly one player. Then, they take turns. Alex goes first, then Boris, then Alex again, and so on.On a player's turn, he must play exactly one of his cards. Then, if the opponent doesn't have any cards stronger than the card played, the opponent loses, and the game ends. Otherwise, the opponent has to play a stronger card (exactly one card as well). These two cards are removed from the game, and the turn ends. If there are no cards left, the game ends in a draw; otherwise it's the opponent's turn.Consider all possible ways to distribute the cards between two players, so that each of them receives exactly half of the cards. You have to calculate three numbers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. You may assume that both players play optimally (i. e. if a player can win no matter how his opponent plays, he wins). Two ways to distribute the cards are different if there is at least one card such that, in one of these ways, it is given to Alex, and in the other way, it is given to Boris.For example, suppose $$$n = 4$$$, Alex receives the cards $$$[2, 3]$$$, and Boris receives the cards $$$[1, 4]$$$. Then the game may go as follows: if Alex plays the card $$$2$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$3$$$. So, the game ends in a draw; if Alex plays the card $$$3$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$2$$$. So, the game ends in a draw. So, in this case, the game ends in a draw.
|
512 megabytes
|
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
public class A_Two_Elevators{
static final long bluff[][]={
{1,0},
{3,2},
{12,7},
{42,27},
{153,98},
{560,363},
{2079,1352},
{7787,5082},
{29392,19227},
{111605,73150},
{425866,279565},
{1631643,1072512},
{6272812,4127787},
{24186087,15930512},
{93489272,61628247},
{362168442,238911947},
{407470704,927891162},
{474237047,614943428},
{319176974,87534470},
{131938523,955113935},
{558075845,644336680},
{544270478,253841470},
{209493498,700054910},
{859106804,457241336},
{921005664,6522991},
{366933608,353887625},
{142064435,432533537},
{741221694,874398972},
{297907370,545598131},
{341102826,248150916}
};
public static void main (String[] args) throws java.lang.Exception
{
FastReader sc = new FastReader() ;
int npp=sc.nextInt();
for(int ippp=0;ippp<npp;ippp++){
int n =sc.nextInt();
n=n/2-1;
System.out.println(bluff[n][0]+" "+bluff[n][1]+" "+1);
}
}
// static final class Utils {
// private static class Shuffler {
// private static void shuffle(int[] x) {
// final Random r = new Random();
// for (int i = 0; i <= x.length - 2; i++) {
// final int j = i + r.nextInt(x.length - i);
// swap(x, i, j);
// }
// }
// private static void shuffle(long[] x) {
// final Random r = new Random();
// for (int i = 0; i <= x.length - 2; i++) {
// final int j = i + r.nextInt(x.length - i);
// swap(x, i, j);
// }
// }
// private static void swap(int[] x, int i, int j) {
// final int t = x[i];
// x[i] = x[j];
// x[j] = t;
// }
// private static void swap(long[] x, int i, int j) {
// final long t = x[i];
// x[i] = x[j];
// x[j] = t;
// }
// }
// public static void shuffleSort(int[] arr) {
// Shuffler.shuffle(arr);
// Arrays.sort(arr);
// }
// public static void shuffleSort(long[] arr) {
// Shuffler.shuffle(arr);
// Arrays.sort(arr);
// }
// private Utils() {}
// }
static class FastReader {
private static final int BUFFER_SIZE = 1 << 16;
private final DataInputStream din;
private final byte[] buffer;
private int bufferPointer, bytesRead;
FastReader() {
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
FastReader(String file_name) throws IOException {
din = new DataInputStream(new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String readLine() throws IOException {
final byte[] buf = new byte[1024]; // line length
int cnt = 0, c;
while ((c = read()) != -1) {
if (c == '\n') {
break;
}
buf[cnt++] = (byte) c;
}
return new String(buf, 0, cnt);
}
public int nextSign() throws IOException {
byte c = read();
while ('+' != c && '-' != c) {
c = read();
}
return '+' == c ? 0 : 1;
}
private static boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); }
private int skip() throws IOException {
int b;
//noinspection StatementWithEmptyBody
while ((b = read()) != -1 && isSpaceChar(b)) {}
return b;
}
public char nc() throws IOException {
return (char) skip();
}
public String next() throws IOException {
int b = skip();
final StringBuilder sb = new StringBuilder();
while (!isSpaceChar(b)) { // when nextLine, (isSpaceChar(b) && b != ' ')
sb.appendCodePoint(b);
b = read();
}
return sb.toString();
}
public int nextInt() throws IOException {
int ret = 0;
byte c = read();
while (c <= ' ') {
c = read();
}
final boolean neg = c == '-';
if (neg) { c = read(); }
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg) { return -ret; }
return ret;
}
public int[] nextIntArray(int n) throws IOException {
final int[] res = new int[n];
for (int i = 0; i < n; i++) {
res[i] = nextInt();
}
return res;
}
public long nextLong() throws IOException {
long ret = 0;
byte c = read();
while (c <= ' ') { c = read(); }
final boolean neg = c == '-';
if (neg) { c = read(); }
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (neg) { return -ret; }
return ret;
}
public long[] nextLongArray(int n) throws IOException {
final long[] res = new long[n];
for (int i = 0; i < n; i++) {
res[i] = nextLong();
}
return res;
}
public double nextDouble() throws IOException {
double ret = 0, div = 1;
byte c = read();
while (c <= ' ') { c = read(); }
final boolean neg = c == '-';
if (neg) { c = read(); }
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (c == '.') {
while ((c = read()) >= '0' && c <= '9') {
ret += (c - '0') / (div *= 10);
}
}
if (neg) { return -ret; }
return ret;
}
private void fillBuffer() throws IOException {
bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
if (bytesRead == -1) { buffer[0] = -1; }
}
private byte read() throws IOException {
if (bufferPointer == bytesRead) { fillBuffer(); }
return buffer[bufferPointer++];
}
public void close() throws IOException {
din.close();
}
}
}
|
Java
|
["5\n\n2\n\n4\n\n6\n\n8\n\n60"]
|
2 seconds
|
["1 0 1\n3 2 1\n12 7 1\n42 27 1\n341102826 248150916 1"]
|
NoteIn the first test case, Alex wins if he receives the card $$$2$$$ (he plays it, and Boris cannot respond). If Alex receives the card $$$1$$$, the game ends in a draw.In the second test case: Alex wins if he receives the cards $$$[3, 4]$$$, $$$[2, 4]$$$ or $$$[1, 4]$$$; Boris wins if Alex receives the cards $$$[1, 2]$$$ or $$$[1, 3]$$$; the game ends in a draw if Alex receives the cards $$$[2, 3]$$$.
|
Java 11
|
standard input
|
[
"combinatorics",
"constructive algorithms",
"dp",
"games"
] |
63fc2fb08d248f8ccdb3f5364c96dac1
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 30$$$) — the number of test cases. Then, $$$t$$$ lines follow. The $$$i$$$-th line contains one even integer $$$n$$$ ($$$2 \le n \le 60$$$).
| 1,500
|
For each test case, print three integers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. Since the answers can be large, print them modulo $$$998244353$$$.
|
standard output
| |
PASSED
|
37f927d4890c6bb52d8a9add95d2234b
|
train_109.jsonl
|
1664462100
|
Consider a game with $$$n$$$ cards ($$$n$$$ is even). Each card has a number written on it, between $$$1$$$ and $$$n$$$. All numbers on the cards are different. We say that a card with number $$$x$$$ is stronger than a card with number $$$y$$$ if $$$x > y$$$.Two players, Alex and Boris, play this game. In the beginning, each of them receives exactly $$$\frac{n}{2}$$$ cards, so each card belongs to exactly one player. Then, they take turns. Alex goes first, then Boris, then Alex again, and so on.On a player's turn, he must play exactly one of his cards. Then, if the opponent doesn't have any cards stronger than the card played, the opponent loses, and the game ends. Otherwise, the opponent has to play a stronger card (exactly one card as well). These two cards are removed from the game, and the turn ends. If there are no cards left, the game ends in a draw; otherwise it's the opponent's turn.Consider all possible ways to distribute the cards between two players, so that each of them receives exactly half of the cards. You have to calculate three numbers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. You may assume that both players play optimally (i. e. if a player can win no matter how his opponent plays, he wins). Two ways to distribute the cards are different if there is at least one card such that, in one of these ways, it is given to Alex, and in the other way, it is given to Boris.For example, suppose $$$n = 4$$$, Alex receives the cards $$$[2, 3]$$$, and Boris receives the cards $$$[1, 4]$$$. Then the game may go as follows: if Alex plays the card $$$2$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$3$$$. So, the game ends in a draw; if Alex plays the card $$$3$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$2$$$. So, the game ends in a draw. So, in this case, the game ends in a draw.
|
512 megabytes
|
import java.util.*;
import java.lang.*;
import java.io.*;
public class Main
{
static long dp1[];
static long dp2[];
static long maxi , ok;
static long mod = 998244353;
// static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
public static void main (String[] args) throws java.lang.Exception
{
FastReader sc =new FastReader();
int t=sc.nextInt();
dp1 = new long[60];
dp2 = new long[60];
for(int i=1;i<=30;i++)
{
maxi = 2 * i;
ok = 1;
for(int j=0;j<=maxi/2-1;j++)
{
ok = ok * (maxi - j);
ok = ok / (j + 1);
}
dp1[i] = 1;
for(int j=0;j<=maxi/2-1;j++)
{
dp1[i] = dp1[i] * (maxi - 1 - j);
dp1[i] = dp1[i] / (j + 1);
}
dp1[i] = (dp1[i] + dp2[i - 1]) % mod;
dp2[i] = (ok - dp1[i] - 1) % mod;
}
// int t=1;
while(t-->0)
{
int n = sc.nextInt();
System.out.println(dp1[n/2]+" "+dp2[n/2]+" "+1);
}
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble()
{
return Double.parseDouble(next());
}
float nextFloat()
{
return Float.parseFloat(next());
}
String nextLine()
{
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
int[] readArray(int n) {
int[] a=new int[n];
for (int i=0; i<n; i++) a[i]=nextInt();
return a;
}
long[] readArrayLong(int n) {
long[] a=new long[n];
for (int i=0; i<n; i++) a[i]=nextLong();
return a;
}
}
static class FenwickTree
{
//Binary Indexed Tree
//1 indexed
public int[] tree;
public int size;
public FenwickTree(int size)
{
this.size = size;
tree = new int[size+5];
}
public void add(int i, int v)
{
while(i <= size)
{
tree[i] += v;
i += i&-i;
}
}
public int find(int i)
{
int res = 0;
while(i >= 1)
{
res += tree[i];
i -= i&-i;
}
return res;
}
public int find(int l, int r)
{
return find(r)-find(l-1);
}
}
public static int[] radixSort2(int[] a)
{
int n = a.length;
int[] c0 = new int[0x101];
int[] c1 = new int[0x101];
int[] c2 = new int[0x101];
int[] c3 = new int[0x101];
for(int v : a) {
c0[(v&0xff)+1]++;
c1[(v>>>8&0xff)+1]++;
c2[(v>>>16&0xff)+1]++;
c3[(v>>>24^0x80)+1]++;
}
for(int i = 0;i < 0xff;i++) {
c0[i+1] += c0[i];
c1[i+1] += c1[i];
c2[i+1] += c2[i];
c3[i+1] += c3[i];
}
int[] t = new int[n];
for(int v : a)t[c0[v&0xff]++] = v;
for(int v : t)a[c1[v>>>8&0xff]++] = v;
for(int v : a)t[c2[v>>>16&0xff]++] = v;
for(int v : t)a[c3[v>>>24^0x80]++] = v;
return a;
}
private static long mergeAndCount(int[] arr, int l,
int m, int r)
{
// Left subarray
int[] left = Arrays.copyOfRange(arr, l, m + 1);
// Right subarray
int[] right = Arrays.copyOfRange(arr, m + 1, r + 1);
int i = 0, j = 0, k = l;long swaps = 0;
while (i < left.length && j < right.length) {
if (left[i] < right[j])
arr[k++] = left[i++];
else {
arr[k++] = right[j++];
swaps += (m + 1) - (l + i);
}
}
while (i < left.length)
arr[k++] = left[i++];
while (j < right.length)
arr[k++] = right[j++];
return swaps;
}
// Merge sort function
private static long mergeSortAndCount(int[] arr, int l,
int r)
{
// Keeps track of the inversion count at a
// particular node of the recursion tree
long count = 0;
if (l < r) {
int m = (l + r) / 2;
// Total inversion count = left subarray count
// + right subarray count + merge count
// Left subarray count
count += mergeSortAndCount(arr, l, m);
// Right subarray count
count += mergeSortAndCount(arr, m + 1, r);
// Merge count
count += mergeAndCount(arr, l, m, r);
}
return count;
}
static void my_sort(long[] arr)
{
ArrayList<Long> list = new ArrayList<>();
for(int i=0;i<arr.length;i++)
{
list.add(arr[i]);
}
Collections.sort(list);
for(int i=0;i<arr.length;i++)
{
arr[i] = list.get(i);
}
}
static void reverse_sorted(int[] arr)
{
ArrayList<Integer> list = new ArrayList<>();
for(int i=0;i<arr.length;i++)
{
list.add(arr[i]);
}
Collections.sort(list , Collections.reverseOrder());
for(int i=0;i<arr.length;i++)
{
arr[i] = list.get(i);
}
}
static int LowerBound(int a[], int x) { // x is the target value or key
int l=-1,r=a.length;
while(l+1<r) {
int m=(l+r)>>>1;
if(a[m]>=x) r=m;
else l=m;
}
return r;
}
static int UpperBound(ArrayList<Integer> list, int x) {// x is the key or target value
int l=-1,r=list.size();
while(l+1<r) {
int m=(l+r)>>>1;
if(list.get(m)<=x) l=m;
else r=m;
}
return l+1;
}
public static HashMap<Integer, Integer> sortByValue(HashMap<Integer, Integer> hm)
{
// Create a list from elements of HashMap
List<Map.Entry<Integer, Integer> > list =
new LinkedList<Map.Entry<Integer, Integer> >(hm.entrySet());
// Sort the list
Collections.sort(list, new Comparator<Map.Entry<Integer, Integer> >() {
public int compare(Map.Entry<Integer, Integer> o1,
Map.Entry<Integer, Integer> o2)
{
return (o1.getValue()).compareTo(o2.getValue());
}
});
// put data from sorted list to hashmap
HashMap<Integer, Integer> temp = new LinkedHashMap<Integer, Integer>();
for (Map.Entry<Integer, Integer> aa : list) {
temp.put(aa.getKey(), aa.getValue());
}
return temp;
}
static class Queue_Pair implements Comparable<Queue_Pair> {
int first , second;
public Queue_Pair(int first, int second) {
this.first=first;
this.second=second;
}
public int compareTo(Queue_Pair o) {
return Integer.compare(o.first, first);
}
}
static void leftRotate(int arr[], int d, int n)
{
for (int i = 0; i < d; i++)
leftRotatebyOne(arr, n);
}
static void leftRotatebyOne(int arr[], int n)
{
int i, temp;
temp = arr[0];
for (i = 0; i < n - 1; i++)
arr[i] = arr[i + 1];
arr[n-1] = temp;
}
static boolean isPalindrome(String str)
{
// Pointers pointing to the beginning
// and the end of the string
int i = 0, j = str.length() - 1;
// While there are characters to compare
while (i < j) {
// If there is a mismatch
if (str.charAt(i) != str.charAt(j))
return false;
// Increment first pointer and
// decrement the other
i++;
j--;
}
// Given string is a palindrome
return true;
}
static boolean palindrome_array(char arr[], int n)
{
// Initialise flag to zero.
int flag = 0;
// Loop till array size n/2.
for (int i = 0; i <= n / 2 && n != 0; i++) {
// Check if first and last element are different
// Then set flag to 1.
if (arr[i] != arr[n - i - 1]) {
flag = 1;
break;
}
}
// If flag is set then print Not Palindrome
// else print Palindrome.
if (flag == 1)
return false;
else
return true;
}
static boolean allElementsEqual(int[] arr,int n)
{
int z=0;
for(int i=0;i<n-1;i++)
{
if(arr[i]==arr[i+1])
{
z++;
}
}
if(z==n-1)
{
return true;
}
else
{
return false;
}
}
static boolean allElementsDistinct(int[] arr,int n)
{
int z=0;
for(int i=0;i<n-1;i++)
{
if(arr[i]!=arr[i+1])
{
z++;
}
}
if(z==n-1)
{
return true;
}
else
{
return false;
}
}
public static void reverse(int[] array)
{
// Length of the array
int n = array.length;
// Swaping the first half elements with last half
// elements
for (int i = 0; i < n / 2; i++) {
// Storing the first half elements temporarily
int temp = array[i];
// Assigning the first half to the last half
array[i] = array[n - i - 1];
// Assigning the last half to the first half
array[n - i - 1] = temp;
}
}
public static void reverse_Long(long[] array)
{
// Length of the array
int n = array.length;
// Swaping the first half elements with last half
// elements
for (int i = 0; i < n / 2; i++) {
// Storing the first half elements temporarily
long temp = array[i];
// Assigning the first half to the last half
array[i] = array[n - i - 1];
// Assigning the last half to the first half
array[n - i - 1] = temp;
}
}
static boolean isSorted(int[] a)
{
for (int i = 0; i < a.length - 1; i++)
{
if (a[i] > a[i + 1]) {
return false;
}
}
return true;
}
static boolean isReverseSorted(int[] a)
{
for (int i = 0; i < a.length - 1; i++)
{
if (a[i] < a[i + 1]) {
return false;
}
}
return true;
}
static int[] rearrangeEvenAndOdd(int arr[], int n)
{
ArrayList<Integer> list = new ArrayList<>();
for(int i=0;i<n;i++)
{
if(arr[i]%2==0)
{
list.add(arr[i]);
}
}
for(int i=0;i<n;i++)
{
if(arr[i]%2!=0)
{
list.add(arr[i]);
}
}
int len = list.size();
int[] array = list.stream().mapToInt(i->i).toArray();
return array;
}
static long[] rearrangeEvenAndOddLong(long arr[], int n)
{
ArrayList<Long> list = new ArrayList<>();
for(int i=0;i<n;i++)
{
if(arr[i]%2==0)
{
list.add(arr[i]);
}
}
for(int i=0;i<n;i++)
{
if(arr[i]%2!=0)
{
list.add(arr[i]);
}
}
int len = list.size();
long[] array = list.stream().mapToLong(i->i).toArray();
return array;
}
static boolean isPrime(long n)
{
// Check if number is less than
// equal to 1
if (n <= 1)
return false;
// Check if number is 2
else if (n == 2)
return true;
// Check if n is a multiple of 2
else if (n % 2 == 0)
return false;
// If not, then just check the odds
for (long i = 3; i <= Math.sqrt(n); i += 2)
{
if (n % i == 0)
return false;
}
return true;
}
static long getSum(long n)
{
long sum = 0;
while (n != 0)
{
sum = sum + n % 10;
n = n/10;
}
return sum;
}
static int gcd(int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
static long gcdLong(long a, long b)
{
if (b == 0)
return a;
return gcdLong(b, a % b);
}
static void swap(int i, int j)
{
int temp = i;
i = j;
j = temp;
}
static int countDigit(int n)
{
return (int)Math.floor(Math.log10(n) + 1);
}
}
class Pair
{
int first;
int second;
Pair(int first , int second)
{
this.first = first;
this.second = second;
}
}
|
Java
|
["5\n\n2\n\n4\n\n6\n\n8\n\n60"]
|
2 seconds
|
["1 0 1\n3 2 1\n12 7 1\n42 27 1\n341102826 248150916 1"]
|
NoteIn the first test case, Alex wins if he receives the card $$$2$$$ (he plays it, and Boris cannot respond). If Alex receives the card $$$1$$$, the game ends in a draw.In the second test case: Alex wins if he receives the cards $$$[3, 4]$$$, $$$[2, 4]$$$ or $$$[1, 4]$$$; Boris wins if Alex receives the cards $$$[1, 2]$$$ or $$$[1, 3]$$$; the game ends in a draw if Alex receives the cards $$$[2, 3]$$$.
|
Java 11
|
standard input
|
[
"combinatorics",
"constructive algorithms",
"dp",
"games"
] |
63fc2fb08d248f8ccdb3f5364c96dac1
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 30$$$) — the number of test cases. Then, $$$t$$$ lines follow. The $$$i$$$-th line contains one even integer $$$n$$$ ($$$2 \le n \le 60$$$).
| 1,500
|
For each test case, print three integers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. Since the answers can be large, print them modulo $$$998244353$$$.
|
standard output
| |
PASSED
|
73aa22949dd96882ad72994f39e99ce6
|
train_109.jsonl
|
1664462100
|
Consider a game with $$$n$$$ cards ($$$n$$$ is even). Each card has a number written on it, between $$$1$$$ and $$$n$$$. All numbers on the cards are different. We say that a card with number $$$x$$$ is stronger than a card with number $$$y$$$ if $$$x > y$$$.Two players, Alex and Boris, play this game. In the beginning, each of them receives exactly $$$\frac{n}{2}$$$ cards, so each card belongs to exactly one player. Then, they take turns. Alex goes first, then Boris, then Alex again, and so on.On a player's turn, he must play exactly one of his cards. Then, if the opponent doesn't have any cards stronger than the card played, the opponent loses, and the game ends. Otherwise, the opponent has to play a stronger card (exactly one card as well). These two cards are removed from the game, and the turn ends. If there are no cards left, the game ends in a draw; otherwise it's the opponent's turn.Consider all possible ways to distribute the cards between two players, so that each of them receives exactly half of the cards. You have to calculate three numbers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. You may assume that both players play optimally (i. e. if a player can win no matter how his opponent plays, he wins). Two ways to distribute the cards are different if there is at least one card such that, in one of these ways, it is given to Alex, and in the other way, it is given to Boris.For example, suppose $$$n = 4$$$, Alex receives the cards $$$[2, 3]$$$, and Boris receives the cards $$$[1, 4]$$$. Then the game may go as follows: if Alex plays the card $$$2$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$3$$$. So, the game ends in a draw; if Alex plays the card $$$3$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$2$$$. So, the game ends in a draw. So, in this case, the game ends in a draw.
|
512 megabytes
|
/* package codechef; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
//public class codechef
public class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
int arrayofnum[][]={{1,0,1},
{3,2,1},
{12,7,1},
{42,27,1},
{153,98,1},
{560,363,1},
{2079,1352,1},
{7787,5082,1},
{29392,19227,1},
{111605,73150,1},
{425866,279565,1},
{1631643,1072512,1},
{6272812,4127787,1},
{24186087,15930512,1},
{93489272,61628247,1},
{362168442,238911947,1},
{407470704,927891162,1},
{474237047,614943428,1},
{319176974,87534470,1},
{131938523,955113935,1},
{558075845,644336680,1},
{544270478,253841470,1},
{209493498,700054910,1},
{859106804,457241336,1},
{921005664,6522991,1},
{366933608,353887625,1},
{142064435,432533537,1},
{741221694,874398972,1},
{297907370,545598131,1},
{341102826,248150916,1}
};
while(t-->0)
{
int gulshan=sc.nextInt();
gulshan=gulshan/2-1;
System.out.println(arrayofnum[gulshan][0]+" "+arrayofnum[gulshan][1]+" "+arrayofnum[gulshan][2]);
}
// your code goes here
}
}
//int p=sc.nextInt();
//int m=sc.nextLong();
//int arr[]=new int[n];
//arr[i]=sc.nextInt();
//String p=sc.next();
//for(int i=0;i<n;i++)
//char ch=s.charAt();
//Collections.sort();
//long arr[]=new long[(int)n]; (int)
// for(long i=0;i<n;i++)
// {
// long p=sc.nextLong();
// if(hm1.containsKey(p))
// {
// hm1.put(p,hm1.get(p)+1);
// }
// else
// {
// hm1.put(p,1);
// }
// }
// for(Map.Entry e:hm1.entrySet)
// {
// if(hm2.containsKey(e.getKey())
// {
// hm2.remove(e.getKey());
// hm1.remove(e.getKey());
// }
// }
|
Java
|
["5\n\n2\n\n4\n\n6\n\n8\n\n60"]
|
2 seconds
|
["1 0 1\n3 2 1\n12 7 1\n42 27 1\n341102826 248150916 1"]
|
NoteIn the first test case, Alex wins if he receives the card $$$2$$$ (he plays it, and Boris cannot respond). If Alex receives the card $$$1$$$, the game ends in a draw.In the second test case: Alex wins if he receives the cards $$$[3, 4]$$$, $$$[2, 4]$$$ or $$$[1, 4]$$$; Boris wins if Alex receives the cards $$$[1, 2]$$$ or $$$[1, 3]$$$; the game ends in a draw if Alex receives the cards $$$[2, 3]$$$.
|
Java 11
|
standard input
|
[
"combinatorics",
"constructive algorithms",
"dp",
"games"
] |
63fc2fb08d248f8ccdb3f5364c96dac1
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 30$$$) — the number of test cases. Then, $$$t$$$ lines follow. The $$$i$$$-th line contains one even integer $$$n$$$ ($$$2 \le n \le 60$$$).
| 1,500
|
For each test case, print three integers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. Since the answers can be large, print them modulo $$$998244353$$$.
|
standard output
| |
PASSED
|
5196d243ec25ea9a66621ad3600f5327
|
train_109.jsonl
|
1664462100
|
Consider a game with $$$n$$$ cards ($$$n$$$ is even). Each card has a number written on it, between $$$1$$$ and $$$n$$$. All numbers on the cards are different. We say that a card with number $$$x$$$ is stronger than a card with number $$$y$$$ if $$$x > y$$$.Two players, Alex and Boris, play this game. In the beginning, each of them receives exactly $$$\frac{n}{2}$$$ cards, so each card belongs to exactly one player. Then, they take turns. Alex goes first, then Boris, then Alex again, and so on.On a player's turn, he must play exactly one of his cards. Then, if the opponent doesn't have any cards stronger than the card played, the opponent loses, and the game ends. Otherwise, the opponent has to play a stronger card (exactly one card as well). These two cards are removed from the game, and the turn ends. If there are no cards left, the game ends in a draw; otherwise it's the opponent's turn.Consider all possible ways to distribute the cards between two players, so that each of them receives exactly half of the cards. You have to calculate three numbers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. You may assume that both players play optimally (i. e. if a player can win no matter how his opponent plays, he wins). Two ways to distribute the cards are different if there is at least one card such that, in one of these ways, it is given to Alex, and in the other way, it is given to Boris.For example, suppose $$$n = 4$$$, Alex receives the cards $$$[2, 3]$$$, and Boris receives the cards $$$[1, 4]$$$. Then the game may go as follows: if Alex plays the card $$$2$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$3$$$. So, the game ends in a draw; if Alex plays the card $$$3$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$2$$$. So, the game ends in a draw. So, in this case, the game ends in a draw.
|
512 megabytes
|
/* package codechef; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
public class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
BufferedReader bu=new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb=new StringBuilder();
int t=Integer.parseInt(bu.readLine()),i,N=60;
f=new long[N+1];
f[0]=1;
for(i=1;i<=N;i++) f[i]=f[i-1]*i%M;
while(t-->0)
{
int n=Integer.parseInt(bu.readLine());
long alex=0,tot=ncr(n,n/2),draw=1;
for(i=n;i-4>=0;i-=4)
{
alex=(alex+ncr(i-1,n/2-((n-i)/2)))%M; //place an A in the end
alex=(alex+ncr(i-4,n/2-((n-i)/2)-1))%M; //AAA in beginning
}
if(i>0) alex=(alex+1)%M;
long bob=(tot-alex-draw+M+M)%M;
sb.append(alex+" "+bob+" "+draw+"\n");
}
System.out.print(sb);
}
static long f[],M=998244353;
static long ncr(int n,int r)
{
if(r>n) return 0;
return f[n]*power(f[r],M-2)%M*power(f[n-r],M-2)%M;
}
static long power(long a,long b)
{
long res=1;
while(b!=0)
{
if(b%2==1) res=res*a%M;
a=a*a%M;
b>>=1;
}
return res;
}
}
|
Java
|
["5\n\n2\n\n4\n\n6\n\n8\n\n60"]
|
2 seconds
|
["1 0 1\n3 2 1\n12 7 1\n42 27 1\n341102826 248150916 1"]
|
NoteIn the first test case, Alex wins if he receives the card $$$2$$$ (he plays it, and Boris cannot respond). If Alex receives the card $$$1$$$, the game ends in a draw.In the second test case: Alex wins if he receives the cards $$$[3, 4]$$$, $$$[2, 4]$$$ or $$$[1, 4]$$$; Boris wins if Alex receives the cards $$$[1, 2]$$$ or $$$[1, 3]$$$; the game ends in a draw if Alex receives the cards $$$[2, 3]$$$.
|
Java 11
|
standard input
|
[
"combinatorics",
"constructive algorithms",
"dp",
"games"
] |
63fc2fb08d248f8ccdb3f5364c96dac1
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 30$$$) — the number of test cases. Then, $$$t$$$ lines follow. The $$$i$$$-th line contains one even integer $$$n$$$ ($$$2 \le n \le 60$$$).
| 1,500
|
For each test case, print three integers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. Since the answers can be large, print them modulo $$$998244353$$$.
|
standard output
| |
PASSED
|
b54fd06dc9f8044f1a514131f0463825
|
train_109.jsonl
|
1664462100
|
Consider a game with $$$n$$$ cards ($$$n$$$ is even). Each card has a number written on it, between $$$1$$$ and $$$n$$$. All numbers on the cards are different. We say that a card with number $$$x$$$ is stronger than a card with number $$$y$$$ if $$$x > y$$$.Two players, Alex and Boris, play this game. In the beginning, each of them receives exactly $$$\frac{n}{2}$$$ cards, so each card belongs to exactly one player. Then, they take turns. Alex goes first, then Boris, then Alex again, and so on.On a player's turn, he must play exactly one of his cards. Then, if the opponent doesn't have any cards stronger than the card played, the opponent loses, and the game ends. Otherwise, the opponent has to play a stronger card (exactly one card as well). These two cards are removed from the game, and the turn ends. If there are no cards left, the game ends in a draw; otherwise it's the opponent's turn.Consider all possible ways to distribute the cards between two players, so that each of them receives exactly half of the cards. You have to calculate three numbers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. You may assume that both players play optimally (i. e. if a player can win no matter how his opponent plays, he wins). Two ways to distribute the cards are different if there is at least one card such that, in one of these ways, it is given to Alex, and in the other way, it is given to Boris.For example, suppose $$$n = 4$$$, Alex receives the cards $$$[2, 3]$$$, and Boris receives the cards $$$[1, 4]$$$. Then the game may go as follows: if Alex plays the card $$$2$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$3$$$. So, the game ends in a draw; if Alex plays the card $$$3$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$2$$$. So, the game ends in a draw. So, in this case, the game ends in a draw.
|
512 megabytes
|
// package c1739;
//
// Educational Codeforces Round 136 (Rated for Div. 2) 2022-09-29 07:35
// C. Card Game
// https://codeforces.com/contest/1739/problem/C
// time limit per test 2 seconds; memory limit per test 512 megabytes
// public class Pseudo for 'Source should satisfy regex [^{}]*public\s+(final)?\s*class\s+(\w+).*'
//
// Consider a game with n cards (n is even). Each card has a number written on it, between 1 and n.
// All numbers on the cards are different. We say that a card with number x is stronger than a card
// with number y if x > y.
//
// Two players, Alex and Boris, play this game. In the beginning, each of them receives exactly
// \frac{n}{2} cards, so each card belongs to exactly one player. Then, they take turns. Alex goes
// first, then Boris, then Alex again, and so on.
//
// On a player's turn, he must play of his cards. Then, if the opponent doesn't have any cards than
// the card played, the opponent loses, and the game ends. Otherwise, the opponent has to play a
// stronger card (exactly one card as well). These two cards are removed from the game, and the turn
// ends. If there are no cards left, the game ends in a draw; otherwise it's the opponent's turn.
//
// Consider all possible ways to distribute the cards between two players, so that each of them
// receives exactly half of the cards. You have to calculate three numbers:
// * the number of ways to distribute the cards so that Alex wins;
// * the number of ways to distribute the cards so that Boris wins;
// * the number of ways to distribute the cards so that the game ends in a draw.
//
// You may assume that both players play optimally (i.?e. if a player can win no matter how his
// opponent plays, he wins). Two ways to distribute the cards are different if there is at least one
// card such that, in one of these ways, it is given to Alex, and in the other way, it is given to
// Boris.
//
// Input
//
// The first line contains one integer t (1 <= t <= 30)-- the number of test cases.
//
// Then, t lines follow. The i-th line contains one integer n (2 <= n <= 60).
//
// Output
//
// For each test case, print three integers:
// * the number of ways to distribute the cards so that Alex wins;
// * the number of ways to distribute the cards so that Boris wins;
// * the number of ways to distribute the cards so that the game ends in a draw.
//
// .
//
// Example
/*
input:
5
2
4
6
8
60
output:
1 0 1
3 2 1
12 7 1
42 27 1
341102826 248150916 1
*/
// Note
//
// In the first test case, Alex wins if he receives the card 2 (he plays it, and Boris cannot
// respond). If Alex receives the card 1, the game ends in a draw.
//
// In the second test case:
// * Alex wins if he receives the cards [3, 4], [2, 4] or [1, 4];
// * Boris wins if Alex receives the cards [1, 2] or [1, 3];
// * the game ends in a draw if Alex receives the cards [2, 3].
//
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.lang.invoke.MethodHandles;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
import java.util.StringTokenizer;
public class C1739C {
static final int MOD = 998244353;
static final Random RAND = new Random();
static Combination COMB = new Combination(60);
static int[] solve(int n) {
int[] ans = new int[3];
// Tie configuration for n = 8:
// 12345678
// BAABBAAB or A:{2,3,6,7} B:{1,4,5,8}
boolean aturn = true;
for (int i = n; i >= 2; i -= 2) {
// A win if get i
int v1 = COMB.choose(i-1, i/2 - 1);
int v2 = i == 2 ? 0 : COMB.choose(i-2, i/2 - 2);
if (aturn) {
ans[0] = (ans[0] + v1) % MOD;
// Assume B get i, B win if get i - 1
ans[1] = (ans[1] + v2) % MOD;
// System.out.format(" i:%d v1:%d v2:%d\n", i, v1, v2);
} else {
ans[1] = (ans[1] + v1) % MOD;
ans[0] = (ans[0] + v2) % MOD;
// System.out.format(" i:%d v1:%d v2:%d\n", i, v2, v1);
}
aturn = !aturn;
}
ans[2]++;
return ans;
}
static class Combination {
final int n;
int[] fact;
int[] finv;
public Combination(int n) {
this.n = n;
this.fact = new int[n+1];
this.finv = new int[n+1];
fact[0] = 1;
finv[0] = 1;
for (int i = 1; i <= n; i++) {
fact[i] = (int) (((long) fact[i-1] * i) % MOD);
finv[i] = (int) (((long) finv[i-1] * inverse(i)) % MOD);
}
}
// Compute (m choose k) % MOD.
public int choose(int m, int k) {
return (int) ((((long)fact[m] * finv[k] % MOD) * finv[m-k]) % MOD);
}
// compute x such that x * a == 1 % MOD
// TIP: used cache for inverse to dramatically reduce execution time.
public static int inverse(long a) {
return power(a, MOD - 2);
}
// To compute x^y % MOD
public static int power(long x, int y) {
if (y == 0) {
return 1;
}
long w = power(x, y / 2);
w = (w * w) % MOD;
return (int) (y % 2 == 0 ? w : (w * x) % MOD);
}
}
static boolean test = false;
static void doTest() {
if (!test) {
return;
}
long t0 = System.currentTimeMillis();
int m = 3;
int n = 2 * m;
int[] cnt = new int[3];
for (int i = 0; i < (1<<n); i++) {
if (Integer.bitCount(i) != m) {
continue;
}
List<Integer> numsa = new ArrayList<>();
List<Integer> numsb = new ArrayList<>();
for (int j = 0; j < n; j++) {
if ((i & (1 << j)) != 0) {
numsa.add(j+1);
} else {
numsb.add(j+1);
}
}
// System.out.format(" A: %s\n", numsa.toString());
// System.out.format(" B: %s\n", numsb.toString());
int w = 2;
for (int j = 0; j < m; j++) {
if (j % 2 == 0) {
// Alex's turn
int ib = 0;
while (ib < numsb.size() && numsb.get(ib) < numsa.get(numsa.size()-1)) {
ib++;
}
if (ib >= numsb.size()) {
w = 0;
break;
}
numsa.remove(numsa.size()-1);
numsb.remove(ib);
} else {
// Bob's turn
int ia = 0;
while (ia < numsa.size() && numsa.get(ia) < numsb.get(numsb.size()-1)) {
ia++;
}
if (ia >= numsa.size()) {
w = 1;
break;
}
numsa.remove(ia);
numsb.remove(numsb.size()-1);
}
}
System.out.format(" %s %d\n", traceBinary(i, n), w);
cnt[w]++;
}
System.out.format("%s\n", Arrays.toString(cnt));
System.out.format("%d msec\n", System.currentTimeMillis() - t0);
System.exit(0);
}
static String traceBinary(int v, int n) {
StringBuilder sb = new StringBuilder();
while (v != 0) {
sb.append((char)('0' + v % 2));
v /= 2;
}
while (sb.length() < n) {
sb.append('0');
}
return sb.reverse().toString();
}
public static void main(String[] args) {
doTest();
MyScanner in = new MyScanner();
int T = in.nextInt();
for (int t = 1; t <= T; t++) {
int n = in.nextInt();
int[] ans = solve(n);
System.out.format("%d %d %d\n", ans[0], ans[1], ans[2]);
}
}
static void output(int[] a) {
StringBuilder sb = new StringBuilder();
for (int v : a) {
sb.append(v);
sb.append(' ');
if (sb.length() > 4000) {
System.out.print(sb.toString());
sb.setLength(0);
}
}
System.out.println(sb.toString());
}
static void myAssert(boolean cond) {
if (!cond) {
throw new RuntimeException("Unexpected");
}
}
static class MyScanner {
BufferedReader br;
StringTokenizer st;
public MyScanner() {
try {
final String USERDIR = System.getProperty("user.dir");
String cname = MethodHandles.lookup().lookupClass().getCanonicalName().replace(".MyScanner", "");
cname = cname.lastIndexOf('.') > 0 ? cname.substring(cname.lastIndexOf('.') + 1) : cname;
final File fin = new File(USERDIR + "/io/c" + cname.substring(1,5) + "/" + cname + ".in");
br = new BufferedReader(new InputStreamReader(fin.exists()
? new FileInputStream(fin) : System.in));
} catch (Exception e) {
br = new BufferedReader(new InputStreamReader(System.in));
}
}
public String next() {
try {
while (st == null || !st.hasMoreElements()) {
st = new StringTokenizer(br.readLine());
}
return st.nextToken();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
}
}
|
Java
|
["5\n\n2\n\n4\n\n6\n\n8\n\n60"]
|
2 seconds
|
["1 0 1\n3 2 1\n12 7 1\n42 27 1\n341102826 248150916 1"]
|
NoteIn the first test case, Alex wins if he receives the card $$$2$$$ (he plays it, and Boris cannot respond). If Alex receives the card $$$1$$$, the game ends in a draw.In the second test case: Alex wins if he receives the cards $$$[3, 4]$$$, $$$[2, 4]$$$ or $$$[1, 4]$$$; Boris wins if Alex receives the cards $$$[1, 2]$$$ or $$$[1, 3]$$$; the game ends in a draw if Alex receives the cards $$$[2, 3]$$$.
|
Java 11
|
standard input
|
[
"combinatorics",
"constructive algorithms",
"dp",
"games"
] |
63fc2fb08d248f8ccdb3f5364c96dac1
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 30$$$) — the number of test cases. Then, $$$t$$$ lines follow. The $$$i$$$-th line contains one even integer $$$n$$$ ($$$2 \le n \le 60$$$).
| 1,500
|
For each test case, print three integers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. Since the answers can be large, print them modulo $$$998244353$$$.
|
standard output
| |
PASSED
|
198877a8fdf3d8054c1a05cff70e501e
|
train_109.jsonl
|
1664462100
|
Consider a game with $$$n$$$ cards ($$$n$$$ is even). Each card has a number written on it, between $$$1$$$ and $$$n$$$. All numbers on the cards are different. We say that a card with number $$$x$$$ is stronger than a card with number $$$y$$$ if $$$x > y$$$.Two players, Alex and Boris, play this game. In the beginning, each of them receives exactly $$$\frac{n}{2}$$$ cards, so each card belongs to exactly one player. Then, they take turns. Alex goes first, then Boris, then Alex again, and so on.On a player's turn, he must play exactly one of his cards. Then, if the opponent doesn't have any cards stronger than the card played, the opponent loses, and the game ends. Otherwise, the opponent has to play a stronger card (exactly one card as well). These two cards are removed from the game, and the turn ends. If there are no cards left, the game ends in a draw; otherwise it's the opponent's turn.Consider all possible ways to distribute the cards between two players, so that each of them receives exactly half of the cards. You have to calculate three numbers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. You may assume that both players play optimally (i. e. if a player can win no matter how his opponent plays, he wins). Two ways to distribute the cards are different if there is at least one card such that, in one of these ways, it is given to Alex, and in the other way, it is given to Boris.For example, suppose $$$n = 4$$$, Alex receives the cards $$$[2, 3]$$$, and Boris receives the cards $$$[1, 4]$$$. Then the game may go as follows: if Alex plays the card $$$2$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$3$$$. So, the game ends in a draw; if Alex plays the card $$$3$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$2$$$. So, the game ends in a draw. So, in this case, the game ends in a draw.
|
512 megabytes
|
import java.util.*;
import java.lang.*;
import java.io.*;
public final class Main {
FastReader s;
// PrintWriter out ;
public static void main(String[] args) throws java.lang.Exception {
new Main().run();
}
void run() {
// out = new PrintWriter(new OutputStreamWriter(System.out));
s = new FastReader();
solve();
}
StringBuffer sb;
int mod = 998244353;
long [][] ncr = new long[65][65];
void solve() {
sb = new StringBuffer();
ncr[0][0] = 1;
for(int i=0;i<=60;i++){
for(int j=0;j<=i;j++){
if(j==0 || j==i){
ncr[i][j] = 1;
}
else{
ncr[i][j] = (ncr[i-1][j] + ncr[i-1][j-1])%mod;
}
}
}
for (int T = s.nextInt(); T > 0; T--)
{
start();
}
System.out.print(sb);
}
static int power(int x, int y, int p)
{
// Initialize result
int res = 1;
// Update x if it is more than or
// equal to p
x = x % p;
while (y > 0) {
// If y is odd, multiply x
// with result
if (y % 2 == 1)
res = (res * x) % p;
// y must be even now
y = y >> 1; // y = y/2
x = (x * x) % p;
}
return res;
}
// Returns n^(-1) mod p
static int modInverse(int n, int p)
{
return power(n, p - 2, p);
}
// Returns nCr % p using Fermat's
// little theorem.
static int nCrModPFermat(int n, int r,
int p)
{
if (n<r)
return 0;
// Base case
if (r == 0)
return 1;
// Fill factorial array so that we
// can find all factorial of r, n
// and n-r
int[] fac = new int[n + 1];
fac[0] = 1;
for (int i = 1; i <= n; i++)
fac[i] = fac[i - 1] * i % p;
return (fac[n] * modInverse(fac[r], p)
% p * modInverse(fac[n - r], p)
% p)
% p;
}
void start()
{
int n = s.nextInt();
long total = ncr[n][n/2];
long ans1 = 0;
long ans2 = 0;
boolean p = true;
for(int i = n; i>=2; i-=2)
{
if(p)
{
int v = (int)(i/2-1);
ans1= (ans1+(ncr[i-1][v]))%mod;
}
else
{
int v = (int)(i - 2);
int rem = (i/2) - 2;
if(rem>=0 && v >=0)
ans1= (ans1+(ncr[i-2][rem]))%mod;
}
p = !p;
}
// ans1-=j;
// ans1 = (ans1+mod)%mod;
ans2 = total-ans1-1;
ans2 = (ans2+mod)%mod;
long draw = 1;
sb.append(ans1+" "+ans2+" "+draw+"\n");
}
void swap(int arr[], int i, int j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
class Pair {
int x;
int y;
// String hash;
public Pair(int x, int y) {
this.x = x;
this.y = y;
// this.hash = x+" "+y;
}
@Override
public String toString() {
return ("{ x = " + this.x + " , " + "y = " + this.y + " }");
}
@Override
public boolean equals(Object ob) {
if (this == ob)
return true;
if (ob == null || this.getClass() != ob.getClass())
return false;
Pair p = (Pair) ob;
return (p.x == this.x) && (p.y == this.y);
}
// @Override
// public int hashCode()
// {
// return hash.hashCode();
// }
}
class LongPair {
long x;
long y;
// String hash;
public LongPair(long x, long y) {
this.x = x;
this.y = y;
// this.hash = x+" "+y;
}
@Override
public String toString() {
return ("{ x = " + this.x + " , " + "y = " + this.y + " }");
}
@Override
public boolean equals(Object ob) {
if (this == ob)
return true;
if (ob == null || this.getClass() != ob.getClass())
return false;
LongPair p = (LongPair) ob;
return (p.x == this.x) && (p.y == this.y);
}
// @Override
// public int hashCode()
// {
// return hash.hashCode();
// }
}
class SegmentTree {
int[] tree;
int n;
public SegmentTree(int[] nums) {
if (nums.length > 0) {
n = nums.length;
tree = new int[n * 2];
buildTree(nums);
}
}
private void buildTree(int[] nums) {
for (int i = n, j = 0; i < 2 * n; i++, j++)
tree[i] = nums[j];
for (int i = n - 1; i > 0; --i)
tree[i] = tree[i * 2] + tree[i * 2 + 1];
}
void update(int pos, int val) {
pos += n;
tree[pos] = val;
while (pos > 0) {
int left = pos;
int right = pos;
if (pos % 2 == 0) {
right = pos + 1;
} else {
left = pos - 1;
}
// parent is updated after child is updated
tree[pos / 2] = tree[left] + tree[right];
pos /= 2;
}
}
public int sumRange(int l, int r) {
// get leaf with value 'l'
l += n;
// get leaf with value 'r'
r += n;
int sum = 0;
while (l <= r) {
if ((l % 2) == 1) {
sum += tree[l];
l++;
}
if ((r % 2) == 0) {
sum += tree[r];
r--;
}
l /= 2;
r /= 2;
}
return sum;
}
}
int gcd(int a, int b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
long gcd(long a, long b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
long power(long x, long y, long p) {
long res = 1;
x = x % p;
while (y > 0) {
if ((y & 1) > 0)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
int find(int dsu[], int i) {
if (dsu[i] == i)
return i;
dsu[i] = find(dsu, dsu[i]);
return dsu[i];
}
void union(int dsu[], int i, int j) {
int a = find(dsu, i);
int b = find(dsu, j);
dsu[a] = b;
}
static void sort(int[] a) {
ArrayList<Integer> l = new ArrayList<>();
for (int i : a)
l.add(i);
Collections.sort(l);
for (int i = 0; i < a.length; i++)
a[i] = l.get(i);
}
static void sort(long[] a) {
ArrayList<Long> l = new ArrayList<>();
for (long i : a)
l.add(i);
Collections.sort(l);
for (int i = 0; i < a.length; i++)
a[i] = l.get(i);
}
static String sort(String s) {
Character ch[] = new Character[s.length()];
for (int i = 0; i < s.length(); i++) {
ch[i] = s.charAt(i);
}
Arrays.sort(ch);
StringBuffer st = new StringBuffer("");
for (int i = 0; i < s.length(); i++) {
st.append(ch[i]);
}
return st.toString();
}
// long array input
public long[] longArr(int len) {
// long arr input
long[] arr = new long[len];
String[] strs = s.nextLine().trim().split("\\s+");
for (int i = 0; i < len; i++) {
arr[i] = Long.parseLong(strs[i]);
}
return arr;
}
// int arr input
public int[] intArr(int len) {
// long arr input
int[] arr = new int[len];
String[] strs = s.nextLine().trim().split("\\s+");
for (int i = 0; i < len; i++) {
arr[i] = Integer.parseInt(strs[i]);
}
return arr;
}
public void printArray(int[] A) {
System.out.println(Arrays.toString(A));
}
public void printArray(long[] A) {
System.out.println(Arrays.toString(A));
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
}
|
Java
|
["5\n\n2\n\n4\n\n6\n\n8\n\n60"]
|
2 seconds
|
["1 0 1\n3 2 1\n12 7 1\n42 27 1\n341102826 248150916 1"]
|
NoteIn the first test case, Alex wins if he receives the card $$$2$$$ (he plays it, and Boris cannot respond). If Alex receives the card $$$1$$$, the game ends in a draw.In the second test case: Alex wins if he receives the cards $$$[3, 4]$$$, $$$[2, 4]$$$ or $$$[1, 4]$$$; Boris wins if Alex receives the cards $$$[1, 2]$$$ or $$$[1, 3]$$$; the game ends in a draw if Alex receives the cards $$$[2, 3]$$$.
|
Java 11
|
standard input
|
[
"combinatorics",
"constructive algorithms",
"dp",
"games"
] |
63fc2fb08d248f8ccdb3f5364c96dac1
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 30$$$) — the number of test cases. Then, $$$t$$$ lines follow. The $$$i$$$-th line contains one even integer $$$n$$$ ($$$2 \le n \le 60$$$).
| 1,500
|
For each test case, print three integers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. Since the answers can be large, print them modulo $$$998244353$$$.
|
standard output
| |
PASSED
|
c8d0bb4e90b574520a95b6c877aa2ad3
|
train_109.jsonl
|
1664462100
|
Consider a game with $$$n$$$ cards ($$$n$$$ is even). Each card has a number written on it, between $$$1$$$ and $$$n$$$. All numbers on the cards are different. We say that a card with number $$$x$$$ is stronger than a card with number $$$y$$$ if $$$x > y$$$.Two players, Alex and Boris, play this game. In the beginning, each of them receives exactly $$$\frac{n}{2}$$$ cards, so each card belongs to exactly one player. Then, they take turns. Alex goes first, then Boris, then Alex again, and so on.On a player's turn, he must play exactly one of his cards. Then, if the opponent doesn't have any cards stronger than the card played, the opponent loses, and the game ends. Otherwise, the opponent has to play a stronger card (exactly one card as well). These two cards are removed from the game, and the turn ends. If there are no cards left, the game ends in a draw; otherwise it's the opponent's turn.Consider all possible ways to distribute the cards between two players, so that each of them receives exactly half of the cards. You have to calculate three numbers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. You may assume that both players play optimally (i. e. if a player can win no matter how his opponent plays, he wins). Two ways to distribute the cards are different if there is at least one card such that, in one of these ways, it is given to Alex, and in the other way, it is given to Boris.For example, suppose $$$n = 4$$$, Alex receives the cards $$$[2, 3]$$$, and Boris receives the cards $$$[1, 4]$$$. Then the game may go as follows: if Alex plays the card $$$2$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$3$$$. So, the game ends in a draw; if Alex plays the card $$$3$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$2$$$. So, the game ends in a draw. So, in this case, the game ends in a draw.
|
512 megabytes
|
import java.io.*;
public class Main2 {
public static long MOD = 998244353;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int testcase = Integer.parseInt(br.readLine());
int[][] pascal = new int[61][61];
pascal[0][0] = 1;
for (int i=1;i<61;i++) {
pascal[i][0] = pascal[i][i] = 1;
for (int j=1;j<i;j++) {
pascal[i][j] = pascal[i-1][j-1] + pascal[i-1][j];
pascal[i][j] %= MOD;
}
}
long[] alex = new long[61];
long[] bori = new long[61];
alex[2] = 1;
for (int i=4;i<=60;i+=2) {
bori[i] = alex[i-2];
alex[i] = bori[i-2];
bori[i] += pascal[i-2][i/2];
alex[i] += pascal[i-1][i/2];
alex[i] %= MOD;
bori[i] %= MOD;
}
C: while(testcase-->0) {
int n = Integer.parseInt(br.readLine());
bw.write(alex[n]+" "+bori[n]+" 1\n");
}
bw.flush();
}
}
|
Java
|
["5\n\n2\n\n4\n\n6\n\n8\n\n60"]
|
2 seconds
|
["1 0 1\n3 2 1\n12 7 1\n42 27 1\n341102826 248150916 1"]
|
NoteIn the first test case, Alex wins if he receives the card $$$2$$$ (he plays it, and Boris cannot respond). If Alex receives the card $$$1$$$, the game ends in a draw.In the second test case: Alex wins if he receives the cards $$$[3, 4]$$$, $$$[2, 4]$$$ or $$$[1, 4]$$$; Boris wins if Alex receives the cards $$$[1, 2]$$$ or $$$[1, 3]$$$; the game ends in a draw if Alex receives the cards $$$[2, 3]$$$.
|
Java 11
|
standard input
|
[
"combinatorics",
"constructive algorithms",
"dp",
"games"
] |
63fc2fb08d248f8ccdb3f5364c96dac1
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 30$$$) — the number of test cases. Then, $$$t$$$ lines follow. The $$$i$$$-th line contains one even integer $$$n$$$ ($$$2 \le n \le 60$$$).
| 1,500
|
For each test case, print three integers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. Since the answers can be large, print them modulo $$$998244353$$$.
|
standard output
| |
PASSED
|
8fdc184560e4312d19ed896f30bc8428
|
train_109.jsonl
|
1664462100
|
Consider a game with $$$n$$$ cards ($$$n$$$ is even). Each card has a number written on it, between $$$1$$$ and $$$n$$$. All numbers on the cards are different. We say that a card with number $$$x$$$ is stronger than a card with number $$$y$$$ if $$$x > y$$$.Two players, Alex and Boris, play this game. In the beginning, each of them receives exactly $$$\frac{n}{2}$$$ cards, so each card belongs to exactly one player. Then, they take turns. Alex goes first, then Boris, then Alex again, and so on.On a player's turn, he must play exactly one of his cards. Then, if the opponent doesn't have any cards stronger than the card played, the opponent loses, and the game ends. Otherwise, the opponent has to play a stronger card (exactly one card as well). These two cards are removed from the game, and the turn ends. If there are no cards left, the game ends in a draw; otherwise it's the opponent's turn.Consider all possible ways to distribute the cards between two players, so that each of them receives exactly half of the cards. You have to calculate three numbers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. You may assume that both players play optimally (i. e. if a player can win no matter how his opponent plays, he wins). Two ways to distribute the cards are different if there is at least one card such that, in one of these ways, it is given to Alex, and in the other way, it is given to Boris.For example, suppose $$$n = 4$$$, Alex receives the cards $$$[2, 3]$$$, and Boris receives the cards $$$[1, 4]$$$. Then the game may go as follows: if Alex plays the card $$$2$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$3$$$. So, the game ends in a draw; if Alex plays the card $$$3$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$2$$$. So, the game ends in a draw. So, in this case, the game ends in a draw.
|
512 megabytes
|
import java.io.*;
import java.lang.reflect.Array;
import java.math.BigInteger;
import java.util.*;
import java.util.function.BinaryOperator;
import java.util.stream.Collectors;
public class Main {
private final static long mod = 998244353;
private final static int MAXN = 1000001;
private static long power(long x, long y, long m) {
long temp;
if (y == 0)
return 1;
temp = power(x, y / 2, m);
temp = (temp * temp) % m;
if (y % 2 == 0)
return temp;
else
return ((x % m) * temp) % m;
}
private static long power(long x, long y) {
return power(x, y, mod);
}
private static long gcd(long a, long b) {
if (b == 0) return a;
return gcd(b, a % b);
}
static int nextPowerOf2(int a) {
return 1 << nextLog2(a);
}
static int nextLog2(int a) {
return (a == 0 ? 0 : 32 - Integer.numberOfLeadingZeros(a - 1));
}
private static long modInverse(long a, long m) {
long m0 = m;
long y = 0, x = 1;
if (m == 1)
return 0;
while (a > 1) {
long q = a / m;
long t = m;
m = a % m;
a = t;
t = y;
y = x - q * y;
x = t;
}
if (x < 0)
x += m0;
return x;
}
private static int[] getLogArr(int n) {
int arr[] = new int[n + 1];
for (int i = 1; i < n + 1; i++) {
arr[i] = (int) (Math.log(i) / Math.log(2) + 1e-10);
}
return arr;
}
private static int log[] = getLogArr(MAXN);
private static int getLRSpt(int st[][], int L, int R, BinaryOperator<Integer> binaryOperator) {
int j = log[R - L + 1];
return binaryOperator.apply(st[L][j], st[R - (1 << j) + 1][j]);
}
private static int[][] getSparseTable(int array[], BinaryOperator<Integer> binaryOperator) {
int k = log[array.length + 1] + 1;
int st[][] = new int[array.length + 1][k + 1];
for (int i = 0; i < array.length; i++)
st[i][0] = array[i];
for (int j = 1; j <= k; j++) {
for (int i = 0; i + (1 << j) <= array.length; i++) {
st[i][j] = binaryOperator.apply(st[i][j - 1], st[i + (1 << (j - 1))][j - 1]);
}
}
return st;
}
private static int[][] getSparseTable(List<Integer> list, BinaryOperator<Integer> binaryOperator) {
int n=list.size();
int k = log[n + 1] + 1;
int st[][] = new int[n + 1][k + 1];
for (int i = 0; i < n; i++)
st[i][0] = list.get(i);
for (int j = 1; j <= k; j++) {
for (int i = 0; i + (1 << j) <= n; i++) {
st[i][j] = binaryOperator.apply(st[i][j - 1], st[i + (1 << (j - 1))][j - 1]);
}
}
return st;
}
static class Subset {
int parent;
int rank;
@Override
public String toString() {
return "" + parent;
}
}
static int find(Subset[] subsets, int i) {
if (subsets[i].parent != i)
subsets[i].parent = find(subsets, subsets[i].parent);
return subsets[i].parent;
}
static void union(Subset[] Subsets, int x, int y) {
int xroot = find(Subsets, x);
int yroot = find(Subsets, y);
if (Subsets[xroot].rank < Subsets[yroot].rank)
Subsets[xroot].parent = yroot;
else if (Subsets[yroot].rank < Subsets[xroot].rank)
Subsets[yroot].parent = xroot;
else {
Subsets[xroot].parent = yroot;
Subsets[yroot].rank++;
}
}
private static int maxx(Integer... a) {
return Collections.max(Arrays.asList(a));
}
private static int minn(Integer... a) {
return Collections.min(Arrays.asList(a));
}
private static long maxx(Long... a) {
return Collections.max(Arrays.asList(a));
}
private static long minn(Long... a) {
return Collections.min(Arrays.asList(a));
}
private static class Pair<T extends Comparable<T>, U extends Comparable<U>> implements Comparable<Pair<T, U>> {
T a;
U b;
public Pair(T a, U b) {
this.a = a;
this.b = b;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Pair pair = (Pair) o;
return a.equals(pair.a) &&
b.equals(pair.b);
}
@Override
public int hashCode() {
return Objects.hash(a, b);
}
@Override
public String toString() {
return "(" + a +
"," + b +
')';
}
@Override
public int compareTo(Pair<T, U> o) {
return (this.a.equals(o.a) ? this.b.equals(o.b) ? 0 : this.b.compareTo(o.b) : this.a.compareTo(o.a));
}
}
public static int upperBound(List<Integer> list, int value) {
int low = 0;
int high = list.size();
while (low < high) {
final int mid = (low + high) / 2;
if (value >= list.get(mid)) {
low = mid + 1;
} else {
high = mid;
}
}
return low;
}
private static int[] getLPSArray(String pattern) {
int i, j, n = pattern.length();
int[] lps = new int[pattern.length()];
lps[0] = 0;
for (i = 1, j = 0; i < n; ) {
if (pattern.charAt(i) == pattern.charAt(j)) {
lps[i++] = ++j;
} else if (j > 0) {
j = lps[j - 1];
} else {
lps[i++] = 0;
}
}
return lps;
}
private static List<Integer> findPattern(String text, String pattern) {
List<Integer> matchedIndexes = new ArrayList<>();
if (pattern.length() == 0) {
return matchedIndexes;
}
int[] lps = getLPSArray(pattern);
int i = 0, j = 0, n = text.length(), m = pattern.length();
while (i < n) {
if (text.charAt(i) == pattern.charAt(j)) {
i++;
j++;
}
if (j == m) {
matchedIndexes.add(i - j);
j = lps[j - 1];
}
if (i < n && text.charAt(i) != pattern.charAt(j)) {
if (j > 0) {
j = lps[j - 1];
} else {
i++;
}
}
}
return matchedIndexes;
}
private static Set<Integer> getDivisors(int n) {
Set<Integer> divisors = new HashSet<>();
divisors.add(1);
divisors.add(n);
for (int i = 2; i <= Math.sqrt(n); i++) {
if (n % i == 0) {
divisors.add(i);
divisors.add(n / i);
}
}
return divisors;
}
private static long getLCM(long a, long b) {
return (Math.max(a, b) / gcd(a, b)) * Math.min(a, b);
}
static long fac[];
static long ifac[];
private static void preCompute(int n) {
fac = new long[n + 1];
ifac = new long[n + 1];
fac[0] = ifac[0] = fac[1] = ifac[1] = 1;
int i;
for (i = 2; i < n + 1; i++) {
fac[i] = (i * fac[i - 1]) % mod;
ifac[i] = (power(i, mod - 2) * ifac[i - 1]) % mod;
}
}
private static long C(int n, int r) {
if (n < 0 || r < 0) return 1;
if (r > n) return 1;
return (fac[n] * ((ifac[r] * ifac[n - r]) % mod)) % mod;
}
static int getSum(int BITree[], int index) {
int sum = 0;
index = index + 1;
while (index > 0) {
sum+=BITree[index];
index -= index & (-index);
}
return sum;
}
public static void updateBIT(int BITree[], int index, int val) {
index = index + 1;
while (index <= BITree.length - 1) {
BITree[index] += val;
index += index & (-index);
}
}
static int[] constructBITree(int n) {
int BITree[] = new int[n + 1];
for (int i = 1; i <= n; i++)
BITree[i] = 0;
return BITree;
}
private static int upperBound(int a[], int l, int r, int x) {
if (l > r) return -1;
if (l == r) {
if (a[l] <= x) {
return -1;
} else {
return a[l];
}
}
int m = (l + r) / 2;
if (a[m] <= x) {
return upperBound(a, m + 1, r, x);
} else {
return upperBound(a, l, m, x);
}
}
private static void mul(long A[][], long B[][]) {
int i, j, k, n = A.length;
long C[][] = new long[n][n];
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
for (k = 0; k < n; k++) {
C[i][j] = (C[i][j] + (A[i][k] * B[k][j]) % mod) % mod;
}
}
}
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
A[i][j] = C[i][j];
}
}
}
private static void power(long A[][], long base[][], long n) {
if (n < 2) {
return;
}
power(A, base, n / 2);
mul(A, A);
if (n % 2 == 1) {
mul(A, base);
}
}
static void reverse(int a[], int l, int r) {
while (l < r) {
int t = a[l];
a[l] = a[r];
a[r] = t;
l++;
r--;
}
}
static void reverse(long a[], int l, int r) {
while (l < r) {
long t = a[l];
a[l] = a[r];
a[r] = t;
l++;
r--;
}
}
public void rotate(int[] nums, int k) {
k = k % nums.length;
reverse(nums, 0, nums.length - k - 1);
reverse(nums, nums.length - k, nums.length - 1);
reverse(nums, 0, nums.length - 1);
}
private static boolean isSorted(int a[]) {
for (int i = 0; i < a.length - 1; i++) {
if (a[i] > a[i + 1]) return false;
}
return true;
}
public static int[] getNGE(int arr[]) {
Stack<Integer> s = new Stack<>();
int n = arr.length;
int nge[] = new int[n];
s.push(0);
for (int i = 1; i < n; i++) {
while (!s.isEmpty() && arr[s.peek()] <= arr[i]) {
nge[s.peek()] = i;
s.pop();
}
s.push(i);
}
while (!s.isEmpty()) {
nge[s.peek()] = n;
s.pop();
}
return nge;
}
public static int[] getPSE(int arr[]) {
Stack<Integer> s = new Stack<>();
int n = arr.length;
int pse[] = new int[n];
s.push(n-1);
for (int i = n-2; i >= 0; i--) {
while (!s.isEmpty() && arr[s.peek()] > arr[i]) {
pse[s.peek()] = i;
s.pop();
}
s.push(i);
}
while (!s.isEmpty()) {
pse[s.peek()] = -1;
s.pop();
}
return pse;
}
public static int[] getNSE(int arr[]) {
Stack<Integer> s = new Stack<>();
int n = arr.length;
int nse[] = new int[n];
s.push(0);
for (int i = 1; i < n; i++) {
while (!s.isEmpty() && arr[s.peek()] > arr[i]) {
nse[s.peek()] = i;
s.pop();
}
s.push(i);
}
while (!s.isEmpty()) {
nse[s.peek()] = n;
s.pop();
}
return nse;
}
public int maximumScore(int[] nums, int k) {
int pse[]=getPSE(nums);
int nse[]=getNSE(nums);
int ans=0;
for(int i=0;i<nums.length;i++){
int p=pse[i];
int n=nse[i];
if(p<k&&n>k){
ans=Math.max(ans,(n-p-1)*nums[i]);
}
}
return ans;
}
public int validSubarraySize(int[] nums, int threshold) {
int nse[]=getNSE(nums);
int pse[]=getPSE(nums);
for(int i=0;i<nums.length;i++){
int len=nse[i]-pse[i]-1;
if(nums[i]>threshold/len) {
return len;
}
}
return -1;
}
private static boolean isPal(String s){
for(int i=0,j=s.length()-1;i<j;i++,j--){
if(s.charAt(i)!=s.charAt(j))return false;
}
return true;
}
private static int[] getSuffixArray(String s) {
int n=s.length();
int su[][]=new int[n][3];
for(int i=0;i<n;i++){
su[i]=new int[]{i, s.charAt(i)-'$', 0};
}
for(int i=0;i<n;i++){
su[i][2]=(i+1<n?su[i+1][1]:-1);
}
Arrays.sort(su, (a,b)->a[1]==b[1]?a[2]-b[2]:a[1]-b[1]);
int ind[]=new int[n];
for(int l=4;l<2*n;l*=2){
int rank=0,prev=su[0][1];
su[0][1]=0;
ind[su[0][0]]=0;
for(int i=1;i<n;i++){
if(su[i][1]==prev&&su[i][2]==su[i-1][2]){
su[i][1]=rank;
} else {
prev=su[i][1];
su[i][1]=++rank;
}
ind[su[i][1]]=1;
}
for(int i=0;i<n;i++){
int np=su[i][1]+l/2;
su[i][2]=(np<n?su[ind[np]][1]:-1);
}
Arrays.sort(su, (a,b)->a[1]==b[1]?a[2]-b[2]:a[1]-b[1]);
}
int suf[]=new int[n];
for(int i=0;i<n;i++){
suf[i]=su[i][0];
}
return suf;
}
private static Set<Long> getPrimeFactors(long n){
Set<Long>pfSet=new HashSet<>();
for(long i=2;i*i<=n;i++){
while(n%i==0){
pfSet.add(i);
n/=i;
}
}
if(n>1){
pfSet.add(n);
}
return pfSet;
}
private static int[] manacherLPS(String s) {
int strLen = 2 * s.length() + 3;
char[] sChars = new char[strLen];
sChars[0] = '@';
sChars[strLen - 1] = '$';
int t = 1;
for (char c : s.toCharArray()) {
sChars[t++] = '#';
sChars[t++] = c;
}
sChars[t] = '#';
int maxRight = 0;
int center = 0;
int[] p = new int[strLen];
for (int i = 1; i < strLen - 1; i++) {
if (i < maxRight) {
p[i] = Math.min(maxRight - i, p[2 * center - i]);
}
while (sChars[i + p[i] + 1] == sChars[i - p[i] - 1]) {
p[i]++;
}
if (i + p[i] > maxRight) {
center = i;
maxRight = i + p[i];
}
}
return p;
}
private static void swap(int a[], int i, int j){
int t=a[i];
a[i]=a[j];
a[j]=t;
}
public static void main(String[] args) throws Exception {
long START_TIME = System.currentTimeMillis();
preCompute(121);
try (FastReader in = new FastReader();
FastWriter out = new FastWriter()) {
int n, t, i, j, f, k, m, ti, tidx, gm, l,r,q;
long dp[][]=new long[31][2];
for(i=1;i<31;i++){
dp[i][0]=(C(2*i-1,i-1)+dp[i-1][1])%mod;
dp[i][1]=(C(2*i,i)-dp[i][0]-1+2*mod)%mod;
}
for (t = in.nextInt(), tidx = 1; tidx <= t; tidx++)
{
long x = 0, y = 0, z = 0, sum = 0, bhc = 0, ans = 0;
//out.print(String.format("Case #%d: ", tidx));
n=in.nextInt()/2;
out.println(dp[n][0]+" "+dp[n][1]+" 1");
}
/*if (args.length > 0 && "ex_time".equals(args[0])) {
out.print("\nTime taken: ");
out.println(System.currentTimeMillis() - START_TIME);
}*/
out.commit();
}
}
public static class FastReader implements Closeable {
private BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
private StringTokenizer st;
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (Exception e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
@Override
public void close() throws IOException {
br.close();
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
int[] nextIntArr(int n) {
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = nextInt();
}
return arr;
}
Integer[] nextIntegerArr(int n) {
Integer[] arr = new Integer[n];
for (int i = 0; i < n; i++) {
arr[i] = nextInt();
}
return arr;
}
double[] nextDoubleArr(int n) {
double[] arr = new double[n];
for (int i = 0; i < n; i++) {
arr[i] = nextDouble();
}
return arr;
}
long[] nextLongArr(int n) {
long[] arr = new long[n];
for (int i = 0; i < n; i++) {
arr[i] = nextLong();
}
return arr;
}
List<Long> nextLongList(int n) {
List<Long> retList = new ArrayList<>();
for (int i = 0; i < n; i++) {
retList.add(nextLong());
}
return retList;
}
String[] nextStrArr(int n) {
String[] arr = new String[n];
for (int i = 0; i < n; i++) {
arr[i] = next();
}
return arr;
}
int[][] nextIntArr2(int n, int m) {
int[][] arr = new int[n][m];
for (int i = 0; i < n; i++) {
arr[i] = nextIntArr(m);
}
return arr;
}
long[][] nextLongArr2(int n, int m) {
long[][] arr = new long[n][m];
for (int i = 0; i < n; i++) {
arr[i] = nextLongArr(m);
}
return arr;
}
}
public static class FastWriter implements Closeable {
BufferedWriter bw;
StringBuilder sb = new StringBuilder();
List<String> list = new ArrayList<>();
Set<String> set = new HashSet<>();
public FastWriter() {
bw = new BufferedWriter(new OutputStreamWriter(System.out));
}
<T> void commit() throws IOException {
bw.write(sb.toString());
bw.flush();
sb = new StringBuilder();
}
public <T> void print(T obj) throws IOException {
sb.append(obj.toString());
commit();
}
public void println() throws IOException {
print("\n");
}
public <T> void println(T obj) throws IOException {
print(obj.toString() + "\n");
}
<T> void printArrLn(T[] arr) throws IOException {
for (int i = 0; i < arr.length - 1; i++) {
print(arr[i] + " ");
}
println(arr[arr.length - 1]);
}
<T> void printArr2(T[][] arr) throws IOException {
for (int j = 0; j < arr.length; j++) {
for (int i = 0; i < arr[j].length - 1; i++) {
print(arr[j][i] + " ");
}
println(arr[j][arr[j].length - 1]);
}
}
<T> void printColl(Collection<T> coll) throws IOException {
List<String> stringList = coll.stream().map(e -> ""+e).collect(Collectors.toList());
println(String.join(" ", stringList));
}
void printCharN(char c, int n) throws IOException {
for (int i = 0; i < n; i++) {
print(c);
}
}
void printIntArr(int []arr) throws IOException {
for (int i = 0; i < arr.length - 1; i++) {
print(arr[i] + " ");
}
println(arr[arr.length - 1]);
}
void printIntArr2(int[][] arr) throws IOException {
for (int j = 0; j < arr.length; j++) {
for (int i = 0; i < arr[j].length - 1; i++) {
print(arr[j][i] + " ");
}
println(arr[j][arr[j].length - 1]);
}
}
@Override
public void close() throws IOException {
bw.close();
}
}
}
|
Java
|
["5\n\n2\n\n4\n\n6\n\n8\n\n60"]
|
2 seconds
|
["1 0 1\n3 2 1\n12 7 1\n42 27 1\n341102826 248150916 1"]
|
NoteIn the first test case, Alex wins if he receives the card $$$2$$$ (he plays it, and Boris cannot respond). If Alex receives the card $$$1$$$, the game ends in a draw.In the second test case: Alex wins if he receives the cards $$$[3, 4]$$$, $$$[2, 4]$$$ or $$$[1, 4]$$$; Boris wins if Alex receives the cards $$$[1, 2]$$$ or $$$[1, 3]$$$; the game ends in a draw if Alex receives the cards $$$[2, 3]$$$.
|
Java 11
|
standard input
|
[
"combinatorics",
"constructive algorithms",
"dp",
"games"
] |
63fc2fb08d248f8ccdb3f5364c96dac1
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 30$$$) — the number of test cases. Then, $$$t$$$ lines follow. The $$$i$$$-th line contains one even integer $$$n$$$ ($$$2 \le n \le 60$$$).
| 1,500
|
For each test case, print three integers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. Since the answers can be large, print them modulo $$$998244353$$$.
|
standard output
| |
PASSED
|
7541feff04fa53707bff932d6f447bc4
|
train_109.jsonl
|
1664462100
|
Consider a game with $$$n$$$ cards ($$$n$$$ is even). Each card has a number written on it, between $$$1$$$ and $$$n$$$. All numbers on the cards are different. We say that a card with number $$$x$$$ is stronger than a card with number $$$y$$$ if $$$x > y$$$.Two players, Alex and Boris, play this game. In the beginning, each of them receives exactly $$$\frac{n}{2}$$$ cards, so each card belongs to exactly one player. Then, they take turns. Alex goes first, then Boris, then Alex again, and so on.On a player's turn, he must play exactly one of his cards. Then, if the opponent doesn't have any cards stronger than the card played, the opponent loses, and the game ends. Otherwise, the opponent has to play a stronger card (exactly one card as well). These two cards are removed from the game, and the turn ends. If there are no cards left, the game ends in a draw; otherwise it's the opponent's turn.Consider all possible ways to distribute the cards between two players, so that each of them receives exactly half of the cards. You have to calculate three numbers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. You may assume that both players play optimally (i. e. if a player can win no matter how his opponent plays, he wins). Two ways to distribute the cards are different if there is at least one card such that, in one of these ways, it is given to Alex, and in the other way, it is given to Boris.For example, suppose $$$n = 4$$$, Alex receives the cards $$$[2, 3]$$$, and Boris receives the cards $$$[1, 4]$$$. Then the game may go as follows: if Alex plays the card $$$2$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$3$$$. So, the game ends in a draw; if Alex plays the card $$$3$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$2$$$. So, the game ends in a draw. So, in this case, the game ends in a draw.
|
512 megabytes
|
import java.io.*;
import java.util.*;
import java.math.*;
import java.math.BigInteger;
public final class B
{
static PrintWriter out = new PrintWriter(System.out);
static StringBuilder ans=new StringBuilder();
static FastReader in=new FastReader();
// static int g[][];
static ArrayList<PairB> g[];
static long mod=(long)998244353,INF=Long.MAX_VALUE;
static boolean set[];
static int max=0;
static int lca[][];
static int par[],col[],D[];
static long fact[];
static int size[],N;
static long sum[][],f[],pow[];
static int seg[],seg1[],seg2[],dp[][];
public static void main(String args[])throws IOException
{
/*
* star,rope,TPST
* BS,LST,MS,MQ
*/
long dp[]=new long[70];
fact=new long[105];
fact[0]=1L;
for(int i=1; i<=100; i++)
{
long a=i;
fact[i]=fact[i-1]*a;
fact[i]%=mod;
}
dp[2]=1;
long sa=1L,sb=0;
for(int i=4; i<=60; i+=2)
{
long ncr=nCr(i-1,i/2);//give i to Alice
// System.out.println("for--> "+i+" "+ncr);
dp[i]=ncr+dp[i-4];
dp[i]%=mod;
if(i>=6)
{
ncr=nCr(i-4,(i/2)-3);
// System.out.println("second--> "+ncr);
dp[i]+=ncr;
}
dp[i]%=mod;
}
int T=i();
outer:while(T-->0)
{
int N=i();
long tot=nCr(N,N/2);
// System.out.println(tot+" "+dp[N]);
tot=(tot+mod-1L)%mod;
tot=(tot-dp[N]+mod)%mod;
out.println(dp[N]+" "+tot+" "+1);
}
out.println(ans);
out.close();
}
static long nCr(int n,int r)
{
long a=fact[n];
long b=fact[r];
long c=fact[n-r];
b=pow(b,mod-2)%mod;
c=pow(c,mod-2)%mod;
b*=c;
b%=mod;
a*=b;
a%=mod;
return a;
}
static boolean move(int a,int b,int N,int M)
{
if(a<=0 || a>N)return false;
if(b<=0 || b>M)return false;
return true;
}
static int f(int i,int k,int dp[][],int N)
{
return 0;
}
static int f1212(int N,int l,int r)
{
int dp[][]=new int[N+1][r+1];
for(int i=l; i<=r; i++)dp[1][i]=1;
for(int len=2; len<=N; len++)
{
for(int val=l; val<=r; val++)
{
for(int j=val; j>=l; j--)dp[len][val]+=dp[len-1][j];
}
}
int s=0;
for(int a[]:dp)for(int x:a)s+=x;
return s;
}
static int josephus(int n, int k)
{
if (n == 1)
return 1;
else
/* The position returned by josephus(n - 1, k)
is adjusted because the recursive call
josephus(n - 1, k) considers the original
position k%n + 1 as position 1 */
return (josephus(n - 1, k) + k - 1) % n + 1;
}
static int f(char X[],int l,int r)
{
if(r-l==1)
{
if(X[l]==X[r])return 2;
return 0;
}
if(dp[l][r]==-1)
{
dp[l][r]=0;
if(X[l]==X[r])
{
int a=X[l]-'a',b=X[l+1]-'a',c=X[r-1]-'a';
int max=Math.max(c, b);
if(max<a)return 0;
a=f(X,l+1,r-1); //3 2 4 3
dp[l][r]=a;
}
else
{
if((X[l]-'a')<(X[r]-'a'))
{
int a=X[l+1]-'a',b=X[l]-'a';
if(a>b)return 1;
if(b>a)return 0;
dp[l][r]=f(X,l+2,r);
}
else
{
int a=X[r-1]-'a',b=X[r]-'a';
if(b<a)return 1;
else if(b>a)return 0;
dp[l][r]=f(X,l,r-2);
}
}
}
return dp[l][r];
}
static long f1(List<Integer> A,int k)
{
long r=-1;
if(A.size()<k)return -1;
long s=0;
HashMap<Integer,Integer> mp=new HashMap<>();
for(int i=0; i<k; i++)
{
s+=A.get(i);
mp.put(A.get(i),mp.getOrDefault(A.get(i), 0)+1);
}
if(mp.size()==k)r=Math.max(r, s);
for(int i=k; i<A.size(); i++)
{
int a=A.get(i-k);
s-=a;
if(mp.get(a)==1)mp.remove(a);
else mp.put(a, mp.get(a)-1);
s+=A.get(i);
mp.put(A.get(i),mp.getOrDefault(A.get(i), 0)+1);
if(mp.size()==k)r=Math.max(r, s);
}
return r;
}
static int func(String X)
{
int N=X.length();
if(N<=1)return 1;
if(N%2!=0)
{
if(X.substring(0,N/2).equals(X.substring(N/2,N)))
if(func(X.substring(0,N/2))==1)
return 1;
}
else
{
if(func(X.substring(0,N-1))==1)return 1;
}
return 0;
}
static boolean f(StringBuilder sb)
{
if(sb.length()<=1)return true;
if(sb.length()%2==0)
{
String X=sb.substring(0,sb.length()/2),
Y=sb.substring(sb.length()/2,sb.length());
if(X.equals(Y))return f(new StringBuilder(X));
}
else
{
String X=sb.substring(0,(sb.length()-1));
return f(new StringBuilder(X));
}
return false;
}
static long f(int N,int M,int i,int k)
{
if(k==M)
{
// System.out.println(k);
return 1;
}
if(dp[i][k]==-1)
{
dp[i][k]=0;
for(int j=i; j<=N; j++)dp[i][k]+=(f(N,M,j,k+1))%mod;
}
return dp[i][k]%mod;
}
static int[] ask(int l,int r)
{
out.println("? "+(l+1)+" "+(r+1));
out.flush();
int A[]=input(r-l+1);
// print(A);
return A;
}
static long kadane(long A[])
{
long lsum=A[0],gsum=0;
gsum=Math.max(gsum, lsum);
for(int i=1; i<A.length; i++)
{
lsum=Math.max(lsum+A[i],A[i]);
gsum=Math.max(gsum,lsum);
}
return gsum;
}
public static boolean pal(int i)
{
StringBuilder sb=new StringBuilder();
StringBuilder rev=new StringBuilder();
int p=1;
while(p<=i)
{
if((i&p)!=0)
{
sb.append("1");
}
else sb.append("0");
p<<=1;
}
rev=new StringBuilder(sb.toString());
rev.reverse();
if(i==8)System.out.println(sb+" "+rev);
return (sb.toString()).equals(rev.toString());
}
public static void reverse(int i,int j,int A[])
{
while(i<j)
{
int t=A[i];
A[i]=A[j];
A[j]=t;
i++;
j--;
}
}
public static int ask(int a,int b,int c)
{
System.out.println("? "+a+" "+b+" "+c);
return i();
}
static int[] reverse(int A[],int N)
{
int B[]=new int[N];
for(int i=N-1; i>=0; i--)
{
B[N-i-1]=A[i];
}
return B;
}
static boolean isPalin(char X[])
{
int i=0,j=X.length-1;
while(i<=j)
{
if(X[i]!=X[j])return false;
i++;
j--;
}
return true;
}
static int distance(int a,int b)
{
int d=D[a]+D[b];
int l=LCA(a,b);
l=2*D[l];
return d-l;
}
static int LCA(int a,int b)
{
if(D[a]<D[b])
{
int t=a;
a=b;
b=t;
}
int d=D[a]-D[b];
int p=1;
for(int i=0; i>=0 && p<=d; i++)
{
if((p&d)!=0)
{
a=lca[a][i];
}
p<<=1;
}
if(a==b)return a;
for(int i=max-1; i>=0; i--)
{
if(lca[a][i]!=-1 && lca[a][i]!=lca[b][i])
{
a=lca[a][i];
b=lca[b][i];
}
}
return lca[a][0];
}
static int[] prefix_function(char X[])//returns pi(i) array
{
int N=X.length;
int pre[]=new int[N];
for(int i=1; i<N; i++)
{
int j=pre[i-1];
while(j>0 && X[i]!=X[j])
j=pre[j-1];
if(X[i]==X[j])j++;
pre[i]=j;
}
return pre;
}
// static TreeNode start;
// public static void f(TreeNode root,TreeNode p,int r)
// {
// if(root==null)return;
// if(p!=null)
// {
// root.par=p;
// }
// if(root.val==r)start=root;
// f(root.left,root,r);
// f(root.right,root,r);
// }
//
static int right(int A[],int Limit,int l,int r)
{
while(r-l>1)
{
int m=(l+r)/2;
if(A[m]<Limit)l=m;
else r=m;
}
return l;
}
static int left(int A[],int a,int l,int r)
{
while(r-l>1)
{
int m=(l+r)/2;
if(A[m]<a)l=m;
else r=m;
}
return l;
}
static void build1(int v,int tl,int tr,int A[])
{
if(tl==tr)
{
seg1[v]=A[tl];
return;
}
int tm=(tl+tr)/2;
build1(v*2,tl,tm,A);
build1(v*2+1,tm+1,tr,A);
seg1[v]=seg1[v*2]+seg1[v*2+1];
}
static void update1(int v,int tl,int tr,int index)
{
if(index==tl && index==tr)
{
seg1[v]++;
}
else
{
int tm=(tl+tr)/2;
if(index<=tm)update1(v*2,tl,tm,index);
else update1(v*2+1,tm+1,tr,index);
seg1[v]=seg1[v*2]+seg1[v*2+1];
}
}
static int ask1(int v,int tl,int tr,int l,int r)
{
if(l>r)return 0;
if(tl==l && r==tr)
{
return seg1[v];
}
int tm=(tl+tr)/2;
return ask1(v*2,tl,tm,l,Math.min(tm, r))+ask1(v*2+1,tm+1,tr,Math.max(tm+1, l),r);
}
static void build2(int v,int tl,int tr,int A[])
{
if(tl==tr)
{
seg2[v]=A[tl];
return;
}
int tm=(tl+tr)/2;
build2(v*2,tl,tm,A);
build2(v*2+1,tm+1,tr,A);
seg2[v]=seg2[v*2]+seg2[v*2+1];
}
static void update2(int v,int tl,int tr,int index)
{
if(index==tl && tl==tr)
{
seg2[v]-=1;
}
else
{
int tm=(tl+tr)/2;
if(index<=tm)update2(v*2,tl,tm,index);
else update2(v*2+1,tm+1,tr,index);
seg2[v]=seg2[v*2]+seg2[v*2+1];
}
}
static int ask2(int v,int tl,int tr,int l,int r)
{
if(l>r)return 0;
if(tl==l && r==tr)
{
return seg2[v];
}
int tm=(tl+tr)/2;
return ask2(v*2,tl,tm,l,Math.min(tm, r))+ask2(v*2+1,tm+1,tr,Math.max(tm+1, l),r);
}
static boolean f(long A[],long m,int N)
{
long B[]=new long[N];
for(int i=0; i<N; i++)
{
B[i]=A[i];
}
for(int i=N-1; i>=0; i--)
{
if(B[i]<m)return false;
if(i>=2)
{
long extra=Math.min(B[i]-m, A[i]);
long x=extra/3L;
B[i-2]+=2L*x;
B[i-1]+=x;
}
}
return true;
}
static int f(int l,int r,long A[],long x)
{
while(r-l>1)
{
int m=(l+r)/2;
if(A[m]>=x)l=m;
else r=m;
}
return r;
}
static boolean f(long m,long H,long A[],int N)
{
long s=m;
for(int i=0; i<N-1;i++)
{
s+=Math.min(m, A[i+1]-A[i]);
}
return s>=H;
}
static long ask(long l,long r)
{
System.out.println("? "+l+" "+r);
return l();
}
static long f(long N,long M)
{
long s=0;
if(N%3==0)
{
N/=3;
s=N*M;
}
else
{
long b=N%3;
N/=3;
N++;
s=N*M;
N--;
long a=N*M;
if(M%3==0)
{
M/=3;
a+=(b*M);
}
else
{
M/=3;
M++;
a+=(b*M);
}
s=Math.min(s, a);
}
return s;
}
static int ask(StringBuilder sb,int a)
{
System.out.println(sb+""+a);
return i();
}
static void swap(char X[],int i,int j)
{
char x=X[i];
X[i]=X[j];
X[j]=x;
}
static int min(int a,int b,int c)
{
return Math.min(Math.min(a, b), c);
}
static long and(int i,int j)
{
System.out.println("and "+i+" "+j);
return l();
}
static long or(int i,int j)
{
System.out.println("or "+i+" "+j);
return l();
}
static int len=0,number=0;
static void f(char X[],int i,int num,int l)
{
if(i==X.length)
{
if(num==0)return;
//update our num
if(isPrime(num))return;
if(l<len)
{
len=l;
number=num;
}
return;
}
int a=X[i]-'0';
f(X,i+1,num*10+a,l+1);
f(X,i+1,num,l);
}
static boolean is_Sorted(int A[])
{
int N=A.length;
for(int i=1; i<=N; i++)if(A[i-1]!=i)return false;
return true;
}
static boolean f(StringBuilder sb,String Y,String order)
{
StringBuilder res=new StringBuilder(sb.toString());
HashSet<Character> set=new HashSet<>();
for(char ch:order.toCharArray())
{
set.add(ch);
for(int i=0; i<sb.length(); i++)
{
char x=sb.charAt(i);
if(set.contains(x))continue;
res.append(x);
}
}
String str=res.toString();
return str.equals(Y);
}
static boolean all_Zero(int f[])
{
for(int a:f)if(a!=0)return false;
return true;
}
static long form(int a,int l)
{
long x=0;
while(l-->0)
{
x*=10;
x+=a;
}
return x;
}
static int count(String X)
{
HashSet<Integer> set=new HashSet<>();
for(char x:X.toCharArray())set.add(x-'0');
return set.size();
}
static int f(long K)
{
long l=0,r=K;
while(r-l>1)
{
long m=(l+r)/2;
if(m*m<K)l=m;
else r=m;
}
return (int)l;
}
// static void build(int v,int tl,int tr,long A[])
// {
// if(tl==tr)
// {
// seg[v]=A[tl];
// }
// else
// {
// int tm=(tl+tr)/2;
// build(v*2,tl,tm,A);
// build(v*2+1,tm+1,tr,A);
// seg[v]=Math.min(seg[v*2], seg[v*2+1]);
// }
// }
static int [] sub(int A[],int B[])
{
int N=A.length;
int f[]=new int[N];
for(int i=N-1; i>=0; i--)
{
if(B[i]<A[i])
{
B[i]+=26;
B[i-1]-=1;
}
f[i]=B[i]-A[i];
}
for(int i=0; i<N; i++)
{
if(f[i]%2!=0)f[i+1]+=26;
f[i]/=2;
}
return f;
}
static int[] f(int N)
{
char X[]=in.next().toCharArray();
int A[]=new int[N];
for(int i=0; i<N; i++)A[i]=X[i]-'a';
return A;
}
static int max(int a ,int b,int c,int d)
{
a=Math.max(a, b);
c=Math.max(c,d);
return Math.max(a, c);
}
static int min(int a ,int b,int c,int d)
{
a=Math.min(a, b);
c=Math.min(c,d);
return Math.min(a, c);
}
static HashMap<Integer,Integer> Hash(int A[])
{
HashMap<Integer,Integer> mp=new HashMap<>();
for(int a:A)
{
int f=mp.getOrDefault(a,0)+1;
mp.put(a, f);
}
return mp;
}
static long mul(long a, long b)
{
return ( a %mod * 1L * b%mod )%mod;
}
static void swap(int A[],int a,int b)
{
int t=A[a];
A[a]=A[b];
A[b]=t;
}
static int find(int a)
{
if(par[a]<0)return a;
return par[a]=find(par[a]);
}
static void union(int a,int b)
{
a=find(a);
b=find(b);
if(a!=b)
{
if(par[a]>par[b]) //this means size of a is less than that of b
{
int t=b;
b=a;
a=t;
}
par[a]+=par[b];
par[b]=a;
}
}
static boolean isSorted(int A[])
{
for(int i=1; i<A.length; i++)
{
if(A[i]<A[i-1])return false;
}
return true;
}
static boolean isDivisible(StringBuilder X,int i,long num)
{
long r=0;
for(; i<X.length(); i++)
{
r=r*10+(X.charAt(i)-'0');
r=r%num;
}
return r==0;
}
static int lower_Bound(int A[],int low,int high, int x)
{
if (low > high)
if (x >= A[high])
return A[high];
int mid = (low + high) / 2;
if (A[mid] == x)
return A[mid];
if (mid > 0 && A[mid - 1] <= x && x < A[mid])
return A[mid - 1];
if (x < A[mid])
return lower_Bound( A, low, mid - 1, x);
return lower_Bound(A, mid + 1, high, x);
}
static String f(String A)
{
String X="";
for(int i=A.length()-1; i>=0; i--)
{
int c=A.charAt(i)-'0';
X+=(c+1)%2;
}
return X;
}
static void sort(long[] a) //check for long
{
ArrayList<Long> l=new ArrayList<>();
for (long i:a) l.add(i);
Collections.sort(l);
for (int i=0; i<a.length; i++) a[i]=l.get(i);
}
static String swap(String X,int i,int j)
{
char ch[]=X.toCharArray();
char a=ch[i];
ch[i]=ch[j];
ch[j]=a;
return new String(ch);
}
static int sD(long n)
{
if (n % 2 == 0 )
return 2;
for (int i = 3; i * i <= n; i += 2) {
if (n % i == 0 )
return i;
}
return (int)n;
}
// static void setGraph(int N,int nodes)
// {
//// size=new int[N+1];
// par=new int[N+1];
// col=new int[N+1];
//// g=new int[N+1][];
// D=new int[N+1];
// int deg[]=new int[N+1];
// int A[][]=new int[nodes][2];
// for(int i=0; i<nodes; i++)
// {
// int a=i(),b=i();
// A[i][0]=a;
// A[i][1]=b;
// deg[a]++;
// deg[b]++;
// }
// for(int i=0; i<=N; i++)
// {
// g[i]=new int[deg[i]];
// deg[i]=0;
// }
// for(int a[]:A)
// {
// int x=a[0],y=a[1];
// g[x][deg[x]++]=y;
// g[y][deg[y]++]=x;
// }
// }
static long pow(long a,long b)
{
//long mod=1000000007;
long pow=1;
long x=a;
while(b!=0)
{
if((b&1)!=0)pow=(pow*x)%mod;
x=(x*x)%mod;
b/=2;
}
return pow;
}
static long toggleBits(long x)//one's complement || Toggle bits
{
int n=(int)(Math.floor(Math.log(x)/Math.log(2)))+1;
return ((1<<n)-1)^x;
}
static int countBits(long a)
{
return (int)(Math.log(a)/Math.log(2)+1);
}
static long fact(long N)
{
long n=2;
if(N<=1)return 1;
else
{
for(int i=3; i<=N; i++)n=(n*i)%mod;
}
return n;
}
static void sort(int[] a) {
ArrayList<Integer> l=new ArrayList<>();
for (int i:a) l.add(i);
Collections.sort(l);
for (int i=0; i<a.length; i++) a[i]=l.get(i);
}
static boolean isPrime(long N)
{
if (N<=1) return false;
if (N<=3) return true;
if (N%2L == 0 || N%3L == 0) return false;
for (long i=5; i*i<=N; i+=2)
if (N%i==0)
return false;
return true;
}
static void print(char A[])
{
for(char c:A)System.out.print(c+" ");
System.out.println();
}
static void print(boolean A[])
{
for(boolean c:A)System.out.print(c+" ");
System.out.println();
}
static void print(int A[])
{
for(int a:A)System.out.print(a+" ");
System.out.println();
}
static void print(long A[])
{
for(long i:A)System.out.print(i+ " ");
System.out.println();
}
static void print(boolean A[][])
{
for(boolean a[]:A)print(a);
}
static void print(long A[][])
{
for(long a[]:A)print(a);
}
static void print(int A[][])
{
for(int a[]:A)print(a);
}
static void print(ArrayList<Integer> A)
{
for(int a:A)System.out.print(a+" ");
System.out.println();
}
static int i()
{
return in.nextInt();
}
static long l()
{
return in.nextLong();
}
static int[] input(int N){
int A[]=new int[N];
for(int i=0; i<N; i++)
{
A[i]=in.nextInt();
}
return A;
}
static long[] inputLong(int N) {
long A[]=new long[N];
for(int i=0; i<A.length; i++)A[i]=in.nextLong();
return A;
}
static long GCD(long a,long b)
{
if(b==0)
{
return a;
}
else return GCD(b,a%b );
}
}
class PairB implements Comparable<PairB>
{
long x;
int index;
PairB(long x,int i)
{
this.x=x;
this.index=i;
}
public int compareTo(PairB X)
{
if(this.x==X.x)
{
return 0;
}
if(this.x>X.x)return 1;
else return -1;
}
}
//Code For FastReader
//Code For FastReader
//Code For FastReader
//Code For FastReader
class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br=new BufferedReader(new InputStreamReader(System.in));
}
String next()
{
while(st==null || !st.hasMoreElements())
{
try
{
st=new StringTokenizer(br.readLine());
}
catch(IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str="";
try
{
str=br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
|
Java
|
["5\n\n2\n\n4\n\n6\n\n8\n\n60"]
|
2 seconds
|
["1 0 1\n3 2 1\n12 7 1\n42 27 1\n341102826 248150916 1"]
|
NoteIn the first test case, Alex wins if he receives the card $$$2$$$ (he plays it, and Boris cannot respond). If Alex receives the card $$$1$$$, the game ends in a draw.In the second test case: Alex wins if he receives the cards $$$[3, 4]$$$, $$$[2, 4]$$$ or $$$[1, 4]$$$; Boris wins if Alex receives the cards $$$[1, 2]$$$ or $$$[1, 3]$$$; the game ends in a draw if Alex receives the cards $$$[2, 3]$$$.
|
Java 11
|
standard input
|
[
"combinatorics",
"constructive algorithms",
"dp",
"games"
] |
63fc2fb08d248f8ccdb3f5364c96dac1
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 30$$$) — the number of test cases. Then, $$$t$$$ lines follow. The $$$i$$$-th line contains one even integer $$$n$$$ ($$$2 \le n \le 60$$$).
| 1,500
|
For each test case, print three integers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. Since the answers can be large, print them modulo $$$998244353$$$.
|
standard output
| |
PASSED
|
c009be9cf1b2e70ebc320180ef5fa7de
|
train_109.jsonl
|
1664462100
|
Consider a game with $$$n$$$ cards ($$$n$$$ is even). Each card has a number written on it, between $$$1$$$ and $$$n$$$. All numbers on the cards are different. We say that a card with number $$$x$$$ is stronger than a card with number $$$y$$$ if $$$x > y$$$.Two players, Alex and Boris, play this game. In the beginning, each of them receives exactly $$$\frac{n}{2}$$$ cards, so each card belongs to exactly one player. Then, they take turns. Alex goes first, then Boris, then Alex again, and so on.On a player's turn, he must play exactly one of his cards. Then, if the opponent doesn't have any cards stronger than the card played, the opponent loses, and the game ends. Otherwise, the opponent has to play a stronger card (exactly one card as well). These two cards are removed from the game, and the turn ends. If there are no cards left, the game ends in a draw; otherwise it's the opponent's turn.Consider all possible ways to distribute the cards between two players, so that each of them receives exactly half of the cards. You have to calculate three numbers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. You may assume that both players play optimally (i. e. if a player can win no matter how his opponent plays, he wins). Two ways to distribute the cards are different if there is at least one card such that, in one of these ways, it is given to Alex, and in the other way, it is given to Boris.For example, suppose $$$n = 4$$$, Alex receives the cards $$$[2, 3]$$$, and Boris receives the cards $$$[1, 4]$$$. Then the game may go as follows: if Alex plays the card $$$2$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$3$$$. So, the game ends in a draw; if Alex plays the card $$$3$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$2$$$. So, the game ends in a draw. So, in this case, the game ends in a draw.
|
512 megabytes
|
import java.util.*;
public class ProblemC {
static int mod = 998244353;
static long qmod(long a, long b) {
long sum = 1;
while (b > 0) {
if (b % 2 == 1) sum = (sum * a) % mod;
a = (a * a) % mod;
b /= 2;
}
return sum;
}
static long inv(long x) {
return qmod(x, mod - 2);
}
public static void main(String[] args) {
var sc = new Scanner(System.in);
int t = sc.nextInt();
long[] dpA = new long[61];
long[] dpB = new long[61];
long[] fac = new long[61];
fac[0] = 1;
for (int i = 1; i < 61; i++) {
fac[i] = (i * fac[i - 1]) % mod;
}
dpA[1] = 1;
dpB[1] = 0;
for (int i = 2; i < 31; i++) {
dpA[i] = (dpB[i - 1] + (fac[2 * i - 1] * inv(fac[i - 1]) % mod * inv(fac[i])) % mod) % mod;
dpB[i] = (((fac[2 * i] * inv(fac[i]) % mod * inv(fac[i])) % mod - 1 - dpA[i]) % mod + mod) % mod;
}
while (t-- > 0) {
int n = sc.nextInt();
System.out.println(dpA[n / 2] + " " + dpB[n / 2] + " 1");
}
}
}
|
Java
|
["5\n\n2\n\n4\n\n6\n\n8\n\n60"]
|
2 seconds
|
["1 0 1\n3 2 1\n12 7 1\n42 27 1\n341102826 248150916 1"]
|
NoteIn the first test case, Alex wins if he receives the card $$$2$$$ (he plays it, and Boris cannot respond). If Alex receives the card $$$1$$$, the game ends in a draw.In the second test case: Alex wins if he receives the cards $$$[3, 4]$$$, $$$[2, 4]$$$ or $$$[1, 4]$$$; Boris wins if Alex receives the cards $$$[1, 2]$$$ or $$$[1, 3]$$$; the game ends in a draw if Alex receives the cards $$$[2, 3]$$$.
|
Java 11
|
standard input
|
[
"combinatorics",
"constructive algorithms",
"dp",
"games"
] |
63fc2fb08d248f8ccdb3f5364c96dac1
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 30$$$) — the number of test cases. Then, $$$t$$$ lines follow. The $$$i$$$-th line contains one even integer $$$n$$$ ($$$2 \le n \le 60$$$).
| 1,500
|
For each test case, print three integers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. Since the answers can be large, print them modulo $$$998244353$$$.
|
standard output
| |
PASSED
|
19c277433fbc514b839b219e2401d5cf
|
train_109.jsonl
|
1664462100
|
Consider a game with $$$n$$$ cards ($$$n$$$ is even). Each card has a number written on it, between $$$1$$$ and $$$n$$$. All numbers on the cards are different. We say that a card with number $$$x$$$ is stronger than a card with number $$$y$$$ if $$$x > y$$$.Two players, Alex and Boris, play this game. In the beginning, each of them receives exactly $$$\frac{n}{2}$$$ cards, so each card belongs to exactly one player. Then, they take turns. Alex goes first, then Boris, then Alex again, and so on.On a player's turn, he must play exactly one of his cards. Then, if the opponent doesn't have any cards stronger than the card played, the opponent loses, and the game ends. Otherwise, the opponent has to play a stronger card (exactly one card as well). These two cards are removed from the game, and the turn ends. If there are no cards left, the game ends in a draw; otherwise it's the opponent's turn.Consider all possible ways to distribute the cards between two players, so that each of them receives exactly half of the cards. You have to calculate three numbers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. You may assume that both players play optimally (i. e. if a player can win no matter how his opponent plays, he wins). Two ways to distribute the cards are different if there is at least one card such that, in one of these ways, it is given to Alex, and in the other way, it is given to Boris.For example, suppose $$$n = 4$$$, Alex receives the cards $$$[2, 3]$$$, and Boris receives the cards $$$[1, 4]$$$. Then the game may go as follows: if Alex plays the card $$$2$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$3$$$. So, the game ends in a draw; if Alex plays the card $$$3$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$2$$$. So, the game ends in a draw. So, in this case, the game ends in a draw.
|
512 megabytes
|
/**
* Created by Himanshu
**/
import java.util.*;
import java.io.*;
public class C1739 {
static final int ALPHABET_SIZE = 26;
public static void main(String[] args) throws IOException {
PrintWriter out = new PrintWriter(System.out);
Reader s = new Reader();
int t = s.i();
while (t-- > 0) {
int n = s.i();
long mod = 998244353L;
long [] a = new long[n/2];
long [] b = new long[n/2];
a[0] = 1;
b[0] = 0;
for (int i=1;i<n/2;i++) {
long val = nCr((i+1)*2,i+1);
a[i] = val/2;
b[i] = (val - a[i] - b[i-1] - 1L);
a[i] = (a[i] + b[i-1]);
}
out.println((a[n/2-1])%mod + " " + (b[n/2-1])%mod + " 1");
}
out.flush();
}
private static long nCr(int n , int r) {
long val = 1L;
for (int i=r+1;i<=n;i++) {
val = val * i;
val = val / (i-r);
}
return val;
}
public static void shuffle(long[] arr) {
int n = arr.length;
Random rand = new Random();
for (int i = 0; i < n; i++) {
long temp = arr[i];
int randomPos = i + rand.nextInt(n - i);
arr[i] = arr[randomPos];
arr[randomPos] = temp;
}
}
private static long phi(long n) {
long result = n;
for (long i = 2; i * i <= n; i++) {
if (n % i == 0) {
while (n % i == 0)
n /= i;
result -= result / i;
}
}
if (n > 1)
result -= result / n;
return result;
}
private static int gcd(int a, int b) {
if(b == 0) return a;
return gcd(b,a%b);
}
public static long nCr(long[] fact, long[] inv, int n, int r, long mod) {
if (n < r)
return 0;
return ((fact[n] * inv[n - r]) % mod * inv[r]) % mod;
}
private static void factorials(long[] fact, long[] inv, long mod, int n) {
fact[0] = 1;
inv[0] = 1;
for (int i = 1; i <= n; ++i) {
fact[i] = (fact[i - 1] * i) % mod;
inv[i] = power(fact[i], mod - 2, mod);
}
}
private static long power(long a, long n, long p) {
long result = 1;
while (n > 0) {
if (n % 2 == 0) {
a = (a * a) % p;
n /= 2;
} else {
result = (result * a) % p;
n--;
}
}
return result;
}
private static long power(long a, long n) {
long result = 1;
while (n > 0) {
if (n % 2 == 0) {
a = (a * a);
n /= 2;
} else {
result = (result * a);
n--;
}
}
return result;
}
private static long query(long[] tree, int in, int start, int end, int l, int r) {
if (start >= l && r >= end) return tree[in];
if (end < l || start > r) return 0;
int mid = (start + end) / 2;
long x = query(tree, 2 * in, start, mid, l, r);
long y = query(tree, 2 * in + 1, mid + 1, end, l, r);
return x + y;
}
private static void update(int[] arr, long[] tree, int in, int start, int end, int idx, int val) {
if (start == end) {
tree[in] = val;
arr[idx] = val;
return;
}
int mid = (start + end) / 2;
if (idx > mid) update(arr, tree, 2 * in + 1, mid + 1, end, idx, val);
else update(arr, tree, 2 * in, start, mid, idx, val);
tree[in] = tree[2 * in] + tree[2 * in + 1];
}
private static void build(int[] arr, long[] tree, int in, int start, int end) {
if (start == end) {
tree[in] = arr[start];
return;
}
int mid = (start + end) / 2;
build(arr, tree, 2 * in, start, mid);
build(arr, tree, 2 * in + 1, mid + 1, end);
tree[in] = (tree[2 * in + 1] + tree[2 * in]);
}
static class Reader {
private InputStream mIs;
private byte[] buf = new byte[1024];
private int curChar, numChars;
public Reader() {
this(System.in);
}
public Reader(InputStream is) {
mIs = is;
}
public int read() {
if (numChars == -1) throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = mIs.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0) return -1;
}
return buf[curChar++];
}
public String nextLine() {
int c = read();
while (isSpaceChar(c)) c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isEndOfLine(c));
return res.toString();
}
public String s() {
int c = read();
while (isSpaceChar(c)) c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public long l() {
int c = read();
while (isSpaceChar(c)) c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
if (c < '0' || c > '9') throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public int i() {
int c = read();
while (isSpaceChar(c)) c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9') throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public double d() throws IOException {
return Double.parseDouble(s());
}
public boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public boolean isEndOfLine(int c) {
return c == '\n' || c == '\r' || c == -1;
}
public int[] arr(int n) {
int[] ret = new int[n];
for (int i = 0; i < n; i++) {
ret[i] = i();
}
return ret;
}
public long[] arrLong(int n) {
long[] ret = new long[n];
for (int i = 0; i < n; i++) {
ret[i] = l();
}
return ret;
}
}
static class TrieNode {
TrieNode[] children = new TrieNode[ALPHABET_SIZE];
boolean isLeaf;
boolean isPalindrome;
public TrieNode() {
isLeaf = false;
isPalindrome = false;
for (int i = 0; i < ALPHABET_SIZE; i++)
children[i] = null;
}
}
// static class pairLong implements Comparator<pairLong> {
// long first, second;
//
// pairLong() {
// }
//
// pairLong(long first, long second) {
// this.first = first;
// this.second = second;
// }
//
// @Override
// public int compare(pairLong p1, pairLong p2) {
// if (p1.first == p2.first) {
// if(p1.second > p2.second) return 1;
// else return -1;
// }
// if(p1.first > p2.first) return 1;
// else return -1;
// }
// }
// static class pair implements Comparator<pair> {
// int first, second;
//
// pair() {
// }
//
// pair(int first, int second) {
// this.first = first;
// this.second = second;
// }
//
// @Override
// public int compare(pair p1, pair p2) {
// if (p1.first == p2.first) return p1.second - p2.second;
// return p1.first - p2.first;
// }
// }
}
|
Java
|
["5\n\n2\n\n4\n\n6\n\n8\n\n60"]
|
2 seconds
|
["1 0 1\n3 2 1\n12 7 1\n42 27 1\n341102826 248150916 1"]
|
NoteIn the first test case, Alex wins if he receives the card $$$2$$$ (he plays it, and Boris cannot respond). If Alex receives the card $$$1$$$, the game ends in a draw.In the second test case: Alex wins if he receives the cards $$$[3, 4]$$$, $$$[2, 4]$$$ or $$$[1, 4]$$$; Boris wins if Alex receives the cards $$$[1, 2]$$$ or $$$[1, 3]$$$; the game ends in a draw if Alex receives the cards $$$[2, 3]$$$.
|
Java 11
|
standard input
|
[
"combinatorics",
"constructive algorithms",
"dp",
"games"
] |
63fc2fb08d248f8ccdb3f5364c96dac1
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 30$$$) — the number of test cases. Then, $$$t$$$ lines follow. The $$$i$$$-th line contains one even integer $$$n$$$ ($$$2 \le n \le 60$$$).
| 1,500
|
For each test case, print three integers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. Since the answers can be large, print them modulo $$$998244353$$$.
|
standard output
| |
PASSED
|
9ba1944614ca48f40a5c0d267d1e214a
|
train_109.jsonl
|
1664462100
|
Consider a game with $$$n$$$ cards ($$$n$$$ is even). Each card has a number written on it, between $$$1$$$ and $$$n$$$. All numbers on the cards are different. We say that a card with number $$$x$$$ is stronger than a card with number $$$y$$$ if $$$x > y$$$.Two players, Alex and Boris, play this game. In the beginning, each of them receives exactly $$$\frac{n}{2}$$$ cards, so each card belongs to exactly one player. Then, they take turns. Alex goes first, then Boris, then Alex again, and so on.On a player's turn, he must play exactly one of his cards. Then, if the opponent doesn't have any cards stronger than the card played, the opponent loses, and the game ends. Otherwise, the opponent has to play a stronger card (exactly one card as well). These two cards are removed from the game, and the turn ends. If there are no cards left, the game ends in a draw; otherwise it's the opponent's turn.Consider all possible ways to distribute the cards between two players, so that each of them receives exactly half of the cards. You have to calculate three numbers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. You may assume that both players play optimally (i. e. if a player can win no matter how his opponent plays, he wins). Two ways to distribute the cards are different if there is at least one card such that, in one of these ways, it is given to Alex, and in the other way, it is given to Boris.For example, suppose $$$n = 4$$$, Alex receives the cards $$$[2, 3]$$$, and Boris receives the cards $$$[1, 4]$$$. Then the game may go as follows: if Alex plays the card $$$2$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$3$$$. So, the game ends in a draw; if Alex plays the card $$$3$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$2$$$. So, the game ends in a draw. So, in this case, the game ends in a draw.
|
512 megabytes
|
import java.util.*;
import java.io.*;
public class Main {
private static final void solve() throws IOException {
int nn = ni(), n = nn >> 1;
var fa = new ModIntFactory(998244353);
var x = fa.create(0);
var y = fa.create(0);
for (int i = nn - 1, j = n; i >= 0; i -= 4, j -= 2) {
if (i >= 0)
x.addAsg(fa.combination(i, j));
if (i >= 1)
y.addAsg(fa.combination(i - 1, j));
if (i >= 2)
y.addAsg(fa.combination(i - 2, j - 1));
if (i >= 3)
x.addAsg(fa.combination(i - 3, j - 1));
}
ou.println(x + " " + y + " 1");
}
public static void main(String[] args) throws IOException {
for (int i = 0, t = ni(); i < t; i++)
solve();
ou.flush();
}
private static final int ni() throws IOException {
return sc.nextInt();
}
private static final int[] ni(int n) throws IOException {
return sc.nextIntArray(n);
}
private static final long nl() throws IOException {
return sc.nextLong();
}
private static final long[] nl(int n) throws IOException {
return sc.nextLongArray(n);
}
private static final String ns() throws IOException {
return sc.next();
}
private static final ContestInputStream sc = new ContestInputStream();
private static final ContestOutputStream ou = new ContestOutputStream();
}
class ModIntFactory {
private final ModArithmetic ma;
private final int mod;
private final boolean usesMontgomery;
private final ModArithmetic.ModArithmeticMontgomery maMontgomery;
private ArrayList<Integer> factorial;
public ModIntFactory(int mod) {
this.ma = ModArithmetic.of(mod);
this.usesMontgomery = ma instanceof ModArithmetic.ModArithmeticMontgomery;
this.maMontgomery = usesMontgomery ? (ModArithmetic.ModArithmeticMontgomery) ma : null;
this.mod = mod;
this.factorial = new ArrayList<>();
}
public ModIntFactory(double mod) {
this((int) mod);
}
public ModInt create(long value) {
if ((value %= mod) < 0)
value += mod;
if (usesMontgomery) {
return new ModInt(maMontgomery.generate(value));
} else {
return new ModInt((int) value);
}
}
private void prepareFactorial(int max) {
factorial.ensureCapacity(max + 1);
if (factorial.size() == 0)
factorial.add(1);
for (int i = factorial.size(); i <= max; i++) {
if (usesMontgomery)
factorial.add(ma.mul(factorial.get(i - 1), maMontgomery.generate(i)));
else
factorial.add(ma.mul(factorial.get(i - 1), i));
}
}
public ModInt factorial(int i) {
prepareFactorial(i);
return create(factorial.get(i));
}
public ModInt permutation(int n, int r) {
if (n < 0 || r < 0 || n < r)
return create(0);
prepareFactorial(n);
return create(ma.div(factorial.get(n), factorial.get(r)));
}
public ModInt combination(int n, int r) {
if (n < 0 || r < 0 || n < r)
return create(0);
prepareFactorial(n);
return create(ma.div(factorial.get(n), ma.mul(factorial.get(r), factorial.get(n - r))));
}
public int getMod() {
return mod;
}
public class ModInt {
private int value;
private ModInt(int value) {
this.value = value;
}
public int mod() {
return mod;
}
public int value() {
if (usesMontgomery) {
return maMontgomery.reduce(value);
}
return value;
}
public ModInt add(ModInt mi) {
return new ModInt(ma.add(value, mi.value));
}
public ModInt add(ModInt mi1, ModInt mi2) {
return new ModInt(ma.add(value, mi1.value)).addAsg(mi2);
}
public ModInt add(ModInt mi1, ModInt mi2, ModInt mi3) {
return new ModInt(ma.add(value, mi1.value)).addAsg(mi2).addAsg(mi3);
}
public ModInt add(ModInt mi1, ModInt mi2, ModInt mi3, ModInt mi4) {
return new ModInt(ma.add(value, mi1.value)).addAsg(mi2).addAsg(mi3).addAsg(mi4);
}
public ModInt add(ModInt mi1, ModInt... mis) {
ModInt mi = add(mi1);
for (ModInt m : mis)
mi.addAsg(m);
return mi;
}
public ModInt add(long mi) {
return new ModInt(ma.add(value, ma.remainder(mi)));
}
public ModInt sub(ModInt mi) {
return new ModInt(ma.sub(value, mi.value));
}
public ModInt sub(long mi) {
return new ModInt(ma.sub(value, ma.remainder(mi)));
}
public ModInt mul(ModInt mi) {
return new ModInt(ma.mul(value, mi.value));
}
public ModInt mul(ModInt mi1, ModInt mi2) {
return new ModInt(ma.mul(value, mi1.value)).mulAsg(mi2);
}
public ModInt mul(ModInt mi1, ModInt mi2, ModInt mi3) {
return new ModInt(ma.mul(value, mi1.value)).mulAsg(mi2).mulAsg(mi3);
}
public ModInt mul(ModInt mi1, ModInt mi2, ModInt mi3, ModInt mi4) {
return new ModInt(ma.mul(value, mi1.value)).mulAsg(mi2).mulAsg(mi3).mulAsg(mi4);
}
public ModInt mul(ModInt mi1, ModInt... mis) {
ModInt mi = mul(mi1);
for (ModInt m : mis)
mi.mulAsg(m);
return mi;
}
public ModInt mul(long mi) {
return new ModInt(ma.mul(value, ma.remainder(mi)));
}
public ModInt div(ModInt mi) {
return new ModInt(ma.div(value, mi.value));
}
public ModInt div(long mi) {
return new ModInt(ma.div(value, ma.remainder(mi)));
}
public ModInt inv() {
return new ModInt(ma.inv(value));
}
public ModInt pow(long b) {
return new ModInt(ma.pow(value, b));
}
public ModInt pow(ModInt mi) {
return new ModInt(ma.pow(value, mi.value));
}
public ModInt addAsg(ModInt mi) {
this.value = ma.add(value, mi.value);
return this;
}
public ModInt addAsg(ModInt mi1, ModInt mi2) {
return addAsg(mi1).addAsg(mi2);
}
public ModInt addAsg(ModInt mi1, ModInt mi2, ModInt mi3) {
return addAsg(mi1).addAsg(mi2).addAsg(mi3);
}
public ModInt addAsg(ModInt mi1, ModInt mi2, ModInt mi3, ModInt mi4) {
return addAsg(mi1).addAsg(mi2).addAsg(mi3).addAsg(mi4);
}
public ModInt addAsg(ModInt... mis) {
for (ModInt m : mis)
addAsg(m);
return this;
}
public ModInt addAsg(long mi) {
this.value = ma.add(value, ma.remainder(mi));
return this;
}
public ModInt subAsg(ModInt mi) {
this.value = ma.sub(value, mi.value);
return this;
}
public ModInt subAsg(long mi) {
this.value = ma.sub(value, ma.remainder(mi));
return this;
}
public ModInt mulAsg(ModInt mi) {
this.value = ma.mul(value, mi.value);
return this;
}
public ModInt mulAsg(ModInt mi1, ModInt mi2) {
return mulAsg(mi1).mulAsg(mi2);
}
public ModInt mulAsg(ModInt mi1, ModInt mi2, ModInt mi3) {
return mulAsg(mi1).mulAsg(mi2).mulAsg(mi3);
}
public ModInt mulAsg(ModInt mi1, ModInt mi2, ModInt mi3, ModInt mi4) {
return mulAsg(mi1).mulAsg(mi2).mulAsg(mi3).mulAsg(mi4);
}
public ModInt mulAsg(ModInt... mis) {
for (ModInt m : mis)
mulAsg(m);
return this;
}
public ModInt mulAsg(long mi) {
this.value = ma.mul(value, ma.remainder(mi));
return this;
}
public ModInt divAsg(ModInt mi) {
this.value = ma.div(value, mi.value);
return this;
}
public ModInt divAsg(long mi) {
this.value = ma.div(value, ma.remainder(mi));
return this;
}
public ModInt powAsg(ModInt mi) {
this.value = ma.pow(value, mi.value());
return this;
}
public ModInt powAsg(long mi) {
this.value = ma.pow(value, mi);
return this;
}
@Override
public String toString() {
return String.valueOf(value());
}
@Override
public boolean equals(Object o) {
if (o instanceof ModInt) {
ModInt mi = (ModInt) o;
return mod() == mi.mod() && value() == mi.value();
}
return false;
}
@Override
public int hashCode() {
return (1 * 37 + mod()) * 37 + value();
}
}
private static abstract class ModArithmetic {
abstract int mod();
abstract int remainder(long value);
abstract int add(int a, int b);
abstract int sub(int a, int b);
abstract int mul(int a, int b);
int div(int a, int b) {
return mul(a, inv(b));
}
int inv(int a) {
int b = mod();
if (b == 1)
return 0;
long u = 1, v = 0;
while (b >= 1) {
int t = a / b;
a -= t * b;
int tmp1 = a;
a = b;
b = tmp1;
u -= t * v;
long tmp2 = u;
u = v;
v = tmp2;
}
if (a != 1)
throw new ArithmeticException("divide by zero");
return remainder(u);
}
int pow(int a, long b) {
if (b < 0)
throw new ArithmeticException("negative power");
int r = 1;
int x = a;
while (b > 0) {
if ((b & 1) == 1)
r = mul(r, x);
x = mul(x, x);
b >>= 1;
}
return r;
}
static ModArithmetic of(int mod) {
if (mod <= 0) {
throw new IllegalArgumentException();
} else if (mod == 1) {
return new ModArithmetic1();
} else if (mod == 2) {
return new ModArithmetic2();
} else if (mod == 998244353) {
return new ModArithmetic998244353();
} else if (mod == 1000000007) {
return new ModArithmetic1000000007();
} else if ((mod & 1) == 1) {
return new ModArithmeticMontgomery(mod);
} else {
return new ModArithmeticBarrett(mod);
}
}
private static final class ModArithmetic1 extends ModArithmetic {
int mod() {
return 1;
}
int remainder(long value) {
return 0;
}
int add(int a, int b) {
return 0;
}
int sub(int a, int b) {
return 0;
}
int mul(int a, int b) {
return 0;
}
int pow(int a, long b) {
return 0;
}
}
private static final class ModArithmetic2 extends ModArithmetic {
int mod() {
return 2;
}
int remainder(long value) {
return (int) (value & 1);
}
int add(int a, int b) {
return a ^ b;
}
int sub(int a, int b) {
return a ^ b;
}
int mul(int a, int b) {
return a & b;
}
}
private static final class ModArithmetic998244353 extends ModArithmetic {
private final int mod = 998244353;
int mod() {
return mod;
}
int remainder(long value) {
return (int) ((value %= mod) < 0 ? value + mod : value);
}
int add(int a, int b) {
int res = a + b;
return res >= mod ? res - mod : res;
}
int sub(int a, int b) {
int res = a - b;
return res < 0 ? res + mod : res;
}
int mul(int a, int b) {
return (int) (((long) a * b) % mod);
}
}
private static final class ModArithmetic1000000007 extends ModArithmetic {
private final int mod = 1000000007;
int mod() {
return mod;
}
int remainder(long value) {
return (int) ((value %= mod) < 0 ? value + mod : value);
}
int add(int a, int b) {
int res = a + b;
return res >= mod ? res - mod : res;
}
int sub(int a, int b) {
int res = a - b;
return res < 0 ? res + mod : res;
}
int mul(int a, int b) {
return (int) (((long) a * b) % mod);
}
}
private static final class ModArithmeticMontgomery extends ModArithmeticDynamic {
private final long negInv;
private final long r2;
private ModArithmeticMontgomery(int mod) {
super(mod);
long inv = 0;
long s = 1, t = 0;
for (int i = 0; i < 32; i++) {
if ((t & 1) == 0) {
t += mod;
inv += s;
}
t >>= 1;
s <<= 1;
}
long r = (1l << 32) % mod;
this.negInv = inv;
this.r2 = (r * r) % mod;
}
private int generate(long x) {
return reduce(x * r2);
}
private int reduce(long x) {
x = (x + ((x * negInv) & 0xffff_ffffl) * mod) >>> 32;
return (int) (x < mod ? x : x - mod);
}
@Override
int remainder(long value) {
return generate((value %= mod) < 0 ? value + mod : value);
}
@Override
int mul(int a, int b) {
return reduce((long) a * b);
}
@Override
int inv(int a) {
return super.inv(reduce(a));
}
@Override
int pow(int a, long b) {
return generate(super.pow(a, b));
}
}
private static final class ModArithmeticBarrett extends ModArithmeticDynamic {
private static final long mask = 0xffff_ffffl;
private final long mh;
private final long ml;
private ModArithmeticBarrett(int mod) {
super(mod);
long a = (1l << 32) / mod;
long b = (1l << 32) % mod;
long m = a * a * mod + 2 * a * b + (b * b) / mod;
mh = m >>> 32;
ml = m & mask;
}
private int reduce(long x) {
long z = (x & mask) * ml;
z = (x & mask) * mh + (x >>> 32) * ml + (z >>> 32);
z = (x >>> 32) * mh + (z >>> 32);
x -= z * mod;
return (int) (x < mod ? x : x - mod);
}
@Override
int remainder(long value) {
return (int) ((value %= mod) < 0 ? value + mod : value);
}
@Override
int mul(int a, int b) {
return reduce((long) a * b);
}
}
private static class ModArithmeticDynamic extends ModArithmetic {
final int mod;
ModArithmeticDynamic(int mod) {
this.mod = mod;
}
int mod() {
return mod;
}
int remainder(long value) {
return (int) ((value %= mod) < 0 ? value + mod : value);
}
int add(int a, int b) {
int sum = a + b;
return sum >= mod ? sum - mod : sum;
}
int sub(int a, int b) {
int sum = a - b;
return sum < 0 ? sum + mod : sum;
}
int mul(int a, int b) {
return (int) (((long) a * b) % mod);
}
}
}
}
final class ContestInputStream extends FilterInputStream {
protected final byte[] buf;
protected int pos = 0;
protected int lim = 0;
private final char[] cbuf;
public ContestInputStream() {
super(System.in);
this.buf = new byte[1 << 13];
this.cbuf = new char[1 << 20];
}
boolean hasRemaining() throws IOException {
if (pos < lim)
return true;
lim = in.read(buf);
pos = 0;
return lim > 0;
}
final int remaining() throws IOException {
if (pos >= lim) {
lim = in.read(buf);
pos = 0;
}
return lim - pos;
}
@Override
public final int available() throws IOException {
if (pos < lim)
return lim - pos + in.available();
return in.available();
}
@Override
public final long skip(long n) throws IOException {
if (pos < lim) {
int rem = lim - pos;
if (n < rem) {
pos += n;
return n;
}
pos = lim;
return rem;
}
return in.skip(n);
}
@Override
public final int read() throws IOException {
if (hasRemaining())
return buf[pos++];
return -1;
}
@Override
public final int read(byte[] b, int off, int len) throws IOException {
if (pos < lim) {
int rem = Math.min(lim - pos, len);
for (int i = 0; i < rem; i++)
b[off + i] = buf[pos + i];
pos += rem;
return rem;
}
return in.read(b, off, len);
}
public final char[] readToken() throws IOException {
int cpos = 0;
int rem;
byte b;
l: while ((rem = remaining()) > 0) {
for (int i = 0; i < rem; i++) {
b = buf[pos + i];
if (b <= 0x20) {
pos += i + 1;
cpos += i;
if (b == 0x0d && hasRemaining() && buf[pos] == 0x0a)
pos++;
break l;
}
cbuf[cpos + i] = (char) b;
}
pos += rem;
cpos += rem;
}
char[] arr = new char[cpos];
for (int i = 0; i < cpos; i++)
arr[i] = cbuf[i];
return arr;
}
public final int readToken(char[] cbuf, int off) throws IOException {
int cpos = off;
int rem;
byte b;
l: while ((rem = remaining()) > 0) {
for (int i = 0; i < rem; i++) {
b = buf[pos + i];
if (b <= 0x20) {
pos += i + 1;
cpos += i;
if (b == 0x0d && hasRemaining() && buf[pos] == 0x0a)
pos++;
break l;
}
cbuf[cpos + i] = (char) b;
}
pos += rem;
cpos += rem;
}
return cpos - off;
}
public final int readToken(char[] cbuf) throws IOException {
return readToken(cbuf, 0);
}
public final String next() throws IOException {
int cpos = 0;
int rem;
byte b;
l: while ((rem = remaining()) > 0) {
for (int i = 0; i < rem; i++) {
b = buf[pos + i];
if (b <= 0x20) {
pos += i + 1;
cpos += i;
if (b == 0x0d && hasRemaining() && buf[pos] == 0x0a)
pos++;
break l;
}
cbuf[cpos + i] = (char) b;
}
pos += rem;
cpos += rem;
}
return String.valueOf(cbuf, 0, cpos);
}
public final char[] nextCharArray() throws IOException {
return readToken();
}
public final int nextInt() throws IOException {
if (!hasRemaining())
return 0;
int value = 0;
byte b = buf[pos++];
if (b == 0x2d) {
while (hasRemaining() && (b = buf[pos++]) > 0x20)
value = value * 10 - b + 0x30;
} else {
do {
value = value * 10 + b - 0x30;
} while (hasRemaining() && (b = buf[pos++]) > 0x20);
}
if (b == 0x0d && hasRemaining() && buf[pos] == 0x0a)
pos++;
return value;
}
public final long nextLong() throws IOException {
if (!hasRemaining())
return 0L;
long value = 0L;
byte b = buf[pos++];
if (b == 0x2d) {
while (hasRemaining() && (b = buf[pos++]) > 0x20)
value = value * 10 - b + 0x30;
} else {
do {
value = value * 10 + b - 0x30;
} while (hasRemaining() && (b = buf[pos++]) > 0x20);
}
if (b == 0x0d && hasRemaining() && buf[pos] == 0x0a)
pos++;
return value;
}
public final char nextChar() throws IOException {
if (!hasRemaining())
throw new EOFException();
final char c = (char) buf[pos++];
if (hasRemaining() && buf[pos++] == 0x0d && hasRemaining() && buf[pos] == 0x0a)
pos++;
return c;
}
public final float nextFloat() throws IOException {
return Float.parseFloat(next());
}
public final double nextDouble() throws IOException {
return Double.parseDouble(next());
}
public final boolean[] nextBooleanArray(char ok) throws IOException {
char[] s = readToken();
int n = s.length;
boolean[] t = new boolean[n];
for (int i = 0; i < n; i++)
t[i] = s[i] == ok;
return t;
}
public final boolean[][] nextBooleanMatrix(int h, int w, char ok) throws IOException {
boolean[][] s = new boolean[h][];
for (int i = 0; i < h; i++) {
char[] t = readToken();
int n = t.length;
s[i] = new boolean[n];
for (int j = 0; j < n; j++)
s[i][j] = t[j] == ok;
}
return s;
}
public final String[] nextStringArray(int len) throws IOException {
String[] arr = new String[len];
for (int i = 0; i < len; i++)
arr[i] = next();
return arr;
}
public final int[] nextIntArray(int len) throws IOException {
int[] arr = new int[len];
for (int i = 0; i < len; i++)
arr[i] = nextInt();
return arr;
}
public final int[] nextIntArray(int len, java.util.function.IntUnaryOperator map) throws IOException {
int[] arr = new int[len];
for (int i = 0; i < len; i++)
arr[i] = map.applyAsInt(nextInt());
return arr;
}
public final long[] nextLongArray(int len, java.util.function.LongUnaryOperator map) throws IOException {
long[] arr = new long[len];
for (int i = 0; i < len; i++)
arr[i] = map.applyAsLong(nextLong());
return arr;
}
public final int[][] nextIntMatrix(int h, int w) throws IOException {
int[][] arr = new int[h][w];
for (int i = 0; i < h; i++)
for (int j = 0; j < w; j++)
arr[i][j] = nextInt();
return arr;
}
public final int[][] nextIntMatrix(int h, int w, java.util.function.IntUnaryOperator map) throws IOException {
int[][] arr = new int[h][w];
for (int i = 0; i < h; i++)
for (int j = 0; j < w; j++)
arr[i][j] = map.applyAsInt(nextInt());
return arr;
}
public final long[] nextLongArray(int len) throws IOException {
long[] arr = new long[len];
for (int i = 0; i < len; i++)
arr[i] = nextLong();
return arr;
}
public final long[][] nextLongMatrix(int h, int w) throws IOException {
long[][] arr = new long[h][w];
for (int i = 0; i < h; i++)
for (int j = 0; j < w; j++)
arr[i][j] = nextLong();
return arr;
}
public final float[] nextFloatArray(int len) throws IOException {
float[] arr = new float[len];
for (int i = 0; i < len; i++)
arr[i] = nextFloat();
return arr;
}
public final double[] nextDoubleArray(int len) throws IOException {
double[] arr = new double[len];
for (int i = 0; i < len; i++)
arr[i] = nextDouble();
return arr;
}
public final char[][] nextCharMatrix(int h, int w) throws IOException {
char[][] arr = new char[h][];
for (int i = 0; i < h; i++)
arr[i] = readToken();
return arr;
}
public final void nextThrow() throws IOException {
next();
return;
}
public final void nextThrow(int n) throws IOException {
for (int i = 0; i < n; i++)
nextThrow();
return;
}
}
final class ContestOutputStream extends FilterOutputStream implements Appendable {
protected final byte[] buf;
protected int pos = 0;
public ContestOutputStream() {
super(System.out);
this.buf = new byte[1 << 13];
}
@Override
public void flush() throws IOException {
out.write(buf, 0, pos);
pos = 0;
out.flush();
}
void put(byte b) throws IOException {
if (pos >= buf.length)
flush();
buf[pos++] = b;
}
int remaining() throws IOException {
if (pos >= buf.length)
flush();
return buf.length - pos;
}
@Override
public void write(int b) throws IOException {
put((byte) b);
}
@Override
public void write(byte[] b, int off, int len) throws IOException {
int o = off;
int l = len;
while (l > 0) {
int rem = Math.min(remaining(), l);
for (int i = 0; i < rem; i++)
buf[pos + i] = b[o + i];
pos += rem;
o += rem;
l -= rem;
}
}
@Override
public ContestOutputStream append(char c) throws IOException {
put((byte) c);
return this;
}
@Override
public ContestOutputStream append(CharSequence csq, int start, int end) throws IOException {
int off = start;
int len = end - start;
while (len > 0) {
int rem = Math.min(remaining(), len);
for (int i = 0; i < rem; i++)
buf[pos + i] = (byte) csq.charAt(off + i);
pos += rem;
off += rem;
len -= rem;
}
return this;
}
@Override
public ContestOutputStream append(CharSequence csq) throws IOException {
return append(csq, 0, csq.length());
}
public ContestOutputStream append(char[] arr, int off, int len) throws IOException {
int o = off;
int l = len;
while (l > 0) {
int rem = Math.min(remaining(), l);
for (int i = 0; i < rem; i++)
buf[pos + i] = (byte) arr[o + i];
pos += rem;
o += rem;
l -= rem;
}
return this;
}
public ContestOutputStream print(char[] arr) throws IOException {
return append(arr, 0, arr.length).newLine();
}
public ContestOutputStream print(boolean value) throws IOException {
if (value)
return append("o");
return append("x");
}
public ContestOutputStream println(boolean value) throws IOException {
if (value)
return append("o\n");
return append("x\n");
}
public ContestOutputStream print(boolean[][] value) throws IOException {
final int n = value.length, m = value[0].length;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++)
print(value[i][j]);
newLine();
}
return this;
}
public ContestOutputStream print(int value) throws IOException {
return append(String.valueOf(value));
}
public ContestOutputStream println(int value) throws IOException {
return append(String.valueOf(value)).newLine();
}
public ContestOutputStream print(long value) throws IOException {
return append(String.valueOf(value));
}
public ContestOutputStream println(long value) throws IOException {
return append(String.valueOf(value)).newLine();
}
public ContestOutputStream print(float value) throws IOException {
return append(String.valueOf(value));
}
public ContestOutputStream println(float value) throws IOException {
return append(String.valueOf(value)).newLine();
}
public ContestOutputStream print(double value) throws IOException {
return append(String.valueOf(value));
}
public ContestOutputStream println(double value) throws IOException {
return append(String.valueOf(value)).newLine();
}
public ContestOutputStream print(char value) throws IOException {
return append(value);
}
public ContestOutputStream println(char value) throws IOException {
return append(value).newLine();
}
public ContestOutputStream print(String value) throws IOException {
return append(value);
}
public ContestOutputStream println(String value) throws IOException {
return append(String.valueOf(value)).newLine();
}
public ContestOutputStream print(Object value) throws IOException {
return append(String.valueOf(value));
}
public ContestOutputStream println(Object value) throws IOException {
return append(String.valueOf(value)).newLine();
}
public ContestOutputStream printYN(boolean yes) throws IOException {
if (yes)
return println("Yes");
return println("No");
}
public ContestOutputStream printAB(boolean yes) throws IOException {
if (yes)
return println("Alice");
return println("Bob");
}
public ContestOutputStream print(CharSequence[] arr) throws IOException {
if (arr.length > 0) {
append(arr[0]);
for (int i = 1; i < arr.length; i++)
append('\u0020').append(arr[i]);
}
return this;
}
public ContestOutputStream print(int[] arr) throws IOException {
if (arr.length > 0) {
print(arr[0]);
for (int i = 1; i < arr.length; i++)
append('\u0020').print(arr[i]);
}
newLine();
return this;
}
public ContestOutputStream print(int[] arr, int length) throws IOException {
if (length > 0)
print(arr[0]);
for (int i = 1; i < length; i++)
append('\u0020').print(arr[i]);
newLine();
return this;
}
public ContestOutputStream println(int[] arr) throws IOException {
for (int i : arr)
print(i).newLine();
return this;
}
public ContestOutputStream println(int[] arr, int length) throws IOException {
for (int i = 0; i < length; i++)
println(arr[i]);
return this;
}
public ContestOutputStream print(boolean[] arr) throws IOException {
if (arr.length > 0) {
print(arr[0]);
for (int i = 1; i < arr.length; i++)
append('\u0020').print(arr[i]);
}
newLine();
return this;
}
public ContestOutputStream print(long[] arr) throws IOException {
if (arr.length > 0) {
print(arr[0]);
for (int i = 1; i < arr.length; i++)
append('\u0020').print(arr[i]);
}
newLine();
return this;
}
public ContestOutputStream print(long[] arr, int length) throws IOException {
if (length > 0)
print(arr[0]);
for (int i = 1; i < length; i++)
append('\u0020').print(arr[i]);
newLine();
return this;
}
public ContestOutputStream println(long[] arr, int length) throws IOException {
for (int i = 0; i < length; i++)
println(arr[i]);
return this;
}
public ContestOutputStream println(long[] arr) throws IOException {
for (long i : arr)
print(i).newLine();
return this;
}
public ContestOutputStream print(float[] arr) throws IOException {
if (arr.length > 0) {
print(arr[0]);
for (int i = 1; i < arr.length; i++)
append('\u0020').print(arr[i]);
}
return this;
}
public ContestOutputStream println(float[] arr) throws IOException {
for (float i : arr)
print(i).newLine();
return this;
}
public ContestOutputStream print(double[] arr) throws IOException {
if (arr.length > 0) {
print(arr[0]);
for (int i = 1; i < arr.length; i++)
append('\u0020').print(arr[i]);
}
return newLine();
}
public ContestOutputStream println(double[] arr) throws IOException {
for (double i : arr)
print(i).newLine();
return this;
}
public ContestOutputStream print(Object[] arr) throws IOException {
if (arr.length > 0) {
print(arr[0]);
for (int i = 1; i < arr.length; i++)
append('\u0020').print(arr[i]);
}
return newLine();
}
public ContestOutputStream print(java.util.ArrayList<?> arr) throws IOException {
if (!arr.isEmpty()) {
final int n = arr.size();
print(arr.get(0));
for (int i = 1; i < n; i++)
print(" ").print(arr.get(i));
}
return newLine();
}
public ContestOutputStream println(java.util.ArrayList<?> arr) throws IOException {
final int n = arr.size();
for (int i = 0; i < n; i++)
println(arr.get(i));
return this;
}
public ContestOutputStream println(Object[] arr) throws IOException {
for (Object i : arr)
print(i).newLine();
return this;
}
public ContestOutputStream newLine() throws IOException {
return append(System.lineSeparator());
}
public ContestOutputStream endl() throws IOException {
newLine().flush();
return this;
}
public ContestOutputStream print(int[][] arr) throws IOException {
for (int[] i : arr)
print(i);
return this;
}
public ContestOutputStream print(long[][] arr) throws IOException {
for (long[] i : arr)
print(i);
return this;
}
public ContestOutputStream print(char[][] arr) throws IOException {
for (char[] i : arr)
print(i);
return this;
}
public ContestOutputStream print(Object[][] arr) throws IOException {
for (Object[] i : arr)
print(i);
return this;
}
public ContestOutputStream println() throws IOException {
return newLine();
}
}
|
Java
|
["5\n\n2\n\n4\n\n6\n\n8\n\n60"]
|
2 seconds
|
["1 0 1\n3 2 1\n12 7 1\n42 27 1\n341102826 248150916 1"]
|
NoteIn the first test case, Alex wins if he receives the card $$$2$$$ (he plays it, and Boris cannot respond). If Alex receives the card $$$1$$$, the game ends in a draw.In the second test case: Alex wins if he receives the cards $$$[3, 4]$$$, $$$[2, 4]$$$ or $$$[1, 4]$$$; Boris wins if Alex receives the cards $$$[1, 2]$$$ or $$$[1, 3]$$$; the game ends in a draw if Alex receives the cards $$$[2, 3]$$$.
|
Java 11
|
standard input
|
[
"combinatorics",
"constructive algorithms",
"dp",
"games"
] |
63fc2fb08d248f8ccdb3f5364c96dac1
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 30$$$) — the number of test cases. Then, $$$t$$$ lines follow. The $$$i$$$-th line contains one even integer $$$n$$$ ($$$2 \le n \le 60$$$).
| 1,500
|
For each test case, print three integers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. Since the answers can be large, print them modulo $$$998244353$$$.
|
standard output
| |
PASSED
|
a20a5bcb1b633c4f201d668c67b4a663
|
train_109.jsonl
|
1664462100
|
Consider a game with $$$n$$$ cards ($$$n$$$ is even). Each card has a number written on it, between $$$1$$$ and $$$n$$$. All numbers on the cards are different. We say that a card with number $$$x$$$ is stronger than a card with number $$$y$$$ if $$$x > y$$$.Two players, Alex and Boris, play this game. In the beginning, each of them receives exactly $$$\frac{n}{2}$$$ cards, so each card belongs to exactly one player. Then, they take turns. Alex goes first, then Boris, then Alex again, and so on.On a player's turn, he must play exactly one of his cards. Then, if the opponent doesn't have any cards stronger than the card played, the opponent loses, and the game ends. Otherwise, the opponent has to play a stronger card (exactly one card as well). These two cards are removed from the game, and the turn ends. If there are no cards left, the game ends in a draw; otherwise it's the opponent's turn.Consider all possible ways to distribute the cards between two players, so that each of them receives exactly half of the cards. You have to calculate three numbers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. You may assume that both players play optimally (i. e. if a player can win no matter how his opponent plays, he wins). Two ways to distribute the cards are different if there is at least one card such that, in one of these ways, it is given to Alex, and in the other way, it is given to Boris.For example, suppose $$$n = 4$$$, Alex receives the cards $$$[2, 3]$$$, and Boris receives the cards $$$[1, 4]$$$. Then the game may go as follows: if Alex plays the card $$$2$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$3$$$. So, the game ends in a draw; if Alex plays the card $$$3$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$2$$$. So, the game ends in a draw. So, in this case, the game ends in a draw.
|
512 megabytes
|
/*input
5
2
4
6
8
60
*/
import java.util.*;
import java.lang.*;
import java.io.*;
public class Main
{
static PrintWriter out;
static int MOD = 998244353;
static FastReader scan;
/*-------- I/O usaing short named function ---------*/
public static String ns(){return scan.next();}
public static int ni(){return scan.nextInt();}
public static long nl(){return scan.nextLong();}
public static double nd(){return scan.nextDouble();}
public static String nln(){return scan.nextLine();}
public static void p(Object o){out.print(o);}
public static void ps(Object o){out.print(o + " ");}
public static void pn(Object o){out.println(o);}
/*-------- for output of an array ---------------------*/
static void iPA(int arr []){
StringBuilder output = new StringBuilder();
for(int i=0; i<arr.length; i++)output.append(arr[i] + " ");out.println(output);
}
static void lPA(long arr []){
StringBuilder output = new StringBuilder();
for(int i=0; i<arr.length; i++)output.append(arr[i] + " ");out.println(output);
}
static void sPA(String arr []){
StringBuilder output = new StringBuilder();
for(int i=0; i<arr.length; i++)output.append(arr[i] + " ");out.println(output);
}
static void dPA(double arr []){
StringBuilder output = new StringBuilder();
for(int i=0; i<arr.length; i++)output.append(arr[i] + " ");out.println(output);
}
/*-------------- for input in an array ---------------------*/
static void iIA(int arr[]){
for(int i=0; i<arr.length; i++)arr[i] = ni();
}
static void lIA(long arr[]){
for(int i=0; i<arr.length; i++)arr[i] = nl();
}
static void sIA(String arr[]){
for(int i=0; i<arr.length; i++)arr[i] = ns();
}
static void dIA(double arr[]){
for(int i=0; i<arr.length; i++)arr[i] = nd();
}
/*------------ for taking input faster ----------------*/
static class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader(){br = new BufferedReader(new InputStreamReader(System.in));}
String next(){
while (st == null || !st.hasMoreElements()){
try{st = new StringTokenizer(br.readLine());}
catch (IOException e){e.printStackTrace();}
}
return st.nextToken();
}
int nextInt(){return Integer.parseInt(next());}
long nextLong(){return Long.parseLong(next());}
double nextDouble(){return Double.parseDouble(next());}
String nextLine(){
String str = "";
try{str = br.readLine();}
catch (IOException e){e.printStackTrace();}
return str;
}
}
// Method to check if x is power of 2
static boolean isPowerOfTwo (int x) { return x!=0 && ((x&(x-1)) == 0);}
//Method to return gcd of two numbers
static int gcd(int a, int b){return a==0?b:gcd(b % a, a); }
//Method to count digit of a number
static int countDigit(long n){return (int)Math.floor(Math.log10(n) + 1);}
//Method for sorting
static void sort(int[] a) {
ArrayList<Integer> l=new ArrayList<>();
for (int i:a) l.add(i);
Collections.sort(l);
for (int i=0; i<a.length; i++) a[i]=l.get(i);
}
//Method for sorting
static void sort(long[] a) {
ArrayList<Long> l=new ArrayList<>();
for (long i:a) l.add(i);
Collections.sort(l);
for (int i=0; i<a.length; i++) a[i]=l.get(i);
}
//Method foAr checking if a number is prime or not
static boolean isPrime(int n) {
if (n <= 1) return false;
if (n <= 3) return true;
if (n % 2 == 0 || n % 3 == 0) return false;
for (int i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
public static void main (String[] args) throws java.lang.Exception
{
out =new PrintWriter(System.out);
scan =new FastReader();
//for fast output sometimes
StringBuilder sb = new StringBuilder();
int t = ni();
long res[][] = new long[61][3];
res[2][0] = 1;
res[2][1] = 0;
res[2][2] = 1;
for(int i=4; i<=60; i+=2){
long temp = nCr(i-1, i/2-1);
temp += res[i-2][1];
temp%=MOD;
res[i][0] = temp;
long temp2 = nCr(i, i/2);
temp2 -= res[i][0];
temp2 -= 1;
temp2 += MOD;
temp2 %= MOD;
res[i][1] = temp2;
res[i][2] = 1;
}
while(t-->0){
int n = ni();
pn(res[n][0] + " " + res[n][1] + " " + res[n][2]);
}
out.flush();
out.close();
}
static long mul(long a, long b){
return (a%MOD * b%MOD)%MOD;
}
static long fact(long n){
long ans=1;
for(int i=2; i<=n; i++)
ans = mul(ans, i);
return ans;
}
static long fastPow(long a, long b){
long result =1;
while(b>0){
if(b%2==1)result = mul(result, a);
a = mul(a, a);
b/=2;
}
return result;
}
static long modInv(long x){
return fastPow(x, MOD-2);
}
static long nCr(long n, long r){
return mul(fact(n), mul(modInv(fact(n-r)), modInv(fact(r))));
}
}
|
Java
|
["5\n\n2\n\n4\n\n6\n\n8\n\n60"]
|
2 seconds
|
["1 0 1\n3 2 1\n12 7 1\n42 27 1\n341102826 248150916 1"]
|
NoteIn the first test case, Alex wins if he receives the card $$$2$$$ (he plays it, and Boris cannot respond). If Alex receives the card $$$1$$$, the game ends in a draw.In the second test case: Alex wins if he receives the cards $$$[3, 4]$$$, $$$[2, 4]$$$ or $$$[1, 4]$$$; Boris wins if Alex receives the cards $$$[1, 2]$$$ or $$$[1, 3]$$$; the game ends in a draw if Alex receives the cards $$$[2, 3]$$$.
|
Java 11
|
standard input
|
[
"combinatorics",
"constructive algorithms",
"dp",
"games"
] |
63fc2fb08d248f8ccdb3f5364c96dac1
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 30$$$) — the number of test cases. Then, $$$t$$$ lines follow. The $$$i$$$-th line contains one even integer $$$n$$$ ($$$2 \le n \le 60$$$).
| 1,500
|
For each test case, print three integers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. Since the answers can be large, print them modulo $$$998244353$$$.
|
standard output
| |
PASSED
|
0f136e8ffc6beea8de493ac5259c8478
|
train_109.jsonl
|
1664462100
|
Consider a game with $$$n$$$ cards ($$$n$$$ is even). Each card has a number written on it, between $$$1$$$ and $$$n$$$. All numbers on the cards are different. We say that a card with number $$$x$$$ is stronger than a card with number $$$y$$$ if $$$x > y$$$.Two players, Alex and Boris, play this game. In the beginning, each of them receives exactly $$$\frac{n}{2}$$$ cards, so each card belongs to exactly one player. Then, they take turns. Alex goes first, then Boris, then Alex again, and so on.On a player's turn, he must play exactly one of his cards. Then, if the opponent doesn't have any cards stronger than the card played, the opponent loses, and the game ends. Otherwise, the opponent has to play a stronger card (exactly one card as well). These two cards are removed from the game, and the turn ends. If there are no cards left, the game ends in a draw; otherwise it's the opponent's turn.Consider all possible ways to distribute the cards between two players, so that each of them receives exactly half of the cards. You have to calculate three numbers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. You may assume that both players play optimally (i. e. if a player can win no matter how his opponent plays, he wins). Two ways to distribute the cards are different if there is at least one card such that, in one of these ways, it is given to Alex, and in the other way, it is given to Boris.For example, suppose $$$n = 4$$$, Alex receives the cards $$$[2, 3]$$$, and Boris receives the cards $$$[1, 4]$$$. Then the game may go as follows: if Alex plays the card $$$2$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$3$$$. So, the game ends in a draw; if Alex plays the card $$$3$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$2$$$. So, the game ends in a draw. So, in this case, the game ends in a draw.
|
512 megabytes
|
import java.math.BigDecimal;
import java.util.Scanner;
public class C {
static BigDecimal[][] dp = new BigDecimal[61][3];
static {
dp[2][0] = new BigDecimal(1);
dp[2][1] = new BigDecimal(0);
dp[2][2] = new BigDecimal(1);
for (int i = 4; i <=60 ; i+=2) {
BigDecimal a = new BigDecimal(1);
for (int j = i-1; j >=i/2+1; j--) {
a = a.multiply(new BigDecimal(j+""));
}
for (int j = 1; j <= i/2-1; j++) {
a = a.divide(new BigDecimal(j+""));
}
BigDecimal all = new BigDecimal(1);
for (int j = i; j >=i/2+1; j--) {
all = all.multiply(new BigDecimal(j+""));
}
for (int j = 1; j <= i/2; j++) {
all = all.divide(new BigDecimal(j+""));
}
dp[i][0] = dp[i-2][1].add(a);
dp[i][1] = all.subtract(dp[i][0]).subtract(new BigDecimal(1));
dp[i][2] = new BigDecimal(1);
}
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
while (t-->0){
int n = sc.nextInt();
BigDecimal a = dp[n][0];
BigDecimal b = dp[n][1];
BigDecimal[] d = a.divideAndRemainder(new BigDecimal(998244353 ));
BigDecimal[] e = b.divideAndRemainder(new BigDecimal(998244353 ));
System.out.println(d[1].intValue() + " " + e[1].intValue() + " "+ dp[n][2].intValue());
}
}
}
|
Java
|
["5\n\n2\n\n4\n\n6\n\n8\n\n60"]
|
2 seconds
|
["1 0 1\n3 2 1\n12 7 1\n42 27 1\n341102826 248150916 1"]
|
NoteIn the first test case, Alex wins if he receives the card $$$2$$$ (he plays it, and Boris cannot respond). If Alex receives the card $$$1$$$, the game ends in a draw.In the second test case: Alex wins if he receives the cards $$$[3, 4]$$$, $$$[2, 4]$$$ or $$$[1, 4]$$$; Boris wins if Alex receives the cards $$$[1, 2]$$$ or $$$[1, 3]$$$; the game ends in a draw if Alex receives the cards $$$[2, 3]$$$.
|
Java 11
|
standard input
|
[
"combinatorics",
"constructive algorithms",
"dp",
"games"
] |
63fc2fb08d248f8ccdb3f5364c96dac1
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 30$$$) — the number of test cases. Then, $$$t$$$ lines follow. The $$$i$$$-th line contains one even integer $$$n$$$ ($$$2 \le n \le 60$$$).
| 1,500
|
For each test case, print three integers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. Since the answers can be large, print them modulo $$$998244353$$$.
|
standard output
| |
PASSED
|
72d7bbd47655696df8ab852eb1bb0c36
|
train_109.jsonl
|
1664462100
|
Consider a game with $$$n$$$ cards ($$$n$$$ is even). Each card has a number written on it, between $$$1$$$ and $$$n$$$. All numbers on the cards are different. We say that a card with number $$$x$$$ is stronger than a card with number $$$y$$$ if $$$x > y$$$.Two players, Alex and Boris, play this game. In the beginning, each of them receives exactly $$$\frac{n}{2}$$$ cards, so each card belongs to exactly one player. Then, they take turns. Alex goes first, then Boris, then Alex again, and so on.On a player's turn, he must play exactly one of his cards. Then, if the opponent doesn't have any cards stronger than the card played, the opponent loses, and the game ends. Otherwise, the opponent has to play a stronger card (exactly one card as well). These two cards are removed from the game, and the turn ends. If there are no cards left, the game ends in a draw; otherwise it's the opponent's turn.Consider all possible ways to distribute the cards between two players, so that each of them receives exactly half of the cards. You have to calculate three numbers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. You may assume that both players play optimally (i. e. if a player can win no matter how his opponent plays, he wins). Two ways to distribute the cards are different if there is at least one card such that, in one of these ways, it is given to Alex, and in the other way, it is given to Boris.For example, suppose $$$n = 4$$$, Alex receives the cards $$$[2, 3]$$$, and Boris receives the cards $$$[1, 4]$$$. Then the game may go as follows: if Alex plays the card $$$2$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$3$$$. So, the game ends in a draw; if Alex plays the card $$$3$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$2$$$. So, the game ends in a draw. So, in this case, the game ends in a draw.
|
512 megabytes
|
import java.io.*;
import java.util.Arrays;
import java.util.*;
import java.util.StringTokenizer;
public class copy {
static int log=30;
static int[][] ancestor;
static int[] depth;
static int[] sieveOfEratosthenes(int n) {
int prime[] = new int[n + 1];
for (int i = 0; i <= n; i++)
prime[i] = (int)1e7;
for (int p = 2; p * p <= n; p++) {
// If prime[p] is not changed, then it is a
// prime
if (prime[p]==(int)1e7) {
// Update all multiples of p
for (int i = p * p; i <= n; i += p)
{
prime[i] = Math.min(prime[i],p);
}
}
}
// Print all prime numbers
// for (int i = 2; i <= 12; i++) {
// System.out.println(prime[i]);
//
// }
return prime;
}
public static long fac(long N, long mod) {
if (N == 0)
return 1;
if(N==1)
return 1;
return ((N % mod) * (fac(N - 1, mod) % mod)) % mod;
}
static long power(long x, long y, long p) {
// Initialize result
long res = 1;
// Update x if it is more than or
// equal to p
x = x % p;
while (y > 0) {
// If y is odd, multiply x
// with result
if (y % 2 == 1)
res = (res * x) % p;
// y must be even now
y = y >> 1; // y = y/2
x = (x * x) % p;
}
return res;
}
// Returns n^(-1) mod p
static long modInverse(long n, long p) {
return power(n, p - 2, p);
}
// Returns nCr % p using Fermat's
// little theorem.
static long nCrModPFermat(int n, int r,
long p,long[] fac) {
if (n < r)
return 0;
// Base case
if (r == 0)
return 1;
return ((fac[n] % p * (modInverse(fac[r], p)
% p)) % p * (modInverse(fac[n-r], p)
% p)) % p;
}
public static int find(int[] parent, int x) {
if (parent[x] == x)
return x;
return find(parent, parent[x]);
}
public static void merge(int[] parent, int[] rank, int x, int y,int[] child) {
int x1 = find(parent, x);
int y1 = find(parent, y);
if (rank[x1] > rank[y1]) {
parent[y1] = x1;
child[x1]+=child[y1];
} else if (rank[y1] > rank[x1]) {
parent[x1] = y1;
child[y1]+=child[x1];
} else {
parent[y1] = x1;
child[x1]+=child[y1];
rank[x1]++;
}
}
public static long gcd(long a, long b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
static int power(int x, int y, int p)
{
int res = 1;
x = x % p;
while (y > 0) {
if (y % 2 == 1)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
// Returns n^(-1) mod p
static int modInverse(int n, int p)
{
return power(n, p - 2, p);
}
// Returns nCr % p using Fermat's
// little theorem.
static int nCrModPFermat(int n, int r,
int p,int[] fac)
{
if (n<r)
return 0;
// Base case
if (r == 0)
return 1;
// Fill factorial array so that we
// can find all factorial of r, n
// and n-r
// int[] fac = new int[n + 1];
// fac[0] = 1;
//
// for (int i = 1; i <= n; i++)
// fac[i] = fac[i - 1] * i % p;
return (fac[n] * modInverse(fac[r], p)
% p * modInverse(fac[n - r], p)
% p)
% p;
}
public static long[][] ncr(int n,int r)
{
long[][] dp=new long[n+1][r+1];
for(int i=0;i<=n;i++)
dp[i][0]=1;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=r;j++)
{
if(j>i)
continue;
dp[i][j]=dp[i-1][j-1]+dp[i-1][j];
}
}
return dp;
}
public static boolean prime(long N)
{
int c=0;
for(int i=2;i*i<=N;i++)
{
if(N%i==0)
++c;
}
return c==0;
}
public static int sparse_ancestor_table(ArrayList<ArrayList<Integer>> arr,int x,int parent,int[] child)
{
int c=0;
for(int i:arr.get(x))
{
if(i!=parent)
{
// System.out.println(i+" hello "+x);
depth[i]=depth[x]+1;
ancestor[i][0]=x;
// if(i==2)
// System.out.println(parent+" hello");
for(int j=1;j<log;j++)
ancestor[i][j]=ancestor[ancestor[i][j-1]][j-1];
c+=sparse_ancestor_table(arr,i,x,child);
}
}
child[x]=1+c;
return child[x];
}
public static int lca(int x,int y)
{
if(depth[x]<depth[y])
{
int temp=x;
x=y;
y=temp;
}
x=get_kth_ancestor(depth[x]-depth[y],x);
if(x==y)
return x;
// System.out.println(x+" "+y);
for(int i=log-1;i>=0;i--)
{
if(ancestor[x][i]!=ancestor[y][i])
{
x=ancestor[x][i];
y=ancestor[y][i];
}
}
return ancestor[x][0];
}
public static int get_kth_ancestor(int K,int x)
{
if(K==0)
return x;
int node=x;
for(int i=0;i<log;i++)
{
if(K%2!=0)
{
node=ancestor[node][i];
}
K/=2;
}
return node;
}
public static ArrayList<Integer> primeFactors(int n)
{
// Print the number of 2s that divide n
ArrayList<Integer> factors=new ArrayList<>();
if(n%2==0)
factors.add(2);
while (n%2==0)
{
n /= 2;
}
// n must be odd at this point. So we can
// skip one element (Note i = i +2)
for (int i = 3; i <= Math.sqrt(n); i+= 2)
{
// While i divides n, print i and divide n
if(n%i==0)
factors.add(i);
while (n%i == 0)
{
n /= i;
}
}
// This condition is to handle the case when
// n is a prime number greater than 2
if (n > 2)
{
factors.add(n);
}
return factors;
}
static long ans=1,mod=1000000007;
public static void recur(long X,long N,int index,ArrayList<Integer> temp)
{
// System.out.println(X);
if(index==temp.size())
{
System.out.println(X);
ans=((ans%mod)*(X%mod))%mod;
return;
}
for(int i=0;i<=60;i++)
{
if(X*Math.pow(temp.get(index),i)<=N)
recur(X*(long)Math.pow(temp.get(index),i),N,index+1,temp);
else
break;
}
}
public static int upper(ArrayList<Integer> temp,int X)
{
int l=0,r=temp.size()-1;
while(l<=r)
{
int mid=(l+r)/2;
if(temp.get(mid)==X)
return mid;
// System.out.println(mid+" "+temp.get(mid));
if(temp.get(mid)<X)
l=mid+1;
else
{
if(mid-1>=0 && temp.get(mid-1)>=X)
r=mid-1;
else
return mid;
}
}
return -1;
}
public static int lower(ArrayList<Integer> temp,int X)
{
int l=0,r=temp.size()-1;
while(l<=r)
{
int mid=(l+r)/2;
if(temp.get(mid)==X)
return mid;
// System.out.println(mid+" "+temp.get(mid));
if(temp.get(mid)>X)
r=mid-1;
else
{
if(mid+1<temp.size() && temp.get(mid+1)<=X)
l=mid+1;
else
return mid;
}
}
return -1;
}
public static int[] check(String shelf,int[][] queries)
{
int[] arr=new int[queries.length];
ArrayList<Integer> indices=new ArrayList<>();
for(int i=0;i<shelf.length();i++)
{
char ch=shelf.charAt(i);
if(ch=='|')
indices.add(i);
}
for(int i=0;i<queries.length;i++)
{
int x=queries[i][0]-1;
int y=queries[i][1]-1;
int left=upper(indices,x);
int right=lower(indices,y);
if(left<=right && left!=-1 && right!=-1)
{
arr[i]=indices.get(right)-indices.get(left)+1-(right-left+1);
}
else
arr[i]=0;
}
return arr;
}
static boolean check;
public static void check(ArrayList<ArrayList<Integer>> arr,int x,int[] color,boolean[] visited)
{
visited[x]=true;
PriorityQueue<Integer> pq=new PriorityQueue<>();
for(int i:arr.get(x))
{
if(color[i]<color[x])
pq.add(color[i]);
if(color[i]==color[x])
check=false;
if(!visited[i])
check(arr,i,color,visited);
}
int start=1;
while(pq.size()>0)
{
int temp=pq.poll();
if(temp==start)
++start;
else
break;
}
if(start!=color[x])
check=false;
}
static boolean cycle;
public static void cycle(boolean[] stack,boolean[] visited,int x,ArrayList<ArrayList<Integer>> arr)
{
if(stack[x])
{
cycle=true;
return;
}
visited[x]=true;
for(int i:arr.get(x))
{
if(!visited[x])
{
cycle(stack,visited,i,arr);
}
}
stack[x]=false;
}
public static int check(char[][] ch,int x,int y)
{
int cnt=0;
int c=0;
int N=ch.length;
int x1=x,y1=y;
while(c<ch.length)
{
if(ch[x][y]=='1')
++cnt;
// if(x1==0 && y1==3)
// System.out.println(x+" "+y+" "+cnt);
x=(x+1)%N;
y=(y+1)%N;
++c;
}
return cnt;
}
public static void s(char[][] arr,int x)
{
char start=arr[arr.length-1][x];
for(int i=arr.length-1;i>0;i--)
{
arr[i][x]=arr[i-1][x];
}
arr[0][x]=start;
}
public static void shuffle(char[][] arr,int x,int down)
{
int N= arr.length;
down%=N;
char[] store=new char[N-down];
for(int i=0;i<N-down;i++)
store[i]=arr[i][x];
for(int i=0;i<arr.length;i++)
{
if(i<down)
{
// Printing rightmost
// kth elements
arr[i][x]=arr[N + i - down][x];
}
else
{
// Prints array after
// 'k' elements
arr[i][x]=store[i-down];
}
}
}
public static String form(int C1,char ch1,char ch2)
{
char ch=ch1;
String s="";
for(int i=1;i<=C1;i++)
{
s+=ch;
if(ch==ch1)
ch=ch2;
else
ch=ch1;
}
return s;
}
public static void printArray(long[] arr)
{
for(int i=0;i<arr.length;i++)
System.out.print(arr[i]+" ");
System.out.println();
}
public static boolean check(long mid,long[] arr,long K)
{
long[] arr1=Arrays.copyOfRange(arr,0,arr.length);
long ans=0;
for(int i=0;i<arr1.length-1;i++)
{
if(arr1[i]+arr1[i+1]>=mid)
{
long check=(arr1[i]+arr1[i+1])/mid;
// if(mid==5)
// System.out.println(check);
long left=check*mid;
left-=arr1[i];
if(left>=0)
arr1[i+1]-=left;
ans+=check;
}
// if(mid==5)
// printArray(arr1);
}
// if(mid==5)
// System.out.println(ans);
ans+=arr1[arr1.length-1]/mid;
return ans>=K;
}
public static long search(long sum,long[] arr,long K)
{
long l=1,r=sum/K;
while(l<=r)
{
long mid=(l+r)/2;
if(check(mid,arr,K))
{
if(mid+1<=sum/K && check(mid+1,arr,K))
l=mid+1;
else
return mid;
}
else
r=mid-1;
}
return -1;
}
public static void primeFactors(int n,HashSet<Integer> hp)
{
// Print the number of 2s that divide n
ArrayList<Integer> factors=new ArrayList<>();
if(n%2==0)
hp.add(2);
while (n%2==0)
{
n /= 2;
}
// n must be odd at this point. So we can
// skip one element (Note i = i +2)
for (int i = 3; i <= Math.sqrt(n); i+= 2)
{
// While i divides n, print i and divide n
if(n%i==0)
hp.add(i);
while (n%i == 0)
{
n /= i;
}
}
// This condition is to handle the case when
// n is a prime number greater than 2
if (n > 2)
{
hp.add(n);
}
}
public static boolean check(String s)
{
HashSet<Character> hp=new HashSet<>();
char ch=s.charAt(0);
for(int i=1;i<s.length();i++)
{
// System.out.println(hp+" "+s.charAt(i));
if(hp.contains(s.charAt(i)))
{
// System.out.println(i);
// System.out.println(hp);
// System.out.println(s.charAt(i));
return false;
}
if(s.charAt(i)!=ch)
{
hp.add(ch);
ch=s.charAt(i);
}
}
return true;
}
public static int check_end(String[] arr,boolean[] st,char ch)
{
for(int i=0;i<arr.length;i++)
{
if(ch==arr[i].charAt(0) && !st[i] && ch==arr[i].charAt(arr[i].length()-1))
return i;
}
for(int i=0;i<arr.length;i++)
{
if(ch==arr[i].charAt(0) && !st[i])
return i;
}
return -1;
}
public static int check_start(String[] arr,boolean[] st,char ch)
{
for(int i=0;i<arr.length;i++)
{
// if(ch=='B')
// {
// if(!st[i])
// System.out.println(arr[i]+" hello");
// }
if(ch==arr[i].charAt(arr[i].length()-1) && !st[i] && ch==arr[i].charAt(0))
return i;
}
for(int i=0;i<arr.length;i++)
{
// if(ch=='B')
// {
// if(!st[i])
// System.out.println(arr[i]+" hello");
// }
if(ch==arr[i].charAt(arr[i].length()-1) && !st[i])
return i;
}
return -1;
}
public static boolean palin(int N)
{
String s="";
while(N>0)
{
s+=N%10;
N/=10;
}
int l=0,r=s.length()-1;
while(l<=r)
{
if(s.charAt(l)!=s.charAt(r))
return false;
++l;
--r;
}
return true;
}
public static boolean check(long org_s,long org_d,long org_n,long check_ele)
{
if(check_ele<org_s)
return false;
if((check_ele-org_s)%org_d!=0)
return false;
long num=(check_ele-org_s)/org_d;
// if(check_ele==5)
// System.out.println(num+" "+org_n);
return num+1<=org_n;
}
public static long check(long c,long c_diff,long mod,long b_start,long c_start, long c_end,long b_end,long b_diff)
{
// System.out.println(c);
long max=Math.max(c,b_diff);
long min=Math.min(c,b_diff);
long lcm=(max/gcd(max,min))*min;
// System.out.println(lcm);
// System.out.println(c);
// System.out.println(c_diff);
// if(b_diff>c)
// {
long start_point=c_diff/c-c_diff/lcm;
// System.out.println(start_point);
// }
// else
// {
// start_point=c_diff/b_diff-c_diff/c;
// }
// System.out.println(c+" "+start_point);
return (start_point%mod*start_point%mod)%mod;
}
public static boolean check_bounds(int x, int y, int N, int M)
{
return x>=0 && x<N && y>=0 && y<M;
}
static boolean found=false;
public static void check(int x,int y,int[][] arr,boolean status[][])
{
if(arr[x][y]==9)
{
found=true;
return;
}
status[x][y]=true;
if(check_bounds(x-1,y, arr.length,arr[0].length)&& !status[x-1][y])
check(x-1,y,arr,status);
if(check_bounds(x+1,y, arr.length,arr[0].length)&& !status[x+1][y])
check(x+1,y,arr,status);
if(check_bounds(x,y-1, arr.length,arr[0].length)&& !status[x][y-1])
check(x,y-1,arr,status);
if(check_bounds(x,y+1, arr.length,arr[0].length)&& !status[x][y+1])
check(x,y+1,arr,status);
}
public static int check(String s1,String s2,int M)
{
int ans=0;
for(int i=0;i<M;i++)
{
ans+=Math.abs(s1.charAt(i)-s2.charAt(i));
}
return ans;
}
public static int check(int[][] arr,int dir1,int dir2,int x1,int y1)
{
int sum=0,N=arr.length,M=arr[0].length;
int x=x1+dir1,y=y1+dir2;
while(x<N && x>=0 && y<M && y>=0)
{
sum+=arr[x][y];
x=x+dir1;
y+=dir2;
}
return sum;
}
public static int check(long[] pref,long X,int N)
{
if(X>pref[N-1])
return -1;
// System.out.println(pref[0]);
if(X<=pref[0])
return 1;
int l=0,r=N-1;
while(l<=r)
{
int mid=(l+r)/2;
if(pref[mid]>=X)
{
if(mid-1>=0 && pref[mid-1]<X)
return mid+1;
else
r=mid-1;
}
else
l=mid+1;
}
return -1;
}
private static long mergeAndCount(long[] arr, int l,
int m, int r)
{
// Left subarray
long[] left = Arrays.copyOfRange(arr, l, m + 1);
// Right subarray
long[] right = Arrays.copyOfRange(arr, m + 1, r + 1);
int i = 0, j = 0, k = l;long swaps = 0;
while (i < left.length && j < right.length) {
if (left[i] < right[j])
arr[k++] = left[i++];
else {
arr[k++] = right[j++];
swaps += (m + 1) - (l + i);
}
}
while (i < left.length)
arr[k++] = left[i++];
while (j < right.length)
arr[k++] = right[j++];
return swaps;
}
// Merge sort function
private static long mergeSortAndCount(long[] arr, int l,
int r)
{
// Keeps track of the inversion count at a
// particular node of the recursion tree
long count = 0;
if (l < r) {
int m = (l + r) / 2;
// Total inversion count = left subarray count
// + right subarray count + merge count
// Left subarray count
count += mergeSortAndCount(arr, l, m);
// Right subarray count
count += mergeSortAndCount(arr, m + 1, r);
// Merge count
count += mergeAndCount(arr, l, m, r);
}
return count;
}
public static long check(long L,long R)
{
long ans=0;
for(int i=1;i<=Math.pow(10,8);i++)
{
long A=i*(long)i;
if(A<L)
continue;
long upper=(long)Math.floor(Math.sqrt(A-L));
long lower=(long)Math.ceil(Math.sqrt(Math.max(A-R,0)));
if(upper>=lower)
ans+=upper-lower+1;
}
return ans;
}
public static int check(ArrayList<ArrayList<Integer>> arr,int x,int parent,int[]store)
{
int index=0;
ArrayList<Integer> temp=arr.get(x);
for(int i:temp)
{
if(i!=parent)
{
index+=check(arr,i,x,store);
}
}
store[x]=index;
return index+1;
}
public static void finans(int[][] store,ArrayList<ArrayList<Integer>> arr,int x,int parent)
{
// ++delete;
// System.out.println(x);
if(store[x][0]==0 && store[x][1]==0)
return;
if(store[x][0]!=0 && store[x][1]==0)
{
++delete;
ans+=store[x][0];
return;
}
if(store[x][0]==0 && store[x][1]!=0)
{
++delete;
ans+=store[x][1];
return;
}
ArrayList<Integer> temp=arr.get(x);
if(store[x][0]!=0 && store[x][1]!=0)
{
++delete;
if(store[x][0]>store[x][1])
{
ans+=store[x][0];
for(int i=temp.size()-1;i>=0;i--)
{
if(temp.get(i)!=parent)
{
finans(store,arr,temp.get(i),x);
break;
}
}
}
else
{
ans+=store[x][1];
for(int i=0;i<temp.size();i++)
{
if(temp.get(i)!=parent)
{
finans(store,arr,temp.get(i),x);
break;
}
}
}
}
}
public static int dfs(ArrayList<ArrayList<Integer>> arr,int x,int parent,int[] store)
{
int index1=-1,index2=-1;
for(int i=0;i<arr.get(x).size();i++)
{
if(arr.get(x).get(i)!=parent)
{
if(index1==-1)
{
index1=i;
}
else
index2=i;
}
}
if(index1==-1)
{
return 0;
}
if(index2==-1)
{
return store[arr.get(x).get(index1)];
}
// System.out.println(x);
// System.out.println();;
return Math.max(store[arr.get(x).get(index1)]+dfs(arr,arr.get(x).get(index2),x,store),store[arr.get(x).get(index2)]+dfs(arr,arr.get(x).get(index1),x,store));
}
static int delete=0;
public static boolean bounds(int x,int y,int N,int M)
{
return x>=0 && x<N && y>=0 && y<M;
}
public static int gcd_check(ArrayList<Integer> temp,char[] ch, int[] arr)
{
ArrayList<Integer> ini=new ArrayList<>(temp);
for(int i=0;i<temp.size();i++)
{
for(int j=0;j<temp.size();j++)
{
int req=temp.get(j);
temp.set(j,arr[req-1]);
}
boolean status=true;
for(int j=0;j<temp.size();j++)
{
if(ch[ini.get(j)-1]!=ch[temp.get(j)-1])
status=false;
}
if(status)
return i+1;
}
return temp.size();
}
static long LcmOfArray(int[] arr, int idx)
{
// lcm(a,b) = (a*b/gcd(a,b))
if (idx == arr.length - 1){
return arr[idx];
}
int a = arr[idx];
long b = LcmOfArray(arr, idx+1);
return (a*b/gcd(a,b)); //
}
public static boolean check(ArrayList<Integer> arr,int sum)
{
for(int i=0;i<arr.size();i++)
{
for(int j=i+1;j<arr.size();j++)
{
for(int k=j+1;k<arr.size();k++)
{
if(arr.get(i)+arr.get(j)+arr.get(k)==sum)
return true;
}
}
}
return false;
}
// Returns true if str1 is smaller than str2.
static boolean isSmaller(String str1, String str2)
{
// Calculate lengths of both string
int n1 = str1.length(), n2 = str2.length();
if (n1 < n2)
return true;
if (n2 < n1)
return false;
for (int i = 0; i < n1; i++)
if (str1.charAt(i) < str2.charAt(i))
return true;
else if (str1.charAt(i) > str2.charAt(i))
return false;
return false;
}
public static int check(List<String> history)
{
int[][] arr=new int[history.size()][history.get(0).length()];
for(int i=0;i<arr.length;i++)
{
for(int j=0;j<arr[0].length;j++)
{
arr[i][j]=history.get(i).charAt(j)-48;
}
}
for(int i=0;i<arr.length;i++)
Arrays.sort(arr[i]);
int sum=0;
for(int i=0;i<arr[0].length;i++)
{
int max=0;
for(int j=0;j<arr.length;j++)
max=Math.max(max,arr[j][i]);
sum+=max;
}
return sum;
}
// Function for find difference of larger numbers
static String findDiff(String str1, String str2)
{
// Before proceeding further, make sure str1
// is not smaller
if (isSmaller(str1, str2)) {
String t = str1;
str1 = str2;
str2 = t;
}
// Take an empty string for storing result
String str = "";
// Calculate length of both string
int n1 = str1.length(), n2 = str2.length();
// Reverse both of strings
str1 = new StringBuilder(str1).reverse().toString();
str2 = new StringBuilder(str2).reverse().toString();
int carry = 0;
// Run loop till small string length
// and subtract digit of str1 to str2
for (int i = 0; i < n2; i++) {
// Do school mathematics, compute difference of
// current digits
int sub
= ((int)(str1.charAt(i) - '0')
- (int)(str2.charAt(i) - '0') - carry);
// If subtraction is less then zero
// we add then we add 10 into sub and
// take carry as 1 for calculating next step
if (sub < 0) {
sub = sub + 10;
carry = 1;
}
else
carry = 0;
str += (char)(sub + '0');
}
// subtract remaining digits of larger number
for (int i = n2; i < n1; i++) {
int sub = ((int)(str1.charAt(i) - '0') - carry);
// if the sub value is -ve, then make it
// positive
if (sub < 0) {
sub = sub + 10;
carry = 1;
}
else
carry = 0;
str += (char)(sub + '0');
}
// reverse resultant string
return new StringBuilder(str).reverse().toString();
}
static int nCr(int n, int r)
{
return fact(n) / (fact(r) *
fact(n - r));
}
// Returns factorial of n
static int fact(int n)
{
int res = 1;
for (int i = 2; i <= n; i++)
res = res * i;
return res;
}
public static void fill(int[][] dp,int[] arr,int N)
{
for(int i=1;i<=N;i++)
dp[i][0]=arr[i];
for (int j = 1; j <= dp[0].length; j++)
for (int i = 1; i + (1 << j) <= N; i++)
dp[i][j] = Math.max(dp[i][j-1], dp[i + (1 << (j - 1))][j - 1]);
}
static int start=0;
// static boolean status;
public static void dfs(ArrayList<ArrayList<Integer>> arr,int[] A,int[] B,int x,int time,int parent,int K,int coins)
{
if(time>K)
return;
ans=Math.max(ans,coins);
for(int i:arr.get(x))
{
if(i!=parent)
{
dfs(arr,A,B,i,time+1,x,K,coins);
dfs(arr,A,B,i,time+1+B[i],x,K,coins+A[i]);
}
}
}
public static boolean dfs_diameter(ArrayList<ArrayList<Integer>> arr,int x,int parent,int node1,int node2,int[] child)
{
boolean status=false;
for(int i:arr.get(x))
{
if(i!=parent)
{
boolean ch=dfs_diameter(arr,i,x,node1,node2,child);
status= status || ch;
if(!ch && x==node1)
ans+=child[i];
child[x]+=child[i];
}
}
child[x]+=1;
return status || (x==node2);
}
public static boolean check_avai(int x,int y,int sx,int sy,int di)
{
// if(x==4 && y==4)
// System.out.println((Math.abs(x-sx)+Math.abs(y-sy))<=di);
return (Math.abs(x-sx)+Math.abs(y-sy))<=di;
}
public static int lower_index(ArrayList<Integer> arr, int x)
{
// System.out.println(x+" MC");
int l=0,r=arr.size()-1;
while(l<=r)
{
int mid=(l+r)/2;
if(arr.get(mid)<x)
l=mid+1;
else
{
if(mid-1>=0 && arr.get(mid-1)>= x)
r=mid-1;
else
return mid;
}
}
return -1;
}
public static int higher_index(ArrayList<Integer> arr, int x)
{
int l=0,r=arr.size()-1;
while(l<=r)
{
int mid=(l+r)/2;
if(arr.get(mid)>x)
r=mid-1;
else
{
if(mid+1<arr.size() && arr.get(mid+1)<=x )
l=mid+1;
else
return mid;
}
}
return -1;
}
static int time;
static void bridgeUtil(int u, boolean visited[], int disc[], int low[], int parent[],ArrayList<ArrayList<Integer>> adj,HashMap<Integer,Integer> hp)
{
// Mark the current node as visited
visited[u] = true;
// Initialize discovery time and low value
disc[u] = low[u] = ++time;
// Go through all vertices adjacent to this
Iterator<Integer> i = adj.get(u).iterator();
while (i.hasNext())
{
int v = i.next(); // v is current adjacent of u
// If v is not visited yet, then make it a child
// of u in DFS tree and recur for it.
// If v is not visited yet, then recur for it
if (!visited[v])
{
parent[v] = u;
bridgeUtil(v, visited, disc, low, parent,adj,hp);
// Check if the subtree rooted with v has a
// connection to one of the ancestors of u
low[u] = Math.min(low[u], low[v]);
// If the lowest vertex reachable from subtree
// under v is below u in DFS tree, then u-v is
// a bridge
if (low[v] < disc[u])
{
hp.put(u,v);
hp.put(v,u);
}
}
// Update low value of u for parent function calls.
else if (v != parent[u])
low[u] = Math.min(low[u], disc[v]);
}
}
static int r;
static int b;
public static void traverse(ArrayList<ArrayList<Integer>> arr,int x,int parent,int[][] dp,int[] level,int K) {
if(parent!=-1)
level[x]=level[parent]+1;
dp[x][0] += 1;
for (int i : arr.get(x))
{
if(i!=parent)
{
traverse(arr,i,x,dp,level,K);
for(int j=1;j<=K;j++)
dp[x][j]+=dp[i][j-1];
}
}
}
static int KMPSearch(int[] pat, int[] txt)
{
int M = pat.length;
int N = txt.length;
// create lps[] that will hold the longest
// prefix suffix values for pattern
int lps[] = new int[M];
int j = 0; // index for pat[]
// Preprocess the pattern (calculate lps[]
// array)
computeLPSArray(pat, M, lps);
int i = 0; // index for txt[]
while ((N - i) >= (M - j)) {
if (pat[j] == txt[i]) {
j++;
i++;
}
if (j == M) {
return i-j;
}
// mismatch after j matches
else if (i < N && pat[j] != txt[i]) {
// Do not match lps[0..lps[j-1]] characters,
// they will match anyway
if (j != 0)
j = lps[j - 1];
else
i = i + 1;
}
}
return -1;
}
static void computeLPSArray(int[] pat, int M, int lps[])
{
// length of the previous longest prefix suffix
int len = 0;
int i = 1;
lps[0] = 0; // lps[0] is always 0
// the loop calculates lps[i] for i = 1 to M-1
while (i < M) {
if (pat[i] == pat[len]) {
len++;
lps[i] = len;
i++;
}
else // (pat[i] != pat[len])
{
// This is tricky. Consider the example.
// AAACAAAA and i = 7. The idea is similar
// to search step.
if (len != 0) {
len = lps[len - 1];
// Also, note that we do not increment
// i here
}
else // if (len == 0)
{
lps[i] = len;
i++;
}
}
}
}
public static ArrayList<ArrayList<Integer>> graph_form(int N)
{
ArrayList<ArrayList<Integer>> arr=new ArrayList<>();
for(int i=0;i<N;i++)
arr.add(new ArrayList<>());
return arr;
}
public static boolean pattern_checker_help(int[] A,int mid)
{
for(int i=0;i<A.length;i++)
{
if(A[i]!=A[i%mid])
return false;
}
return true;
}
public static boolean pattern_checker(int[] A)
{
ArrayList<Integer> arr=new ArrayList<>();
for(int i=2;i*i<=A.length;i++)
{
if(A.length%i==0)
{
arr.add(i);
arr.add(A.length/i);
}
}
boolean status=false;
for(int i:arr)
{
status =status || pattern_checker_help(A,i);
}
return status;
}
// public static boolean copy()
static int tim;
public static void euler_tour(ArrayList<ArrayList<Integer>> arr,int x,int parent,HashMap<Integer,Integer> hp,int[] dist)
{
// System.out.println(x+" hello");
hp.put(dist[x],hp.getOrDefault(dist[x],0)+1);
for(int i:arr.get(x))
{
if(i!=parent)
{
dist[i]=dist[x]+1;
euler_tour(arr,i,x,hp,dist);
}
}
}
public static double ch(ArrayList<div> arr, double mid)
{
double max=0;
for(int i=0;i<arr.size();i++)
{
max=Math.max(max,arr.get(i).time+Math.abs(arr.get(i).x-mid));
}
// System.out.println(mid+" "+max);
return max;
}
public static double check(ArrayList<div> arr)
{
double l=0,r=Math.pow(10,8);
int c=0;
while(c<=100)
{
double mid=(l+r)/2;
if(ch(arr,mid)<ch(arr,mid+0.000001))
r=mid;
else
l=mid;
++c;
}
return l;
}
public static void main(String[] args) throws IOException{
Reader.init(System.in);
// BufferedWriter output = new BufferedWriter(new FileWriter("C:/Users/asus/Downloads/answer.txt"));
BufferedWriter output = new BufferedWriter(new OutputStreamWriter(System.out));
int T=Reader.nextInt();
for(int m=1;m<=T;m++)
{
// output.write("Case #"+m+": ");
int N=Reader.nextInt();
long mod=998244353;
long[] fac=new long[N+1];
fac[0]=1;
for(int i=1;i<=N;i++)
fac[i]=(fac[i-1]%mod*(i%mod))%mod;
// output.write(fac[4]+"\n");
long ans_n=nCrModPFermat(N-1,N/2-1,mod,fac);
// output.write(ans_n+"\n");
int places=N/2-2;
int options=N-3;
while(places>0)
{
ans_n=((ans_n)%mod+(2%mod*(long)(nCrModPFermat(options-1,places-1,mod,fac))%mod)%mod)%mod;
if(places>=2)
ans_n=(ans_n%mod-nCrModPFermat(options-2,places-2,mod,fac)%mod+mod)%mod;
places-=2;
options-=4;
}
long bob=(nCrModPFermat(N,N/2,mod,fac)%mod-ans_n%mod+mod)%mod;
bob=(bob%mod-1+mod)%mod;
output.write(ans_n+" "+bob+" "+1+"\n");
}
// output.close();
output.flush();
}
}
class div
{
int x;
int time;
// long l;
div(int x,int time)
{
this.x=x;
this.time=time;
// this.l=l;
}
}
class Reader {
static BufferedReader reader;
static StringTokenizer tokenizer;
static void init(InputStream input) {
reader = new BufferedReader(
new InputStreamReader(input));
tokenizer = new StringTokenizer("");
}
static String next() throws IOException {
while (!tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(
reader.readLine());
}
return tokenizer.nextToken();
}
static int nextInt() throws IOException {
return Integer.parseInt(next());
}
static long nextLong() throws IOException {
return Long.parseLong(next());
}
static double nextDouble() throws IOException {
return Double.parseDouble(next());
}
}
class TreeNode
{
int data;
TreeNode left;
TreeNode right;
TreeNode(int data)
{
left=null;
right=null;
this.data=data;
}
}
//class div {
//
// long limit;
// long y;
// int id;
//
//
// div(String x,int y,int id) {
// this.x=x;
// this.y=y;
// this.id=id;
//
//// this.coins=coins;
//
// }
//}
class trie_node
{
trie_node[] arr;
trie_node()
{
arr=new trie_node[26];
}
public static void insert(trie_node root,String s)
{
trie_node tmp=root;
for(int i=0;i<s.length();i++)
{
if(tmp.arr[s.charAt(i)-97]!=null)
{
tmp=tmp.arr[s.charAt(i)-97];
}
else
{
tmp.arr[s.charAt(i)-97]=new trie_node();
tmp=tmp.arr[s.charAt(i)-97];
}
}
}
public static boolean search(trie_node root,String s)
{
trie_node tmp=root;
for(int i=0;i<s.length();i++)
{
if(tmp.arr[s.charAt(i)-97]!=null)
{
tmp=tmp.arr[s.charAt(i)-97];
}
else
{
return false;
}
}
return true;
}
}
|
Java
|
["5\n\n2\n\n4\n\n6\n\n8\n\n60"]
|
2 seconds
|
["1 0 1\n3 2 1\n12 7 1\n42 27 1\n341102826 248150916 1"]
|
NoteIn the first test case, Alex wins if he receives the card $$$2$$$ (he plays it, and Boris cannot respond). If Alex receives the card $$$1$$$, the game ends in a draw.In the second test case: Alex wins if he receives the cards $$$[3, 4]$$$, $$$[2, 4]$$$ or $$$[1, 4]$$$; Boris wins if Alex receives the cards $$$[1, 2]$$$ or $$$[1, 3]$$$; the game ends in a draw if Alex receives the cards $$$[2, 3]$$$.
|
Java 11
|
standard input
|
[
"combinatorics",
"constructive algorithms",
"dp",
"games"
] |
63fc2fb08d248f8ccdb3f5364c96dac1
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 30$$$) — the number of test cases. Then, $$$t$$$ lines follow. The $$$i$$$-th line contains one even integer $$$n$$$ ($$$2 \le n \le 60$$$).
| 1,500
|
For each test case, print three integers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. Since the answers can be large, print them modulo $$$998244353$$$.
|
standard output
| |
PASSED
|
b9047dd8054f645ef242d465fafc6910
|
train_109.jsonl
|
1664462100
|
Consider a game with $$$n$$$ cards ($$$n$$$ is even). Each card has a number written on it, between $$$1$$$ and $$$n$$$. All numbers on the cards are different. We say that a card with number $$$x$$$ is stronger than a card with number $$$y$$$ if $$$x > y$$$.Two players, Alex and Boris, play this game. In the beginning, each of them receives exactly $$$\frac{n}{2}$$$ cards, so each card belongs to exactly one player. Then, they take turns. Alex goes first, then Boris, then Alex again, and so on.On a player's turn, he must play exactly one of his cards. Then, if the opponent doesn't have any cards stronger than the card played, the opponent loses, and the game ends. Otherwise, the opponent has to play a stronger card (exactly one card as well). These two cards are removed from the game, and the turn ends. If there are no cards left, the game ends in a draw; otherwise it's the opponent's turn.Consider all possible ways to distribute the cards between two players, so that each of them receives exactly half of the cards. You have to calculate three numbers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. You may assume that both players play optimally (i. e. if a player can win no matter how his opponent plays, he wins). Two ways to distribute the cards are different if there is at least one card such that, in one of these ways, it is given to Alex, and in the other way, it is given to Boris.For example, suppose $$$n = 4$$$, Alex receives the cards $$$[2, 3]$$$, and Boris receives the cards $$$[1, 4]$$$. Then the game may go as follows: if Alex plays the card $$$2$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$3$$$. So, the game ends in a draw; if Alex plays the card $$$3$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$2$$$. So, the game ends in a draw. So, in this case, the game ends in a draw.
|
512 megabytes
|
import java.io.*;
import java.math.BigInteger;
import java.util.*;
/**
*
* @author eslam
*/
public class Solution {
// Beginning of the solution
static Kattio input = new Kattio();
static BufferedWriter log = new BufferedWriter(new OutputStreamWriter(System.out));
static ArrayList<ArrayList<Integer>> powerSet = new ArrayList<>();
static ArrayList<LinkedList<Integer>> allprem = new ArrayList<>();
static ArrayList<LinkedList<String>> allprems = new ArrayList<>();
static ArrayList<Long> luc = new ArrayList<>();
static long mod = (long) (Math.pow(10, 9) + 7);
static int grid[][] = {{0, 0, 1, -1, 1, 1, -1, -1}, {1, -1, 0, 0, 1, -1, 1, -1}};
static int dp[][][];
static double cmp = 0.000000001;
static long b[];
public static void main(String[] args) throws IOException {
//Kattio input = new Kattio("input");
// BufferedWriter log = new BufferedWriter(new FileWriter("output.txt"));
int test = input.nextInt();
loop:
for (int o = 1; o <= test; o++) {
long n = input.nextInt();
long c = 1;
long re = n / 2 - 1;
long b = n - 1;
BigInteger ans = BigInteger.ZERO;
boolean ca = true;
while (b > 0) {
if (ca) {
BigInteger f1 = f(b);
BigInteger f2 = f(re);
BigInteger f3 = f(b - re);
f3 = f2.multiply(f3);
ans = ans.add(f1.divide(f3));
b--;
} else {
if (re >= 2) {
re -= 2;
b -= 2;
BigInteger f1 = f(b);
BigInteger f2 = f(re);
BigInteger f3 = f(b - re);
f3 = f2.multiply(f3);
ans = ans.add(f1.divide(f3));
b--;
} else {
break;
}
}
ca = !ca;
}
BigInteger f1 = f(n);
BigInteger f2 = f(n / 2);
BigInteger f3 = f(n / 2);
f3 = f2.multiply(f3);
BigInteger a = f1.divide(f3);
a = a.subtract(ans);
a = a.subtract(BigInteger.ONE);
log.write(ans.remainder(new BigInteger("998244353")) + " " + a.remainder(new BigInteger("998244353")) + " " + 1 + "\n");
}
log.flush();
}
static boolean can(long n, long k, long a[]) {
if (k < 0) {
return false;
}
if (k % 3 == 0) {
Arrays.sort(a);
long an = (a[2] - a[1]) + (a[2] - a[0]);
if (an > n) {
return false;
}
n -= an;
if (n % 3 != 0) {
return false;
}
} else {
return false;
}
return true;
}
static void prefixSum2D(long arr[][]) {
for (int i = 0; i < arr.length; i++) {
prefixSum(arr[i]);
}
for (int i = 0; i < arr[0].length; i++) {
for (int j = 1; j < arr.length; j++) {
arr[j][i] += arr[j - 1][i];
}
}
}
public static long baseToDecimal(String w, long base) {
long r = 0;
long l = 0;
for (int i = w.length() - 1; i > -1; i--) {
long x = (w.charAt(i) - '0') * (long) Math.pow(base, l);
r = r + x;
l++;
}
return r;
}
static int bs(int v, ArrayList<Integer> a) {
int max = a.size() - 1;
int min = 0;
int ans = 0;
while (max >= min) {
int mid = (max + min) / 2;
if (a.get(mid) >= v) {
ans = a.size() - mid;
max = mid - 1;
} else {
min = mid + 1;
}
}
return ans;
}
static Comparator<tri> cmpTri() {
Comparator<tri> c = new Comparator<tri>() {
@Override
public int compare(tri o1, tri o2) {
if (o1.x > o2.x) {
return 1;
} else if (o1.x < o2.x) {
return -1;
} else {
if (o1.y > o2.y) {
return 1;
} else if (o1.y < o2.y) {
return -1;
} else {
if (o1.z > o2.z) {
return 1;
} else if (o1.z < o2.z) {
return -1;
} else {
return 0;
}
}
}
}
};
return c;
}
static Comparator<pair> cmpPair2() {
Comparator<pair> c = new Comparator<pair>() {
@Override
public int compare(pair o1, pair o2) {
if (o1.y > o2.y) {
return 1;
} else if (o1.y < o2.y) {
return -1;
} else {
if (o1.x > o2.x) {
return 1;
} else if (o1.x < o2.x) {
return -1;
} else {
return 0;
}
}
}
};
return c;
}
static Comparator<pair> cmpPair() {
Comparator<pair> c = new Comparator<pair>() {
@Override
public int compare(pair o1, pair o2) {
if (o1.x > o2.x) {
return 1;
} else if (o1.x < o2.x) {
return -1;
} else {
if (o1.y > o2.y) {
return 1;
} else if (o1.y < o2.y) {
return -1;
} else {
return 0;
}
}
}
};
return c;
}
static class rec {
long x1;
long x2;
long y1;
long y2;
public rec(long x1, long y1, long x2, long y2) {
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
}
public long getArea() {
return (x2 - x1) * (y2 - y1);
}
}
static long sumOfRange(int x1, int y1, int x2, int y2, long a[][]) {
return (a[x2][y2] - a[x1 - 1][y2] - a[x2][y1 - 1]) + a[x1 - 1][y1 - 1];
}
public static int[][] bfs(int i, int j, String w[]) {
Queue<pair> q = new ArrayDeque<>();
q.add(new pair(i, j));
int dis[][] = new int[w.length][w[0].length()];
for (int k = 0; k < w.length; k++) {
Arrays.fill(dis[k], -1);
}
dis[i][j] = 0;
while (!q.isEmpty()) {
pair p = q.poll();
int cost = dis[p.x][p.y];
for (int k = 0; k < 4; k++) {
int nx = p.x + grid[0][k];
int ny = p.y + grid[1][k];
if (isValid(nx, ny, w.length, w[0].length())) {
if (dis[nx][ny] == -1 && w[nx].charAt(ny) == '.') {
q.add(new pair(nx, ny));
dis[nx][ny] = cost + 1;
}
}
}
}
return dis;
}
public static void dfs(int node, ArrayList<Integer> a[], boolean vi[]) {
vi[node] = true;
for (Integer ch : a[node]) {
if (!vi[ch]) {
dfs(ch, a, vi);
}
}
}
public static void graphRepresintion(ArrayList<Integer>[] a, int q) throws IOException {
for (int i = 0; i < a.length; i++) {
a[i] = new ArrayList<>();
}
while (q-- > 0) {
int x = input.nextInt();
int y = input.nextInt();
a[x].add(y);
a[y].add(x);
}
}
public static boolean isValid(int i, int j, int n, int m) {
return (i > -1 && i < n) && (j > -1 && j < m);
}
// present in the left and right indices
public static int[] swap(int data[], int left, int right) {
// Swap the data
int temp = data[left];
data[left] = data[right];
data[right] = temp;
// Return the updated array
return data;
}
// Function to reverse the sub-array
// starting from left to the right
// both inclusive
public static int[] reverse(int data[], int left, int right) {
// Reverse the sub-array
while (left < right) {
int temp = data[left];
data[left++] = data[right];
data[right--] = temp;
}
// Return the updated array
return data;
}
// Function to find the next permutation
// of the given integer array
public static boolean findNextPermutation(int data[]) {
// If the given dataset is empty
// or contains only one element
// next_permutation is not possible
if (data.length <= 1) {
return false;
}
int last = data.length - 2;
// find the longest non-increasing suffix
// and find the pivot
while (last >= 0) {
if (data[last] < data[last + 1]) {
break;
}
last--;
}
// If there is no increasing pair
// there is no higher order permutation
if (last < 0) {
return false;
}
int nextGreater = data.length - 1;
// Find the rightmost successor to the pivot
for (int i = data.length - 1; i > last; i--) {
if (data[i] > data[last]) {
nextGreater = i;
break;
}
}
// Swap the successor and the pivot
data = swap(data, nextGreater, last);
// Reverse the suffix
data = reverse(data, last + 1, data.length - 1);
// Return true as the next_permutation is done
return true;
}
public static pair[] dijkstra(int node, ArrayList<pair> a[]) {
PriorityQueue<tri> q = new PriorityQueue<>(new Comparator<tri>() {
@Override
public int compare(tri o1, tri o2) {
if (o1.y > o2.y) {
return 1;
} else if (o1.y < o2.y) {
return -1;
} else {
return 0;
}
}
});
q.add(new tri(node, 0, -1));
pair distance[] = new pair[a.length];
while (!q.isEmpty()) {
tri p = q.poll();
int cost = p.y;
if (distance[p.x] != null) {
continue;
}
distance[p.x] = new pair(p.z, cost);
ArrayList<pair> nodes = a[p.x];
for (pair node1 : nodes) {
if (distance[node1.x] == null) {
tri pa = new tri(node1.x, cost + node1.y, p.x);
q.add(pa);
}
}
}
return distance;
}
public static String revs(String w) {
String ans = "";
for (int i = w.length() - 1; i > -1; i--) {
ans += w.charAt(i);
}
return ans;
}
public static boolean isPalindrome(String w) {
for (int i = 0; i < w.length() / 2; i++) {
if (w.charAt(i) != w.charAt(w.length() - i - 1)) {
return false;
}
}
return true;
}
public static void getPowerSet(Queue<Integer> a) {
int n = a.poll();
if (!a.isEmpty()) {
getPowerSet(a);
}
int s = powerSet.size();
for (int i = 0; i < s; i++) {
ArrayList<Integer> ne = new ArrayList<>();
ne.add(n);
for (int j = 0; j < powerSet.get(i).size(); j++) {
ne.add(powerSet.get(i).get(j));
}
powerSet.add(ne);
}
ArrayList<Integer> p = new ArrayList<>();
p.add(n);
powerSet.add(p);
}
public static int getlo(int va) {
int v = 1;
while (v <= va) {
if ((va&v) != 0) {
return v;
}
v <<= 1;
}
return 0;
}
static long fast_pow(long a, long p, long mod) {
long res = 1;
while (p > 0) {
if (p % 2 == 0) {
a = (a * a) % mod;
p /= 2;
} else {
res = (res * a) % mod;
p--;
}
}
return res;
}
public static int countPrimeInRange(int n, boolean isPrime[]) {
int cnt = 0;
Arrays.fill(isPrime, true);
for (int i = 2; i * i <= n; i++) {
if (isPrime[i]) {
for (int j = i * 2; j <= n; j += i) {
isPrime[j] = false;
}
}
}
for (int i = 2; i <= n; i++) {
if (isPrime[i]) {
cnt++;
}
}
return cnt;
}
public static void create(long num) {
luc.add(num);
if (num > power(10, 9)) {
return;
}
create(num * 10 + 4);
create(num * 10 + 7);
}
public static long ceil(long a, long b) {
return (a + b - 1) / b;
}
public static long round(long a, long b) {
if (a < 0) {
return (a - b / 2) / b;
}
return (a + b / 2) / b;
}
public static void allPremutationsst(LinkedList<String> l, boolean visited[], ArrayList<String> st) {
if (l.size() == st.size()) {
allprems.add(l);
}
for (int i = 0; i < st.size(); i++) {
if (!visited[i]) {
visited[i] = true;
LinkedList<String> nl = new LinkedList<>();
for (String x : l) {
nl.add(x);
}
nl.add(st.get(i));
allPremutationsst(nl, visited, st);
visited[i] = false;
}
}
}
public static void allPremutations(LinkedList<Integer> l, boolean visited[], int a[]) {
if (l.size() == a.length) {
allprem.add(l);
}
for (int i = 0; i < a.length; i++) {
if (!visited[i]) {
visited[i] = true;
LinkedList<Integer> nl = new LinkedList<>();
for (Integer x : l) {
nl.add(x);
}
nl.add(a[i]);
allPremutations(nl, visited, a);
visited[i] = false;
}
}
}
public static int binarySearch(long[] a, long value) {
int l = 0;
int r = a.length - 1;
while (l <= r) {
int m = (l + r) / 2;
if (a[m] == value) {
return m;
} else if (a[m] > value) {
r = m - 1;
} else {
l = m + 1;
}
}
return -1;
}
public static void reverse(int l, int r, char ch[]) {
for (int i = 0; i < r / 2; i++) {
char c = ch[i];
ch[i] = ch[r - i - 1];
ch[r - i - 1] = c;
}
}
public static int logK(long v, long k) {
int ans = 0;
while (v > 1) {
ans++;
v /= k;
}
return ans;
}
public static long power(long a, long n) {
if (n == 1) {
return a;
}
long pow = power(a, n / 2);
pow *= pow;
if (n % 2 != 0) {
pow *= a;
}
return pow;
}
public static long get(long max, long x) {
if (x == 1) {
return max;
}
int cnt = 0;
while (max > 0) {
cnt++;
max /= x;
}
return cnt;
}
public static int numOF0(long v) {
long x = 1;
int cnt = 0;
while (x <= v) {
if ((x & v) == 0) {
cnt++;
}
x <<= 1;
}
return cnt;
}
public static int log2(double n) {
int cnt = 0;
while (n > 1) {
n /= 2;
cnt++;
}
return cnt;
}
public static int[] bfs(int node, ArrayList<Integer> a[]) {
Queue<Integer> q = new LinkedList<>();
q.add(node);
int distances[] = new int[a.length];
Arrays.fill(distances, -1);
distances[node] = 0;
while (!q.isEmpty()) {
int parent = q.poll();
ArrayList<Integer> nodes = a[parent];
int cost = distances[parent];
for (Integer node1 : nodes) {
if (distances[node1] == -1) {
q.add(node1);
distances[node1] = cost + 1;
}
}
}
return distances;
}
public static void primeFactors(int n, HashMap<Integer, ArrayList<Integer>> h, int ind) {
boolean ca = true;
while (n % 2 == 0) {
if (ca) {
if (h.get(2) == null) {
h.put(2, new ArrayList<>());
}
h.get(2).add(ind);
ca = false;
}
n /= 2;
}
for (int i = 3; i <= Math.sqrt(n); i += 2) {
ca = true;
while (n % i == 0) {
if (ca) {
if (h.get(i) == null) {
h.put(i, new ArrayList<>());
}
h.get(i).add(ind);
ca = false;
}
n /= i;
}
if (n < i) {
break;
}
}
if (n > 2) {
if (h.get(n) == null) {
h.put(n, new ArrayList<>());
}
h.get(n).add(ind);
}
}
// end of solution
public static BigInteger f(long n) {
if (n <= 1) {
return BigInteger.ONE;
}
long t = n - 1;
BigInteger b = new BigInteger(t + "");
BigInteger ans = new BigInteger(n + "");
while (t > 1) {
ans = ans.multiply(b);
b = b.subtract(BigInteger.ONE);
t--;
}
return ans;
}
public static long factorial(long n) {
if (n <= 1) {
return 1;
}
long t = n - 1;
while (t > 1) {
n = mod((mod(n, mod) * mod(t, mod)), mod);
t--;
}
return n;
}
public static long rev(long n) {
long t = n;
long ans = 0;
while (t > 0) {
ans = ans * 10 + t % 10;
t /= 10;
}
return ans;
}
public static boolean isPalindrome(int n) {
int t = n;
int ans = 0;
while (t > 0) {
ans = ans * 10 + t % 10;
t /= 10;
}
return ans == n;
}
static class tri {
int x, y, z;
public tri(int x, int y, int z) {
this.x = x;
this.y = y;
this.z = z;
}
@Override
public String toString() {
return x + " " + y + " " + z;
}
}
static boolean isPrime(long num) {
if (num == 1) {
return false;
}
if (num == 2) {
return true;
}
if (num % 2 == 0) {
return false;
}
if (num == 3) {
return true;
}
for (int i = 3; i <= Math.sqrt(num) + 1; i += 2) {
if (num % i == 0) {
return false;
}
}
return true;
}
public static void prefixSum(long[] a) {
for (int i = 1; i < a.length; i++) {
a[i] = a[i] + a[i - 1];
}
}
public static void suffixSum(long[] a) {
for (int i = a.length - 2; i > -1; i--) {
a[i] = a[i] + a[i + 1];
}
}
static long mod(long a, long b) {
long r = a % b;
return r < 0 ? r + b : r;
}
public static long binaryToDecimal(String w) {
long r = 0;
long l = 0;
for (int i = w.length() - 1; i > -1; i--) {
long x = (w.charAt(i) - '0') * (long) Math.pow(2, l);
r = r + x;
l++;
}
return r;
}
public static String decimalAnyBase(long n, long base) {
String w = "";
while (n > 0) {
w = n % base + w;
n /= base;
}
return w;
}
public static String decimalToBinary(long n) {
String w = "";
while (n > 0) {
w = n % 2 + w;
n /= 2;
}
return w;
}
public static boolean isSorted(int[] a) {
for (int i = 0; i < a.length - 1; i++) {
if (a[i] >= a[i + 1]) {
return false;
}
}
return true;
}
public static void print(long[] a) throws IOException {
for (int i = 0; i < a.length; i++) {
log.write(a[i] + " ");
}
log.write("\n");
}
public static void read(int[] a) {
for (int i = 0; i < a.length; i++) {
a[i] = input.nextInt();
}
}
static class gepair {
long x;
long y;
public gepair(long x, long y) {
this.x = x;
this.y = y;
}
@Override
public String toString() {
return x + " " + y;
}
}
static class pair {
int x;
int y;
public pair(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public String toString() {
return x + " " + y;
}
}
static class pai {
long x;
int y;
public pai(long x, int y) {
this.x = x;
this.y = y;
}
@Override
public String toString() {
return x + " " + y;
}
}
public static long LCM(long x, long y) {
return x / GCD(x, y) * y;
}
public static long GCD(long x, long y) {
if (y == 0) {
return x;
}
return GCD(y, x % y);
}
public static void simplifyTheFraction(int a, int b) {
long GCD = GCD(a, b);
System.out.println(a / GCD + " " + b / GCD);
}
static class Kattio extends PrintWriter {
private BufferedReader r;
private StringTokenizer st;
// standard input
public Kattio() {
this(System.in, System.out);
}
public Kattio(InputStream i, OutputStream o) {
super(o);
r = new BufferedReader(new InputStreamReader(i));
}
// USACO-style file input
public Kattio(String problemName) throws IOException {
super(problemName + ".out");
r = new BufferedReader(new FileReader(problemName + ".in"));
}
// returns null if no more input
String nextLine() {
String str = "";
try {
str = r.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
public String next() {
try {
while (st == null || !st.hasMoreTokens()) {
st = new StringTokenizer(r.readLine());
}
return st.nextToken();
} catch (Exception e) {
}
return null;
}
public int nextInt() {
return Integer.parseInt(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
public long nextLong() {
return Long.parseLong(next());
}
}
}
|
Java
|
["5\n\n2\n\n4\n\n6\n\n8\n\n60"]
|
2 seconds
|
["1 0 1\n3 2 1\n12 7 1\n42 27 1\n341102826 248150916 1"]
|
NoteIn the first test case, Alex wins if he receives the card $$$2$$$ (he plays it, and Boris cannot respond). If Alex receives the card $$$1$$$, the game ends in a draw.In the second test case: Alex wins if he receives the cards $$$[3, 4]$$$, $$$[2, 4]$$$ or $$$[1, 4]$$$; Boris wins if Alex receives the cards $$$[1, 2]$$$ or $$$[1, 3]$$$; the game ends in a draw if Alex receives the cards $$$[2, 3]$$$.
|
Java 11
|
standard input
|
[
"combinatorics",
"constructive algorithms",
"dp",
"games"
] |
63fc2fb08d248f8ccdb3f5364c96dac1
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 30$$$) — the number of test cases. Then, $$$t$$$ lines follow. The $$$i$$$-th line contains one even integer $$$n$$$ ($$$2 \le n \le 60$$$).
| 1,500
|
For each test case, print three integers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. Since the answers can be large, print them modulo $$$998244353$$$.
|
standard output
| |
PASSED
|
c414be0a7aa1148434603de596fc0610
|
train_109.jsonl
|
1664462100
|
Consider a game with $$$n$$$ cards ($$$n$$$ is even). Each card has a number written on it, between $$$1$$$ and $$$n$$$. All numbers on the cards are different. We say that a card with number $$$x$$$ is stronger than a card with number $$$y$$$ if $$$x > y$$$.Two players, Alex and Boris, play this game. In the beginning, each of them receives exactly $$$\frac{n}{2}$$$ cards, so each card belongs to exactly one player. Then, they take turns. Alex goes first, then Boris, then Alex again, and so on.On a player's turn, he must play exactly one of his cards. Then, if the opponent doesn't have any cards stronger than the card played, the opponent loses, and the game ends. Otherwise, the opponent has to play a stronger card (exactly one card as well). These two cards are removed from the game, and the turn ends. If there are no cards left, the game ends in a draw; otherwise it's the opponent's turn.Consider all possible ways to distribute the cards between two players, so that each of them receives exactly half of the cards. You have to calculate three numbers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. You may assume that both players play optimally (i. e. if a player can win no matter how his opponent plays, he wins). Two ways to distribute the cards are different if there is at least one card such that, in one of these ways, it is given to Alex, and in the other way, it is given to Boris.For example, suppose $$$n = 4$$$, Alex receives the cards $$$[2, 3]$$$, and Boris receives the cards $$$[1, 4]$$$. Then the game may go as follows: if Alex plays the card $$$2$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$3$$$. So, the game ends in a draw; if Alex plays the card $$$3$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$2$$$. So, the game ends in a draw. So, in this case, the game ends in a draw.
|
512 megabytes
|
import java.math.*;
public class C {
public Object solve () {
int N = sc.nextInt();
long H = modInv(2);
long [] F = new long [N+1], G = new long [N+1];
F[2] = 1;
for (int n = 4; n <= N; ++n) {
long t = BIN(n, n/2);
F[n] = mod(H * t + G[n-2]);
G[n] = mod(t - F[n] - 1);
}
long [] res = { F[N], G[N], 1 };
return res;
}
private static final int CONTEST_TYPE = 2;
static long [] FACT;
private static void init () {
FACT = modFact(100);
}
long BIN (int N, int K) {
long A = FACT[N], B = FACT[K], C = FACT[N-K];
long BC = modInv(B*C);
long res = mod(A * BC);
return res;
}
private static final int MOD = 998244353;
private static long mod (long x) { return mod(x, MOD); }
private static long mod (long x, long mod) { return ((x % mod) + mod) % mod; }
private static long [] modFact (int N) {
long [] res = new long [N+1]; res[0] = 1;
for (int i = 1; i <= N; ++i)
res[i] = mod(res[i-1] * i);
return res;
}
private static long modInv (long x) { return modInv(x, MOD); }
private static long modInv (long x, int mod) { return BigInteger.valueOf(x).modInverse(BigInteger.valueOf(mod)).longValue(); }
//////////////////////////////////////////////////////////////////////////////////// OFF
private static IOUtils.MyScanner sc = new IOUtils.MyScanner();
private static class IOUtils {
public static class MyScanner {
public String next () { newLine(); return line[index++]; }
public int nextInt () { return Integer.parseInt(next()); }
//////////////////////////////////////////////
private boolean eol () { return index == line.length; }
private String readLine () {
try {
int b;
StringBuilder res = new StringBuilder();
while ((b = System.in.read()) < ' ')
continue;
res.append((char)b);
while ((b = System.in.read()) >= ' ')
res.append((char)b);
return res.toString();
} catch (Exception e) {
throw new Error (e);
}
}
private MyScanner () {
try {
while (System.in.available() == 0)
Thread.sleep(1);
start();
} catch (Exception e) {
throw new Error(e);
}
}
private String [] line;
private int index;
private void newLine () {
if (line == null || eol()) {
line = split(readLine());
index = 0;
}
}
private String [] split (String s) { return s.length() > 0 ? s.split(" ") : new String [0]; }
}
private static String build (Object o, Object ... A) { return buildDelim(" ", o, A); }
private static String buildDelim (String delim, Object o, Object ... A) {
java.util.List<String> str = new java.util.ArrayList<>();
append(str, o);
for (Object p : A)
append(str, p);
return String.join(delim, str.toArray(new String [0]));
}
//////////////////////////////////////////////////////////////////////////////////
private static java.text.DecimalFormat formatter = new java.text.DecimalFormat("#.#########");
private static void start () { if (t == 0) t = millis(); }
private static void append (java.util.function.Consumer<Object> f, java.util.function.Consumer<Object> g, Object o) {
if (o.getClass().isArray()) {
int len = java.lang.reflect.Array.getLength(o);
for (int i = 0; i < len; ++i)
f.accept(java.lang.reflect.Array.get(o, i));
}
else if (o instanceof Iterable<?>)
((Iterable<?>)o).forEach(f::accept);
else
g.accept(o instanceof Double ? formatter.format(o) : o);
}
private static void append (java.util.List<String> str, Object o) {
append(x -> append(str, x), x -> str.add(x.toString()), o);
}
private static java.io.PrintWriter pw = new java.io.PrintWriter(System.out);
private static void flush () { pw.flush(); System.out.flush(); }
private static void print (Object o, Object ... A) {
String res = build(o, A);
if (DEBUG == 0) {
String out = res.split(System.lineSeparator())[0];
if (out.length() > 80)
out = out.substring(0, 80);
if (out.length() < res.length())
out += " ...";
err(out, '(', time(), ')');
}
if (DEBUG == 2)
err(res, '(', time(), ')');
if (res.length() > 0)
pw.println(res);
if (DEBUG == 1)
flush();
}
private static void err (Object o, Object ... A) { System.err.println(build(o, A)); }
private static int DEBUG;
private static void exit () {
String end = "------------------" + System.lineSeparator() + time();
switch(DEBUG) {
case 1: print(end); break;
case 0:
case 2: err(end); break;
}
IOUtils.pw.close();
System.out.flush();
System.exit(0);
}
private static long t;
private static long millis () { return System.currentTimeMillis(); }
private static String time () { return "Time: " + (millis() - t) / 1000.0; }
private static void run (int N) {
try { DEBUG = Integer.parseInt(System.getProperties().get("DEBUG").toString()); }
catch (Throwable t) {}
for (int n = 1; n <= N; ++n) {
Object res = new C().solve();
if (res != null) {
@SuppressWarnings("all")
Object o = CONTEST_TYPE == 0 ? "Case #" + n + ": " + build(res) : res;
print(o);
}
}
exit();
}
}
////////////////////////////////////////////////////////////////////////////////////
public static void main (String[] args) {
@SuppressWarnings("all")
int N = CONTEST_TYPE == 1 ? 1 : sc.nextInt();
init();
IOUtils.run(N);
}
}
|
Java
|
["5\n\n2\n\n4\n\n6\n\n8\n\n60"]
|
2 seconds
|
["1 0 1\n3 2 1\n12 7 1\n42 27 1\n341102826 248150916 1"]
|
NoteIn the first test case, Alex wins if he receives the card $$$2$$$ (he plays it, and Boris cannot respond). If Alex receives the card $$$1$$$, the game ends in a draw.In the second test case: Alex wins if he receives the cards $$$[3, 4]$$$, $$$[2, 4]$$$ or $$$[1, 4]$$$; Boris wins if Alex receives the cards $$$[1, 2]$$$ or $$$[1, 3]$$$; the game ends in a draw if Alex receives the cards $$$[2, 3]$$$.
|
Java 11
|
standard input
|
[
"combinatorics",
"constructive algorithms",
"dp",
"games"
] |
63fc2fb08d248f8ccdb3f5364c96dac1
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 30$$$) — the number of test cases. Then, $$$t$$$ lines follow. The $$$i$$$-th line contains one even integer $$$n$$$ ($$$2 \le n \le 60$$$).
| 1,500
|
For each test case, print three integers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. Since the answers can be large, print them modulo $$$998244353$$$.
|
standard output
| |
PASSED
|
f0d2ba1e9248c716025d193fd2aeae4e
|
train_109.jsonl
|
1664462100
|
Consider a game with $$$n$$$ cards ($$$n$$$ is even). Each card has a number written on it, between $$$1$$$ and $$$n$$$. All numbers on the cards are different. We say that a card with number $$$x$$$ is stronger than a card with number $$$y$$$ if $$$x > y$$$.Two players, Alex and Boris, play this game. In the beginning, each of them receives exactly $$$\frac{n}{2}$$$ cards, so each card belongs to exactly one player. Then, they take turns. Alex goes first, then Boris, then Alex again, and so on.On a player's turn, he must play exactly one of his cards. Then, if the opponent doesn't have any cards stronger than the card played, the opponent loses, and the game ends. Otherwise, the opponent has to play a stronger card (exactly one card as well). These two cards are removed from the game, and the turn ends. If there are no cards left, the game ends in a draw; otherwise it's the opponent's turn.Consider all possible ways to distribute the cards between two players, so that each of them receives exactly half of the cards. You have to calculate three numbers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. You may assume that both players play optimally (i. e. if a player can win no matter how his opponent plays, he wins). Two ways to distribute the cards are different if there is at least one card such that, in one of these ways, it is given to Alex, and in the other way, it is given to Boris.For example, suppose $$$n = 4$$$, Alex receives the cards $$$[2, 3]$$$, and Boris receives the cards $$$[1, 4]$$$. Then the game may go as follows: if Alex plays the card $$$2$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$3$$$. So, the game ends in a draw; if Alex plays the card $$$3$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$2$$$. So, the game ends in a draw. So, in this case, the game ends in a draw.
|
512 megabytes
|
// C. Card Game
// Educational Codeforces Round 136 (Rated for Div. 2)
// Codeforces
// Solution by Anmol Sharma
import java.util.*;
import java.lang.*;
import java.io.*;
public class Main {
public static long mod = 998244353;
public static void main(String[] args) throws java.lang.Exception {
Reader sc = new Reader();
FastNum in = new FastNum(System.in);
Writer out = new Writer(System.out);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
// long[] alex = new long[61];
// long[] bob = new long[61];
// alex[2] = 1;
// bob[2] = 0;
// for (int i = 4; i < 61; i += 2) {
// long ans = nCr(i - 1, i / 2);
// if(i % 4 == 0) {
// bob[i] = modSum(bob[i - 2], ans);
// alex[i] = modSum(alex[i - 2], modSub(nCr(i, i / 2), modSum(ans, 1)));
// } else {
//
// }
// }
int tests = in.nextInt();
for (int test = 1; test <= tests; test++) {
int n = in.nextInt();
long t = n;
long alex = 0, bob = 0;
boolean f = true;
while (n > 0) {
if (f) {
long ans = nCr(n - 1, n / 2);
alex = modSum(alex, ans);
f = false;
} else {
long ans = 0;
if(n - 2 >= 0) {
ans = nCr(n - 2, n / 2 - 2);
}
alex = modSum(alex, ans);
f = true;
}
n -= 2;
}
if (t % 4 == 0) alex -= 1;
long ans = nCr(t, t / 2);
ans = modSub(ans, 1);
bob = modSub(ans, alex);
out.println(alex + " " + bob + " " + 1);
}
out.flush();
}
static long modSum(long p, long q) {
if (q > 0) {
return (p + q) % mod;
} else {
return (p + q + mod) % mod;
}
}
static long modSub(long p, long q) {
if (q > 0) {
return (p - q + mod) % mod;
} else {
return (p - q) % mod;
}
}
static long invMod(long p, long q, long m) {
long expo = m - 2;
while (expo != 0) {
if ((expo & 1) == 1) {
p = (p * q) % m;
}
q = (q * q) % m;
expo >>= 1;
}
return p;
}
public static int gcd(int a, int b) {
if (a == 0)
return b;
return gcd(b % a, a);
}
public static long power(long a, long b) {
if (b == 0)
return 1;
long answer = power(a, b / 2) % mod;
answer = (answer * answer) % mod;
if (b % 2 != 0)
answer = (answer * a) % mod;
return answer;
}
public static void swap(int x, int y) {
int t = x;
x = y;
y = t;
}
public static long min(long a, long b) {
if (a < b) return a;
return b;
}
public static long divide(long a, long b) {
return (a % mod * (power(b, mod - 2) % mod)) % mod;
}
public static long nCr(long n, long r) {
long answer = 1;
long k = min(r, n - r);
for (long i = 0; i < k; i++) {
answer = (answer % mod * (n - i) % mod) % mod;
answer = divide(answer, i + 1);
}
return answer % mod;
}
public static boolean plaindrome(String str) {
StringBuilder sb = new StringBuilder();
sb.append(str);
return (str.equals((sb.reverse()).toString()));
}
public static class Pair {
int a;
int b;
Pair(int s, int e) {
a = s;
b = e;
}
static void sort(Pair[] a) {
Arrays.sort(a, (o1, o2) -> {
if (o1.a == o2.a) {
return o1.b - o2.b;
} else {
return o1.a - o2.a;
}
});
}
@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
Pair pair = (Pair) o;
return a == pair.a && b == pair.b;
}
@Override
public int hashCode() {
return Objects.hash(a, b);
}
}
static class Assert {
static void check(boolean e) {
if (!e) {
throw new AssertionError();
}
}
}
static class FastNum implements AutoCloseable {
InputStream is;
byte buffer[] = new byte[1 << 16];
int size = 0;
int pos = 0;
FastNum(InputStream is) {
this.is = is;
}
int nextChar() {
if (pos >= size) {
try {
size = is.read(buffer);
} catch (IOException e) {
throw new IOError(e);
}
pos = 0;
if (size == -1) {
return -1;
}
}
Assert.check(pos < size);
int c = buffer[pos] & 0xFF;
pos++;
return c;
}
int nextInt() {
int c = nextChar();
while (c == ' ' || c == '\r' || c == '\n' || c == '\t') {
c = nextChar();
}
if (c == '-') {
c = nextChar();
Assert.check('0' <= c && c <= '9');
int n = -(c - '0');
c = nextChar();
while ('0' <= c && c <= '9') {
int d = c - '0';
c = nextChar();
Assert.check(n > Integer.MIN_VALUE / 10
|| n == Integer.MIN_VALUE / 10 && d <= -(Integer.MIN_VALUE % 10));
n = n * 10 - d;
}
return n;
} else {
Assert.check('0' <= c && c <= '9');
int n = c - '0';
c = nextChar();
while ('0' <= c && c <= '9') {
int d = c - '0';
c = nextChar();
Assert.check(
n < Integer.MAX_VALUE / 10 || n == Integer.MAX_VALUE / 10 && d <= Integer.MAX_VALUE % 10);
n = n * 10 + d;
}
return n;
}
}
char nextCh() {
int c = nextChar();
while (c == ' ' || c == '\r' || c == '\n' || c == '\t') {
c = nextChar();
}
return (char) c;
}
long nextLong() {
int c = nextChar();
while (c == ' ' || c == '\r' || c == '\n' || c == '\t') {
c = nextChar();
}
if (c == '-') {
c = nextChar();
Assert.check('0' <= c && c <= '9');
long n = -(c - '0');
c = nextChar();
while ('0' <= c && c <= '9') {
int d = c - '0';
c = nextChar();
Assert.check(n > Long.MIN_VALUE / 10
|| n == Long.MIN_VALUE / 10 && d <= -(Long.MIN_VALUE % 10));
n = n * 10 - d;
}
return n;
} else {
Assert.check('0' <= c && c <= '9');
long n = c - '0';
c = nextChar();
while ('0' <= c && c <= '9') {
int d = c - '0';
c = nextChar();
Assert.check(
n < Long.MAX_VALUE / 10 || n == Long.MAX_VALUE / 10 && d <= Long.MAX_VALUE % 10);
n = n * 10 + d;
}
return n;
}
}
int[] nextIntArray(int n) {
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = nextInt();
}
return arr;
}
long[] nextLongArray(int n) {
long[] arr = new long[n];
for (int i = 0; i < n; i++) {
arr[i] = nextInt();
}
return arr;
}
char[] nextCharArray(int n) {
char[] arr = new char[n];
for (int i = 0; i < n; i++) {
arr[i] = nextCh();
}
return arr;
}
@Override
public void close() {
}
}
static class Writer extends PrintWriter {
public Writer(java.io.Writer out) {
super(out);
}
public Writer(java.io.Writer out, boolean autoFlush) {
super(out, autoFlush);
}
public Writer(OutputStream out) {
super(out);
}
public Writer(OutputStream out, boolean autoFlush) {
super(out, autoFlush);
}
public void printArray(int[] arr) {
for (int j : arr) {
print(j);
print(' ');
}
println();
}
public void printArray(long[] arr) {
for (long j : arr) {
print(j);
print(' ');
}
println();
}
}
static class Reader {
final private int BUFFER_SIZE = 1 << 16;
private final DataInputStream din;
private final byte[] buffer;
private int bufferPointer, bytesRead;
public Reader() {
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public Reader(String file_name) throws IOException {
din = new DataInputStream(new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String readLine() throws IOException {
byte[] buf = new byte[64]; // line length
int cnt = 0, c;
while ((c = read()) != -1) {
if (c == '\n')
break;
buf[cnt++] = (byte) c;
}
return new String(buf, 0, cnt);
}
public int nextInt() throws IOException {
int ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public long nextLong() throws IOException {
long ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public double nextDouble() throws IOException {
double ret = 0, div = 1;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (c == '.') {
while ((c = read()) >= '0' && c <= '9') {
ret += (c - '0') / (div *= 10);
}
}
if (neg)
return -ret;
return ret;
}
private void fillBuffer() throws IOException {
bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
if (bytesRead == -1)
buffer[0] = -1;
}
private byte read() throws IOException {
if (bufferPointer == bytesRead)
fillBuffer();
return buffer[bufferPointer++];
}
public void close() throws IOException {
if (din == null)
return;
din.close();
}
}
}
|
Java
|
["5\n\n2\n\n4\n\n6\n\n8\n\n60"]
|
2 seconds
|
["1 0 1\n3 2 1\n12 7 1\n42 27 1\n341102826 248150916 1"]
|
NoteIn the first test case, Alex wins if he receives the card $$$2$$$ (he plays it, and Boris cannot respond). If Alex receives the card $$$1$$$, the game ends in a draw.In the second test case: Alex wins if he receives the cards $$$[3, 4]$$$, $$$[2, 4]$$$ or $$$[1, 4]$$$; Boris wins if Alex receives the cards $$$[1, 2]$$$ or $$$[1, 3]$$$; the game ends in a draw if Alex receives the cards $$$[2, 3]$$$.
|
Java 11
|
standard input
|
[
"combinatorics",
"constructive algorithms",
"dp",
"games"
] |
63fc2fb08d248f8ccdb3f5364c96dac1
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 30$$$) — the number of test cases. Then, $$$t$$$ lines follow. The $$$i$$$-th line contains one even integer $$$n$$$ ($$$2 \le n \le 60$$$).
| 1,500
|
For each test case, print three integers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. Since the answers can be large, print them modulo $$$998244353$$$.
|
standard output
| |
PASSED
|
53aaa76532ace2d316dfeeb227a6b325
|
train_109.jsonl
|
1664462100
|
Consider a game with $$$n$$$ cards ($$$n$$$ is even). Each card has a number written on it, between $$$1$$$ and $$$n$$$. All numbers on the cards are different. We say that a card with number $$$x$$$ is stronger than a card with number $$$y$$$ if $$$x > y$$$.Two players, Alex and Boris, play this game. In the beginning, each of them receives exactly $$$\frac{n}{2}$$$ cards, so each card belongs to exactly one player. Then, they take turns. Alex goes first, then Boris, then Alex again, and so on.On a player's turn, he must play exactly one of his cards. Then, if the opponent doesn't have any cards stronger than the card played, the opponent loses, and the game ends. Otherwise, the opponent has to play a stronger card (exactly one card as well). These two cards are removed from the game, and the turn ends. If there are no cards left, the game ends in a draw; otherwise it's the opponent's turn.Consider all possible ways to distribute the cards between two players, so that each of them receives exactly half of the cards. You have to calculate three numbers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. You may assume that both players play optimally (i. e. if a player can win no matter how his opponent plays, he wins). Two ways to distribute the cards are different if there is at least one card such that, in one of these ways, it is given to Alex, and in the other way, it is given to Boris.For example, suppose $$$n = 4$$$, Alex receives the cards $$$[2, 3]$$$, and Boris receives the cards $$$[1, 4]$$$. Then the game may go as follows: if Alex plays the card $$$2$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$3$$$. So, the game ends in a draw; if Alex plays the card $$$3$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$2$$$. So, the game ends in a draw. So, in this case, the game ends in a draw.
|
512 megabytes
|
import java.io.*;
import java.math.*;
import java.util.*;
public class C_Card_Game
{
public static void main(String[] args)throws Exception
{
new Solver().solve();
}
}
//* Success is not final, failure is not fatal: it is the courage to continue that counts.
class Solver {
public long factorial[];
public long pow(long base, long exp, long MOD) {
base %= MOD;
long ret = 1;
while (exp > 0) {
if ((exp & 1) == 1) ret = ret * base % MOD;
base = base * base % MOD;
exp >>= 1;
}
return ret;
}
public void setFactorial() {
factorial = new long[MAXN];
factorial[0] = 1;
for (int i = 1; i < MAXN; ++i) factorial[i] = factorial[i - 1] * i % MOD;
}
public long getFactorial(int n) {
if (factorial == null) setFactorial();
return factorial[n];
}
public long ncr(int n, int r) {
if (r > n) return 0;
if (factorial == null) setFactorial();
long numerator = factorial[n];
long denominator = factorial[r] * factorial[n - r] % MOD;
return numerator * pow(denominator, MOD - 2, MOD) % MOD;
}
void solve() throws Exception
{
int t = sc.nextInt();
long[][] dp = new long[66][2];
dp[2][0] = 1;
dp[2][1] = 0;
dp[4][0] = 3;
dp[4][1] = 2;
for(int i =4 ;i<66;i+=2){
dp[i][0] = (ncr(i-1,i/2-1)+dp[i-2][1])%MOD;
dp[i][1] = (ncr(i,i/2)-dp[i][0]-1+MOD)%MOD;
}
while(t-->0){
int n =sc.nextInt();
sc.println(dp[n][0]+" "+dp[n][1]+" "+1);
}
sc.flush();
}
final Helper sc;
final int MAXN = 1000_006;
final long MOD = (long)998244353;
Solver() {
sc = new Helper(MOD, MAXN);
sc.initIO(System.in, System.out);
}
}
class Helper {
final long MOD;
final int MAXN;
final Random rnd;
public Helper(long mod, int maxn) {
MOD = mod;
MAXN = maxn;
rnd = new Random();
}
public static int[] sieve;
public static ArrayList<Integer> primes;
public void setSieve() {
primes = new ArrayList<>();
sieve = new int[MAXN];
int i, j;
for (i = 2; i*i < MAXN; ++i)
if (sieve[i] == 0) {
primes.add(i);
for (j = i*i; j < MAXN; j += i) {
sieve[j] = i;
}
}
}
public static long[] factorial;
public void setFactorial() {
factorial = new long[MAXN];
factorial[0] = 1;
for (int i = 1; i < MAXN; ++i) factorial[i] = factorial[i - 1] * i % MOD;
}
public long getFactorial(int n) {
if (factorial == null) setFactorial();
return factorial[n];
}
public long ncr(int n, int r) {
if (r > n) return 0;
if (factorial == null) setFactorial();
long numerator = factorial[n];
long denominator = factorial[r] * factorial[n - r] % MOD;
return numerator * pow(denominator, MOD - 2, MOD) % MOD;
}
public long[] getLongArray(int size) throws Exception {
long[] ar = new long[size];
for (int i = 0; i < size; ++i) ar[i] = nextLong();
return ar;
}
public int[] getIntArray(int size) throws Exception {
int[] ar = new int[size];
for (int i = 0; i < size; ++i) ar[i] = nextInt();
return ar;
}
public long gcd(long a, long b) {
return b == 0 ? a : gcd(b, a % b);
}
public int gcd(int a, int b) {
return b == 0 ? a : gcd(b, a % b);
}
public long max(long[] ar) {
long ret = ar[0];
for (long itr : ar) ret = Math.max(ret, itr);
return ret;
}
public int max(int[] ar) {
int ret = ar[0];
for (int itr : ar) ret = Math.max(ret, itr);
return ret;
}
public long min(long[] ar) {
long ret = ar[0];
for (long itr : ar) ret = Math.min(ret, itr);
return ret;
}
public int min(int[] ar) {
int ret = ar[0];
for (int itr : ar) ret = Math.min(ret, itr);
return ret;
}
public long sum(long[] ar) {
long sum = 0;
for (long itr : ar) sum += itr;
return sum;
}
public long sum(int[] ar) {
long sum = 0;
for (int itr : ar) sum += itr;
return sum;
}
public void shuffle(int[] ar) {
int r;
for (int i = 0; i < ar.length; ++i) {
r = rnd.nextInt(ar.length);
if (r != i) {
ar[i] ^= ar[r];
ar[r] ^= ar[i];
ar[i] ^= ar[r];
}
}
}
public void shuffle(long[] ar) {
int r;
for (int i = 0; i < ar.length; ++i) {
r = rnd.nextInt(ar.length);
if (r != i) {
ar[i] ^= ar[r];
ar[r] ^= ar[i];
ar[i] ^= ar[r];
}
}
}
public long pow(long base, long exp, long MOD) {
base %= MOD;
long ret = 1;
while (exp > 0) {
if ((exp & 1) == 1) ret = ret * base % MOD;
base = base * base % MOD;
exp >>= 1;
}
return ret;
}
static byte[] buf = new byte[1000_006];
static int index, total;
static InputStream in;
static BufferedWriter bw;
public void initIO(InputStream is, OutputStream os) {
try {
in = is;
bw = new BufferedWriter(new OutputStreamWriter(os));
} catch (Exception e) {
}
}
public void initIO(String inputFile, String outputFile) {
try {
in = new FileInputStream(inputFile);
bw = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(outputFile)));
} catch (Exception e) {
}
}
private int scan() throws Exception {
if (index >= total) {
index = 0;
total = in.read(buf);
if (total <= 0)
return -1;
}
return buf[index++];
}
public String nextLine() throws Exception {
int c;
for (c = scan(); c <= 32; c = scan()) ;
StringBuilder sb = new StringBuilder();
for (; c >= 32; c = scan())
sb.append((char) c);
return sb.toString();
}
public String next() throws Exception {
int c;
for (c = scan(); c <= 32; c = scan()) ;
StringBuilder sb = new StringBuilder();
for (; c > 32; c = scan())
sb.append((char) c);
return sb.toString();
}
public int nextInt() throws Exception {
int c, val = 0;
for (c = scan(); c <= 32; c = scan()) ;
boolean neg = c == '-';
if (c == '-' || c == '+')
c = scan();
for (; c >= '0' && c <= '9'; c = scan())
val = (val << 3) + (val << 1) + (c & 15);
return neg ? -val : val;
}
public long nextLong() throws Exception {
int c;
long val = 0;
for (c = scan(); c <= 32; c = scan()) ;
boolean neg = c == '-';
if (c == '-' || c == '+')
c = scan();
for (; c >= '0' && c <= '9'; c = scan())
val = (val << 3) + (val << 1) + (c & 15);
return neg ? -val : val;
}
public void print(Object a) throws Exception {
bw.write(a.toString());
}
public void printsp(Object a) throws Exception {
print(a);
print(" ");
}
public void println() throws Exception {
bw.write("\n");
}
public void printArray(int[] arr) throws Exception{
for(int i = 0;i<arr.length;i++){
print(arr[i]+ " ");
}
println();
}
public void println(Object a) throws Exception {
print(a);
println();
}
public void flush() throws Exception {
bw.flush();
}
}
|
Java
|
["5\n\n2\n\n4\n\n6\n\n8\n\n60"]
|
2 seconds
|
["1 0 1\n3 2 1\n12 7 1\n42 27 1\n341102826 248150916 1"]
|
NoteIn the first test case, Alex wins if he receives the card $$$2$$$ (he plays it, and Boris cannot respond). If Alex receives the card $$$1$$$, the game ends in a draw.In the second test case: Alex wins if he receives the cards $$$[3, 4]$$$, $$$[2, 4]$$$ or $$$[1, 4]$$$; Boris wins if Alex receives the cards $$$[1, 2]$$$ or $$$[1, 3]$$$; the game ends in a draw if Alex receives the cards $$$[2, 3]$$$.
|
Java 11
|
standard input
|
[
"combinatorics",
"constructive algorithms",
"dp",
"games"
] |
63fc2fb08d248f8ccdb3f5364c96dac1
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 30$$$) — the number of test cases. Then, $$$t$$$ lines follow. The $$$i$$$-th line contains one even integer $$$n$$$ ($$$2 \le n \le 60$$$).
| 1,500
|
For each test case, print three integers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. Since the answers can be large, print them modulo $$$998244353$$$.
|
standard output
| |
PASSED
|
6d640ff85ab0ec5669dbbc23c990ca12
|
train_109.jsonl
|
1664462100
|
Consider a game with $$$n$$$ cards ($$$n$$$ is even). Each card has a number written on it, between $$$1$$$ and $$$n$$$. All numbers on the cards are different. We say that a card with number $$$x$$$ is stronger than a card with number $$$y$$$ if $$$x > y$$$.Two players, Alex and Boris, play this game. In the beginning, each of them receives exactly $$$\frac{n}{2}$$$ cards, so each card belongs to exactly one player. Then, they take turns. Alex goes first, then Boris, then Alex again, and so on.On a player's turn, he must play exactly one of his cards. Then, if the opponent doesn't have any cards stronger than the card played, the opponent loses, and the game ends. Otherwise, the opponent has to play a stronger card (exactly one card as well). These two cards are removed from the game, and the turn ends. If there are no cards left, the game ends in a draw; otherwise it's the opponent's turn.Consider all possible ways to distribute the cards between two players, so that each of them receives exactly half of the cards. You have to calculate three numbers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. You may assume that both players play optimally (i. e. if a player can win no matter how his opponent plays, he wins). Two ways to distribute the cards are different if there is at least one card such that, in one of these ways, it is given to Alex, and in the other way, it is given to Boris.For example, suppose $$$n = 4$$$, Alex receives the cards $$$[2, 3]$$$, and Boris receives the cards $$$[1, 4]$$$. Then the game may go as follows: if Alex plays the card $$$2$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$3$$$. So, the game ends in a draw; if Alex plays the card $$$3$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$2$$$. So, the game ends in a draw. So, in this case, the game ends in a draw.
|
512 megabytes
|
/*
Author: Spidey2182
#: 1739C
*/
import java.util.*;
public class CardGame implements Runnable {
static ContestScanner sc = new ContestScanner();
static ContestPrinter out = new ContestPrinter();
public static void main(String[] args) {
new Thread(null, new CardGame(), "main", 1 << 28).start();
}
public void run() {
fact = new long[61];
invFact = new long[61];
fact[0] = 1;
for (int i = 1; i < 61; i++)
fact[i] = fact[i - 1] % MOD9 * i % MOD9;
invFact[60] = pow(fact[60], MOD9 - 2);
for (int i = 60; i >= 1; i--)
invFact[i - 1] = invFact[i] % MOD9 * i % MOD9;
int testCases=sc.nextInt();
for(int testCase=1; testCase<=testCases; testCase++) {
int n=sc.nextInt();
long a=0, b=0, d=1;
for(int i=n, j=1; i>0; i-=4, j+=2) {
a = (a + nCr(i - 1, (n >> 1) - j)) % MOD9;
b = (b + nCr(i - 2, (n >> 1) - j - 1)) % MOD9;
b = (b + nCr(i - 3, (n >> 1) - j)) % MOD9;
a = (a + nCr(i - 4, (n >> 1) - j)) % MOD9;
}
out.printf("%d %d %d\n", a, b, d);
}
out.flush();
out.close();
}
static long[] fact;
static long[] invFact;
static long nCr(int n, int r) {
if (r < 0 || r > n)
return 0;
return fact[n] % MOD9 * invFact[r] % MOD9 * invFact[n - r] % MOD9;
}
static long pow(long a, long b) {
long ret = 1;
long tmp = a;
while (b > 0) {
if ((b & 1) == 1) {
ret = (ret * tmp) % MOD9;
}
tmp = (tmp * tmp) % MOD9;
b = b >> 1;
}
return ret;
}
static final int MOD = (int) 1e9 + 7;
static final int MOD9 = 998244353;
static int gcd(int a, int b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
static long gcd(long a, long b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
}
//Credits: https://github.com/NASU41/AtCoderLibraryForJava/tree/master/ContestIO
class ContestScanner {
private final java.io.InputStream in;
private final byte[] buffer = new byte[1024];
private int ptr = 0;
private int buflen = 0;
private static final long LONG_MAX_TENTHS = 922337203685477580L;
private static final int LONG_MAX_LAST_DIGIT = 7;
private static final int LONG_MIN_LAST_DIGIT = 8;
public ContestScanner(java.io.InputStream in) {
this.in = in;
}
public ContestScanner(java.io.File file) throws java.io.FileNotFoundException {
this(new java.io.BufferedInputStream(new java.io.FileInputStream(file)));
}
public ContestScanner() {
this(System.in);
}
private boolean hasNextByte() {
if (ptr < buflen) {
return true;
} else {
ptr = 0;
try {
buflen = in.read(buffer);
} catch (java.io.IOException e) {
e.printStackTrace();
}
if (buflen <= 0) {
return false;
}
}
return true;
}
private int readByte() {
if (hasNextByte()) return buffer[ptr++];
else return -1;
}
private static boolean isPrintableChar(int c) {
return 33 <= c && c <= 126;
}
public boolean hasNext() {
while (hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++;
return hasNextByte();
}
public String next() {
if (!hasNext()) throw new NoSuchElementException();
StringBuilder sb = new StringBuilder();
int b = readByte();
while (isPrintableChar(b)) {
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
public long nextLong() {
if (!hasNext()) throw new NoSuchElementException();
long n = 0;
boolean minus = false;
int b = readByte();
if (b == '-') {
minus = true;
b = readByte();
}
if (b < '0' || '9' < b) {
throw new NumberFormatException();
}
while (true) {
if ('0' <= b && b <= '9') {
int digit = b - '0';
if (n >= LONG_MAX_TENTHS) {
if (n == LONG_MAX_TENTHS) {
if (minus) {
if (digit <= LONG_MIN_LAST_DIGIT) {
n = -n * 10 - digit;
b = readByte();
if (!isPrintableChar(b)) {
return n;
} else if (b < '0' || '9' < b) {
throw new NumberFormatException(
String.format("%d%s... is not number", n, Character.toString(b))
);
}
}
} else {
if (digit <= LONG_MAX_LAST_DIGIT) {
n = n * 10 + digit;
b = readByte();
if (!isPrintableChar(b)) {
return n;
} else if (b < '0' || '9' < b) {
throw new NumberFormatException(
String.format("%d%s... is not number", n, Character.toString(b))
);
}
}
}
}
throw new ArithmeticException(
String.format("%s%d%d... overflows long.", minus ? "-" : "", n, digit)
);
}
n = n * 10 + digit;
} else if (b == -1 || !isPrintableChar(b)) {
return minus ? -n : n;
} else {
throw new NumberFormatException();
}
b = readByte();
}
}
public int nextInt() {
long nl = nextLong();
if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE) throw new NumberFormatException();
return (int) nl;
}
public double nextDouble() {
return Double.parseDouble(next());
}
public long[] nextLongArray(int length) {
long[] array = new long[length];
for (int i = 0; i < length; i++) array[i] = this.nextLong();
return array;
}
public long[] nextLongArray(int length, java.util.function.LongUnaryOperator map) {
long[] array = new long[length];
for (int i = 0; i < length; i++) array[i] = map.applyAsLong(this.nextLong());
return array;
}
public int[] nextIntArray(int length) {
int[] array = new int[length];
for (int i = 0; i < length; i++) array[i] = this.nextInt();
return array;
}
public int[] nextIntArray(int length, java.util.function.IntUnaryOperator map) {
int[] array = new int[length];
for (int i = 0; i < length; i++) array[i] = map.applyAsInt(this.nextInt());
return array;
}
public double[] nextDoubleArray(int length) {
double[] array = new double[length];
for (int i = 0; i < length; i++) array[i] = this.nextDouble();
return array;
}
public double[] nextDoubleArray(int length, java.util.function.DoubleUnaryOperator map) {
double[] array = new double[length];
for (int i = 0; i < length; i++) array[i] = map.applyAsDouble(this.nextDouble());
return array;
}
public long[][] nextLongMatrix(int height, int width) {
long[][] mat = new long[height][width];
for (int h = 0; h < height; h++)
for (int w = 0; w < width; w++) {
mat[h][w] = this.nextLong();
}
return mat;
}
public int[][] nextIntMatrix(int height, int width) {
int[][] mat = new int[height][width];
for (int h = 0; h < height; h++)
for (int w = 0; w < width; w++) {
mat[h][w] = this.nextInt();
}
return mat;
}
public double[][] nextDoubleMatrix(int height, int width) {
double[][] mat = new double[height][width];
for (int h = 0; h < height; h++)
for (int w = 0; w < width; w++) {
mat[h][w] = this.nextDouble();
}
return mat;
}
public char[][] nextCharMatrix(int height, int width) {
char[][] mat = new char[height][width];
for (int h = 0; h < height; h++) {
String s = this.next();
for (int w = 0; w < width; w++) {
mat[h][w] = s.charAt(w);
}
}
return mat;
}
}
class ContestPrinter extends java.io.PrintWriter {
public ContestPrinter(java.io.PrintStream stream) {
super(stream);
}
public ContestPrinter(java.io.File file) throws java.io.FileNotFoundException {
super(new java.io.PrintStream(file));
}
public ContestPrinter() {
super(System.out);
}
private static String dtos(double x, int n) {
StringBuilder sb = new StringBuilder();
if (x < 0) {
sb.append('-');
x = -x;
}
x += Math.pow(10, -n) / 2;
sb.append((long) x);
sb.append(".");
x -= (long) x;
for (int i = 0; i < n; i++) {
x *= 10;
sb.append((int) x);
x -= (int) x;
}
return sb.toString();
}
@Override
public void print(float f) {
super.print(dtos(f, 20));
}
@Override
public void println(float f) {
super.println(dtos(f, 20));
}
@Override
public void print(double d) {
super.print(dtos(d, 20));
}
@Override
public void println(double d) {
super.println(dtos(d, 20));
}
public void printArray(int[] array, String separator) {
int n = array.length;
if (n == 0) {
super.println();
return;
}
for (int i = 0; i < n - 1; i++) {
super.print(array[i]);
super.print(separator);
}
super.println(array[n - 1]);
}
public void printArray(int[] array) {
this.printArray(array, " ");
}
public void printArray(int[] array, String separator, java.util.function.IntUnaryOperator map) {
int n = array.length;
if (n == 0) {
super.println();
return;
}
for (int i = 0; i < n - 1; i++) {
super.print(map.applyAsInt(array[i]));
super.print(separator);
}
super.println(map.applyAsInt(array[n - 1]));
}
public void printArray(int[] array, java.util.function.IntUnaryOperator map) {
this.printArray(array, " ", map);
}
public void printArray(long[] array, String separator) {
int n = array.length;
if (n == 0) {
super.println();
return;
}
for (int i = 0; i < n - 1; i++) {
super.print(array[i]);
super.print(separator);
}
super.println(array[n - 1]);
}
public void printArray(long[] array) {
this.printArray(array, " ");
}
public void printArray(long[] array, String separator, java.util.function.LongUnaryOperator map) {
int n = array.length;
if (n == 0) {
super.println();
return;
}
for (int i = 0; i < n - 1; i++) {
super.print(map.applyAsLong(array[i]));
super.print(separator);
}
super.println(map.applyAsLong(array[n - 1]));
}
public void printArray(long[] array, java.util.function.LongUnaryOperator map) {
this.printArray(array, " ", map);
}
public <T> void printArray(T[] array, String separator) {
int n = array.length;
if (n == 0) {
super.println();
return;
}
for (int i = 0; i < n - 1; i++) {
super.print(array[i]);
super.print(separator);
}
super.println(array[n - 1]);
}
public <T> void printArray(T[] array) {
this.printArray(array, " ");
}
public <T> void printArray(T[] array, String separator, java.util.function.UnaryOperator<T> map) {
int n = array.length;
if (n == 0) {
super.println();
return;
}
for (int i = 0; i < n - 1; i++) {
super.print(map.apply(array[i]));
super.print(separator);
}
super.println(map.apply(array[n - 1]));
}
public <T> void printArray(T[] array, java.util.function.UnaryOperator<T> map) {
this.printArray(array, " ", map);
}
}
|
Java
|
["5\n\n2\n\n4\n\n6\n\n8\n\n60"]
|
2 seconds
|
["1 0 1\n3 2 1\n12 7 1\n42 27 1\n341102826 248150916 1"]
|
NoteIn the first test case, Alex wins if he receives the card $$$2$$$ (he plays it, and Boris cannot respond). If Alex receives the card $$$1$$$, the game ends in a draw.In the second test case: Alex wins if he receives the cards $$$[3, 4]$$$, $$$[2, 4]$$$ or $$$[1, 4]$$$; Boris wins if Alex receives the cards $$$[1, 2]$$$ or $$$[1, 3]$$$; the game ends in a draw if Alex receives the cards $$$[2, 3]$$$.
|
Java 11
|
standard input
|
[
"combinatorics",
"constructive algorithms",
"dp",
"games"
] |
63fc2fb08d248f8ccdb3f5364c96dac1
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 30$$$) — the number of test cases. Then, $$$t$$$ lines follow. The $$$i$$$-th line contains one even integer $$$n$$$ ($$$2 \le n \le 60$$$).
| 1,500
|
For each test case, print three integers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. Since the answers can be large, print them modulo $$$998244353$$$.
|
standard output
| |
PASSED
|
90e32b06e182a7dd1b5113300c7d6ac8
|
train_109.jsonl
|
1664462100
|
Consider a game with $$$n$$$ cards ($$$n$$$ is even). Each card has a number written on it, between $$$1$$$ and $$$n$$$. All numbers on the cards are different. We say that a card with number $$$x$$$ is stronger than a card with number $$$y$$$ if $$$x > y$$$.Two players, Alex and Boris, play this game. In the beginning, each of them receives exactly $$$\frac{n}{2}$$$ cards, so each card belongs to exactly one player. Then, they take turns. Alex goes first, then Boris, then Alex again, and so on.On a player's turn, he must play exactly one of his cards. Then, if the opponent doesn't have any cards stronger than the card played, the opponent loses, and the game ends. Otherwise, the opponent has to play a stronger card (exactly one card as well). These two cards are removed from the game, and the turn ends. If there are no cards left, the game ends in a draw; otherwise it's the opponent's turn.Consider all possible ways to distribute the cards between two players, so that each of them receives exactly half of the cards. You have to calculate three numbers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. You may assume that both players play optimally (i. e. if a player can win no matter how his opponent plays, he wins). Two ways to distribute the cards are different if there is at least one card such that, in one of these ways, it is given to Alex, and in the other way, it is given to Boris.For example, suppose $$$n = 4$$$, Alex receives the cards $$$[2, 3]$$$, and Boris receives the cards $$$[1, 4]$$$. Then the game may go as follows: if Alex plays the card $$$2$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$3$$$. So, the game ends in a draw; if Alex plays the card $$$3$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$2$$$. So, the game ends in a draw. So, in this case, the game ends in a draw.
|
512 megabytes
|
import java.io.*;
import java.util.*;
public class c {
static long m = 998244353;
public static void main(String[] args) throws Exception {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
StringBuilder b = new StringBuilder();
int nc = Integer.parseInt(in.readLine());
long[][] ncr = new long[65][65];
ncr[0][0] = 1;
for(int i = 0; i+1 < ncr.length; i++){
for(int j = 0; j <= i; j++){
ncr[i+1][j] = mod(ncr[i+1][j] + ncr[i][j]);
ncr[i+1][j+1] = mod(ncr[i+1][j+1] + ncr[i][j]);
}
}
for(int cn = 0; cn < nc; cn++){
int n = Integer.parseInt(in.readLine());
if(n == 2){
b.append("1 0 1\n");
continue;
}
long d = 1;
long total = ncr[n][n/2];
long a = ncr[n-1][n/2-1];
int num = n-4;
int denom = n/2 - 3;
while(num - 1 > 0){
a = mod(a + ncr[num][denom]);
a = mod(a + ncr[num-1][denom]);
num -= 4;
denom -= 2;
}
long boris = mod(total - 1 - a);
if(boris <= 0){
boris = (boris + m) % m;
boris = (boris + m) % m;
}
b.append(a + " " + boris + " " + d + "\n");
}
System.out.print(b);
in.close();
}
public static long mod(long k){
return k % m;
}
}
|
Java
|
["5\n\n2\n\n4\n\n6\n\n8\n\n60"]
|
2 seconds
|
["1 0 1\n3 2 1\n12 7 1\n42 27 1\n341102826 248150916 1"]
|
NoteIn the first test case, Alex wins if he receives the card $$$2$$$ (he plays it, and Boris cannot respond). If Alex receives the card $$$1$$$, the game ends in a draw.In the second test case: Alex wins if he receives the cards $$$[3, 4]$$$, $$$[2, 4]$$$ or $$$[1, 4]$$$; Boris wins if Alex receives the cards $$$[1, 2]$$$ or $$$[1, 3]$$$; the game ends in a draw if Alex receives the cards $$$[2, 3]$$$.
|
Java 11
|
standard input
|
[
"combinatorics",
"constructive algorithms",
"dp",
"games"
] |
63fc2fb08d248f8ccdb3f5364c96dac1
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 30$$$) — the number of test cases. Then, $$$t$$$ lines follow. The $$$i$$$-th line contains one even integer $$$n$$$ ($$$2 \le n \le 60$$$).
| 1,500
|
For each test case, print three integers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. Since the answers can be large, print them modulo $$$998244353$$$.
|
standard output
| |
PASSED
|
7a9da4709f7cfccbed75fb0ee33c2a67
|
train_109.jsonl
|
1664462100
|
Consider a game with $$$n$$$ cards ($$$n$$$ is even). Each card has a number written on it, between $$$1$$$ and $$$n$$$. All numbers on the cards are different. We say that a card with number $$$x$$$ is stronger than a card with number $$$y$$$ if $$$x > y$$$.Two players, Alex and Boris, play this game. In the beginning, each of them receives exactly $$$\frac{n}{2}$$$ cards, so each card belongs to exactly one player. Then, they take turns. Alex goes first, then Boris, then Alex again, and so on.On a player's turn, he must play exactly one of his cards. Then, if the opponent doesn't have any cards stronger than the card played, the opponent loses, and the game ends. Otherwise, the opponent has to play a stronger card (exactly one card as well). These two cards are removed from the game, and the turn ends. If there are no cards left, the game ends in a draw; otherwise it's the opponent's turn.Consider all possible ways to distribute the cards between two players, so that each of them receives exactly half of the cards. You have to calculate three numbers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. You may assume that both players play optimally (i. e. if a player can win no matter how his opponent plays, he wins). Two ways to distribute the cards are different if there is at least one card such that, in one of these ways, it is given to Alex, and in the other way, it is given to Boris.For example, suppose $$$n = 4$$$, Alex receives the cards $$$[2, 3]$$$, and Boris receives the cards $$$[1, 4]$$$. Then the game may go as follows: if Alex plays the card $$$2$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$3$$$. So, the game ends in a draw; if Alex plays the card $$$3$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$2$$$. So, the game ends in a draw. So, in this case, the game ends in a draw.
|
512 megabytes
|
import java.util.*;
import java.io.*;
import java.math.*;
import java.awt.geom.*;
import static java.lang.Math.*;
public class Solution implements Runnable {
long mod1 = (long) 1e9 + 7;
long mod2 = 998244353;
long win[] = new long[61];
long loss[] = new long[61];
public void solve() throws Exception {
int t = sc.nextInt();
win[2] = 1;
loss[2] = 0;
for(int i=4;i<=60;i+=2) {
long total_ways = ncr(i, i/2, mod2);
long draws = 1;
long wins_with_max_value_with_Alice = ncr(i-1, i/2-1, mod2);
long wins_without_max_value_without_Alice = loss[i-2];
win[i] = ((wins_with_max_value_with_Alice+wins_without_max_value_without_Alice)%mod2+mod2)%mod2;
loss[i] = ((total_ways-1-win[i])%mod2+mod2)%mod2;
}
while (t-- > 0) {
int n=sc.nextInt();
out.println(win[n]+" "+loss[n]+" "+1);
}
}
static long gcd(long a, long b) {
if (a == 0)
return b;
return gcd(b % a, a);
}
static void sort(int[] a) {
ArrayList<Integer> l = new ArrayList<>();
for (int i : a)
l.add(i);
Collections.sort(l);
for (int i = 0; i < a.length; i++)
a[i] = l.get(i);
}
static long ncr(int n, int r, long p) {
if (r > n)
return 0l;
if (r > n - r)
r = n - r;
long C[] = new long[r + 1];
C[0] = 1;
for (int i = 1; i <= n; i++) {
for (int j = Math.min(i, r); j > 0; j--)
C[j] = (C[j] + C[j - 1]) % p;
}
return C[r] % p;
}
void sieveOfEratosthenes(boolean prime[], int size) {
for (int i = 0; i < size; i++)
prime[i] = true;
for (int p = 2; p * p < size; p++) {
if (prime[p] == true) {
for (int i = p * p; i < size; i += p)
prime[i] = false;
}
}
}
static int LowerBound(int a[], int x) { // smallest index having value >= x
int l = -1, r = a.length;
while (l + 1 < r) {
int m = (l + r) >>> 1;
if (a[m] >= x)
r = m;
else
l = m;
}
return r;
}
static int UpperBound(int a[], int x) {// biggest index having value <= x
int l = -1, r = a.length;
while (l + 1 < r) {
int m = (l + r) >>> 1;
if (a[m] <= x)
l = m;
else
r = m;
}
return l + 1;
}
public long power(long x, long y, long p) {
long res = 1;
// out.println(x+" "+y);
x = x % p;
if (x == 0)
return 0;
while (y > 0) {
if ((y & 1) == 1)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
static Throwable uncaught;
BufferedReader in;
FastScanner sc;
PrintWriter out;
@Override
public void run() {
try {
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
sc = new FastScanner(in);
solve();
} catch (Throwable uncaught) {
Solution.uncaught = uncaught;
} finally {
out.close();
}
}
public static void main(String[] args) throws Throwable {
Thread thread = new Thread(null, new Solution(), "", (1 << 26));
thread.start();
thread.join();
if (Solution.uncaught != null) {
throw Solution.uncaught;
}
}
}
class FastScanner {
BufferedReader in;
StringTokenizer st;
public FastScanner(BufferedReader in) {
this.in = in;
}
public String nextToken() throws Exception {
while (st == null || !st.hasMoreTokens()) {
st = new StringTokenizer(in.readLine());
}
return st.nextToken();
}
public int nextInt() throws Exception {
return Integer.parseInt(nextToken());
}
public int[] readArray(int n) throws Exception {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
public long nextLong() throws Exception {
return Long.parseLong(nextToken());
}
public double nextDouble() throws Exception {
return Double.parseDouble(nextToken());
}
}
|
Java
|
["5\n\n2\n\n4\n\n6\n\n8\n\n60"]
|
2 seconds
|
["1 0 1\n3 2 1\n12 7 1\n42 27 1\n341102826 248150916 1"]
|
NoteIn the first test case, Alex wins if he receives the card $$$2$$$ (he plays it, and Boris cannot respond). If Alex receives the card $$$1$$$, the game ends in a draw.In the second test case: Alex wins if he receives the cards $$$[3, 4]$$$, $$$[2, 4]$$$ or $$$[1, 4]$$$; Boris wins if Alex receives the cards $$$[1, 2]$$$ or $$$[1, 3]$$$; the game ends in a draw if Alex receives the cards $$$[2, 3]$$$.
|
Java 11
|
standard input
|
[
"combinatorics",
"constructive algorithms",
"dp",
"games"
] |
63fc2fb08d248f8ccdb3f5364c96dac1
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 30$$$) — the number of test cases. Then, $$$t$$$ lines follow. The $$$i$$$-th line contains one even integer $$$n$$$ ($$$2 \le n \le 60$$$).
| 1,500
|
For each test case, print three integers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. Since the answers can be large, print them modulo $$$998244353$$$.
|
standard output
| |
PASSED
|
2e03db08a35e076374bab4ca3a52ca16
|
train_109.jsonl
|
1664462100
|
Consider a game with $$$n$$$ cards ($$$n$$$ is even). Each card has a number written on it, between $$$1$$$ and $$$n$$$. All numbers on the cards are different. We say that a card with number $$$x$$$ is stronger than a card with number $$$y$$$ if $$$x > y$$$.Two players, Alex and Boris, play this game. In the beginning, each of them receives exactly $$$\frac{n}{2}$$$ cards, so each card belongs to exactly one player. Then, they take turns. Alex goes first, then Boris, then Alex again, and so on.On a player's turn, he must play exactly one of his cards. Then, if the opponent doesn't have any cards stronger than the card played, the opponent loses, and the game ends. Otherwise, the opponent has to play a stronger card (exactly one card as well). These two cards are removed from the game, and the turn ends. If there are no cards left, the game ends in a draw; otherwise it's the opponent's turn.Consider all possible ways to distribute the cards between two players, so that each of them receives exactly half of the cards. You have to calculate three numbers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. You may assume that both players play optimally (i. e. if a player can win no matter how his opponent plays, he wins). Two ways to distribute the cards are different if there is at least one card such that, in one of these ways, it is given to Alex, and in the other way, it is given to Boris.For example, suppose $$$n = 4$$$, Alex receives the cards $$$[2, 3]$$$, and Boris receives the cards $$$[1, 4]$$$. Then the game may go as follows: if Alex plays the card $$$2$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$3$$$. So, the game ends in a draw; if Alex plays the card $$$3$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$2$$$. So, the game ends in a draw. So, in this case, the game ends in a draw.
|
512 megabytes
|
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Stack;
public class Main {
static long MOD = 998244353l;
int min = Integer.MAX_VALUE;
int max = 0;
char result[][];
int count = 0;
int pattern = 0;
public static void main(String[] args) throws Exception {
// FileInputStream fis = new FileInputStream(new File("1.txt"));
var sc = new FastScanner();
// var sc = new FastScanner(fis);
// var pw = new FastPrintStream("test_normal_result.csv");
var pw = new FastPrintStream();
solve(sc, pw);
sc.close();
pw.flush();
pw.close();
}
List<Integer> list[];
public static void solve(FastScanner sc, FastPrintStream pw) {
int times = sc.nextInt();
for (int t = 0; t < times; t++) {
int n = sc.nextInt();
long re = 0;
long sum = Main.XCY(n / 2, n);
long w1 = Main.XCY(n/2-1, n-1);
for (int i = 5; i < n; i += 4) {
w1 = (w1+(Main.XCY(n/2-i/2-1, n-i)*2)%MOD)%MOD;
if (n/2-i/2-2>=0) {
w1 = (w1+(Main.XCY(n/2-i/2-2, n-i))%MOD)%MOD;
}
}
long w2 = (sum + MOD - (w1 + 1) % MOD) % MOD;
pw.println(w1 + " " + w2 + " 1");
}
}
public static boolean check(int i, int j, int endi, int endj, int[][] map) {
int points = 0;
if (endi - i == 1 && map[i][j] == 4 && map[endi][j] == 4) {
return false;
}
if (endi - i == 1 && map[i][j] == 4 && map[i][endj] == 4) {
return false;
}
for (int ti = i; ti <= endi; ti++) {
for (int tj = j; tj <= endj; tj++) {
if (map[ti][tj] == 2) {
return false;
}
if (map[ti][tj] == 1) {
points++;
}
}
}
if (points == 3) {
return true;
}
return false;
}
public static void fillmap(int i, int j, int endi, int endj, int[][] map, int line) {
for (int ti = i; ti <= endi; ti++) {
for (int tj = j; tj <= endj; tj++) {
if (map[ti][tj] != 1) {
map[ti][tj] = line;
}
}
}
}
public static boolean isSame(int x1, int y1, int x2, int y2) {
if (x1 == x2) {
if (Math.abs(y1 - y2) == 1) {
return true;
}
}
if (y1 == y2) {
if (Math.abs(x1 - x2) == 1) {
return true;
}
}
if (Math.abs(x1 - x2) == 1) {
if (x1 - x2 == y1 - y2) {
return true;
}
}
return false;
}
public static long countRe(int high, int used[], long now, long min, long b[]) {
long re = Long.MAX_VALUE;
long temp = min;
for (int i = high; i >= 0; i--) {
if (used[i] == 0 && temp < now) {
temp += b[i];
if (temp >= now) {
re = Math.min(re, temp);
temp -= b[i];
}
}
}
if (re == Long.MAX_VALUE) {
re = min;
}
return re;
}
public static Point checkPoint(char ch) {
Point re = new Point(0, 0);
switch (ch) {
case 'N': {
re.x--;
break;
}
case 'S': {
re.x++;
break;
}
case 'E': {
re.y++;
break;
}
case 'W': {
re.y--;
break;
}
}
return re;
}
public char[][] copyArray(char c[][], int n) {
char re[][] = new char[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
re[i][j] = c[i][j];
}
}
return re;
}
public static void swap(int[] s, int i, int j) {
int tmp = s[i];
s[i] = s[j];
s[j] = tmp;
}
public void permutation(int[] s, int from, int to, int a[]) {
if (to <= 1)
return;
if (from == to) {
check(s, a);
} else {
for (int i = from; i <= to; i++) {
swap(s, i, from);
permutation(s, from + 1, to, a);
swap(s, from, i);
}
}
}
public void check(int[] s, int a[]) {
int re = 0;
int b[][] = new int[2][3];
for (int i = 0; i < 6; i++) {
b[i / 3][i % 3] = a[s[i]];
}
int maxx = 0;
int maxy = 0;
int minx = 10000;
int miny = 10000;
for (int i = 0; i < 3; i++) {
maxx = Math.max(maxx, b[0][i]);
minx = Math.min(minx, b[0][i]);
maxy = Math.max(maxy, b[1][i]);
miny = Math.min(miny, b[1][i]);
}
re = (maxx - minx) * (maxy - miny) * 2;
re = re - (Math.abs(b[0][0] - b[0][1]) * Math.abs(b[1][0] - b[1][1]));
re = re - (Math.abs(b[0][0] - b[0][2]) * Math.abs(b[1][0] - b[1][2]));
re = re - (Math.abs(b[0][2] - b[0][1]) * Math.abs(b[1][2] - b[1][1]));
max = Math.max(re, max);
}
public static long anothertoTen(long ano, int another) {
long ten = 0;
long now = 1;
long temp = ano;
while (temp > 0) {
long i = temp % 10;
ten += now * i;
now *= another;
temp /= 10;
}
return ten;
}
public static long tentoAnother(long ten, int another) {
Stack<Long> stack = new Stack<Long>();
while (ten > 0) {
stack.add(ten % another);
ten /= another;
}
long re = 0;
while (!stack.isEmpty()) {
long pop = stack.pop();
re = re * 10 + pop;
}
return re;
}
// 2C5 = 5*4/(2*1)
public static long XCY(long x, long y) {
long temp = 1;
for (int i = 0; i < x; i++) {
temp = (temp * (y - i)) % MOD;
}
long tempx = 1;
for (int i = 2; i <= x; i++) {
tempx = (tempx * i) % MOD;
}
tempx = modpow(tempx, (long) MOD - 2);
temp = (temp * tempx) % MOD;
return temp;
}
static long modpow(long N, Long K) {
return BigInteger.valueOf(N).modPow(BigInteger.valueOf(K), BigInteger.valueOf(MOD)).longValue();
}
static long modpow(long N, Long K, long mod) {
return BigInteger.valueOf(N).modPow(BigInteger.valueOf(K), BigInteger.valueOf(mod)).longValue();
}
public static long gcd(long a, long b) {
if (b == 0) {
return a;
}
if (a < b) {
return gcd(b, a);
}
return gcd(b, a % b);
}
public static int gcd(int a, int b) {
if (b == 0) {
return a;
}
if (a < b) {
return gcd(b, a);
}
return gcd(b, a % b);
}
}
class Node implements Comparable<Node> {
int tyoten;
long minDistance;
public Node(int t, long m) {
tyoten = t;
minDistance = m;
}
@Override
public int compareTo(Node o) {
int res = -1;
if (this.minDistance - o.minDistance >= 0) {
res = 1;
}
return res;
}
}
class Result {
String pattern;
int index;
public Result(String p, int i) {
pattern = p;
index = i;
}
}
class Vertex {
String key;
Vertex(String key) {
this.key = key;
}
}
class Edge {
Vertex start;
Vertex end;
long key;
Edge(Vertex start, Vertex end, long key) {
this.start = start;
this.end = end;
this.key = key;
}
}
class Target {
int t;
long c;
boolean bool;
}
class Point {
long x;
int y;
public Point() {
}
public Point(long x, int y) {
this.x = x;
this.y = y;
}
public long getx() {
return x;
}
public static Point min(Object ox, Object oy) {
Point x = (Point) ox;
Point y = (Point) oy;
if (x.x < y.x) {
return x;
}
return y;
}
public int compareTo(Object p) {
Point t = (Point) p;
if (this.x > t.x) {
return 1;
}
if (this.x < t.x) {
return -1;
}
return this.y - t.y;
}
@Override
public boolean equals(Object p) {
Point t = (Point) p;
return this.x == t.x;
}
}
class PointY implements Comparable {
int a;
int b;
public PointY(int x, int y) {
a = x;
b = y;
}
public int compareTo(Object p) {
PointY t = (PointY) p;
if (this.a > t.a) {
return -1;
}
if (this.a < t.a) {
return 1;
}
return 0;
}
}
class FastPrintStream implements AutoCloseable {
private static final int BUF_SIZE = 1 << 15;
private final byte[] buf = new byte[BUF_SIZE];
private int ptr = 0;
private final java.lang.reflect.Field strField;
private final java.nio.charset.CharsetEncoder encoder;
private java.io.OutputStream out;
public FastPrintStream(java.io.OutputStream out) {
this.out = out;
java.lang.reflect.Field f;
try {
f = java.lang.String.class.getDeclaredField("value");
f.setAccessible(true);
} catch (NoSuchFieldException | SecurityException e) {
f = null;
}
this.strField = f;
this.encoder = java.nio.charset.StandardCharsets.US_ASCII.newEncoder();
}
public FastPrintStream(java.io.File file) throws java.io.IOException {
this(new java.io.FileOutputStream(file));
}
public FastPrintStream(java.lang.String filename) throws java.io.IOException {
this(new java.io.File(filename));
}
public FastPrintStream() {
this(System.out);
try {
java.lang.reflect.Field f = java.io.PrintStream.class.getDeclaredField("autoFlush");
f.setAccessible(true);
f.set(System.out, false);
} catch (IllegalAccessException | IllegalArgumentException | NoSuchFieldException e) {
// ignore
}
}
public FastPrintStream println() {
if (ptr == BUF_SIZE)
internalFlush();
buf[ptr++] = (byte) '\n';
return this;
}
public FastPrintStream println(java.lang.Object o) {
return print(o).println();
}
public FastPrintStream println(java.lang.String s) {
return print(s).println();
}
public FastPrintStream println(char[] s) {
return print(s).println();
}
public FastPrintStream println(char c) {
return print(c).println();
}
public FastPrintStream println(int x) {
return print(x).println();
}
public FastPrintStream println(long x) {
return print(x).println();
}
public FastPrintStream println(double d, int precision) {
return print(d, precision).println();
}
private FastPrintStream print(byte[] bytes) {
int n = bytes.length;
if (ptr + n > BUF_SIZE) {
internalFlush();
try {
out.write(bytes);
} catch (java.io.IOException e) {
throw new RuntimeException();
}
} else {
System.arraycopy(bytes, 0, buf, ptr, n);
ptr += n;
}
return this;
}
public FastPrintStream print(java.lang.Object o) {
return print(o.toString());
}
public FastPrintStream print(java.lang.String s) {
if (strField == null) {
return print(s.getBytes());
} else {
try {
return print((byte[]) strField.get(s));
} catch (IllegalAccessException e) {
return print(s.getBytes());
}
}
}
public FastPrintStream print(char[] s) {
try {
return print(encoder.encode(java.nio.CharBuffer.wrap(s)).array());
} catch (java.nio.charset.CharacterCodingException e) {
byte[] bytes = new byte[s.length];
for (int i = 0; i < s.length; i++) {
bytes[i] = (byte) s[i];
}
return print(bytes);
}
}
public FastPrintStream print(char c) {
if (ptr == BUF_SIZE)
internalFlush();
buf[ptr++] = (byte) c;
return this;
}
public FastPrintStream print(int x) {
if (x == 0) {
if (ptr == BUF_SIZE)
internalFlush();
buf[ptr++] = '0';
return this;
}
int d = len(x);
if (ptr + d > BUF_SIZE)
internalFlush();
if (x < 0) {
buf[ptr++] = '-';
x = -x;
d--;
}
int j = ptr += d;
while (x > 0) {
buf[--j] = (byte) ('0' + (x % 10));
x /= 10;
}
return this;
}
public FastPrintStream print(long x) {
if (x == 0) {
if (ptr == BUF_SIZE)
internalFlush();
buf[ptr++] = '0';
return this;
}
int d = len(x);
if (ptr + d > BUF_SIZE)
internalFlush();
if (x < 0) {
buf[ptr++] = '-';
x = -x;
d--;
}
int j = ptr += d;
while (x > 0) {
buf[--j] = (byte) ('0' + (x % 10));
x /= 10;
}
return this;
}
public FastPrintStream print(double d, int precision) {
if (d < 0) {
print('-');
d = -d;
}
d += Math.pow(10, -d) / 2;
print((long) d).print('.');
d -= (long) d;
for (int i = 0; i < precision; i++) {
d *= 10;
print((int) d);
d -= (int) d;
}
return this;
}
private void internalFlush() {
try {
out.write(buf, 0, ptr);
ptr = 0;
} catch (java.io.IOException e) {
throw new RuntimeException(e);
}
}
public void flush() {
try {
out.write(buf, 0, ptr);
out.flush();
ptr = 0;
} catch (java.io.IOException e) {
throw new RuntimeException(e);
}
}
public void close() {
try {
out.close();
} catch (java.io.IOException e) {
throw new RuntimeException(e);
}
}
private static int len(int x) {
int d = 1;
if (x >= 0) {
d = 0;
x = -x;
}
int p = -10;
for (int i = 1; i < 10; i++, p *= 10)
if (x > p)
return i + d;
return 10 + d;
}
private static int len(long x) {
int d = 1;
if (x >= 0) {
d = 0;
x = -x;
}
long p = -10;
for (int i = 1; i < 19; i++, p *= 10)
if (x > p)
return i + d;
return 19 + d;
}
}
class FastScanner implements AutoCloseable {
private final java.io.InputStream in;
private final byte[] buf = new byte[2048];
private int ptr = 0;
private int buflen = 0;
public FastScanner(java.io.InputStream in) {
this.in = in;
}
public FastScanner() {
this(System.in);
}
private boolean hasNextByte() {
if (ptr < buflen)
return true;
ptr = 0;
try {
buflen = in.read(buf);
} catch (java.io.IOException e) {
throw new RuntimeException(e);
}
return buflen > 0;
}
private int readByte() {
return hasNextByte() ? buf[ptr++] : -1;
}
public boolean hasNext() {
while (hasNextByte() && !(32 < buf[ptr] && buf[ptr] < 127))
ptr++;
return hasNextByte();
}
private StringBuilder nextSequence() {
if (!hasNext())
throw new java.util.NoSuchElementException();
StringBuilder sb = new StringBuilder();
for (int b = readByte(); 32 < b && b < 127; b = readByte()) {
sb.appendCodePoint(b);
}
return sb;
}
public String next() {
return nextSequence().toString();
}
public String next(int len) {
return new String(nextChars(len));
}
public char nextChar() {
if (!hasNextByte())
throw new java.util.NoSuchElementException();
return (char) readByte();
}
public char[] nextChars() {
StringBuilder sb = nextSequence();
int l = sb.length();
char[] dst = new char[l];
sb.getChars(0, l, dst, 0);
return dst;
}
public char[] nextChars(int len) {
if (!hasNext())
throw new java.util.NoSuchElementException();
char[] s = new char[len];
int i = 0;
int b = readByte();
while (32 < b && b < 127 && i < len) {
s[i++] = (char) b;
b = readByte();
}
if (i != len) {
throw new java.util.NoSuchElementException(
String.format("Next token has smaller length than expected.", len));
}
return s;
}
public long nextLong() {
if (!hasNext())
throw new java.util.NoSuchElementException();
long n = 0;
boolean minus = false;
int b = readByte();
if (b == '-') {
minus = true;
b = readByte();
}
if (b < '0' || '9' < b)
throw new NumberFormatException();
while (true) {
if ('0' <= b && b <= '9') {
n = n * 10 + b - '0';
} else if (b == -1 || !(32 < b && b < 127)) {
return minus ? -n : n;
} else
throw new NumberFormatException();
b = readByte();
}
}
public int nextInt() {
return Math.toIntExact(nextLong());
}
public double nextDouble() {
return Double.parseDouble(next());
}
public void close() {
try {
in.close();
} catch (java.io.IOException e) {
throw new RuntimeException(e);
}
}
}
/**
* @verified https://atcoder.jp/contests/practice2/tasks/practice2_j
*/
class SegTree<S> {
final int MAX;
final int N;
final java.util.function.BinaryOperator<S> op;
final S E;
final S[] data;
@SuppressWarnings("unchecked")
public SegTree(int n, java.util.function.BinaryOperator<S> op, S e) {
this.MAX = n;
int k = 1;
while (k < n)
k <<= 1;
this.N = k;
this.E = e;
this.op = op;
this.data = (S[]) new Object[N << 1];
java.util.Arrays.fill(data, E);
}
public SegTree(S[] dat, java.util.function.BinaryOperator<S> op, S e) {
this(dat.length, op, e);
build(dat);
}
private void build(S[] dat) {
int l = dat.length;
System.arraycopy(dat, 0, data, N, l);
for (int i = N - 1; i > 0; i--) {
data[i] = op.apply(data[i << 1 | 0], data[i << 1 | 1]);
}
}
public void set(int p, S x) {
exclusiveRangeCheck(p);
data[p += N] = x;
p >>= 1;
while (p > 0) {
data[p] = op.apply(data[p << 1 | 0], data[p << 1 | 1]);
p >>= 1;
}
}
public S get(int p) {
exclusiveRangeCheck(p);
return data[p + N];
}
public S prod(int l, int r) {
if (l > r) {
throw new IllegalArgumentException(String.format("Invalid range: [%d, %d)", l, r));
}
inclusiveRangeCheck(l);
inclusiveRangeCheck(r);
S sumLeft = E;
S sumRight = E;
l += N;
r += N;
while (l < r) {
if ((l & 1) == 1)
sumLeft = op.apply(sumLeft, data[l++]);
if ((r & 1) == 1)
sumRight = op.apply(data[--r], sumRight);
l >>= 1;
r >>= 1;
}
return op.apply(sumLeft, sumRight);
}
public S allProd() {
return data[1];
}
public int maxRight(int l, java.util.function.Predicate<S> f) {
inclusiveRangeCheck(l);
if (!f.test(E)) {
throw new IllegalArgumentException("Identity element must satisfy the condition.");
}
if (l == MAX)
return MAX;
l += N;
S sum = E;
do {
l >>= Long.numberOfTrailingZeros(l);
if (!f.test(op.apply(sum, data[l]))) {
while (l < N) {
l = l << 1;
if (f.test(op.apply(sum, data[l]))) {
sum = op.apply(sum, data[l]);
l++;
}
}
return l - N;
}
sum = op.apply(sum, data[l]);
l++;
} while ((l & -l) != l);
return MAX;
}
public int minLeft(int r, java.util.function.Predicate<S> f) {
inclusiveRangeCheck(r);
if (!f.test(E)) {
throw new IllegalArgumentException("Identity element must satisfy the condition.");
}
if (r == 0)
return 0;
r += N;
S sum = E;
do {
r--;
while (r > 1 && (r & 1) == 1)
r >>= 1;
if (!f.test(op.apply(data[r], sum))) {
while (r < N) {
r = r << 1 | 1;
if (f.test(op.apply(data[r], sum))) {
sum = op.apply(data[r], sum);
r--;
}
}
return r + 1 - N;
}
sum = op.apply(data[r], sum);
} while ((r & -r) != r);
return 0;
}
private void exclusiveRangeCheck(int p) {
if (p < 0 || p >= MAX) {
throw new IndexOutOfBoundsException(
String.format("Index %d out of bounds for the range [%d, %d).", p, 0, MAX));
}
}
private void inclusiveRangeCheck(int p) {
if (p < 0 || p > MAX) {
throw new IndexOutOfBoundsException(
String.format("Index %d out of bounds for the range [%d, %d].", p, 0, MAX));
}
}
// **************** DEBUG **************** //
private int indent = 6;
public void setIndent(int newIndent) {
this.indent = newIndent;
}
@Override
public String toString() {
return toSimpleString();
}
public String toDetailedString() {
return toDetailedString(1, 0);
}
private String toDetailedString(int k, int sp) {
if (k >= N)
return indent(sp) + data[k];
String s = "";
s += toDetailedString(k << 1 | 1, sp + indent);
s += "\n";
s += indent(sp) + data[k];
s += "\n";
s += toDetailedString(k << 1 | 0, sp + indent);
return s;
}
private static String indent(int n) {
StringBuilder sb = new StringBuilder();
while (n-- > 0)
sb.append(' ');
return sb.toString();
}
public String toSimpleString() {
StringBuilder sb = new StringBuilder();
sb.append('[');
for (int i = 0; i < N; i++) {
sb.append(data[i + N]);
if (i < N - 1)
sb.append(',').append(' ');
}
sb.append(']');
return sb.toString();
}
}
class DSU {
private int n;
private int[] parentOrSize;
public DSU(int n) {
this.n = n;
this.parentOrSize = new int[n];
Arrays.fill(parentOrSize, -1);
}
int merge(int a, int b) {
if (!(0 <= a && a < n) || !(0 <= b && b < n)) {
return -1;
}
int x = leader(a);
int y = leader(b);
if (x == y)
return x;
if (-parentOrSize[x] < -parentOrSize[y]) {
int tmp = x;
x = y;
y = tmp;
}
parentOrSize[x] += parentOrSize[y];
parentOrSize[y] = x;
return x;
}
boolean same(int a, int b) {
if (!(0 <= a && a < n) || !(0 <= b && b < n)) {
return false;
}
return leader(a) == leader(b);
}
int leader(int a) {
if (parentOrSize[a] < 0) {
return a;
} else {
parentOrSize[a] = leader(parentOrSize[a]);
return parentOrSize[a];
}
}
int size(int a) {
if (!(0 <= a && a < n)) {
return -1;
}
return -parentOrSize[leader(a)];
}
ArrayList<ArrayList<Integer>> groups() {
int[] leaderBuf = new int[n];
int[] groupSize = new int[n];
for (int i = 0; i < n; i++) {
leaderBuf[i] = leader(i);
groupSize[leaderBuf[i]]++;
}
ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
for (int i = 0; i < n; i++) {
result.add(new ArrayList<>());
}
for (int i = 0; i < n; i++) {
result.get(leaderBuf[i]).add(i);
}
return result;
}
}
|
Java
|
["5\n\n2\n\n4\n\n6\n\n8\n\n60"]
|
2 seconds
|
["1 0 1\n3 2 1\n12 7 1\n42 27 1\n341102826 248150916 1"]
|
NoteIn the first test case, Alex wins if he receives the card $$$2$$$ (he plays it, and Boris cannot respond). If Alex receives the card $$$1$$$, the game ends in a draw.In the second test case: Alex wins if he receives the cards $$$[3, 4]$$$, $$$[2, 4]$$$ or $$$[1, 4]$$$; Boris wins if Alex receives the cards $$$[1, 2]$$$ or $$$[1, 3]$$$; the game ends in a draw if Alex receives the cards $$$[2, 3]$$$.
|
Java 11
|
standard input
|
[
"combinatorics",
"constructive algorithms",
"dp",
"games"
] |
63fc2fb08d248f8ccdb3f5364c96dac1
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 30$$$) — the number of test cases. Then, $$$t$$$ lines follow. The $$$i$$$-th line contains one even integer $$$n$$$ ($$$2 \le n \le 60$$$).
| 1,500
|
For each test case, print three integers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. Since the answers can be large, print them modulo $$$998244353$$$.
|
standard output
| |
PASSED
|
131eb5c51133b5ff39d4b38ae1ddd036
|
train_109.jsonl
|
1664462100
|
Consider a game with $$$n$$$ cards ($$$n$$$ is even). Each card has a number written on it, between $$$1$$$ and $$$n$$$. All numbers on the cards are different. We say that a card with number $$$x$$$ is stronger than a card with number $$$y$$$ if $$$x > y$$$.Two players, Alex and Boris, play this game. In the beginning, each of them receives exactly $$$\frac{n}{2}$$$ cards, so each card belongs to exactly one player. Then, they take turns. Alex goes first, then Boris, then Alex again, and so on.On a player's turn, he must play exactly one of his cards. Then, if the opponent doesn't have any cards stronger than the card played, the opponent loses, and the game ends. Otherwise, the opponent has to play a stronger card (exactly one card as well). These two cards are removed from the game, and the turn ends. If there are no cards left, the game ends in a draw; otherwise it's the opponent's turn.Consider all possible ways to distribute the cards between two players, so that each of them receives exactly half of the cards. You have to calculate three numbers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. You may assume that both players play optimally (i. e. if a player can win no matter how his opponent plays, he wins). Two ways to distribute the cards are different if there is at least one card such that, in one of these ways, it is given to Alex, and in the other way, it is given to Boris.For example, suppose $$$n = 4$$$, Alex receives the cards $$$[2, 3]$$$, and Boris receives the cards $$$[1, 4]$$$. Then the game may go as follows: if Alex plays the card $$$2$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$3$$$. So, the game ends in a draw; if Alex plays the card $$$3$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$2$$$. So, the game ends in a draw. So, in this case, the game ends in a draw.
|
512 megabytes
|
import java.io.*;
import java.util.*;
public class C {
static final int INF = 998244353;
public static void main(String[] args) throws IOException {
StringBuilder sb = new StringBuilder();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine());
int[] a = new int[61], b = new int[61];
a[2] = 1;
b[2] = 0;
a[4] = 3;
b[4] = 2;
int[][] comb = new int[61][61];
for (int i = 0; i < 61; i++) {
comb[i][0] = 1;
}
for (int i = 1; i < 61; i++) {
for (int j = 1; j <= i; j++) {
comb[i][j] = comb[i-1][j] + comb[i-1][j-1];
comb[i][j] %= INF;
}
}
for (int i = 6; i <= 60; i += 2) {
a[i] = comb[i-1][i/2] + b[i-2];
a[i] %= INF;
b[i] = comb[i][i/2] - a[i] - 1 + INF;
b[i] %= INF;
}
tc: while (t-- > 0) {
int n = Integer.parseInt(br.readLine());
sb.append(a[n] + " " + b[n] + " " + "1\n");
}
System.out.println(sb.toString());
}
}
|
Java
|
["5\n\n2\n\n4\n\n6\n\n8\n\n60"]
|
2 seconds
|
["1 0 1\n3 2 1\n12 7 1\n42 27 1\n341102826 248150916 1"]
|
NoteIn the first test case, Alex wins if he receives the card $$$2$$$ (he plays it, and Boris cannot respond). If Alex receives the card $$$1$$$, the game ends in a draw.In the second test case: Alex wins if he receives the cards $$$[3, 4]$$$, $$$[2, 4]$$$ or $$$[1, 4]$$$; Boris wins if Alex receives the cards $$$[1, 2]$$$ or $$$[1, 3]$$$; the game ends in a draw if Alex receives the cards $$$[2, 3]$$$.
|
Java 11
|
standard input
|
[
"combinatorics",
"constructive algorithms",
"dp",
"games"
] |
63fc2fb08d248f8ccdb3f5364c96dac1
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 30$$$) — the number of test cases. Then, $$$t$$$ lines follow. The $$$i$$$-th line contains one even integer $$$n$$$ ($$$2 \le n \le 60$$$).
| 1,500
|
For each test case, print three integers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. Since the answers can be large, print them modulo $$$998244353$$$.
|
standard output
| |
PASSED
|
0d8cfedde49a0c4f36c20da0652205bc
|
train_109.jsonl
|
1664462100
|
Consider a game with $$$n$$$ cards ($$$n$$$ is even). Each card has a number written on it, between $$$1$$$ and $$$n$$$. All numbers on the cards are different. We say that a card with number $$$x$$$ is stronger than a card with number $$$y$$$ if $$$x > y$$$.Two players, Alex and Boris, play this game. In the beginning, each of them receives exactly $$$\frac{n}{2}$$$ cards, so each card belongs to exactly one player. Then, they take turns. Alex goes first, then Boris, then Alex again, and so on.On a player's turn, he must play exactly one of his cards. Then, if the opponent doesn't have any cards stronger than the card played, the opponent loses, and the game ends. Otherwise, the opponent has to play a stronger card (exactly one card as well). These two cards are removed from the game, and the turn ends. If there are no cards left, the game ends in a draw; otherwise it's the opponent's turn.Consider all possible ways to distribute the cards between two players, so that each of them receives exactly half of the cards. You have to calculate three numbers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. You may assume that both players play optimally (i. e. if a player can win no matter how his opponent plays, he wins). Two ways to distribute the cards are different if there is at least one card such that, in one of these ways, it is given to Alex, and in the other way, it is given to Boris.For example, suppose $$$n = 4$$$, Alex receives the cards $$$[2, 3]$$$, and Boris receives the cards $$$[1, 4]$$$. Then the game may go as follows: if Alex plays the card $$$2$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$3$$$. So, the game ends in a draw; if Alex plays the card $$$3$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$2$$$. So, the game ends in a draw. So, in this case, the game ends in a draw.
|
512 megabytes
|
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Deque;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Map.Entry;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Random;
import java.util.Scanner;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.TreeSet;
public class Main {
public static void main(String[]args) throws IOException {
new Main().run();
}
File input=new File("D:\\test\\input.txt");
void run() throws IOException{
// new solve().setIO(input, System.out).run();
new solve().setIO(System.in,System.out).run();
}
class solve extends ioTask{
// StreamTokenizer st = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
// int in() throws IOException {
// st.nextToken();
// return (int)st.nval;
// }
// long inl() throws IOException {
// st.nextToken();
// return (long)st.nval;
// }
// double ind()throws IOException {
// st.nextToken();
// return st.nval;
// }
// String ins() throws IOException {
// st.nextToken();
// return st.sval;
// }
int t,n,c,m,i,j,mx,k,tmp,u,v,w,l,r;
long[]a=new long[31];
long[]b=new long[31];
long[]all=new long[31];
long []inv=new long[31];
long p=998244353;
long c(long n,long m) {
long ans=1;
for(int i=1;i<=m;i++) {
ans=((ans*n--)%p*inv[i])%p;
}
return ans;
}
void run()throws IOException {
inv[1]=1;
for(i=2;i<=30;i++) {
inv[i]=(p-p/i)*inv[(int)(p%i)]%p;
}
for(i=1;i<=30;i++) {
all[i]=c(i<<1,i);
}
a[1]=1;
for(i=2;i<=30;i++) {
a[i]=((all[i]*inv[2]%p+b[i-1])%p+p)%p;
b[i]=((all[i]-1-a[i])%p+p)%p;
}
t=in.in();
while(t-->0) {
n=in.in()>>1;
out.println(a[n]+" "+b[n]+" 1");
}
out.close();
}
}
class Graph{
int[]to,nxt,head;
// int[]w;
int cnt;
private int st=1;
void init(int n) {
cnt=st;
Arrays.fill(head, 1,n+1,0);
}
public Graph(){}
public Graph(int n,int m) {
to=new int[m+1];
nxt=new int[m+1];
head=new int[n+1];
// w=new int[m+1];
cnt=st;
}
void add(int u,int v) {
to[cnt]=v;
nxt[cnt]=head[u];
// w[cnt]=l;
head[u]=cnt++;
}
Graph copy() {
Graph g=new Graph();
g.to=to.clone();
g.head=head.clone();
g.nxt=nxt.clone();
// g.w=w.clone();
g.cnt=cnt;
return g;
}
void show(int n){
for(int u=1,v;u<=n;u++){
for(int j=head[u];j>0;j=nxt[j]){
v=to[j];
if(u<v) {
System.out.printf("%-4d%-4d%-4d\n",u,v
// ,w[j]
);
}
}
}
}
}
class In{
private StringTokenizer in=new StringTokenizer("");
private InputStream is;
private BufferedReader bf;
public In(File file) throws IOException {
is=new FileInputStream(file);
init();
}
public In(InputStream is) throws IOException
{
this.is=is;
init();
}
private void init() throws IOException {
bf=new BufferedReader(new InputStreamReader(is));
}
boolean hasNext() throws IOException {
return in.hasMoreTokens()||bf.ready();
}
String ins() throws IOException {
while(!in.hasMoreTokens()) {
in=new StringTokenizer(bf.readLine());
}
return in.nextToken();
}
int in() throws IOException {
return Integer.parseInt(ins());
}
long inl() throws IOException {
return Long.parseLong(ins());
}
double ind() throws IOException {
return Double.parseDouble(ins());
}
String line() throws IOException {
return bf.readLine();
}
}
class Out{
PrintWriter out;
private OutputStream os;
private void init() {
out=new PrintWriter(new BufferedWriter(new OutputStreamWriter(os)));
}
public Out(File file) throws IOException {
os=new FileOutputStream(file);
init();
}
public Out(OutputStream os) throws IOException
{
this.os=os;
init();
}
}
abstract class ioTask{
In in;
PrintWriter out;
public ioTask setIO(File in,File out) throws IOException{
this.in=new In(in);
this.out=new Out(out).out;
return this;
}
public ioTask setIO(File in,OutputStream out) throws IOException{
this.in=new In(in);
this.out=new Out(out).out;
return this;
}
public ioTask setIO(InputStream in,OutputStream out) throws IOException{
this.in=new In(in);
this.out=new Out(out).out;
return this;
}
public ioTask setIO(InputStream in,File out) throws IOException{
this.in=new In(in);
this.out=new Out(out).out;
return this;
}
void run()throws IOException{
}
}
}
|
Java
|
["5\n\n2\n\n4\n\n6\n\n8\n\n60"]
|
2 seconds
|
["1 0 1\n3 2 1\n12 7 1\n42 27 1\n341102826 248150916 1"]
|
NoteIn the first test case, Alex wins if he receives the card $$$2$$$ (he plays it, and Boris cannot respond). If Alex receives the card $$$1$$$, the game ends in a draw.In the second test case: Alex wins if he receives the cards $$$[3, 4]$$$, $$$[2, 4]$$$ or $$$[1, 4]$$$; Boris wins if Alex receives the cards $$$[1, 2]$$$ or $$$[1, 3]$$$; the game ends in a draw if Alex receives the cards $$$[2, 3]$$$.
|
Java 11
|
standard input
|
[
"combinatorics",
"constructive algorithms",
"dp",
"games"
] |
63fc2fb08d248f8ccdb3f5364c96dac1
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 30$$$) — the number of test cases. Then, $$$t$$$ lines follow. The $$$i$$$-th line contains one even integer $$$n$$$ ($$$2 \le n \le 60$$$).
| 1,500
|
For each test case, print three integers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. Since the answers can be large, print them modulo $$$998244353$$$.
|
standard output
| |
PASSED
|
24a1699e11cf9685f6313dd63575785e
|
train_109.jsonl
|
1664462100
|
Consider a game with $$$n$$$ cards ($$$n$$$ is even). Each card has a number written on it, between $$$1$$$ and $$$n$$$. All numbers on the cards are different. We say that a card with number $$$x$$$ is stronger than a card with number $$$y$$$ if $$$x > y$$$.Two players, Alex and Boris, play this game. In the beginning, each of them receives exactly $$$\frac{n}{2}$$$ cards, so each card belongs to exactly one player. Then, they take turns. Alex goes first, then Boris, then Alex again, and so on.On a player's turn, he must play exactly one of his cards. Then, if the opponent doesn't have any cards stronger than the card played, the opponent loses, and the game ends. Otherwise, the opponent has to play a stronger card (exactly one card as well). These two cards are removed from the game, and the turn ends. If there are no cards left, the game ends in a draw; otherwise it's the opponent's turn.Consider all possible ways to distribute the cards between two players, so that each of them receives exactly half of the cards. You have to calculate three numbers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. You may assume that both players play optimally (i. e. if a player can win no matter how his opponent plays, he wins). Two ways to distribute the cards are different if there is at least one card such that, in one of these ways, it is given to Alex, and in the other way, it is given to Boris.For example, suppose $$$n = 4$$$, Alex receives the cards $$$[2, 3]$$$, and Boris receives the cards $$$[1, 4]$$$. Then the game may go as follows: if Alex plays the card $$$2$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$3$$$. So, the game ends in a draw; if Alex plays the card $$$3$$$, then Boris has to respond with the card $$$4$$$. Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is $$$1$$$; he plays it, and Alex responds with the card $$$2$$$. So, the game ends in a draw. So, in this case, the game ends in a draw.
|
512 megabytes
|
import java.util.*;
import java.io.*;
public class C {
static Scanner sc = new Scanner(System.in);
static PrintWriter pw = new PrintWriter(System.out);
static long binomialCoeff(int n, int k) {
if (n == 0 || k < 0) {
return 0;
}
long C[] = new long[k + 1];
// nC0 is 1
C[0] = 1;
for (int i = 1; i <= n; i++) {
// Compute next row of pascal
// triangle using the previous row
for (int j = Math.min(i, k); j > 0; j--)
C[j] = C[j] + C[j - 1];
}
return C[k];
}
static void solve() {
int n = sc.nextInt();
long MOD = 998244353;
//A wins
long A = 0;
int passA = 0;
for (int i = 0; i < n; i ++) {
if (i % 4 == 0 || i % 4 == 3) {
A += binomialCoeff(n - i - 1, n / 2 - passA - 1) % MOD;
} else {
passA ++;
}
}
A %= MOD;
long B = 0;
int passB = 0;
for (int i = 0; i < n; i ++) {
if (i % 4 == 1 || i % 4 == 2) {
B += binomialCoeff(n - i - 1, n / 2 - passB - 1) % MOD;
} else {
passB ++;
}
}
B %= MOD;
pw.println(String.valueOf(A) + " " + String.valueOf(B) + " 1");
}
public static void main(String[] args){
int t = sc.nextInt();
for (int i = 0; i < t; i ++){
solve();
}
// solve();
pw.close();
}
}
|
Java
|
["5\n\n2\n\n4\n\n6\n\n8\n\n60"]
|
2 seconds
|
["1 0 1\n3 2 1\n12 7 1\n42 27 1\n341102826 248150916 1"]
|
NoteIn the first test case, Alex wins if he receives the card $$$2$$$ (he plays it, and Boris cannot respond). If Alex receives the card $$$1$$$, the game ends in a draw.In the second test case: Alex wins if he receives the cards $$$[3, 4]$$$, $$$[2, 4]$$$ or $$$[1, 4]$$$; Boris wins if Alex receives the cards $$$[1, 2]$$$ or $$$[1, 3]$$$; the game ends in a draw if Alex receives the cards $$$[2, 3]$$$.
|
Java 11
|
standard input
|
[
"combinatorics",
"constructive algorithms",
"dp",
"games"
] |
63fc2fb08d248f8ccdb3f5364c96dac1
|
The first line contains one integer $$$t$$$ ($$$1 \le t \le 30$$$) — the number of test cases. Then, $$$t$$$ lines follow. The $$$i$$$-th line contains one even integer $$$n$$$ ($$$2 \le n \le 60$$$).
| 1,500
|
For each test case, print three integers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. Since the answers can be large, print them modulo $$$998244353$$$.
|
standard output
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.