Search is not available for this dataset
name
stringlengths
2
112
description
stringlengths
29
13k
source
int64
1
7
difficulty
int64
0
25
solution
stringlengths
7
983k
language
stringclasses
4 values
1173_E1. Nauuo and Pictures (easy version)
The only difference between easy and hard versions is constraints. Nauuo is a girl who loves random picture websites. One day she made a random picture website by herself which includes n pictures. When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The i-th picture has a non-negative weight w_i, and the probability of the i-th picture being displayed is \frac{w_i}{βˆ‘_{j=1}^nw_j}. That is to say, the probability of a picture to be displayed is proportional to its weight. However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add 1 to its weight; otherwise, she would subtract 1 from its weight. Nauuo will visit the website m times. She wants to know the expected weight of each picture after all the m visits modulo 998244353. Can you help her? The expected weight of the i-th picture can be denoted by \frac {q_i} {p_i} where \gcd(p_i,q_i)=1, you need to print an integer r_i satisfying 0≀ r_i<998244353 and r_iβ‹… p_i≑ q_i\pmod{998244353}. It can be proved that such r_i exists and is unique. Input The first line contains two integers n and m (1≀ n≀ 50, 1≀ m≀ 50) β€” the number of pictures and the number of visits to the website. The second line contains n integers a_1,a_2,…,a_n (a_i is either 0 or 1) β€” if a_i=0 , Nauuo does not like the i-th picture; otherwise Nauuo likes the i-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains n integers w_1,w_2,…,w_n (1≀ w_i≀50) β€” the initial weights of the pictures. Output The output contains n integers r_1,r_2,…,r_n β€” the expected weights modulo 998244353. Examples Input 2 1 0 1 2 1 Output 332748119 332748119 Input 1 2 1 1 Output 3 Input 3 3 0 1 1 4 3 5 Output 160955686 185138929 974061117 Note In the first example, if the only visit shows the first picture with a probability of \frac 2 3, the final weights are (1,1); if the only visit shows the second picture with a probability of \frac1 3, the final weights are (2,2). So, both expected weights are \frac2 3β‹… 1+\frac 1 3β‹… 2=\frac4 3 . Because 332748119β‹… 3≑ 4\pmod{998244353}, you need to print 332748119 instead of \frac4 3 or 1.3333333333. In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, w_1 will be increased by 1. So, the expected weight is 1+2=3. Nauuo is very naughty so she didn't give you any hint of the third example.
2
11
import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.PrintWriter; import java.util.StringTokenizer; public class Main { static class Task { int NN = 200005; int MOD = 1000000007; int INF = 2000000000; long INFINITY = 2000000000000000000L; long cur; long sumLike=0, sumHate=0; int n, m; long [][][][] dp; long expo(long x, long y) { long ret = 1, sq =x; while(y>0) { if(y%2!=0) { ret = (ret * sq) % 998244353; } sq = (sq * sq) % 998244353; y >>= 1; } return ret; } long invMod(long p, long q) { return (p * expo(q, 998244353 - 2))%998244353; } long rec(int c1, int c2, int c, int flag) { int mul = flag==0?-1:1; if(dp[c1][c2][c][flag] != -1) return dp[c1][c2][c][flag]; if(c==m) { return dp[c1][c2][c][flag] = cur + mul*c1; } long p = (cur + mul*c1); long q; if(mul == 1) q = (sumLike + c2 + sumHate - (c-c2)); else q = (sumHate - c2 + sumLike + (c-c2)); long ret = (rec(c1+1, c2+1, c+1, flag)*invMod(p, q))%998244353; if(mul == 1) p = (sumLike + c2) - (cur + c1); else p = (sumHate - c2) - (cur - c1); ret = (ret + rec(c1, c2+1, c+1, flag)*invMod(p, q))%998244353; if(mul == 1) p = (sumHate - (c-c2)); else p = sumLike + (c-c2); ret = (ret + rec(c1, c2, c+1, flag)*invMod(p, q))%998244353; return dp[c1][c2][c][flag] = ret; } public void solve(InputReader in, PrintWriter out) { n = in.nextInt(); m = in.nextInt(); int [] a = new int[n]; for(int i=0;i<n;++i) a[i] = in.nextInt(); int [] w = new int[n]; for(int i=0;i<n;++i) { w[i] = in.nextInt(); if(a[i] == 0) { sumHate += w[i]; } else { sumLike += w[i]; } } dp = new long[m+1][m+1][m+1][2]; for(int i=0;i<n;++i) { cur=w[i]; for(int c1=0;c1<=m;++c1) { for(int c2=0;c2<=m;++c2) { for(int c=0;c<=m;++c) { dp[c1][c2][c][0] = dp[c1][c2][c][1] = -1; } } } out.println(rec(0, 0, 0, a[i])); } } } static void prepareIO(boolean isFileIO) { //long t1 = System.currentTimeMillis(); Task solver = new Task(); // Standard IO if(!isFileIO) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); solver.solve(in, out); //out.println("time(s): " + (1.0*(System.currentTimeMillis()-t1))/1000.0); out.close(); } // File IO else { String IPfilePath = System.getProperty("user.home") + "/Downloads/ip.in"; String OPfilePath = System.getProperty("user.home") + "/Downloads/op.out"; InputReader fin = new InputReader(IPfilePath); PrintWriter fout = null; try { fout = new PrintWriter(new File(OPfilePath)); } catch (FileNotFoundException e) { e.printStackTrace(); } solver.solve(fin, fout); //fout.println("time(s): " + (1.0*(System.currentTimeMillis()-t1))/1000.0); fout.close(); } } public static void main(String[] args) { prepareIO(false); } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public InputReader(String filePath) { File file = new File(filePath); try { reader = new BufferedReader(new FileReader(file)); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } tokenizer = null; } public String nextLine() { String str = ""; try { str = reader.readLine(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return str; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } } }
JAVA
1173_E1. Nauuo and Pictures (easy version)
The only difference between easy and hard versions is constraints. Nauuo is a girl who loves random picture websites. One day she made a random picture website by herself which includes n pictures. When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The i-th picture has a non-negative weight w_i, and the probability of the i-th picture being displayed is \frac{w_i}{βˆ‘_{j=1}^nw_j}. That is to say, the probability of a picture to be displayed is proportional to its weight. However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add 1 to its weight; otherwise, she would subtract 1 from its weight. Nauuo will visit the website m times. She wants to know the expected weight of each picture after all the m visits modulo 998244353. Can you help her? The expected weight of the i-th picture can be denoted by \frac {q_i} {p_i} where \gcd(p_i,q_i)=1, you need to print an integer r_i satisfying 0≀ r_i<998244353 and r_iβ‹… p_i≑ q_i\pmod{998244353}. It can be proved that such r_i exists and is unique. Input The first line contains two integers n and m (1≀ n≀ 50, 1≀ m≀ 50) β€” the number of pictures and the number of visits to the website. The second line contains n integers a_1,a_2,…,a_n (a_i is either 0 or 1) β€” if a_i=0 , Nauuo does not like the i-th picture; otherwise Nauuo likes the i-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains n integers w_1,w_2,…,w_n (1≀ w_i≀50) β€” the initial weights of the pictures. Output The output contains n integers r_1,r_2,…,r_n β€” the expected weights modulo 998244353. Examples Input 2 1 0 1 2 1 Output 332748119 332748119 Input 1 2 1 1 Output 3 Input 3 3 0 1 1 4 3 5 Output 160955686 185138929 974061117 Note In the first example, if the only visit shows the first picture with a probability of \frac 2 3, the final weights are (1,1); if the only visit shows the second picture with a probability of \frac1 3, the final weights are (2,2). So, both expected weights are \frac2 3β‹… 1+\frac 1 3β‹… 2=\frac4 3 . Because 332748119β‹… 3≑ 4\pmod{998244353}, you need to print 332748119 instead of \frac4 3 or 1.3333333333. In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, w_1 will be increased by 1. So, the expected weight is 1+2=3. Nauuo is very naughty so she didn't give you any hint of the third example.
2
11
#include <bits/stdc++.h> using namespace std; void READ(vector<long long> &v, long long n) { long long a; for (long long i = 0; i < n; i = i + 1) { cin >> a; v.push_back(a); } } void PRINT(vector<long long> &v) { long long a; for (long long i = 0; i < v.size(); i = i + 1) { cout << v[i] << " "; } cout << "\n"; } long long power(long long k, long long n, long long m = 998244353) { long long res = 1; while (n) { if (n % 2 != 0) { res = (res * k) % m; } k = (k * k) % m; n = n / 2; } return (res); } long long fact(long long n, long long m = 998244353) { long long a = 1; if (n == 0 || n == 1) { return (1); } while (n > 0) { a = (a % m * n % m) % m; n--; } return (a); } long long n, m, k; long long good, bad; long long dp[3003][3003]; long long solve(long long i, long long j) { if (i == 0 && j == 0) { return (1); } if (i < 0 || j < 0) { return (0); } if (dp[i][j] != -1) { return (dp[i][j]); } dp[i][j] = ((solve(i - 1, j) % 998244353 * (good + i - 1) % 998244353 * power(good + bad + i - 1 - j, 998244353 - 2) % 998244353) % 998244353 + (solve(i, j - 1) % 998244353 * (bad - j + 1) % 998244353 * power(good + bad + i - j + 1, 998244353 - 2) % 998244353)) % 998244353; return (dp[i][j]); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); std::cout.unsetf(std::ios::floatfield); std::cout.precision(6); long long a, b, c, d, e, x, i, j, t, y, w; double p, q, r, u; char ch, chr; cin >> n >> m; vector<long long> v; memset(dp, -1, sizeof(dp)); for (i = 0; i < n; i = i + 1) { cin >> a; v.push_back(a); } good = bad = 0; vector<long long> v1; x = 0; y = 0; for (i = 0; i < n; i = i + 1) { cin >> a; if (v[i]) { good += a; } else { bad += a; } v1.push_back(a); } x = y = 0; for (i = 0; i <= m; i = i + 1) { x = (x % 998244353 + (solve(i, m - i) % 998244353 * i % 998244353) % 998244353) % 998244353; y = (y % 998244353 + 998244353 - (solve(i, m - i) % 998244353 * (m - i) % 998244353) % 998244353) % 998244353; } a = power(good, 998244353 - 2); b = power(bad, 998244353 - 2); for (i = 0; i < n; i = i + 1) { if (v[i]) { cout << (v1[i] + x % 998244353 * v1[i] % 998244353 * a % 998244353) % 998244353 << "\n"; } else { cout << (v1[i] + (y % 998244353 * v1[i] % 998244353 * b % 998244353) % 998244353) % 998244353 << "\n"; } } }
CPP
1173_E1. Nauuo and Pictures (easy version)
The only difference between easy and hard versions is constraints. Nauuo is a girl who loves random picture websites. One day she made a random picture website by herself which includes n pictures. When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The i-th picture has a non-negative weight w_i, and the probability of the i-th picture being displayed is \frac{w_i}{βˆ‘_{j=1}^nw_j}. That is to say, the probability of a picture to be displayed is proportional to its weight. However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add 1 to its weight; otherwise, she would subtract 1 from its weight. Nauuo will visit the website m times. She wants to know the expected weight of each picture after all the m visits modulo 998244353. Can you help her? The expected weight of the i-th picture can be denoted by \frac {q_i} {p_i} where \gcd(p_i,q_i)=1, you need to print an integer r_i satisfying 0≀ r_i<998244353 and r_iβ‹… p_i≑ q_i\pmod{998244353}. It can be proved that such r_i exists and is unique. Input The first line contains two integers n and m (1≀ n≀ 50, 1≀ m≀ 50) β€” the number of pictures and the number of visits to the website. The second line contains n integers a_1,a_2,…,a_n (a_i is either 0 or 1) β€” if a_i=0 , Nauuo does not like the i-th picture; otherwise Nauuo likes the i-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains n integers w_1,w_2,…,w_n (1≀ w_i≀50) β€” the initial weights of the pictures. Output The output contains n integers r_1,r_2,…,r_n β€” the expected weights modulo 998244353. Examples Input 2 1 0 1 2 1 Output 332748119 332748119 Input 1 2 1 1 Output 3 Input 3 3 0 1 1 4 3 5 Output 160955686 185138929 974061117 Note In the first example, if the only visit shows the first picture with a probability of \frac 2 3, the final weights are (1,1); if the only visit shows the second picture with a probability of \frac1 3, the final weights are (2,2). So, both expected weights are \frac2 3β‹… 1+\frac 1 3β‹… 2=\frac4 3 . Because 332748119β‹… 3≑ 4\pmod{998244353}, you need to print 332748119 instead of \frac4 3 or 1.3333333333. In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, w_1 will be increased by 1. So, the expected weight is 1+2=3. Nauuo is very naughty so she didn't give you any hint of the third example.
2
11
#include <bits/stdc++.h> using namespace std; constexpr int MOD = 998244353; int n, m, a[200001], w[200001], dp[3001][3001], sum[2], inv[10001], ans[2]; int ksm(int a, int b) { int s = 1; while (b) { if (b & 1) { s = s * static_cast<long long>(a) % MOD; } a = a * static_cast<long long>(a) % MOD; b >>= 1; } return s; } int main(int argc, char const *argv[]) { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> m; for (int i = 1; i <= n; i++) { cin >> a[i]; } for (int i = 1; i <= n; i++) { cin >> w[i]; sum[a[i]] += w[i]; } for (int i = -m; i <= m; i++) { inv[i + m] = ksm((sum[0] + sum[1] + i + MOD) % MOD, MOD - 2); } dp[0][0] = 1; for (int i = 0; i <= m; i++) { for (int j = 0; j <= m; j++) { dp[i][j + 1] = (dp[i][j + 1] + dp[i][j] * static_cast<long long>(sum[0] - j) % MOD * inv[i - j + m] % MOD) % MOD; dp[i + 1][j] = (dp[i + 1][j] + dp[i][j] * static_cast<long long>(sum[1] + i) % MOD * inv[i - j + m] % MOD) % MOD; } } for (int i = 0; i <= m; i++) { ans[0] = (ans[0] + dp[m - i][i] * static_cast<long long>(sum[0] - i) % MOD) % MOD; ans[1] = (ans[1] + dp[i][m - i] * static_cast<long long>(sum[1] + i) % MOD) % MOD; } ans[0] = ans[0] * static_cast<long long>(ksm(sum[0], MOD - 2)) % MOD; ans[1] = ans[1] * static_cast<long long>(ksm(sum[1], MOD - 2)) % MOD; for (int i = 1; i <= n; i++) { cout << w[i] * static_cast<long long>(ans[a[i]]) % MOD << '\n'; } return 0; }
CPP
1173_E1. Nauuo and Pictures (easy version)
The only difference between easy and hard versions is constraints. Nauuo is a girl who loves random picture websites. One day she made a random picture website by herself which includes n pictures. When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The i-th picture has a non-negative weight w_i, and the probability of the i-th picture being displayed is \frac{w_i}{βˆ‘_{j=1}^nw_j}. That is to say, the probability of a picture to be displayed is proportional to its weight. However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add 1 to its weight; otherwise, she would subtract 1 from its weight. Nauuo will visit the website m times. She wants to know the expected weight of each picture after all the m visits modulo 998244353. Can you help her? The expected weight of the i-th picture can be denoted by \frac {q_i} {p_i} where \gcd(p_i,q_i)=1, you need to print an integer r_i satisfying 0≀ r_i<998244353 and r_iβ‹… p_i≑ q_i\pmod{998244353}. It can be proved that such r_i exists and is unique. Input The first line contains two integers n and m (1≀ n≀ 50, 1≀ m≀ 50) β€” the number of pictures and the number of visits to the website. The second line contains n integers a_1,a_2,…,a_n (a_i is either 0 or 1) β€” if a_i=0 , Nauuo does not like the i-th picture; otherwise Nauuo likes the i-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains n integers w_1,w_2,…,w_n (1≀ w_i≀50) β€” the initial weights of the pictures. Output The output contains n integers r_1,r_2,…,r_n β€” the expected weights modulo 998244353. Examples Input 2 1 0 1 2 1 Output 332748119 332748119 Input 1 2 1 1 Output 3 Input 3 3 0 1 1 4 3 5 Output 160955686 185138929 974061117 Note In the first example, if the only visit shows the first picture with a probability of \frac 2 3, the final weights are (1,1); if the only visit shows the second picture with a probability of \frac1 3, the final weights are (2,2). So, both expected weights are \frac2 3β‹… 1+\frac 1 3β‹… 2=\frac4 3 . Because 332748119β‹… 3≑ 4\pmod{998244353}, you need to print 332748119 instead of \frac4 3 or 1.3333333333. In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, w_1 will be increased by 1. So, the expected weight is 1+2=3. Nauuo is very naughty so she didn't give you any hint of the third example.
2
11
#include <bits/stdc++.h> using namespace std; const long long mod = 998244353; int n, m; long long w[51], dp[51][51][51]; bool a[51]; long long mul(long long a, long long b) { return (a * b) % mod; } long long add(long long a, long long b) { return (a + b) % mod; } long long quickpow(long long a, long long b) { if (b < 0) return 0; long long ret = 1; a %= mod; while (b) { if (b & 1) ret = (ret * a) % mod; b >>= 1; a = (a * a) % mod; } return ret; } long long inv(long long a) { return quickpow(a, mod - 2); } long long solve(int x) { long long pos = 0, neg = 0; for (int i = 1; i <= n; i++) { if (i == x) continue; if (a[i]) pos += w[i]; else neg += w[i]; } memset(dp, 0, sizeof(dp)); dp[0][0][0] = 1; for (int i = 0; i <= m - 1; i++) { for (int j = 0; j <= 50; j++) { for (int k = 0; k <= 50; k++) { if (dp[i][j][k] == 0) continue; long long me; if (a[x]) me = w[x] + (i - j - k); else me = w[x] - (i - j - k); long long pos_w = pos + j; long long neg_w = neg - k; long long total_inv = inv(add(me, add(pos_w, neg_w))); dp[i + 1][j + 1][k] = add(dp[i + 1][j + 1][k], mul(dp[i][j][k], mul(pos_w, total_inv))); dp[i + 1][j][k + 1] = add(dp[i + 1][j][k + 1], mul(dp[i][j][k], mul(neg_w, total_inv))); dp[i + 1][j][k] = add(dp[i + 1][j][k], mul(dp[i][j][k], mul(me, total_inv))); } } } long long res = 0; for (int j = 0; j <= 50; j++) { for (int k = 0; k <= 50; k++) { if (dp[m][j][k] == 0) continue; long long me; if (a[x]) me = w[x] + (m - j - k); else me = w[x] - (m - j - k); res = add(res, mul(me, dp[m][j][k])); } } return res; } int main(void) { cin >> n >> m; for (int i = 1; i <= n; i++) cin >> a[i]; for (int i = 1; i <= n; i++) cin >> w[i]; for (int i = 1; i <= n; i++) { cout << solve(i) << endl; } }
CPP
1173_E1. Nauuo and Pictures (easy version)
The only difference between easy and hard versions is constraints. Nauuo is a girl who loves random picture websites. One day she made a random picture website by herself which includes n pictures. When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The i-th picture has a non-negative weight w_i, and the probability of the i-th picture being displayed is \frac{w_i}{βˆ‘_{j=1}^nw_j}. That is to say, the probability of a picture to be displayed is proportional to its weight. However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add 1 to its weight; otherwise, she would subtract 1 from its weight. Nauuo will visit the website m times. She wants to know the expected weight of each picture after all the m visits modulo 998244353. Can you help her? The expected weight of the i-th picture can be denoted by \frac {q_i} {p_i} where \gcd(p_i,q_i)=1, you need to print an integer r_i satisfying 0≀ r_i<998244353 and r_iβ‹… p_i≑ q_i\pmod{998244353}. It can be proved that such r_i exists and is unique. Input The first line contains two integers n and m (1≀ n≀ 50, 1≀ m≀ 50) β€” the number of pictures and the number of visits to the website. The second line contains n integers a_1,a_2,…,a_n (a_i is either 0 or 1) β€” if a_i=0 , Nauuo does not like the i-th picture; otherwise Nauuo likes the i-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains n integers w_1,w_2,…,w_n (1≀ w_i≀50) β€” the initial weights of the pictures. Output The output contains n integers r_1,r_2,…,r_n β€” the expected weights modulo 998244353. Examples Input 2 1 0 1 2 1 Output 332748119 332748119 Input 1 2 1 1 Output 3 Input 3 3 0 1 1 4 3 5 Output 160955686 185138929 974061117 Note In the first example, if the only visit shows the first picture with a probability of \frac 2 3, the final weights are (1,1); if the only visit shows the second picture with a probability of \frac1 3, the final weights are (2,2). So, both expected weights are \frac2 3β‹… 1+\frac 1 3β‹… 2=\frac4 3 . Because 332748119β‹… 3≑ 4\pmod{998244353}, you need to print 332748119 instead of \frac4 3 or 1.3333333333. In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, w_1 will be increased by 1. So, the expected weight is 1+2=3. Nauuo is very naughty so she didn't give you any hint of the third example.
2
11
#include <bits/stdc++.h> using namespace std; const int MOD = 998244353; pair<int, int> v[55]; int dp[2][55][55][55], inv[3005], ans[2]; int liked, disliked; int exp(int a, int b) { int r = 1; while (b) { if (b & 1) r = ((long long)r * a) % MOD; b /= 2; a = ((long long)a * a) % MOD; } return r; } inline void add(int& a, int b) { a += b; if (a >= MOD) a -= MOD; } inline int mul(int a, int b) { return ((long long)a * b) % MOD; } int main() { int n, m; scanf("%d%d", &n, &m); for (int i = 0; i < n; i++) scanf("%d", &v[i].first); for (int i = 0; i < n; i++) { scanf("%d", &v[i].second); liked += v[i].first * v[i].second; disliked += (!v[i].first) * v[i].second; } for (int i = 0; i <= 3000; i++) inv[i] = exp(i, MOD - 2); dp[1][1][0][0] = dp[0][1][0][0] = 1; for (int w = 1; w < 51; w++) for (int i = 0; i < m; i++) for (int j = 0; j <= i; j++) { int k = i - j; if (liked + j + disliked - k <= 0) continue; int aux = inv[liked + j + disliked - k]; add(dp[1][w][i + 1][j + 1], mul(mul(dp[1][w][i][j], (liked + j - w)), aux)); add(dp[1][w][i + 1][j], mul(mul(dp[1][w][i][j], (disliked - k)), aux)); add(dp[1][w + 1][i + 1][j + 1], mul(mul(dp[1][w][i][j], w), aux)); } for (int w = 1; w > 0; w--) for (int i = 0; i < m; i++) for (int j = 0; j <= i; j++) { int k = i - j; if (liked + j + disliked - k <= 0) continue; int aux = inv[liked + j + disliked - k]; add(dp[0][w][i + 1][j], mul(mul(dp[0][w][i][j], (disliked - k - w)), aux)); add(dp[0][w][i + 1][j + 1], mul(mul(dp[0][w][i][j], (liked + j)), aux)); add(dp[0][w - 1][i + 1][j], mul(mul(dp[0][w][i][j], w), aux)); } for (int i = 0; i <= 1; i++) for (int w = 0; w <= 51; w++) for (int j = 0; j <= m; j++) add(ans[i], mul(w, dp[i][w][m][j])); for (int cur = 0; cur < n; cur++) printf("%d\n", mul(ans[v[cur].first], v[cur].second)); return 0; }
CPP
1173_E1. Nauuo and Pictures (easy version)
The only difference between easy and hard versions is constraints. Nauuo is a girl who loves random picture websites. One day she made a random picture website by herself which includes n pictures. When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The i-th picture has a non-negative weight w_i, and the probability of the i-th picture being displayed is \frac{w_i}{βˆ‘_{j=1}^nw_j}. That is to say, the probability of a picture to be displayed is proportional to its weight. However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add 1 to its weight; otherwise, she would subtract 1 from its weight. Nauuo will visit the website m times. She wants to know the expected weight of each picture after all the m visits modulo 998244353. Can you help her? The expected weight of the i-th picture can be denoted by \frac {q_i} {p_i} where \gcd(p_i,q_i)=1, you need to print an integer r_i satisfying 0≀ r_i<998244353 and r_iβ‹… p_i≑ q_i\pmod{998244353}. It can be proved that such r_i exists and is unique. Input The first line contains two integers n and m (1≀ n≀ 50, 1≀ m≀ 50) β€” the number of pictures and the number of visits to the website. The second line contains n integers a_1,a_2,…,a_n (a_i is either 0 or 1) β€” if a_i=0 , Nauuo does not like the i-th picture; otherwise Nauuo likes the i-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains n integers w_1,w_2,…,w_n (1≀ w_i≀50) β€” the initial weights of the pictures. Output The output contains n integers r_1,r_2,…,r_n β€” the expected weights modulo 998244353. Examples Input 2 1 0 1 2 1 Output 332748119 332748119 Input 1 2 1 1 Output 3 Input 3 3 0 1 1 4 3 5 Output 160955686 185138929 974061117 Note In the first example, if the only visit shows the first picture with a probability of \frac 2 3, the final weights are (1,1); if the only visit shows the second picture with a probability of \frac1 3, the final weights are (2,2). So, both expected weights are \frac2 3β‹… 1+\frac 1 3β‹… 2=\frac4 3 . Because 332748119β‹… 3≑ 4\pmod{998244353}, you need to print 332748119 instead of \frac4 3 or 1.3333333333. In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, w_1 will be increased by 1. So, the expected weight is 1+2=3. Nauuo is very naughty so she didn't give you any hint of the third example.
2
11
#include <bits/stdc++.h> using namespace std; inline int read() { int x = 0; char c = getchar(); while (c < '0' || c > '9') c = getchar(); while (c >= '0' && c <= '9') x = (x << 3) + (x << 1) + c - '0', c = getchar(); return x; } const int maxn = 2e5 + 10; const int mod = 998244353; const double eps = 1e-9; long long f[51][51], g[51][51]; long long inv[3000]; long long qpow(int a, int b) { if (b == 0) return 1; long long now = qpow(a, b / 2); now = now * now % mod; if (b & 1) now = now * a % mod; return now; } int like[51], cnt[2], w[51]; int main() { inv[0] = inv[1] = 1; for (int i = 2; i < 3000; ++i) inv[i] = qpow(i, mod - 2); int n = read(), m = read(); for (int i = 1; i <= n; ++i) like[i] = read(); for (int i = 1; i <= n; ++i) { w[i] = read(); cnt[like[i]] += w[i]; } int sum = cnt[0] + cnt[1]; for (int j = 0; j <= m; ++j) { f[j][m - j] = 1; g[j][m - j] = 1; } for (int j = m - 1; j >= 0; --j) { for (int k = m - j - 1; k >= 0; --k) { f[j][k] = ((cnt[1] + j + 1) * inv[sum + j - k] % mod * f[j + 1][k] % mod + (cnt[0] - k) * inv[sum + j - k] % mod * f[j][k + 1]) % mod; g[j][k] = ((cnt[1] + j) * inv[sum + j - k] % mod * g[j + 1][k] % mod + (cnt[0] - k - 1) * inv[sum + j - k] % mod * g[j][k + 1]) % mod; } } for (int i = 1; i <= n; ++i) { if (like[i]) printf("%lld\n", w[i] * f[0][0] % mod); else printf("%lld\n", w[i] * g[0][0] % mod); } }
CPP
1173_E1. Nauuo and Pictures (easy version)
The only difference between easy and hard versions is constraints. Nauuo is a girl who loves random picture websites. One day she made a random picture website by herself which includes n pictures. When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The i-th picture has a non-negative weight w_i, and the probability of the i-th picture being displayed is \frac{w_i}{βˆ‘_{j=1}^nw_j}. That is to say, the probability of a picture to be displayed is proportional to its weight. However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add 1 to its weight; otherwise, she would subtract 1 from its weight. Nauuo will visit the website m times. She wants to know the expected weight of each picture after all the m visits modulo 998244353. Can you help her? The expected weight of the i-th picture can be denoted by \frac {q_i} {p_i} where \gcd(p_i,q_i)=1, you need to print an integer r_i satisfying 0≀ r_i<998244353 and r_iβ‹… p_i≑ q_i\pmod{998244353}. It can be proved that such r_i exists and is unique. Input The first line contains two integers n and m (1≀ n≀ 50, 1≀ m≀ 50) β€” the number of pictures and the number of visits to the website. The second line contains n integers a_1,a_2,…,a_n (a_i is either 0 or 1) β€” if a_i=0 , Nauuo does not like the i-th picture; otherwise Nauuo likes the i-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains n integers w_1,w_2,…,w_n (1≀ w_i≀50) β€” the initial weights of the pictures. Output The output contains n integers r_1,r_2,…,r_n β€” the expected weights modulo 998244353. Examples Input 2 1 0 1 2 1 Output 332748119 332748119 Input 1 2 1 1 Output 3 Input 3 3 0 1 1 4 3 5 Output 160955686 185138929 974061117 Note In the first example, if the only visit shows the first picture with a probability of \frac 2 3, the final weights are (1,1); if the only visit shows the second picture with a probability of \frac1 3, the final weights are (2,2). So, both expected weights are \frac2 3β‹… 1+\frac 1 3β‹… 2=\frac4 3 . Because 332748119β‹… 3≑ 4\pmod{998244353}, you need to print 332748119 instead of \frac4 3 or 1.3333333333. In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, w_1 will be increased by 1. So, the expected weight is 1+2=3. Nauuo is very naughty so she didn't give you any hint of the third example.
2
11
#include <bits/stdc++.h> const int N = 55; const int P = 998244353; int n, m, a[N], w[N], inv[N * N], f[N][N][N][N]; void add(int &x, int y) { (x += y) >= P && (x -= P); } void init(int n) { inv[1] = 1; for (int i = 2; i <= n; i++) { inv[i] = 1LL * (P - P / i) * inv[P % i] % P; } } int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); a[i] = (a[i] << 1) - 1; } int sumA = 0, sumB = 0; for (int i = 1; i <= n; i++) { scanf("%d", &w[i]); (a[i] > 0 ? sumA : sumB) += w[i]; } init(sumA + sumB + m); for (int t = 1; t <= n; t++) { memset(f, 0, sizeof(f)); f[0][0][0][0] = 1; for (int i = 0; i < m; i++) { for (int j = 0; j <= i; j++) { for (int k = 0; k <= i; k++) { for (int d = 0; d <= (a[t] > 0 ? j : k); d++) { if (!f[i][j][k][d]) continue; int x = 1LL * f[i][j][k][d] * inv[sumA + sumB + j - k] % P; if (a[t] > 0) { add(f[i + 1][j + 1][k][d + 1], 1LL * (w[t] + d) * x % P); add(f[i + 1][j + 1][k][d], 1LL * (sumA + j - w[t] - d) * x % P); add(f[i + 1][j][k + 1][d], 1LL * (sumB - k) * x % P); } else { add(f[i + 1][j][k + 1][d + 1], 1LL * (w[t] - d) * x % P); add(f[i + 1][j][k + 1][d], 1LL * (sumB - k - w[t] + d) * x % P); add(f[i + 1][j + 1][k][d], 1LL * (sumA + j) * x % P); } } } } } int ans = 0; for (int j = 0; j <= m; j++) { for (int k = 0; k <= m; k++) { for (int d = 0; d <= (a[t] > 0 ? j : k); d++) { add(ans, 1LL * f[m][j][k][d] * (w[t] + a[t] * d) % P); } } } printf("%d\n", ans); } return 0; }
CPP
1173_E1. Nauuo and Pictures (easy version)
The only difference between easy and hard versions is constraints. Nauuo is a girl who loves random picture websites. One day she made a random picture website by herself which includes n pictures. When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The i-th picture has a non-negative weight w_i, and the probability of the i-th picture being displayed is \frac{w_i}{βˆ‘_{j=1}^nw_j}. That is to say, the probability of a picture to be displayed is proportional to its weight. However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add 1 to its weight; otherwise, she would subtract 1 from its weight. Nauuo will visit the website m times. She wants to know the expected weight of each picture after all the m visits modulo 998244353. Can you help her? The expected weight of the i-th picture can be denoted by \frac {q_i} {p_i} where \gcd(p_i,q_i)=1, you need to print an integer r_i satisfying 0≀ r_i<998244353 and r_iβ‹… p_i≑ q_i\pmod{998244353}. It can be proved that such r_i exists and is unique. Input The first line contains two integers n and m (1≀ n≀ 50, 1≀ m≀ 50) β€” the number of pictures and the number of visits to the website. The second line contains n integers a_1,a_2,…,a_n (a_i is either 0 or 1) β€” if a_i=0 , Nauuo does not like the i-th picture; otherwise Nauuo likes the i-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains n integers w_1,w_2,…,w_n (1≀ w_i≀50) β€” the initial weights of the pictures. Output The output contains n integers r_1,r_2,…,r_n β€” the expected weights modulo 998244353. Examples Input 2 1 0 1 2 1 Output 332748119 332748119 Input 1 2 1 1 Output 3 Input 3 3 0 1 1 4 3 5 Output 160955686 185138929 974061117 Note In the first example, if the only visit shows the first picture with a probability of \frac 2 3, the final weights are (1,1); if the only visit shows the second picture with a probability of \frac1 3, the final weights are (2,2). So, both expected weights are \frac2 3β‹… 1+\frac 1 3β‹… 2=\frac4 3 . Because 332748119β‹… 3≑ 4\pmod{998244353}, you need to print 332748119 instead of \frac4 3 or 1.3333333333. In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, w_1 will be increased by 1. So, the expected weight is 1+2=3. Nauuo is very naughty so she didn't give you any hint of the third example.
2
11
#include <bits/stdc++.h> using namespace std; const int MOD = 998244353; const int MAXN = 50; const int MAXM = 50; const int MAXW = 100; bool likes[MAXN]; int weight[MAXN]; int dp[MAXW + 1][MAXM + 1][MAXM + 1][MAXM + 1]; int dp2[MAXW + 1][MAXM + 1][MAXM + 1][MAXM + 1]; void modInv(int a, int b, int& ainv, int& binv) { if (a == 0) { ainv = 0; binv = 1; return; } int x, y; modInv(b % a, a, x, y); ainv = (y - ((long long)x * (b / a)) % MOD + MOD) % MOD; binv = x % MOD; } int main() { int n, m; cin >> n >> m; for (int i = 0; i < n; i++) cin >> likes[i]; int good = 0, bad = 0; for (int i = 0; i < n; i++) { cin >> weight[i]; if (likes[i]) good += weight[i]; else bad += weight[i]; } int trash; for (int w = 1; w <= MAXW; w++) { for (int i = 0; i <= m; i++) { for (int j = 0; j <= m; j++) { dp[w][0][i][j] = w; dp2[w][0][i][j] = w; } } } for (int v = 1; v <= m; v++) { for (int w = MAXW - v; w >= 0; w--) { for (int i = m - v; i >= 0; i--) { for (int j = m - v; j >= 0; j--) { if (w > good + i || j > bad) continue; int pInv; modInv(good + bad + i - j, MOD, pInv, trash); int r1 = (((long long)w * pInv % MOD) * dp[w + 1][v - 1][i + 1][j]) % MOD; int r2 = (((long long)(good + i - w) * pInv % MOD) * dp[w][v - 1][i + 1][j]) % MOD; int r3 = (((long long)(bad - j) * pInv % MOD) * dp[w][v - 1][i][j + 1]) % MOD; dp[w][v][i][j] = ((long long)r1 + r2 + r3) % MOD; assert(dp[w][v][i][j] >= 0); } } } } for (int v = 1; v <= m; v++) { for (int w = 1; w <= MAXW - v; w++) { for (int i = m - v; i >= 0; i--) { for (int j = m - v; j >= 0; j--) { if (w > bad - j || j > bad) continue; int pInv; modInv(good + bad + i - j, MOD, pInv, trash); int r1 = (((long long)w * pInv % MOD) * dp2[w - 1][v - 1][i][j + 1]) % MOD; int r2 = (((long long)(bad - j - w) * pInv % MOD) * dp2[w][v - 1][i][j + 1]) % MOD; int r3 = (((long long)(good + i) * pInv % MOD) * dp2[w][v - 1][i + 1][j]) % MOD; dp2[w][v][i][j] = ((long long)r1 + r2 + r3) % MOD; assert(dp2[w][v][i][j] >= 0); } } } } for (int i = 0; i < n; i++) { if (likes[i]) { cout << dp[weight[i]][m][0][0] << endl; } else { cout << dp2[weight[i]][m][0][0] << endl; } } return 0; }
CPP
1173_E1. Nauuo and Pictures (easy version)
The only difference between easy and hard versions is constraints. Nauuo is a girl who loves random picture websites. One day she made a random picture website by herself which includes n pictures. When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The i-th picture has a non-negative weight w_i, and the probability of the i-th picture being displayed is \frac{w_i}{βˆ‘_{j=1}^nw_j}. That is to say, the probability of a picture to be displayed is proportional to its weight. However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add 1 to its weight; otherwise, she would subtract 1 from its weight. Nauuo will visit the website m times. She wants to know the expected weight of each picture after all the m visits modulo 998244353. Can you help her? The expected weight of the i-th picture can be denoted by \frac {q_i} {p_i} where \gcd(p_i,q_i)=1, you need to print an integer r_i satisfying 0≀ r_i<998244353 and r_iβ‹… p_i≑ q_i\pmod{998244353}. It can be proved that such r_i exists and is unique. Input The first line contains two integers n and m (1≀ n≀ 50, 1≀ m≀ 50) β€” the number of pictures and the number of visits to the website. The second line contains n integers a_1,a_2,…,a_n (a_i is either 0 or 1) β€” if a_i=0 , Nauuo does not like the i-th picture; otherwise Nauuo likes the i-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains n integers w_1,w_2,…,w_n (1≀ w_i≀50) β€” the initial weights of the pictures. Output The output contains n integers r_1,r_2,…,r_n β€” the expected weights modulo 998244353. Examples Input 2 1 0 1 2 1 Output 332748119 332748119 Input 1 2 1 1 Output 3 Input 3 3 0 1 1 4 3 5 Output 160955686 185138929 974061117 Note In the first example, if the only visit shows the first picture with a probability of \frac 2 3, the final weights are (1,1); if the only visit shows the second picture with a probability of \frac1 3, the final weights are (2,2). So, both expected weights are \frac2 3β‹… 1+\frac 1 3β‹… 2=\frac4 3 . Because 332748119β‹… 3≑ 4\pmod{998244353}, you need to print 332748119 instead of \frac4 3 or 1.3333333333. In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, w_1 will be increased by 1. So, the expected weight is 1+2=3. Nauuo is very naughty so she didn't give you any hint of the third example.
2
11
#include <bits/stdc++.h> using namespace std; const int64_t MAX = 55, MOD = 998244353; int64_t n, m, sumW = 0, sumL = 0, C[MAX], W[MAX], F[MAX][MAX][2 * MAX][2]; bool Free[MAX][MAX][2 * MAX][2]; int64_t Mul(int64_t x, int64_t k) { if (k == 0) return 1; int64_t tmp = Mul(x, k / 2); if (k % 2 == 0) return tmp * tmp % MOD; return tmp * tmp % MOD * x % MOD; } int64_t DP(int64_t step, int64_t like, int64_t curW, int64_t type) { if (step == m) return curW; if (Free[step][like][curW][type] == true) return F[step][like][curW][type]; Free[step][like][curW][type] = true; int64_t cursumW = sumW + like - (step - like); int64_t cursumL = sumL + like; int64_t rs = 0; int64_t invcursumW = Mul(cursumW, MOD - 2); if (cursumL - curW * type > 0) rs = (rs + (cursumL - curW * type) * DP(step + 1, like + 1, curW, type) % MOD * invcursumW) % MOD; if (cursumW - cursumL - curW * (1 - type) > 0) rs = (rs + (cursumW - cursumL - curW * (1 - type)) * DP(step + 1, like, curW, type) % MOD * invcursumW) % MOD; if (curW > 0) rs = (rs + curW * DP(step + 1, like + type, curW + (type == 1 ? 1 : -1), type) % MOD * invcursumW) % MOD; return F[step][like][curW][type] = rs; } int main() { ios_base::sync_with_stdio(false); cin >> n >> m; for (int64_t i = 1; i <= n; i++) cin >> C[i]; for (int64_t i = 1; i <= n; i++) { cin >> W[i]; sumW += W[i]; sumL += W[i] * C[i]; } for (int64_t i = 1; i <= n; i++) cout << DP(0, 0, W[i], C[i]) << "\n"; }
CPP
1173_E1. Nauuo and Pictures (easy version)
The only difference between easy and hard versions is constraints. Nauuo is a girl who loves random picture websites. One day she made a random picture website by herself which includes n pictures. When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The i-th picture has a non-negative weight w_i, and the probability of the i-th picture being displayed is \frac{w_i}{βˆ‘_{j=1}^nw_j}. That is to say, the probability of a picture to be displayed is proportional to its weight. However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add 1 to its weight; otherwise, she would subtract 1 from its weight. Nauuo will visit the website m times. She wants to know the expected weight of each picture after all the m visits modulo 998244353. Can you help her? The expected weight of the i-th picture can be denoted by \frac {q_i} {p_i} where \gcd(p_i,q_i)=1, you need to print an integer r_i satisfying 0≀ r_i<998244353 and r_iβ‹… p_i≑ q_i\pmod{998244353}. It can be proved that such r_i exists and is unique. Input The first line contains two integers n and m (1≀ n≀ 50, 1≀ m≀ 50) β€” the number of pictures and the number of visits to the website. The second line contains n integers a_1,a_2,…,a_n (a_i is either 0 or 1) β€” if a_i=0 , Nauuo does not like the i-th picture; otherwise Nauuo likes the i-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains n integers w_1,w_2,…,w_n (1≀ w_i≀50) β€” the initial weights of the pictures. Output The output contains n integers r_1,r_2,…,r_n β€” the expected weights modulo 998244353. Examples Input 2 1 0 1 2 1 Output 332748119 332748119 Input 1 2 1 1 Output 3 Input 3 3 0 1 1 4 3 5 Output 160955686 185138929 974061117 Note In the first example, if the only visit shows the first picture with a probability of \frac 2 3, the final weights are (1,1); if the only visit shows the second picture with a probability of \frac1 3, the final weights are (2,2). So, both expected weights are \frac2 3β‹… 1+\frac 1 3β‹… 2=\frac4 3 . Because 332748119β‹… 3≑ 4\pmod{998244353}, you need to print 332748119 instead of \frac4 3 or 1.3333333333. In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, w_1 will be increased by 1. So, the expected weight is 1+2=3. Nauuo is very naughty so she didn't give you any hint of the third example.
2
11
#include <bits/stdc++.h> using namespace std; inline long long Getint() { char ch = getchar(); long long x = 0, fh = 1; while (ch < '0' || ch > '9') { if (ch == '-') fh = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { (x *= 10) += ch ^ 48; ch = getchar(); } return x * fh; } const int Mod = 998244353; const int N = 55; const int M = 3005; int n, m, su, A[N], W[N]; int nw, cnt, top[2]; int vis[M][N * 2]; long long f[2][M][N * 2]; struct nod { int x, y; nod() {} nod(int a, int b) { x = a; y = b; } } q[2][M * N * 2]; inline void Up(int x, int y, long long w) { if (vis[x][y] == cnt) (f[nw][x][y] += w) %= Mod; else vis[x][y] = cnt, f[nw][x][y] = w % Mod, q[nw][++top[nw]] = nod(x, y); } inline void Clear() { cnt++; top[nw] = 0; } long long inv[M * N]; long long Ans[N]; inline long long Solve(int x) { nw = 0; Clear(); int bsu = 0; for (int i = 1; i <= n; i++) { if (A[i] == 0) bsu += W[i]; } Up(bsu, W[x], 1); for (int t = 1; t <= m; t++) { nw ^= 1; Clear(); for (int i = 1; i <= top[nw ^ 1]; i++) { nod w = q[nw ^ 1][i]; int gd = t - 1 - (bsu - w.x); int wsu = su + gd - (bsu - w.x); long long p = w.y * inv[wsu] % Mod; if (A[x] == 0) { long long badres = w.x - w.y; long long godres = wsu - w.x; long long p1 = badres * inv[wsu] % Mod; long long p2 = godres * inv[wsu] % Mod; if (badres) Up(w.x - 1, w.y, f[nw ^ 1][w.x][w.y] * p1 % Mod); if (w.y) Up(w.x - 1, w.y - 1, f[nw ^ 1][w.x][w.y] * p % Mod); Up(w.x, w.y, f[nw ^ 1][w.x][w.y] * p2 % Mod); } else { long long badres = w.x; long long godres = wsu - w.x - w.y; long long p1 = badres * inv[wsu] % Mod; long long p2 = godres * inv[wsu] % Mod; if (badres) Up(w.x - 1, w.y, f[nw ^ 1][w.x][w.y] * p1 % Mod); Up(w.x, w.y, f[nw ^ 1][w.x][w.y] * p2 % Mod); Up(w.x, w.y + 1, f[nw ^ 1][w.x][w.y] * p % Mod); } } } for (int i = 1; i <= top[nw]; i++) { nod w = q[nw][i]; Ans[x] += w.y * f[nw][w.x][w.y] % Mod; } return (Ans[x] % Mod + Mod) % Mod; } int main() { n = Getint(); m = Getint(); inv[0] = inv[1] = 1; for (int i = 2; i <= 5000; i++) { inv[i] = (Mod - Mod / i) * inv[Mod % i] % Mod; } for (int i = 1; i <= n; i++) A[i] = Getint(); for (int i = 1; i <= n; i++) W[i] = Getint(), su += W[i]; for (int i = 1; i <= n; i++) cout << Solve(i) << '\n'; return 0; }
CPP
1173_E1. Nauuo and Pictures (easy version)
The only difference between easy and hard versions is constraints. Nauuo is a girl who loves random picture websites. One day she made a random picture website by herself which includes n pictures. When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The i-th picture has a non-negative weight w_i, and the probability of the i-th picture being displayed is \frac{w_i}{βˆ‘_{j=1}^nw_j}. That is to say, the probability of a picture to be displayed is proportional to its weight. However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add 1 to its weight; otherwise, she would subtract 1 from its weight. Nauuo will visit the website m times. She wants to know the expected weight of each picture after all the m visits modulo 998244353. Can you help her? The expected weight of the i-th picture can be denoted by \frac {q_i} {p_i} where \gcd(p_i,q_i)=1, you need to print an integer r_i satisfying 0≀ r_i<998244353 and r_iβ‹… p_i≑ q_i\pmod{998244353}. It can be proved that such r_i exists and is unique. Input The first line contains two integers n and m (1≀ n≀ 50, 1≀ m≀ 50) β€” the number of pictures and the number of visits to the website. The second line contains n integers a_1,a_2,…,a_n (a_i is either 0 or 1) β€” if a_i=0 , Nauuo does not like the i-th picture; otherwise Nauuo likes the i-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains n integers w_1,w_2,…,w_n (1≀ w_i≀50) β€” the initial weights of the pictures. Output The output contains n integers r_1,r_2,…,r_n β€” the expected weights modulo 998244353. Examples Input 2 1 0 1 2 1 Output 332748119 332748119 Input 1 2 1 1 Output 3 Input 3 3 0 1 1 4 3 5 Output 160955686 185138929 974061117 Note In the first example, if the only visit shows the first picture with a probability of \frac 2 3, the final weights are (1,1); if the only visit shows the second picture with a probability of \frac1 3, the final weights are (2,2). So, both expected weights are \frac2 3β‹… 1+\frac 1 3β‹… 2=\frac4 3 . Because 332748119β‹… 3≑ 4\pmod{998244353}, you need to print 332748119 instead of \frac4 3 or 1.3333333333. In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, w_1 will be increased by 1. So, the expected weight is 1+2=3. Nauuo is very naughty so she didn't give you any hint of the third example.
2
11
#include <bits/stdc++.h> #pragma GCC optimize(2) using namespace std; const int inf = 0x3f3f3f3f; const int mod = 998244353; using ll = long long; using vi = vector<int>; using pii = pair<int, int>; inline int read() { int x = 0, f = 1, c = getchar(); for (; !isdigit(c); c = getchar()) if (c == 45) f ^= 1; for (; isdigit(c); c = getchar()) x = x * 10 + c - 48; return f ? x : -x; } int qp(int x, int n) { return !n ? 1 : 1LL * qp(1LL * x * x % mod, n >> 1) * (n & 1 ? x : 1) % mod; } int inv(int x) { return qp(x, mod - 2); } int n, m; int a[55], w[55]; int dp[55][55][55][55]; int main() { n = read(), m = read(); for (int i = 0; i < (int)(n); ++i) a[i] = read(); int sum = 0, like = 0; for (int i = 0; i < (int)(n); ++i) w[i] = read(), sum += w[i], like += w[i] * a[i]; for (int i = 0; i < (int)(n); ++i) dp[0][0][i][0] = 1; for (int i = 0; i < (int)(m); ++i) for (int j = 0; j < (int)(i + 1); ++j) { int nwsum = sum + j - (i - j); int invnwsum = inv(nwsum); for (int k = 0; k < (int)(n); ++k) for (int l = 0; l < (int)(i + 1); ++l) { if (!dp[i][j][k][l]) continue; int nwlike = like + j; for (int sel = 0; sel < (int)(2); ++sel) { int nj = j + sel; int ful = sel ? nwlike : nwsum - nwlike; int nwk = l * (a[k] ? 1 : -1) + w[k]; if (nwk <= 0) continue; if (sel == a[k]) ful -= nwk; (dp[i + 1][nj][k][l] += 1LL * dp[i][j][k][l] * ful % mod * invnwsum % mod) %= mod; if (sel == a[k]) (dp[i + 1][nj][k][l + 1] += 1LL * dp[i][j][k][l] * nwk % mod * invnwsum % mod) %= mod; } } } for (int i = 0; i < (int)(n); ++i) { int ans = 0; for (int j = 0; j < (int)(m + 1); ++j) for (int l = 0; l < (int)(m + 1); ++l) { (ans += 1LL * (l * (a[i] ? 1 : -1) + w[i]) * dp[m][j][i][l] % mod) %= mod; } printf("%d\n", ans); } return 0; }
CPP
1173_E1. Nauuo and Pictures (easy version)
The only difference between easy and hard versions is constraints. Nauuo is a girl who loves random picture websites. One day she made a random picture website by herself which includes n pictures. When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The i-th picture has a non-negative weight w_i, and the probability of the i-th picture being displayed is \frac{w_i}{βˆ‘_{j=1}^nw_j}. That is to say, the probability of a picture to be displayed is proportional to its weight. However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add 1 to its weight; otherwise, she would subtract 1 from its weight. Nauuo will visit the website m times. She wants to know the expected weight of each picture after all the m visits modulo 998244353. Can you help her? The expected weight of the i-th picture can be denoted by \frac {q_i} {p_i} where \gcd(p_i,q_i)=1, you need to print an integer r_i satisfying 0≀ r_i<998244353 and r_iβ‹… p_i≑ q_i\pmod{998244353}. It can be proved that such r_i exists and is unique. Input The first line contains two integers n and m (1≀ n≀ 50, 1≀ m≀ 50) β€” the number of pictures and the number of visits to the website. The second line contains n integers a_1,a_2,…,a_n (a_i is either 0 or 1) β€” if a_i=0 , Nauuo does not like the i-th picture; otherwise Nauuo likes the i-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains n integers w_1,w_2,…,w_n (1≀ w_i≀50) β€” the initial weights of the pictures. Output The output contains n integers r_1,r_2,…,r_n β€” the expected weights modulo 998244353. Examples Input 2 1 0 1 2 1 Output 332748119 332748119 Input 1 2 1 1 Output 3 Input 3 3 0 1 1 4 3 5 Output 160955686 185138929 974061117 Note In the first example, if the only visit shows the first picture with a probability of \frac 2 3, the final weights are (1,1); if the only visit shows the second picture with a probability of \frac1 3, the final weights are (2,2). So, both expected weights are \frac2 3β‹… 1+\frac 1 3β‹… 2=\frac4 3 . Because 332748119β‹… 3≑ 4\pmod{998244353}, you need to print 332748119 instead of \frac4 3 or 1.3333333333. In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, w_1 will be increased by 1. So, the expected weight is 1+2=3. Nauuo is very naughty so she didn't give you any hint of the third example.
2
11
#include <bits/stdc++.h> using namespace std; const long long int maxn = 105; const long long int modd = 998244353; long long int dp[maxn][maxn][maxn], n, m, i; long long int a[maxn], w[maxn]; void count(long long int a, long long int b, long long int &x, long long int &y) { if (a == 1 && b == 0) { x = 1; y = 1; return; } else { count(b, a % b, y, x); y = (y - (a / b) * x % modd) % modd; return; } } long long int qny(long long int a) { long long int x, y; count(modd, a, x, y); y = (y + modd) % modd; return y; } void do_it(long long int x) { long long int i, j, k, p, ta = 0, tb = 0, tm = 0, tt; if (a[x] == 0) tt = -1; else tt = 1; for (i = 1; i <= n; i++) { if (i == x) tm += w[i]; else { if (a[i] == 1) ta += w[i]; if (a[i] == 0) tb += w[i]; } } dp[0][0][0] = 1; for (k = 0; k <= m; k++) { for (i = 0; i <= k; i++) { for (j = 0; j <= k - i; j++) { if (dp[k][i][j] == 0) continue; p = k - i - j; dp[k + 1][i + 1][j] = (dp[k + 1][i + 1][j] + dp[k][i][j] * (ta + i) % modd * qny(ta + i + tb - j + tm + tt * p) % modd) % modd; dp[k + 1][i][j + 1] = (dp[k + 1][i][j + 1] + dp[k][i][j] * (tb - j) % modd * qny(ta + i + tb - j + tm + tt * p) % modd) % modd; dp[k + 1][i][j] = (dp[k + 1][i][j] + dp[k][i][j] * (tm + tt * p) % modd * qny(ta + i + tb - j + tm + tt * p) % modd) % modd; } } } long long int ans = 0; for (i = 0; i <= m; i++) { for (j = 0; j <= m; j++) { if (dp[m][i][j] == 0) continue; p = m - i - j; ans = (ans + (dp[m][i][j] * ((w[x] + p * tt) % modd)) % modd) % modd; } } ans = (ans + modd) % modd; printf("%lld\n", ans); return; } int main() { scanf("%lld%lld", &n, &m); long long int i; for (i = 1; i <= n; i++) scanf("%lld", &a[i]); for (i = 1; i <= n; i++) scanf("%lld", &w[i]); for (i = 1; i <= n; i++) { memset(dp, 0, sizeof(dp)); do_it(i); } return 0; }
CPP
1173_E1. Nauuo and Pictures (easy version)
The only difference between easy and hard versions is constraints. Nauuo is a girl who loves random picture websites. One day she made a random picture website by herself which includes n pictures. When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The i-th picture has a non-negative weight w_i, and the probability of the i-th picture being displayed is \frac{w_i}{βˆ‘_{j=1}^nw_j}. That is to say, the probability of a picture to be displayed is proportional to its weight. However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add 1 to its weight; otherwise, she would subtract 1 from its weight. Nauuo will visit the website m times. She wants to know the expected weight of each picture after all the m visits modulo 998244353. Can you help her? The expected weight of the i-th picture can be denoted by \frac {q_i} {p_i} where \gcd(p_i,q_i)=1, you need to print an integer r_i satisfying 0≀ r_i<998244353 and r_iβ‹… p_i≑ q_i\pmod{998244353}. It can be proved that such r_i exists and is unique. Input The first line contains two integers n and m (1≀ n≀ 50, 1≀ m≀ 50) β€” the number of pictures and the number of visits to the website. The second line contains n integers a_1,a_2,…,a_n (a_i is either 0 or 1) β€” if a_i=0 , Nauuo does not like the i-th picture; otherwise Nauuo likes the i-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains n integers w_1,w_2,…,w_n (1≀ w_i≀50) β€” the initial weights of the pictures. Output The output contains n integers r_1,r_2,…,r_n β€” the expected weights modulo 998244353. Examples Input 2 1 0 1 2 1 Output 332748119 332748119 Input 1 2 1 1 Output 3 Input 3 3 0 1 1 4 3 5 Output 160955686 185138929 974061117 Note In the first example, if the only visit shows the first picture with a probability of \frac 2 3, the final weights are (1,1); if the only visit shows the second picture with a probability of \frac1 3, the final weights are (2,2). So, both expected weights are \frac2 3β‹… 1+\frac 1 3β‹… 2=\frac4 3 . Because 332748119β‹… 3≑ 4\pmod{998244353}, you need to print 332748119 instead of \frac4 3 or 1.3333333333. In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, w_1 will be increased by 1. So, the expected weight is 1+2=3. Nauuo is very naughty so she didn't give you any hint of the third example.
2
11
#include <bits/stdc++.h> using namespace std; int n, m, a[55], w[55]; long long f[55][55][55]; const int mod = 998244353; int ksm(int x, int k) { int ret = 1, tmp = x; while (k) { if (k & 1) ret = 1ll * ret * tmp % mod; tmp = 1ll * tmp * tmp % mod; k >>= 1; } return ret; } int inv(int x) { return ksm(x, mod - 2); } int main() { int sum = 0, sum0 = 0, sum1 = 0; cin >> n >> m; for (int i = 1; i <= n; i++) cin >> a[i]; for (int i = 1; i <= n; i++) cin >> w[i], sum += w[i], sum1 += a[i] == 1 ? w[i] : 0, sum0 += a[i] == 0 ? w[i] : 0; for (int i = 1; i <= n; i++) { memset(f, 0, sizeof(f)); f[0][0][0] = 1; for (int j = 0; j < m; j++) { for (int k = 0; k <= j; k++) { int cursum = sum + k - (j - k); for (int l = 0; l <= j; l++) { if (!f[j][k][l]) continue; int curw = w[i] + (a[i] ? l : -l), curp = 1ll * curw * inv(cursum) % mod; if (a[i]) { int cur0 = 1ll * (sum0 - (j - k)) * inv(cursum) % mod; int cur1 = 1ll * (sum1 + k - curw) * inv(cursum) % mod; (f[j + 1][k + 1][l + 1] += 1ll * curp * f[j][k][l]) %= mod; (f[j + 1][k + 1][l] += 1ll * cur1 * f[j][k][l]) %= mod; (f[j + 1][k][l] += 1ll * cur0 * f[j][k][l]) %= mod; } else { int cur0 = 1ll * (sum0 - (j - k) - curw) * inv(cursum) % mod; int cur1 = 1ll * (sum1 + k) * inv(cursum) % mod; (f[j + 1][k][l + 1] += 1ll * curp * f[j][k][l]) %= mod; (f[j + 1][k + 1][l] += 1ll * cur1 * f[j][k][l]) %= mod; (f[j + 1][k][l] += 1ll * cur0 * f[j][k][l]) %= mod; } } } } int ans = 0; for (int k = 0; k <= m; k++) for (int l = 0; l <= m; l++) ans = (ans + 1ll * f[m][k][l] * (w[i] + (a[i] ? l : -l))) % mod; cout << (ans + mod) % mod << "\n"; } return 0; }
CPP
1173_E1. Nauuo and Pictures (easy version)
The only difference between easy and hard versions is constraints. Nauuo is a girl who loves random picture websites. One day she made a random picture website by herself which includes n pictures. When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The i-th picture has a non-negative weight w_i, and the probability of the i-th picture being displayed is \frac{w_i}{βˆ‘_{j=1}^nw_j}. That is to say, the probability of a picture to be displayed is proportional to its weight. However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add 1 to its weight; otherwise, she would subtract 1 from its weight. Nauuo will visit the website m times. She wants to know the expected weight of each picture after all the m visits modulo 998244353. Can you help her? The expected weight of the i-th picture can be denoted by \frac {q_i} {p_i} where \gcd(p_i,q_i)=1, you need to print an integer r_i satisfying 0≀ r_i<998244353 and r_iβ‹… p_i≑ q_i\pmod{998244353}. It can be proved that such r_i exists and is unique. Input The first line contains two integers n and m (1≀ n≀ 50, 1≀ m≀ 50) β€” the number of pictures and the number of visits to the website. The second line contains n integers a_1,a_2,…,a_n (a_i is either 0 or 1) β€” if a_i=0 , Nauuo does not like the i-th picture; otherwise Nauuo likes the i-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains n integers w_1,w_2,…,w_n (1≀ w_i≀50) β€” the initial weights of the pictures. Output The output contains n integers r_1,r_2,…,r_n β€” the expected weights modulo 998244353. Examples Input 2 1 0 1 2 1 Output 332748119 332748119 Input 1 2 1 1 Output 3 Input 3 3 0 1 1 4 3 5 Output 160955686 185138929 974061117 Note In the first example, if the only visit shows the first picture with a probability of \frac 2 3, the final weights are (1,1); if the only visit shows the second picture with a probability of \frac1 3, the final weights are (2,2). So, both expected weights are \frac2 3β‹… 1+\frac 1 3β‹… 2=\frac4 3 . Because 332748119β‹… 3≑ 4\pmod{998244353}, you need to print 332748119 instead of \frac4 3 or 1.3333333333. In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, w_1 will be increased by 1. So, the expected weight is 1+2=3. Nauuo is very naughty so she didn't give you any hint of the third example.
2
11
#include <bits/stdc++.h> using namespace std; template <typename T, typename U> inline void smin(T &a, const U &b) { if (a > b) a = b; } template <typename T, typename U> inline void smax(T &a, const U &b) { if (a < b) a = b; } template <class T> inline void gn(T &first) { char c, sg = 0; while (c = getchar(), (c > '9' || c < '0') && c != '-') ; for ((c == '-' ? sg = 1, c = getchar() : 0), first = 0; c >= '0' && c <= '9'; c = getchar()) first = (first << 1) + (first << 3) + c - '0'; if (sg) first = -first; } template <class T1, class T2> inline void gn(T1 &x1, T2 &x2) { gn(x1), gn(x2); } int power(int a, int b, int m, int ans = 1) { for (; b; b >>= 1, a = 1LL * a * a % m) if (b & 1) ans = 1LL * ans * a % m; return ans; } long long dp[3100][3100]; int a[200010]; int w[200010]; int main() { int n, m; cin >> n >> m; for (int i = 1; i <= n; i++) gn(w[i]); for (int i = 1; i <= n; i++) gn(a[i]); long long tot_dis = 0, tot = 0, tot_like = 0; for (int i = 1; i <= n; i++) { if (w[i]) tot_like += a[i]; else tot_dis += a[i]; tot += a[i]; } dp[0][0] = 1; for (int i = 1; i <= m; i++) { dp[i][0] = dp[i - 1][0] * (tot_like + i - 1) % 998244353 * power(tot + i - 1, 998244353 - 2, 998244353) % 998244353; for (int j = 1; j <= i; j++) if (tot_dis >= j) { long long first = 0, second = 0; first = dp[i - 1][j - 1] * power(tot + i + 1 - 2 * j, 998244353 - 2, 998244353) % 998244353 * (tot_dis - j + 1) % 998244353; if (i != j) second = dp[i - 1][j] * power(tot + i - 1 - 2 * j, 998244353 - 2, 998244353) % 998244353 * (tot_like + i - 1 - j) % 998244353; dp[i][j] = (first + second) % 998244353; } } long long sum = 0; for (int j = 0; j <= m; j++) sum += j * dp[m][j] % 998244353 * power(tot_dis, 998244353 - 2, 998244353) % 998244353; sum %= 998244353; long long s = 0; for (int j = 0; j <= m; j++) s += j * dp[m][m - j] % 998244353 * power(tot_like, 998244353 - 2, 998244353) % 998244353; for (int i = 1; i <= n; i++) { if (w[i]) { long long first = s * a[i] % 998244353; first = (first + a[i]) % 998244353; printf("%I64d\n", first); continue; } long long first = sum * a[i] % 998244353; first = a[i] - first; first = (first % 998244353 + 998244353) % 998244353; printf("%I64d\n", first); } return 0; }
CPP
1173_E1. Nauuo and Pictures (easy version)
The only difference between easy and hard versions is constraints. Nauuo is a girl who loves random picture websites. One day she made a random picture website by herself which includes n pictures. When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The i-th picture has a non-negative weight w_i, and the probability of the i-th picture being displayed is \frac{w_i}{βˆ‘_{j=1}^nw_j}. That is to say, the probability of a picture to be displayed is proportional to its weight. However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add 1 to its weight; otherwise, she would subtract 1 from its weight. Nauuo will visit the website m times. She wants to know the expected weight of each picture after all the m visits modulo 998244353. Can you help her? The expected weight of the i-th picture can be denoted by \frac {q_i} {p_i} where \gcd(p_i,q_i)=1, you need to print an integer r_i satisfying 0≀ r_i<998244353 and r_iβ‹… p_i≑ q_i\pmod{998244353}. It can be proved that such r_i exists and is unique. Input The first line contains two integers n and m (1≀ n≀ 50, 1≀ m≀ 50) β€” the number of pictures and the number of visits to the website. The second line contains n integers a_1,a_2,…,a_n (a_i is either 0 or 1) β€” if a_i=0 , Nauuo does not like the i-th picture; otherwise Nauuo likes the i-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains n integers w_1,w_2,…,w_n (1≀ w_i≀50) β€” the initial weights of the pictures. Output The output contains n integers r_1,r_2,…,r_n β€” the expected weights modulo 998244353. Examples Input 2 1 0 1 2 1 Output 332748119 332748119 Input 1 2 1 1 Output 3 Input 3 3 0 1 1 4 3 5 Output 160955686 185138929 974061117 Note In the first example, if the only visit shows the first picture with a probability of \frac 2 3, the final weights are (1,1); if the only visit shows the second picture with a probability of \frac1 3, the final weights are (2,2). So, both expected weights are \frac2 3β‹… 1+\frac 1 3β‹… 2=\frac4 3 . Because 332748119β‹… 3≑ 4\pmod{998244353}, you need to print 332748119 instead of \frac4 3 or 1.3333333333. In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, w_1 will be increased by 1. So, the expected weight is 1+2=3. Nauuo is very naughty so she didn't give you any hint of the third example.
2
11
#include <bits/stdc++.h> using namespace std; const int N = 200010; const int M = 3010; const int mod = 998244353; int qpow(int x, int y) { int out = 1; while (y) { if (y & 1) out = (long long)out * x % mod; x = (long long)x * x % mod; y >>= 1; } return out; } int n, m, a[N], w[N], f[M][M], g[M][M], inv[M << 1], sum[3]; int main() { int i, j; scanf("%d%d", &n, &m); for (i = 1; i <= n; ++i) scanf("%d", a + i); for (i = 1; i <= n; ++i) { scanf("%d", w + i); sum[a[i]] += w[i]; sum[2] += w[i]; } for (i = max(0, m - sum[0]); i <= 2 * m; ++i) inv[i] = qpow(sum[2] + i - m, mod - 2); for (i = m; i >= 0; --i) { f[i][m - i] = g[i][m - i] = 1; for (j = min(m - i - 1, sum[0]); j >= 0; --j) { f[i][j] = ((long long)(sum[1] + i + 1) * f[i + 1][j] + (long long)(sum[0] - j) * f[i][j + 1]) % mod * inv[i - j + m] % mod; g[i][j] = ((long long)(sum[1] + i) * g[i + 1][j] + (long long)(sum[0] - j - 1) * g[i][j + 1]) % mod * inv[i - j + m] % mod; } } for (i = 1; i <= n; ++i) printf("%d\n", int((long long)w[i] * (a[i] ? f[0][0] : g[0][0]) % mod)); return 0; }
CPP
1173_E1. Nauuo and Pictures (easy version)
The only difference between easy and hard versions is constraints. Nauuo is a girl who loves random picture websites. One day she made a random picture website by herself which includes n pictures. When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The i-th picture has a non-negative weight w_i, and the probability of the i-th picture being displayed is \frac{w_i}{βˆ‘_{j=1}^nw_j}. That is to say, the probability of a picture to be displayed is proportional to its weight. However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add 1 to its weight; otherwise, she would subtract 1 from its weight. Nauuo will visit the website m times. She wants to know the expected weight of each picture after all the m visits modulo 998244353. Can you help her? The expected weight of the i-th picture can be denoted by \frac {q_i} {p_i} where \gcd(p_i,q_i)=1, you need to print an integer r_i satisfying 0≀ r_i<998244353 and r_iβ‹… p_i≑ q_i\pmod{998244353}. It can be proved that such r_i exists and is unique. Input The first line contains two integers n and m (1≀ n≀ 50, 1≀ m≀ 50) β€” the number of pictures and the number of visits to the website. The second line contains n integers a_1,a_2,…,a_n (a_i is either 0 or 1) β€” if a_i=0 , Nauuo does not like the i-th picture; otherwise Nauuo likes the i-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains n integers w_1,w_2,…,w_n (1≀ w_i≀50) β€” the initial weights of the pictures. Output The output contains n integers r_1,r_2,…,r_n β€” the expected weights modulo 998244353. Examples Input 2 1 0 1 2 1 Output 332748119 332748119 Input 1 2 1 1 Output 3 Input 3 3 0 1 1 4 3 5 Output 160955686 185138929 974061117 Note In the first example, if the only visit shows the first picture with a probability of \frac 2 3, the final weights are (1,1); if the only visit shows the second picture with a probability of \frac1 3, the final weights are (2,2). So, both expected weights are \frac2 3β‹… 1+\frac 1 3β‹… 2=\frac4 3 . Because 332748119β‹… 3≑ 4\pmod{998244353}, you need to print 332748119 instead of \frac4 3 or 1.3333333333. In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, w_1 will be increased by 1. So, the expected weight is 1+2=3. Nauuo is very naughty so she didn't give you any hint of the third example.
2
11
#include <bits/stdc++.h> using namespace std; using INT = long long; const int mod = 998244353; int power(int a, int b, int m, int ans = 1) { for (; b; b >>= 1, a = 1LL * a * a % m) if (b & 1) ans = 1LL * ans * a % m; return ans; } const int N = 50; int a[110], w[110], dp[110][110][110], tmp[110][110][110]; void add(int &x, int y) { y = (y + mod) % mod; x += y; if (x >= mod) x -= mod; } int main() { int n, m; cin >> n >> m; for (int i = 1; i <= n; i++) cin >> a[i]; int sum = 0, ss = 0; for (int j = 1; j <= n; j++) { cin >> w[j]; sum += w[j]; if (a[j] == 0) ss += w[j]; } for (int i = 1; i <= n; i++) { dp[i][0][0] = 1; } for (int k = 1; k <= m; k++) { for (int i = 1; i <= n; i++) { for (int j = 0; j <= k - 1; j++) { for (int l = 0; l <= k - 1; l++) { int op = (a[i] == 0); int f = a[i]; if (f == 0) f = -1; int tot = sum + k - 1 - 2 * l; int pb = (INT)power(tot, mod - 2, mod) * (w[i] + f * j) % mod; int t = ss - l; if (a[i] == 0) t -= (w[i] - j); int qb = (INT)power(tot, mod - 2, mod) * t % mod; qb = (qb + mod) % mod; pb = (pb + mod) % mod; add(tmp[i][j + 1][l + op], (INT)pb * dp[i][j][l] % mod); add(tmp[i][j][l + 1], (INT)qb * dp[i][j][l] % mod); add(tmp[i][j][l], (INT)dp[i][j][l] * (1 - pb - qb) % mod); } } } for (int i = 1; i <= n; i++) for (int j = 0; j <= k; j++) for (int l = 0; l <= k; l++) { dp[i][j][l] = tmp[i][j][l]; tmp[i][j][l] = 0; } } for (int i = 1; i <= n; i++) { int ans = 0; int f = a[i]; if (f == 0) f = -1; for (int j = 0; j <= m; j++) { for (int l = 0; l <= m; l++) { add(ans, (INT)(w[i] + f * j) * dp[i][j][l] % mod); } } printf("%d\n", ans); } return 0; }
CPP
1173_E1. Nauuo and Pictures (easy version)
The only difference between easy and hard versions is constraints. Nauuo is a girl who loves random picture websites. One day she made a random picture website by herself which includes n pictures. When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The i-th picture has a non-negative weight w_i, and the probability of the i-th picture being displayed is \frac{w_i}{βˆ‘_{j=1}^nw_j}. That is to say, the probability of a picture to be displayed is proportional to its weight. However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add 1 to its weight; otherwise, she would subtract 1 from its weight. Nauuo will visit the website m times. She wants to know the expected weight of each picture after all the m visits modulo 998244353. Can you help her? The expected weight of the i-th picture can be denoted by \frac {q_i} {p_i} where \gcd(p_i,q_i)=1, you need to print an integer r_i satisfying 0≀ r_i<998244353 and r_iβ‹… p_i≑ q_i\pmod{998244353}. It can be proved that such r_i exists and is unique. Input The first line contains two integers n and m (1≀ n≀ 50, 1≀ m≀ 50) β€” the number of pictures and the number of visits to the website. The second line contains n integers a_1,a_2,…,a_n (a_i is either 0 or 1) β€” if a_i=0 , Nauuo does not like the i-th picture; otherwise Nauuo likes the i-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains n integers w_1,w_2,…,w_n (1≀ w_i≀50) β€” the initial weights of the pictures. Output The output contains n integers r_1,r_2,…,r_n β€” the expected weights modulo 998244353. Examples Input 2 1 0 1 2 1 Output 332748119 332748119 Input 1 2 1 1 Output 3 Input 3 3 0 1 1 4 3 5 Output 160955686 185138929 974061117 Note In the first example, if the only visit shows the first picture with a probability of \frac 2 3, the final weights are (1,1); if the only visit shows the second picture with a probability of \frac1 3, the final weights are (2,2). So, both expected weights are \frac2 3β‹… 1+\frac 1 3β‹… 2=\frac4 3 . Because 332748119β‹… 3≑ 4\pmod{998244353}, you need to print 332748119 instead of \frac4 3 or 1.3333333333. In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, w_1 will be increased by 1. So, the expected weight is 1+2=3. Nauuo is very naughty so she didn't give you any hint of the third example.
2
11
#include <bits/stdc++.h> using namespace std; const int MAXN = 1e5 + 10; const int MAXM = 3010; const int MOD = 998244353; int qpow(int a, int b) { int base = 1; while (b) { if (b & 1) base = 1ll * base * a % MOD; a = 1ll * a * a % MOD; b >>= 1; } return base; } int n, m, A[MAXN], W[MAXN], F[MAXM][MAXM], G[MAXM][MAXM], Inv[MAXM << 1], sum[10]; namespace io { inline int read() { int x = 0, f = 1; char ch = getchar(); while (!isdigit(ch)) if (ch == '-') f = -1, ch = getchar(); while (isdigit(ch)) x = x * 10 + ch - '0', ch = getchar(); return x * f; } char buf[1 << 21]; inline void write(int x) { if (x == 0) { putchar('0'); return; } int tmp = x < 0 ? -x : x; if (x < 0) putchar('-'); int cnt = 0; while (tmp > 0) { buf[cnt++] = tmp % 10 + '0'; tmp /= 10; } while (cnt > 0) putchar(buf[--cnt]); } } // namespace io using namespace io; int main() { n = read(), m = read(); for (register int i = 1; i <= n; ++i) A[i] = read(); for (register int i = 1; i <= n; ++i) W[i] = read(), sum[A[i]] += W[i], sum[2] += W[i]; for (register int i = 0 > m - sum[0] ? 0 : m - sum[0]; i <= 2 * m; ++i) Inv[i] = qpow(sum[2] + i - m, MOD - 2); for (register int i = m; i >= 0; --i) { F[i][m - i] = G[i][m - i] = 1; for (register int j = m - i - 1 < sum[0] ? m - i - 1 : sum[0]; j >= 0; --j) { F[i][j] = (1ll * (sum[1] + i + 1) * F[i + 1][j] + 1ll * (sum[0] - j) * F[i][j + 1]) % MOD * Inv[i - j + m] % MOD; G[i][j] = (1ll * (sum[1] + i) * G[i + 1][j] + 1ll * (sum[0] - j - 1) * G[i][j + 1]) % MOD * Inv[i - j + m] % MOD; } } for (register int i = 1; i <= n; ++i) write(int(1ll * W[i] * (A[i] ? F[0][0] : G[0][0]) % MOD)), putchar('\n'); return 0; }
CPP
1173_E1. Nauuo and Pictures (easy version)
The only difference between easy and hard versions is constraints. Nauuo is a girl who loves random picture websites. One day she made a random picture website by herself which includes n pictures. When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The i-th picture has a non-negative weight w_i, and the probability of the i-th picture being displayed is \frac{w_i}{βˆ‘_{j=1}^nw_j}. That is to say, the probability of a picture to be displayed is proportional to its weight. However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add 1 to its weight; otherwise, she would subtract 1 from its weight. Nauuo will visit the website m times. She wants to know the expected weight of each picture after all the m visits modulo 998244353. Can you help her? The expected weight of the i-th picture can be denoted by \frac {q_i} {p_i} where \gcd(p_i,q_i)=1, you need to print an integer r_i satisfying 0≀ r_i<998244353 and r_iβ‹… p_i≑ q_i\pmod{998244353}. It can be proved that such r_i exists and is unique. Input The first line contains two integers n and m (1≀ n≀ 50, 1≀ m≀ 50) β€” the number of pictures and the number of visits to the website. The second line contains n integers a_1,a_2,…,a_n (a_i is either 0 or 1) β€” if a_i=0 , Nauuo does not like the i-th picture; otherwise Nauuo likes the i-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains n integers w_1,w_2,…,w_n (1≀ w_i≀50) β€” the initial weights of the pictures. Output The output contains n integers r_1,r_2,…,r_n β€” the expected weights modulo 998244353. Examples Input 2 1 0 1 2 1 Output 332748119 332748119 Input 1 2 1 1 Output 3 Input 3 3 0 1 1 4 3 5 Output 160955686 185138929 974061117 Note In the first example, if the only visit shows the first picture with a probability of \frac 2 3, the final weights are (1,1); if the only visit shows the second picture with a probability of \frac1 3, the final weights are (2,2). So, both expected weights are \frac2 3β‹… 1+\frac 1 3β‹… 2=\frac4 3 . Because 332748119β‹… 3≑ 4\pmod{998244353}, you need to print 332748119 instead of \frac4 3 or 1.3333333333. In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, w_1 will be increased by 1. So, the expected weight is 1+2=3. Nauuo is very naughty so she didn't give you any hint of the third example.
2
11
#include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; const long long LINF = 0x3f3f3f3f3f3f3f3fLL; const double EPS = 1e-8; const int MOD = 998244353; const int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1}; int mod = MOD; struct ModInt { unsigned val; ModInt() : val(0) {} ModInt(long long x) : val(x >= 0 ? x % mod : x % mod + mod) {} ModInt pow(long long exponent) { ModInt tmp = *this, res = 1; while (exponent > 0) { if (exponent & 1) res *= tmp; tmp *= tmp; exponent >>= 1; } return res; } ModInt &operator+=(const ModInt &rhs) { if ((val += rhs.val) >= mod) val -= mod; return *this; } ModInt &operator-=(const ModInt &rhs) { if ((val += mod - rhs.val) >= mod) val -= mod; return *this; } ModInt &operator*=(const ModInt &rhs) { val = (unsigned long long)val * rhs.val % mod; return *this; } ModInt &operator/=(const ModInt &rhs) { return *this *= rhs.inv(); } bool operator==(const ModInt &rhs) const { return val == rhs.val; } bool operator!=(const ModInt &rhs) const { return val != rhs.val; } bool operator<(const ModInt &rhs) const { return val < rhs.val; } bool operator<=(const ModInt &rhs) const { return val <= rhs.val; } bool operator>(const ModInt &rhs) const { return val > rhs.val; } bool operator>=(const ModInt &rhs) const { return val >= rhs.val; } ModInt operator-() const { return ModInt(-val); } ModInt operator+(const ModInt &rhs) const { return ModInt(*this) += rhs; } ModInt operator-(const ModInt &rhs) const { return ModInt(*this) -= rhs; } ModInt operator*(const ModInt &rhs) const { return ModInt(*this) *= rhs; } ModInt operator/(const ModInt &rhs) const { return ModInt(*this) /= rhs; } friend ostream &operator<<(ostream &os, const ModInt &rhs) { return os << rhs.val; } friend istream &operator>>(istream &is, ModInt &rhs) { long long x; is >> x; rhs = ModInt(x); return is; } private: ModInt inv() const { unsigned a = val, b = mod; int x = 1, y = 0; while (b) { unsigned tmp = a / b; a -= tmp * b; swap(a, b); x -= tmp * y; swap(x, y); } return ModInt(x); } }; ModInt abs(const ModInt &x) { return x.val; } struct Combinatorics { Combinatorics(int MAX = 5000000) { MAX <<= 1; fact.resize(MAX + 1); fact_inv.resize(MAX + 1); fact[0] = 1; for (int i = (1); i < (MAX + 1); ++i) fact[i] = fact[i - 1] * i; fact_inv[MAX] = ModInt(1) / fact[MAX]; for (int i = MAX; i > 0; --i) fact_inv[i - 1] = fact_inv[i] * i; } ModInt nCk(int n, int k) { if (n < 0 || n < k || k < 0) return ModInt(0); return fact[n] * fact_inv[k] * fact_inv[n - k]; } ModInt nPk(int n, int k) { if (n < 0 || n < k || k < 0) return ModInt(0); return fact[n] * fact_inv[n - k]; } ModInt nHk(int n, int k) { if (n < 0 || k < 0) return ModInt(0); return (k == 0 ? ModInt(1) : nCk(n + k - 1, k)); } private: vector<ModInt> fact, fact_inv; }; int main() { cin.tie(0); ios::sync_with_stdio(false); int n, m; cin >> n >> m; vector<int> a(n); for (int i = (0); i < (n); ++i) cin >> a[i]; vector<int> w(n); for (int i = (0); i < (n); ++i) cin >> w[i]; int dislike = 0, like = 0; for (int i = (0); i < (n); ++i) { if (a[i] == 0) dislike += w[i]; else like += w[i]; } for (int i = (0); i < (n); ++i) { vector<vector<vector<ModInt> > > dp( m + 1, vector<vector<ModInt> >(m + 1, vector<ModInt>(m + 1, 0))); dp[0][0][0] = 1; int dl = dislike, l = like; if (a[i] == 0) dl -= w[i]; else l -= w[i]; for (int x = (0); x < (m); ++x) { for (int y = (0); y < (m + 1); ++y) for (int z = (0); z < (m + 1); ++z) { int sum = like + dislike - z + (x - y - z) + (a[i] == 0 ? -y : y); if (a[i] == 0 && y > w[i]) continue; if (z > dl || y + z > x || sum <= 0) continue; dp[x + 1][y + 1][z] += dp[x][y][z] * (w[i] + (a[i] == 0 ? -y : y)) / sum; dp[x + 1][y][z + 1] += dp[x][y][z] * (dl - z) / sum; dp[x + 1][y][z] += dp[x][y][z] * (l + (x - y - z)) / sum; } } ModInt ans = 0; for (int y = (0); y < (m + 1); ++y) for (int z = (0); z < (m + 1); ++z) { if (a[i] == 0 && y > w[i]) continue; if (z > dl || y + z > m) continue; ans += dp[m][y][z] * (w[i] + (a[i] == 0 ? -y : y)); } cout << ans << '\n'; } return 0; }
CPP
1173_E1. Nauuo and Pictures (easy version)
The only difference between easy and hard versions is constraints. Nauuo is a girl who loves random picture websites. One day she made a random picture website by herself which includes n pictures. When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The i-th picture has a non-negative weight w_i, and the probability of the i-th picture being displayed is \frac{w_i}{βˆ‘_{j=1}^nw_j}. That is to say, the probability of a picture to be displayed is proportional to its weight. However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add 1 to its weight; otherwise, she would subtract 1 from its weight. Nauuo will visit the website m times. She wants to know the expected weight of each picture after all the m visits modulo 998244353. Can you help her? The expected weight of the i-th picture can be denoted by \frac {q_i} {p_i} where \gcd(p_i,q_i)=1, you need to print an integer r_i satisfying 0≀ r_i<998244353 and r_iβ‹… p_i≑ q_i\pmod{998244353}. It can be proved that such r_i exists and is unique. Input The first line contains two integers n and m (1≀ n≀ 50, 1≀ m≀ 50) β€” the number of pictures and the number of visits to the website. The second line contains n integers a_1,a_2,…,a_n (a_i is either 0 or 1) β€” if a_i=0 , Nauuo does not like the i-th picture; otherwise Nauuo likes the i-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains n integers w_1,w_2,…,w_n (1≀ w_i≀50) β€” the initial weights of the pictures. Output The output contains n integers r_1,r_2,…,r_n β€” the expected weights modulo 998244353. Examples Input 2 1 0 1 2 1 Output 332748119 332748119 Input 1 2 1 1 Output 3 Input 3 3 0 1 1 4 3 5 Output 160955686 185138929 974061117 Note In the first example, if the only visit shows the first picture with a probability of \frac 2 3, the final weights are (1,1); if the only visit shows the second picture with a probability of \frac1 3, the final weights are (2,2). So, both expected weights are \frac2 3β‹… 1+\frac 1 3β‹… 2=\frac4 3 . Because 332748119β‹… 3≑ 4\pmod{998244353}, you need to print 332748119 instead of \frac4 3 or 1.3333333333. In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, w_1 will be increased by 1. So, the expected weight is 1+2=3. Nauuo is very naughty so she didn't give you any hint of the third example.
2
11
#include <bits/stdc++.h> using namespace std; const int MOD = 998244353; const int N = 55; long long ans; long long dp[N][N * 2][N]; long long rev[N * 2]; int n, m, tot, one, zero; int l[N], w[N]; long long Pow(int x, int y) { long long ret = 1ll, mt = 1ll * x; while (y) { if (y & 1) ret = (ret * mt) % MOD; y >>= 1; mt = (mt * mt) % MOD; } return ret; } void print(int x, int y, int z) { printf("dp[%d][%d][%d]=%I64d\n", x, y, z, dp[x][y][z]); } int main() { scanf("%d%d", &n, &m); for (int i = 0; i < n; i++) { scanf("%d", l + i); if (l[i] == 0) l[i]--; } for (int i = 0; i < n; i++) { scanf("%d", w + i); tot += w[i]; if (l[i] == 1) one += w[i]; else zero += w[i]; } for (int i = -50; i <= 50; i++) if (tot + i >= 0) rev[N + i] = Pow(tot + i, MOD - 2); for (int x = 0; x < n; x++) { memset(dp, 0, sizeof(dp)); dp[0][N][0] = 1ll; for (int i = 0; i < m; i++) for (int j = N - 50; j <= N + 50; j++) for (int k = 0; k <= 50; k++) if (dp[i][j][k]) { dp[i + 1][j + l[x]][k + 1] = (dp[i + 1][j + l[x]][k + 1] + dp[i][j][k] * (w[x] + k * l[x]) % MOD * rev[j] % MOD) % MOD; int J = j - N, ONE = one, ZERO = zero; int inc = (J + i) / 2, dec = (J - i) / 2; if (l[x] == 1) ONE = (ONE - w[x] - k * l[x] + MOD) % MOD; else ZERO = (ZERO - w[x] - k * l[x] + MOD) % MOD; ONE += inc; ZERO += dec; dp[i + 1][j + 1][k] = (dp[i + 1][j + 1][k] + dp[i][j][k] * ONE % MOD * rev[j] % MOD) % MOD; dp[i + 1][j - 1][k] = (dp[i + 1][j - 1][k] + dp[i][j][k] * ZERO % MOD * rev[j] % MOD) % MOD; } ans = 0ll; for (int j = N - 50; j <= N + 50; j++) for (int k = 0; k <= 50; k++) { ans = (ans + dp[m][j][k] * (w[x] + k * l[x] % MOD + MOD) % MOD) % MOD; } printf("%I64d\n", ans); } return 0; }
CPP
1173_E1. Nauuo and Pictures (easy version)
The only difference between easy and hard versions is constraints. Nauuo is a girl who loves random picture websites. One day she made a random picture website by herself which includes n pictures. When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The i-th picture has a non-negative weight w_i, and the probability of the i-th picture being displayed is \frac{w_i}{βˆ‘_{j=1}^nw_j}. That is to say, the probability of a picture to be displayed is proportional to its weight. However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add 1 to its weight; otherwise, she would subtract 1 from its weight. Nauuo will visit the website m times. She wants to know the expected weight of each picture after all the m visits modulo 998244353. Can you help her? The expected weight of the i-th picture can be denoted by \frac {q_i} {p_i} where \gcd(p_i,q_i)=1, you need to print an integer r_i satisfying 0≀ r_i<998244353 and r_iβ‹… p_i≑ q_i\pmod{998244353}. It can be proved that such r_i exists and is unique. Input The first line contains two integers n and m (1≀ n≀ 50, 1≀ m≀ 50) β€” the number of pictures and the number of visits to the website. The second line contains n integers a_1,a_2,…,a_n (a_i is either 0 or 1) β€” if a_i=0 , Nauuo does not like the i-th picture; otherwise Nauuo likes the i-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains n integers w_1,w_2,…,w_n (1≀ w_i≀50) β€” the initial weights of the pictures. Output The output contains n integers r_1,r_2,…,r_n β€” the expected weights modulo 998244353. Examples Input 2 1 0 1 2 1 Output 332748119 332748119 Input 1 2 1 1 Output 3 Input 3 3 0 1 1 4 3 5 Output 160955686 185138929 974061117 Note In the first example, if the only visit shows the first picture with a probability of \frac 2 3, the final weights are (1,1); if the only visit shows the second picture with a probability of \frac1 3, the final weights are (2,2). So, both expected weights are \frac2 3β‹… 1+\frac 1 3β‹… 2=\frac4 3 . Because 332748119β‹… 3≑ 4\pmod{998244353}, you need to print 332748119 instead of \frac4 3 or 1.3333333333. In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, w_1 will be increased by 1. So, the expected weight is 1+2=3. Nauuo is very naughty so she didn't give you any hint of the third example.
2
11
#include <bits/stdc++.h> using namespace std; using namespace std; int n, m; long long bm(long long base, int power) { if (power == 0) { return 1; } if (power % 2 == 0) { long long ret = bm(base, power / 2); return (ret * ret) % 998244353; } else { return (base * bm(base, power - 1)) % 998244353; } } long long mod_inv(long long num) { return bm(num, 998244353 - 2); } int dp[2560][110][60]; bool foo; int init_pos_wei = 0; int init_neg_wei = 0; long long func_pos(long long pos_wei, long long cur_wei, int vis) { long long pos_used = (pos_wei + cur_wei - init_pos_wei); long long neg_used = vis - pos_used; long long neg_wei = init_neg_wei - neg_used; if (vis == m) { return cur_wei; } if (dp[pos_wei][cur_wei][vis] != -1) { return dp[pos_wei][cur_wei][vis]; } long long ans = 0; long long all = pos_wei + neg_wei + cur_wei; if (cur_wei) { ans += (((cur_wei * mod_inv(all)) % 998244353) * func_pos(pos_wei, cur_wei + 1, vis + 1)) % 998244353; } if (pos_wei) { ans += (((pos_wei * mod_inv(all)) % 998244353) * func_pos(pos_wei + 1, cur_wei, vis + 1)) % 998244353; } if (neg_wei) { ans += (((neg_wei * mod_inv(all)) % 998244353) * func_pos(pos_wei, cur_wei, vis + 1)) % 998244353; } ans %= 998244353; return dp[pos_wei][cur_wei][vis] = ans; } long long func_neg(long long neg_wei, long long cur_wei, int vis) { long long neg_used = init_neg_wei - (neg_wei + cur_wei); long long pos_used = vis - neg_used; long long pos_wei = init_pos_wei + pos_used; if (vis == m) { return cur_wei; } if (dp[neg_wei][cur_wei][vis] != -1) { return dp[neg_wei][cur_wei][vis]; } long long ans = 0; long long all = pos_wei + neg_wei + cur_wei; if (cur_wei) { ans += (((cur_wei * mod_inv(all)) % 998244353) * func_neg(neg_wei, cur_wei - 1, vis + 1)) % 998244353; } if (pos_wei) { ans += (((pos_wei * mod_inv(all)) % 998244353) * func_neg(neg_wei, cur_wei, vis + 1)) % 998244353; } if (neg_wei) { ans += (((neg_wei * mod_inv(all)) % 998244353) * func_neg(neg_wei - 1, cur_wei, vis + 1)) % 998244353; } ans %= 998244353; return dp[neg_wei][cur_wei][vis] = ans; } int res[60]; bool is_like[60]; int wei[60]; int main() { cin >> n >> m; for (int i = 0; i <= n - 1; i++) { cin >> is_like[i]; } for (int i = 0; i <= n - 1; i++) { cin >> wei[i]; if (is_like[i]) { init_pos_wei += wei[i]; } else { init_neg_wei += wei[i]; } } memset((dp), -1, sizeof(dp)); for (int i = 0; i <= n - 1; i++) { if (is_like[i]) { res[i] = func_pos(init_pos_wei - wei[i], wei[i], 0); } } memset((dp), -1, sizeof(dp)); for (int i = 0; i <= n - 1; i++) { if (!is_like[i]) { res[i] = func_neg(init_neg_wei - wei[i], wei[i], 0); } } for (int i = 0; i <= n - 1; i++) { cout << res[i] << "\n"; } return 0; }
CPP
1173_E1. Nauuo and Pictures (easy version)
The only difference between easy and hard versions is constraints. Nauuo is a girl who loves random picture websites. One day she made a random picture website by herself which includes n pictures. When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The i-th picture has a non-negative weight w_i, and the probability of the i-th picture being displayed is \frac{w_i}{βˆ‘_{j=1}^nw_j}. That is to say, the probability of a picture to be displayed is proportional to its weight. However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add 1 to its weight; otherwise, she would subtract 1 from its weight. Nauuo will visit the website m times. She wants to know the expected weight of each picture after all the m visits modulo 998244353. Can you help her? The expected weight of the i-th picture can be denoted by \frac {q_i} {p_i} where \gcd(p_i,q_i)=1, you need to print an integer r_i satisfying 0≀ r_i<998244353 and r_iβ‹… p_i≑ q_i\pmod{998244353}. It can be proved that such r_i exists and is unique. Input The first line contains two integers n and m (1≀ n≀ 50, 1≀ m≀ 50) β€” the number of pictures and the number of visits to the website. The second line contains n integers a_1,a_2,…,a_n (a_i is either 0 or 1) β€” if a_i=0 , Nauuo does not like the i-th picture; otherwise Nauuo likes the i-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains n integers w_1,w_2,…,w_n (1≀ w_i≀50) β€” the initial weights of the pictures. Output The output contains n integers r_1,r_2,…,r_n β€” the expected weights modulo 998244353. Examples Input 2 1 0 1 2 1 Output 332748119 332748119 Input 1 2 1 1 Output 3 Input 3 3 0 1 1 4 3 5 Output 160955686 185138929 974061117 Note In the first example, if the only visit shows the first picture with a probability of \frac 2 3, the final weights are (1,1); if the only visit shows the second picture with a probability of \frac1 3, the final weights are (2,2). So, both expected weights are \frac2 3β‹… 1+\frac 1 3β‹… 2=\frac4 3 . Because 332748119β‹… 3≑ 4\pmod{998244353}, you need to print 332748119 instead of \frac4 3 or 1.3333333333. In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, w_1 will be increased by 1. So, the expected weight is 1+2=3. Nauuo is very naughty so she didn't give you any hint of the third example.
2
11
#include <bits/stdc++.h> using namespace std; const long long N = 60; const long long INF = 0x3f3f3f3f; const long long iinf = 1 << 30; const long long linf = 2e18; const long long MOD = 998244353; const double eps = 1e-7; void douout(double x) { printf("%lf\n", x + 0.0000000001); } template <class T> void print(T a) { cout << a << endl; exit(0); } template <class T> void chmin(T &a, T b) { if (a > b) a = b; } template <class T> void chmax(T &a, T b) { if (a < b) a = b; } void upd(long long &a, long long b) { a = (long long)(a + b) % MOD; } template <class T> void mul(T &a, T b) { a = (long long)a * b % MOD; } template <class T> T read() { long long f = 1; T x = 0; char ch = getchar(); while (!isdigit(ch)) { if (ch == '-') f = -1; ch = getchar(); } while (isdigit(ch)) { x = x * 10 + ch - '0'; ch = getchar(); } return x * f; } long long n, m, suma, sumb; long long a[N], w[N], inv[N * N]; long long f[N][N][N][N]; void init(long long x) { inv[1] = 1; for (long long i = (2); i <= (x); i++) inv[i] = 1ll * inv[MOD % i] * (MOD - MOD / i) % MOD; } signed main() { scanf("%lld%lld", &n, &m); for (long long i = (1); i <= (n); i++) scanf("%lld", &a[i]), a[i] = (a[i] * 2) - 1; for (long long i = (1); i <= (n); i++) scanf("%lld", &w[i]); for (long long i = (1); i <= (n); i++) { if (a[i] > 0) upd(suma, w[i]); else upd(sumb, w[i]); } init(suma + sumb + m); for (long long t = (1); t <= (n); t++) { memset(f, 0, sizeof(f)); f[0][0][0][0] = 1; for (long long i = (0); i <= (m - 1); i++) for (long long j = (0); j <= (i); j++) for (long long k = (0); k <= (i); k++) for (long long d = (0); d <= ((a[t] > 0 ? j : k)); d++) { if (!f[i][j][k][d]) continue; long long x = 1ll * f[i][j][k][d] * inv[suma + sumb + j - k] % MOD; if (a[t] > 0) { upd(f[i + 1][j + 1][k][d + 1], 1ll * (w[t] + d) * x % MOD); upd(f[i + 1][j + 1][k][d], 1ll * (suma + j - w[t] - d) * x % MOD); upd(f[i + 1][j][k + 1][d], 1ll * (sumb - k) * x % MOD); } else { upd(f[i + 1][j][k + 1][d + 1], 1ll * (w[t] - d) * x % MOD); upd(f[i + 1][j][k + 1][d], 1ll * (sumb - k - w[t] + d) * x % MOD); upd(f[i + 1][j + 1][k][d], 1ll * (suma + j) * x % MOD); } } long long ans = 0; for (long long j = (0); j <= (m); j++) for (long long k = (0); k <= (m); k++) for (long long d = (0); d <= ((a[t] > 0 ? j : k)); d++) upd(ans, 1ll * (f[m][j][k][d] * (w[t] + a[t] * d) % MOD)); printf("%lld\n", ans); } return 0; }
CPP
1173_E1. Nauuo and Pictures (easy version)
The only difference between easy and hard versions is constraints. Nauuo is a girl who loves random picture websites. One day she made a random picture website by herself which includes n pictures. When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The i-th picture has a non-negative weight w_i, and the probability of the i-th picture being displayed is \frac{w_i}{βˆ‘_{j=1}^nw_j}. That is to say, the probability of a picture to be displayed is proportional to its weight. However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add 1 to its weight; otherwise, she would subtract 1 from its weight. Nauuo will visit the website m times. She wants to know the expected weight of each picture after all the m visits modulo 998244353. Can you help her? The expected weight of the i-th picture can be denoted by \frac {q_i} {p_i} where \gcd(p_i,q_i)=1, you need to print an integer r_i satisfying 0≀ r_i<998244353 and r_iβ‹… p_i≑ q_i\pmod{998244353}. It can be proved that such r_i exists and is unique. Input The first line contains two integers n and m (1≀ n≀ 50, 1≀ m≀ 50) β€” the number of pictures and the number of visits to the website. The second line contains n integers a_1,a_2,…,a_n (a_i is either 0 or 1) β€” if a_i=0 , Nauuo does not like the i-th picture; otherwise Nauuo likes the i-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains n integers w_1,w_2,…,w_n (1≀ w_i≀50) β€” the initial weights of the pictures. Output The output contains n integers r_1,r_2,…,r_n β€” the expected weights modulo 998244353. Examples Input 2 1 0 1 2 1 Output 332748119 332748119 Input 1 2 1 1 Output 3 Input 3 3 0 1 1 4 3 5 Output 160955686 185138929 974061117 Note In the first example, if the only visit shows the first picture with a probability of \frac 2 3, the final weights are (1,1); if the only visit shows the second picture with a probability of \frac1 3, the final weights are (2,2). So, both expected weights are \frac2 3β‹… 1+\frac 1 3β‹… 2=\frac4 3 . Because 332748119β‹… 3≑ 4\pmod{998244353}, you need to print 332748119 instead of \frac4 3 or 1.3333333333. In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, w_1 will be increased by 1. So, the expected weight is 1+2=3. Nauuo is very naughty so she didn't give you any hint of the third example.
2
11
#include <bits/stdc++.h> using namespace std; const int P = 998244353, INF = 0x3f3f3f3f; long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } long long qpow(long long a, long long n) { long long r = 1 % P; for (a %= P; n; a = a * a % P, n >>= 1) if (n & 1) r = r * a % P; return r; } long long inv(long long first) { return first <= 1 ? 1 : inv(P % first) * (P - P / first) % P; } inline int rd() { int first = 0; char p = getchar(); while (p < '0' || p > '9') p = getchar(); while (p >= '0' && p <= '9') first = first * 10 + p - '0', p = getchar(); return first; } const int N = 1e6 + 10; int n, m, a[N], w[N], s1, s0; int in[N]; int dp[55][55][55]; int solve(int a, int w) { memset(dp, 0, sizeof dp); dp[0][0][0] = 1; int ans = 0; if (a) { for (int i = 1; i <= m + 1; ++i) for (int j = 0; j <= i - 1; ++j) for (int k = 0; k <= j; ++k) if (dp[i - 1][j][k]) { int &r = dp[i - 1][j][k]; int now = w + k, tot = s1 + s0 + j - (i - 1 - j), p = (long long)now * in[tot] % P; dp[i][j + 1][k + 1] = (dp[i][j + 1][k + 1] + (long long)p * r) % P; now = s1 - w + j - k, p = (long long)now * in[tot] % P; dp[i][j + 1][k] = (dp[i][j + 1][k] + (long long)p * r) % P; now = s0 - (i - 1 - j), p = (long long)now * in[tot] % P; dp[i][j][k] = (dp[i][j][k] + (long long)p * r) % P; if (i - 1 == m) ans = (ans + (long long)(w + k) * r) % P; } return ans; } for (int i = 1; i <= m + 1; ++i) for (int j = 0; j <= i - 1; ++j) for (int k = 0; k <= min(w, j); ++k) if (dp[i - 1][j][k]) { int &r = dp[i - 1][j][k]; int now = w - k, tot = s0 + s1 - j + (i - 1 - j), p = (long long)now * in[tot] % P; dp[i][j + 1][k + 1] = (dp[i][j + 1][k + 1] + (long long)p * r) % P; now = s1 + (i - 1 - j), p = (long long)now * in[tot] % P; dp[i][j][k] = (dp[i][j][k] + (long long)p * r) % P; now = s0 - w - j + k, p = (long long)now * in[tot] % P; dp[i][j + 1][k] = (dp[i][j + 1][k] + (long long)p * r) % P; if (i - 1 == m) ans = (ans + (long long)(w - k) * r) % P; } return ans; } int main() { in[0] = 1; for (int i = 1; i <= N - 1; ++i) in[i] = inv(i); scanf("%d%d", &n, &m); for (int i = 1; i <= n; ++i) scanf("%d", a + i); for (int i = 1; i <= n; ++i) { scanf("%d", w + i); if (a[i]) s1 += w[i]; else s0 += w[i]; } for (int i = 1; i <= n; ++i) printf("%d\n", solve(a[i], w[i])); }
CPP
1173_E1. Nauuo and Pictures (easy version)
The only difference between easy and hard versions is constraints. Nauuo is a girl who loves random picture websites. One day she made a random picture website by herself which includes n pictures. When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The i-th picture has a non-negative weight w_i, and the probability of the i-th picture being displayed is \frac{w_i}{βˆ‘_{j=1}^nw_j}. That is to say, the probability of a picture to be displayed is proportional to its weight. However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add 1 to its weight; otherwise, she would subtract 1 from its weight. Nauuo will visit the website m times. She wants to know the expected weight of each picture after all the m visits modulo 998244353. Can you help her? The expected weight of the i-th picture can be denoted by \frac {q_i} {p_i} where \gcd(p_i,q_i)=1, you need to print an integer r_i satisfying 0≀ r_i<998244353 and r_iβ‹… p_i≑ q_i\pmod{998244353}. It can be proved that such r_i exists and is unique. Input The first line contains two integers n and m (1≀ n≀ 50, 1≀ m≀ 50) β€” the number of pictures and the number of visits to the website. The second line contains n integers a_1,a_2,…,a_n (a_i is either 0 or 1) β€” if a_i=0 , Nauuo does not like the i-th picture; otherwise Nauuo likes the i-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains n integers w_1,w_2,…,w_n (1≀ w_i≀50) β€” the initial weights of the pictures. Output The output contains n integers r_1,r_2,…,r_n β€” the expected weights modulo 998244353. Examples Input 2 1 0 1 2 1 Output 332748119 332748119 Input 1 2 1 1 Output 3 Input 3 3 0 1 1 4 3 5 Output 160955686 185138929 974061117 Note In the first example, if the only visit shows the first picture with a probability of \frac 2 3, the final weights are (1,1); if the only visit shows the second picture with a probability of \frac1 3, the final weights are (2,2). So, both expected weights are \frac2 3β‹… 1+\frac 1 3β‹… 2=\frac4 3 . Because 332748119β‹… 3≑ 4\pmod{998244353}, you need to print 332748119 instead of \frac4 3 or 1.3333333333. In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, w_1 will be increased by 1. So, the expected weight is 1+2=3. Nauuo is very naughty so she didn't give you any hint of the third example.
2
11
#include <bits/stdc++.h> using namespace std; const int N = 55; const long long mod = 998244353; long long a[N], w[N], dp[N][N][N], pre[N][N][N]; long long exgcd(long long a, long long b, long long &x, long long &y) { if (b == 0) { x = 1; y = 0; return a; } long long r = exgcd(b, a % b, x, y); long long t = x; x = y; y = t - a / b * y; return r; } long long get_inv(long long a, long long b) { long long x, y; long long d = exgcd(b, mod, x, y); x = (x + mod) % mod; return x * a % mod; } long long solve(long long a, long long b, long long c, long long m, int type) { memset(dp, 0, sizeof(dp)); memset(pre, 0, sizeof(pre)); dp[0][0][0] = 1; for (int t = 1; t <= m; t++) { for (int i = 0; i <= t; i++) { for (int j = 0; j <= t; j++) { for (int k = 0; k <= t; k++) { if (dp[i][j][k] == 0) continue; long long sum = a + b + c + j - k; if (type == 0) sum -= i; else sum += i; if (type == 0 && a - i > 0) { pre[i + 1][j][k] = (pre[i + 1][j][k] + dp[i][j][k] * get_inv(a - i, sum) % mod) % mod; } else if (type == 1) { pre[i + 1][j][k] = (pre[i + 1][j][k] + dp[i][j][k] * get_inv(a + i, sum) % mod) % mod; } pre[i][j + 1][k] = (pre[i][j + 1][k] + dp[i][j][k] * get_inv(b + j, sum) % mod) % mod; if (c - k > 0) pre[i][j][k + 1] = (pre[i][j][k + 1] + dp[i][j][k] * get_inv(c - k, sum) % mod) % mod; } } } for (int i = 0; i <= t; i++) { for (int j = 0; j <= t; j++) { for (int k = 0; k <= t; k++) { dp[i][j][k] = pre[i][j][k]; pre[i][j][k] = 0; } } } } long long res = 0; for (int i = 0; i <= m; i++) { for (int j = 0; j <= m; j++) for (int k = 0; k <= m; k++) { long long v; if (type == 0) v = a - i; else v = a + i; if (v < 0 || c - k < 0 || dp[i][j][k] == 0) continue; res = (res + v * dp[i][j][k] % mod) % mod; } } return res; } int main() { int n, m, one = 0, zero = 0; scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) { scanf("%d", a + i); } for (int i = 1; i <= n; i++) { scanf("%d", w + i); if (a[i] == 1) one += w[i]; else zero += w[i]; } for (int i = 1; i <= n; i++) { long long sum1 = one, sum2 = zero; if (a[i] == 1) sum1 -= w[i]; else sum2 -= w[i]; printf("%lld\n", solve(w[i], sum1, sum2, m, a[i])); } }
CPP
1173_E1. Nauuo and Pictures (easy version)
The only difference between easy and hard versions is constraints. Nauuo is a girl who loves random picture websites. One day she made a random picture website by herself which includes n pictures. When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The i-th picture has a non-negative weight w_i, and the probability of the i-th picture being displayed is \frac{w_i}{βˆ‘_{j=1}^nw_j}. That is to say, the probability of a picture to be displayed is proportional to its weight. However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add 1 to its weight; otherwise, she would subtract 1 from its weight. Nauuo will visit the website m times. She wants to know the expected weight of each picture after all the m visits modulo 998244353. Can you help her? The expected weight of the i-th picture can be denoted by \frac {q_i} {p_i} where \gcd(p_i,q_i)=1, you need to print an integer r_i satisfying 0≀ r_i<998244353 and r_iβ‹… p_i≑ q_i\pmod{998244353}. It can be proved that such r_i exists and is unique. Input The first line contains two integers n and m (1≀ n≀ 50, 1≀ m≀ 50) β€” the number of pictures and the number of visits to the website. The second line contains n integers a_1,a_2,…,a_n (a_i is either 0 or 1) β€” if a_i=0 , Nauuo does not like the i-th picture; otherwise Nauuo likes the i-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains n integers w_1,w_2,…,w_n (1≀ w_i≀50) β€” the initial weights of the pictures. Output The output contains n integers r_1,r_2,…,r_n β€” the expected weights modulo 998244353. Examples Input 2 1 0 1 2 1 Output 332748119 332748119 Input 1 2 1 1 Output 3 Input 3 3 0 1 1 4 3 5 Output 160955686 185138929 974061117 Note In the first example, if the only visit shows the first picture with a probability of \frac 2 3, the final weights are (1,1); if the only visit shows the second picture with a probability of \frac1 3, the final weights are (2,2). So, both expected weights are \frac2 3β‹… 1+\frac 1 3β‹… 2=\frac4 3 . Because 332748119β‹… 3≑ 4\pmod{998244353}, you need to print 332748119 instead of \frac4 3 or 1.3333333333. In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, w_1 will be increased by 1. So, the expected weight is 1+2=3. Nauuo is very naughty so she didn't give you any hint of the third example.
2
11
#include <bits/stdc++.h> using namespace std; const int mod = 998244353; const int M = 2e5 + 10; const int N = 3e3 + 10; int n, m, a[M], w[M], sum[3], f[N][N], g[N][N], inv[N << 1]; int qpow(int a, int b) { int y = 1; for (; b; b >>= 1, a = (long long)a * a % mod) if (b & 1) y = (long long)y * a % mod; return y; } int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); for (int i = 1; i <= n; i++) scanf("%d", &w[i]), sum[a[i]] += w[i]; for (int i = max(0, m - sum[0]); i <= 2 * m; i++) inv[i] = qpow(sum[0] + sum[1] + i - m, mod - 2); for (int i = m; i >= 0; i--) { f[i][m - i] = g[i][m - i] = 1; for (int j = min(m - i - 1, sum[0]); j >= 0; j--) { f[i][j] = ((long long)f[i + 1][j] * (sum[1] + i + 1) % mod + (long long)f[i][j + 1] * (sum[0] - j)) % mod * inv[m + i - j] % mod; g[i][j] = ((long long)g[i][j + 1] * (sum[0] - j - 1) % mod + (long long)g[i + 1][j] * (sum[1] + i)) % mod * inv[m + i - j] % mod; } } for (int i = 1; i <= n; i++) printf("%d\n", int((long long)w[i] * (a[i] ? f[0][0] : g[0][0]) % mod)); return 0; }
CPP
1173_E1. Nauuo and Pictures (easy version)
The only difference between easy and hard versions is constraints. Nauuo is a girl who loves random picture websites. One day she made a random picture website by herself which includes n pictures. When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The i-th picture has a non-negative weight w_i, and the probability of the i-th picture being displayed is \frac{w_i}{βˆ‘_{j=1}^nw_j}. That is to say, the probability of a picture to be displayed is proportional to its weight. However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add 1 to its weight; otherwise, she would subtract 1 from its weight. Nauuo will visit the website m times. She wants to know the expected weight of each picture after all the m visits modulo 998244353. Can you help her? The expected weight of the i-th picture can be denoted by \frac {q_i} {p_i} where \gcd(p_i,q_i)=1, you need to print an integer r_i satisfying 0≀ r_i<998244353 and r_iβ‹… p_i≑ q_i\pmod{998244353}. It can be proved that such r_i exists and is unique. Input The first line contains two integers n and m (1≀ n≀ 50, 1≀ m≀ 50) β€” the number of pictures and the number of visits to the website. The second line contains n integers a_1,a_2,…,a_n (a_i is either 0 or 1) β€” if a_i=0 , Nauuo does not like the i-th picture; otherwise Nauuo likes the i-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains n integers w_1,w_2,…,w_n (1≀ w_i≀50) β€” the initial weights of the pictures. Output The output contains n integers r_1,r_2,…,r_n β€” the expected weights modulo 998244353. Examples Input 2 1 0 1 2 1 Output 332748119 332748119 Input 1 2 1 1 Output 3 Input 3 3 0 1 1 4 3 5 Output 160955686 185138929 974061117 Note In the first example, if the only visit shows the first picture with a probability of \frac 2 3, the final weights are (1,1); if the only visit shows the second picture with a probability of \frac1 3, the final weights are (2,2). So, both expected weights are \frac2 3β‹… 1+\frac 1 3β‹… 2=\frac4 3 . Because 332748119β‹… 3≑ 4\pmod{998244353}, you need to print 332748119 instead of \frac4 3 or 1.3333333333. In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, w_1 will be increased by 1. So, the expected weight is 1+2=3. Nauuo is very naughty so she didn't give you any hint of the third example.
2
11
#include <bits/stdc++.h> using namespace std; const long long mod = 998244353; int n, m; int a[66], w[66]; long long dp[66][66][66]; int sa, sb, sum; long long inv[10010]; void solve1(int x) { memset(dp, 0, sizeof(dp)); dp[0][0][0] = 1; long long zi, mu; for (int i = 0; i < m; ++i) for (int j = 0; j <= i; ++j) for (int k = j; k <= i; ++k) { if (!dp[i][j][k]) continue; mu = inv[sum + k * 2 - i]; zi = (w[x] + j) % mod; (dp[i + 1][j + 1][k + 1] += dp[i][j][k] * zi % mod * mu % mod) %= mod; zi = (sa + k - j - w[x] + mod) % mod; (dp[i + 1][j][k + 1] += dp[i][j][k] * zi % mod * mu % mod) %= mod; zi = (sb - i + k + mod) % mod; (dp[i + 1][j][k] += dp[i][j][k] * zi % mod * mu % mod) %= mod; } long long r = 0; for (int j = 0; j <= m; ++j) for (int k = j; k <= m; ++k) { if (!dp[m][j][k]) continue; r += dp[m][j][k] * (w[x] + j) % mod; r %= mod; } printf("%lld\n", r); return; } void solve2(int x) { memset(dp, 0, sizeof(dp)); dp[0][0][0] = 1; long long zi, mu; for (int i = 0; i < m; ++i) for (int j = 0; j <= i; ++j) for (int k = j; k <= i; ++k) { if (!dp[i][j][k]) continue; mu = inv[sum - k * 2 + i]; zi = (w[x] - j) % mod; (dp[i + 1][j + 1][k + 1] += dp[i][j][k] * zi % mod * mu % mod) %= mod; zi = (sb - w[x] - k + j + mod) % mod; (dp[i + 1][j][k + 1] += dp[i][j][k] * zi % mod * mu % mod) %= mod; zi = (sa + i - k + mod) % mod; (dp[i + 1][j][k] += dp[i][j][k] * zi % mod * mu % mod) %= mod; } long long r = 0; for (int j = 0; j <= m; ++j) for (int k = j; k <= m; ++k) { if (!dp[m][j][k]) continue; r += dp[m][j][k] * max(0, w[x] - j) % mod; r %= mod; } printf("%lld\n", r); return; } int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; ++i) scanf("%d", &a[i]); for (int i = 1; i <= n; ++i) { scanf("%d", &w[i]); sa += a[i] * w[i]; sb += (a[i] ^ 1) * w[i]; } sum = sa + sb; inv[1] = 1; for (int i = 2; i <= 10000; ++i) inv[i] = (mod - mod / i) * inv[mod % i] % mod; for (int i = 1; i <= n; ++i) if (a[i]) solve1(i); else solve2(i); return 0; }
CPP
1173_E1. Nauuo and Pictures (easy version)
The only difference between easy and hard versions is constraints. Nauuo is a girl who loves random picture websites. One day she made a random picture website by herself which includes n pictures. When Nauuo visits the website, she sees exactly one picture. The website does not display each picture with equal probability. The i-th picture has a non-negative weight w_i, and the probability of the i-th picture being displayed is \frac{w_i}{βˆ‘_{j=1}^nw_j}. That is to say, the probability of a picture to be displayed is proportional to its weight. However, Nauuo discovered that some pictures she does not like were displayed too often. To solve this problem, she came up with a great idea: when she saw a picture she likes, she would add 1 to its weight; otherwise, she would subtract 1 from its weight. Nauuo will visit the website m times. She wants to know the expected weight of each picture after all the m visits modulo 998244353. Can you help her? The expected weight of the i-th picture can be denoted by \frac {q_i} {p_i} where \gcd(p_i,q_i)=1, you need to print an integer r_i satisfying 0≀ r_i<998244353 and r_iβ‹… p_i≑ q_i\pmod{998244353}. It can be proved that such r_i exists and is unique. Input The first line contains two integers n and m (1≀ n≀ 50, 1≀ m≀ 50) β€” the number of pictures and the number of visits to the website. The second line contains n integers a_1,a_2,…,a_n (a_i is either 0 or 1) β€” if a_i=0 , Nauuo does not like the i-th picture; otherwise Nauuo likes the i-th picture. It is guaranteed that there is at least one picture which Nauuo likes. The third line contains n integers w_1,w_2,…,w_n (1≀ w_i≀50) β€” the initial weights of the pictures. Output The output contains n integers r_1,r_2,…,r_n β€” the expected weights modulo 998244353. Examples Input 2 1 0 1 2 1 Output 332748119 332748119 Input 1 2 1 1 Output 3 Input 3 3 0 1 1 4 3 5 Output 160955686 185138929 974061117 Note In the first example, if the only visit shows the first picture with a probability of \frac 2 3, the final weights are (1,1); if the only visit shows the second picture with a probability of \frac1 3, the final weights are (2,2). So, both expected weights are \frac2 3β‹… 1+\frac 1 3β‹… 2=\frac4 3 . Because 332748119β‹… 3≑ 4\pmod{998244353}, you need to print 332748119 instead of \frac4 3 or 1.3333333333. In the second example, there is only one picture which Nauuo likes, so every time Nauuo visits the website, w_1 will be increased by 1. So, the expected weight is 1+2=3. Nauuo is very naughty so she didn't give you any hint of the third example.
2
11
#include <bits/stdc++.h> using namespace std; long long dpf[110][100][60]; long long dpg[110][100][60]; int diff; int likes_init = 0; const long long mod = 998244353; long long fast_exp(long long b, int e) { b = b % mod; if (e == 0) return 1; if (e == 1) return b; if (e % 2 == 1) return (b * fast_exp(b, e - 1)) % mod; return fast_exp((b * b) % mod, e / 2); } long long modinv(long long x) { assert(x % mod != 0); return fast_exp(x, mod - 2); } long long f(int curr, int likes, int runs) { if (dpf[curr][likes - likes_init][runs] == 1LL << 40) { int dislike = likes + runs - diff; if (curr == 0) return 0; if (runs == 0) { return curr; } if (dislike < 0) return 0; long long tmp1, tmp2, tmp3; tmp1 = (curr == 0 ? 0 : curr * f(curr + 1, likes + 1, runs - 1)); tmp2 = (likes - curr == 0 ? 0 : (likes - curr) * f(curr, likes + 1, runs - 1)); tmp3 = (dislike == 0 ? 0 : dislike * f(curr, likes, runs - 1)); long long tmp = (tmp1 + tmp2 + tmp3) % mod; long long ans = (tmp * modinv(likes + dislike)) % mod; return dpf[curr][likes - likes_init][runs] = ans; } else { return dpf[curr][likes - likes_init][runs]; } } long long g(int curr, int likes, int runs) { if (dpg[curr][likes - likes_init][runs] == 1LL << 40) { int dislike = likes + runs - diff; if (curr == 0) return 0; if (runs == 0) { return curr; } if (dislike < 0) return 0; long long tmp1 = (curr == 0 ? 0 : curr * g(curr - 1, likes, runs - 1)); long long tmp2 = (likes == 0 ? 0 : likes * g(curr, likes + 1, runs - 1)); long long tmp3 = (dislike - curr == 0 ? 0 : (dislike - curr) * g(curr, likes, runs - 1)); long long tmp = (tmp1 + tmp2 + tmp3) % mod; long long ans = (tmp * modinv(likes + dislike)) % mod; return dpg[curr][likes - likes_init][runs] = ans; } else { return dpg[curr][likes - likes_init][runs]; } } int main() { int n, m; cin >> n >> m; diff = m; int a[n]; for (int i = 0; i < 110; i++) { for (int j = 0; j < 100; j++) { for (int k = 0; k < 60; k++) { dpf[i][j][k] = 1LL << 40; dpg[i][j][k] = 1LL << 40; } } } bool good[n]; int tl = 0; for (int i = 0; i < n; i++) { cin >> good[i]; } for (int i = 0; i < n; i++) { cin >> a[i]; if (good[i]) { diff += a[i]; tl += a[i]; } else diff -= a[i]; } likes_init = tl; for (int i = 0; i < n; i++) { if (good[i]) { cout << (f(a[i], tl, m) % mod + mod) % mod << "\n"; } else { cout << (g(a[i], tl, m) % mod + mod) % mod << "\n"; } } return 0; }
CPP
1191_B. Tokitsukaze and Mahjong
Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, …, 9m, 1p, 2p, …, 9p, 1s, 2s, …, 9s. In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand. Do you know the minimum number of extra suited tiles she needs to draw so that she can win? Here are some useful definitions in this game: * A mentsu, also known as meld, is formed by a koutsu or a shuntsu; * A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu; * A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu. Some examples: * [2m, 3p, 2s, 4m, 1s, 2s, 4s] β€” it contains no koutsu or shuntsu, so it includes no mentsu; * [4s, 3m, 3p, 4s, 5p, 4s, 5p] β€” it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu; * [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β€” it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu. Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite. Input The only line contains three strings β€” the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s. Output Print a single integer β€” the minimum number of extra suited tiles she needs to draw. Examples Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 1 Note In the first example, Tokitsukaze already has a shuntsu. In the second example, Tokitsukaze already has a koutsu. In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β€” 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p].
2
8
string = input().split() first = string[0] second = string[1] third = string[2] string.sort(key=lambda x: int(x[0])) if first == second == third or first[1] == second[1] == third[1] and all(int(string[i][0]) == int(string[i - 1][0]) + 1 for i in range(1, 3)): print(0) elif (first == second or second == third or third == first or (string[0][1] == string[1][1] and int(string[1][0]) - int(string[0][0]) <= 2) or (string[1][1] == string[2][1] and int(string[2][0]) - int(string[1][0]) <= 2) or (string[0][1] == string[2][1] and int(string[2][0]) - int(string[0][0]) <= 2)): print(1) else: print(2)
PYTHON3
1191_B. Tokitsukaze and Mahjong
Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, …, 9m, 1p, 2p, …, 9p, 1s, 2s, …, 9s. In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand. Do you know the minimum number of extra suited tiles she needs to draw so that she can win? Here are some useful definitions in this game: * A mentsu, also known as meld, is formed by a koutsu or a shuntsu; * A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu; * A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu. Some examples: * [2m, 3p, 2s, 4m, 1s, 2s, 4s] β€” it contains no koutsu or shuntsu, so it includes no mentsu; * [4s, 3m, 3p, 4s, 5p, 4s, 5p] β€” it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu; * [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β€” it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu. Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite. Input The only line contains three strings β€” the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s. Output Print a single integer β€” the minimum number of extra suited tiles she needs to draw. Examples Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 1 Note In the first example, Tokitsukaze already has a shuntsu. In the second example, Tokitsukaze already has a koutsu. In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β€” 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p].
2
8
if __name__ == '__main__': s = list(map(lambda x: (int(x[0]), x[1]), input().split())) st = list(set([x for _, x in s])) best = 2 for a in st: mp = sorted(map(lambda t: t[0], list(filter(lambda x: x[1] == a, s)))) if len(mp) == 3 and (mp[0] == mp[1] == mp[2] or (mp[0] + 1 == mp[1] and mp[1] + 1 == mp[2])): best = min(best, 0) if (len(mp) >= 2 and (mp[0] + 1 == mp[1] or mp[0] + 2 == mp[1] or mp[0] == mp[1])) or \ (len(mp) == 3 and (mp[1] + 1 == mp[2] or mp[1] + 2 == mp[2] or mp[1] == mp[2])): best = min(best, 1) print(best)
PYTHON3
1191_B. Tokitsukaze and Mahjong
Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, …, 9m, 1p, 2p, …, 9p, 1s, 2s, …, 9s. In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand. Do you know the minimum number of extra suited tiles she needs to draw so that she can win? Here are some useful definitions in this game: * A mentsu, also known as meld, is formed by a koutsu or a shuntsu; * A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu; * A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu. Some examples: * [2m, 3p, 2s, 4m, 1s, 2s, 4s] β€” it contains no koutsu or shuntsu, so it includes no mentsu; * [4s, 3m, 3p, 4s, 5p, 4s, 5p] β€” it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu; * [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β€” it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu. Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite. Input The only line contains three strings β€” the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s. Output Print a single integer β€” the minimum number of extra suited tiles she needs to draw. Examples Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 1 Note In the first example, Tokitsukaze already has a shuntsu. In the second example, Tokitsukaze already has a koutsu. In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β€” 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p].
2
8
a,b,c=sorted(6*ord(y)+int(x)for x,y in input().split()) s={b-a,c-b} print(2-bool(s&{0,1,2})-(s<{0,1}))
PYTHON3
1191_B. Tokitsukaze and Mahjong
Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, …, 9m, 1p, 2p, …, 9p, 1s, 2s, …, 9s. In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand. Do you know the minimum number of extra suited tiles she needs to draw so that she can win? Here are some useful definitions in this game: * A mentsu, also known as meld, is formed by a koutsu or a shuntsu; * A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu; * A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu. Some examples: * [2m, 3p, 2s, 4m, 1s, 2s, 4s] β€” it contains no koutsu or shuntsu, so it includes no mentsu; * [4s, 3m, 3p, 4s, 5p, 4s, 5p] β€” it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu; * [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β€” it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu. Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite. Input The only line contains three strings β€” the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s. Output Print a single integer β€” the minimum number of extra suited tiles she needs to draw. Examples Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 1 Note In the first example, Tokitsukaze already has a shuntsu. In the second example, Tokitsukaze already has a koutsu. In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β€” 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p].
2
8
import java.util.*; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); String a = in.next(); String b = in.next(); String c = in.next(); int min = 2; if (a.equals(b) && b.equals(c)) { min = 0; } if (a.charAt(1) == b.charAt(1) && b.charAt(1) == c.charAt(1)) { int[] arr = new int[]{a.charAt(0) - '0', b.charAt(0) - '0', c.charAt(0) - '0'}; Arrays.sort(arr); if (arr[0] + 1 == arr[1] && arr[1] + 1 == arr[2]) { min = 0; } } if (a.equals(b) || b.equals(c) || a.equals(c)) { min = Math.min(min, 1); } int A = a.charAt(0) - '0'; int B = b.charAt(0) - '0'; int C = c.charAt(0) - '0'; if (a.charAt(1) == b.charAt(1) && Math.abs(A - B) <= 2) { min = Math.min(min, 1); } if (a.charAt(1) == c.charAt(1) && Math.abs(A - C) <= 2) { min = Math.min(min, 1); } if (b.charAt(1) == c.charAt(1) && Math.abs(B - C) <= 2) { min = Math.min(min, 1); } System.out.println(min); } }
JAVA
1191_B. Tokitsukaze and Mahjong
Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, …, 9m, 1p, 2p, …, 9p, 1s, 2s, …, 9s. In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand. Do you know the minimum number of extra suited tiles she needs to draw so that she can win? Here are some useful definitions in this game: * A mentsu, also known as meld, is formed by a koutsu or a shuntsu; * A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu; * A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu. Some examples: * [2m, 3p, 2s, 4m, 1s, 2s, 4s] β€” it contains no koutsu or shuntsu, so it includes no mentsu; * [4s, 3m, 3p, 4s, 5p, 4s, 5p] β€” it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu; * [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β€” it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu. Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite. Input The only line contains three strings β€” the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s. Output Print a single integer β€” the minimum number of extra suited tiles she needs to draw. Examples Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 1 Note In the first example, Tokitsukaze already has a shuntsu. In the second example, Tokitsukaze already has a koutsu. In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β€” 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p].
2
8
from sys import stdin, stdout from math import floor, gcd, fabs, factorial, fmod, sqrt, inf, log from collections import defaultdict as dd, deque from heapq import merge, heapify, heappop, heappush, nsmallest from bisect import bisect_left as bl, bisect_right as br, bisect mod = pow(10, 9) + 7 mod2 = 998244353 def inp(): return stdin.readline().strip() def out(var, end="\n"): stdout.write(str(var)+"\n") def outa(*var, end="\n"): stdout.write(' '.join(map(str, var)) + end) def lmp(): return list(mp()) def mp(): return map(int, inp().split()) def smp(): return map(str, inp().split()) def l1d(n, val=0): return [val for i in range(n)] def l2d(n, m, val=0): return [l1d(m, val) for j in range(n)] def remadd(x, y): return 1 if x%y else 0 def ceil(a,b): return (a+b-1)//b def isprime(x): if x<=1: return False if x in (2, 3): return True if x%2 == 0: return False for i in range(3, int(sqrt(x))+1, 2): if x%i == 0: return False return True ml = inp().split() ans = len(set(ml))-1 for i in range(1, 10): for j in "psm": k = 3 if str(i)+j in ml: k-=1 if str(i+1)+j in ml: k-=1 if str(i+2)+j in ml: k-=1 ans = min(ans, k) print(ans)
PYTHON3
1191_B. Tokitsukaze and Mahjong
Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, …, 9m, 1p, 2p, …, 9p, 1s, 2s, …, 9s. In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand. Do you know the minimum number of extra suited tiles she needs to draw so that she can win? Here are some useful definitions in this game: * A mentsu, also known as meld, is formed by a koutsu or a shuntsu; * A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu; * A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu. Some examples: * [2m, 3p, 2s, 4m, 1s, 2s, 4s] β€” it contains no koutsu or shuntsu, so it includes no mentsu; * [4s, 3m, 3p, 4s, 5p, 4s, 5p] β€” it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu; * [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β€” it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu. Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite. Input The only line contains three strings β€” the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s. Output Print a single integer β€” the minimum number of extra suited tiles she needs to draw. Examples Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 1 Note In the first example, Tokitsukaze already has a shuntsu. In the second example, Tokitsukaze already has a koutsu. In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β€” 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p].
2
8
s ={} s['s'] = [] s['m'] = [] s['p'] = [] a = input() h = a[0:2] s[h[1]].append(int(h[0])) h = a[3:5] s[h[1]].append(int(h[0])) h = a[6:8] s[h[1]].append(int(h[0])) s['s'].sort() s['m'].sort() s['p'].sort() if len(s['s']) == 1 and len(s['p']) == 1 : print("2") for x in s : if len(s[x]) == 2 : if s[x][1]-s[x][0] == 0 or s[x][1]-s[x][0] == 1 or s[x][1]-s[x][0] == 2: print("1") else: print("2") break elif len(s[x]) == 3: a = [s[x][1]-s[x][0] , s[x][2]-s[x][1]] if (a[0] == 0 and a[1] == 0) or (a[0] == 1 and a[1] == 1) : print("0") elif a[0] == 0 or a[0] == 1 or a[0] == 2 or a[1] == 0 or a[1] == 1 or a[1] == 2 : print("1") else: print("2") break
PYTHON3
1191_B. Tokitsukaze and Mahjong
Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, …, 9m, 1p, 2p, …, 9p, 1s, 2s, …, 9s. In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand. Do you know the minimum number of extra suited tiles she needs to draw so that she can win? Here are some useful definitions in this game: * A mentsu, also known as meld, is formed by a koutsu or a shuntsu; * A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu; * A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu. Some examples: * [2m, 3p, 2s, 4m, 1s, 2s, 4s] β€” it contains no koutsu or shuntsu, so it includes no mentsu; * [4s, 3m, 3p, 4s, 5p, 4s, 5p] β€” it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu; * [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β€” it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu. Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite. Input The only line contains three strings β€” the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s. Output Print a single integer β€” the minimum number of extra suited tiles she needs to draw. Examples Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 1 Note In the first example, Tokitsukaze already has a shuntsu. In the second example, Tokitsukaze already has a koutsu. In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β€” 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p].
2
8
import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.Arrays; public class Ques1 { public static void main(String[] args) throws Exception{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String[] z = br.readLine().split(" "); int[][] mps = new int[3][3]; int ml=0, pl=0, sl=0, i, j, f=0; for(i=0;i<3;i++){ if(z[i].charAt(1)=='m'){ mps[0][ml++]=z[i].charAt(0); } else if(z[i].charAt(1)=='p'){ mps[1][pl++]=z[i].charAt(0); } else{ mps[2][sl++]=z[i].charAt(0); } } for(i=0;i<3;i++){ Arrays.sort(mps[i]); } if(z[0].equals(z[1]) && z[1].equals(z[2])){ System.out.println(0); f=1; } if(f==0){ for(i=0;i<3 && f==0;i++){ if(ml==3 && mps[0][0]!=0 && mps[0][1]==mps[0][0]+1 && mps[0][2]==mps[0][1]+1){ System.out.println(0); f=1; } else if(pl==3 && mps[1][0]!=0 && mps[1][1]==mps[1][0]+1 && mps[1][2]==mps[1][1]+1){ System.out.println(0); f=1; } else if(sl==3 && mps[2][0]!=0 && mps[2][1]==mps[2][0]+1 && mps[2][2]==mps[2][1]+1){ System.out.println(0); f=1; } } } if(f==0){ if(z[0].equals(z[1]) || z[0].equals(z[2]) || z[1].equals(z[2])){ System.out.println(1); f=1; } else { for(i=0;i<3;i++){ if(f==0 && ml>=2 && ((mps[0][0]!=0 && mps[0][1]-mps[0][0]<=2) || (mps[0][1]!=0 && mps[0][2]-mps[0][1]<=2))){ System.out.println(1); f=1; } if(f==0 && pl>=2 && ((mps[1][0]!=0 && mps[1][1]-mps[1][0]<=2) || (mps[1][1]!=0 && mps[1][2]-mps[1][1]<=2))){ System.out.println(1); f=1; } if(f==0 && sl>=2 && ((mps[2][0]!=0 && mps[2][1]-mps[2][0]<=2) || (mps[2][1]!=0 && mps[2][2]-mps[2][1]<=2))){ System.out.println(1); f=1; } } } } if(f==0){ System.out.println(2); } } }
JAVA
1191_B. Tokitsukaze and Mahjong
Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, …, 9m, 1p, 2p, …, 9p, 1s, 2s, …, 9s. In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand. Do you know the minimum number of extra suited tiles she needs to draw so that she can win? Here are some useful definitions in this game: * A mentsu, also known as meld, is formed by a koutsu or a shuntsu; * A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu; * A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu. Some examples: * [2m, 3p, 2s, 4m, 1s, 2s, 4s] β€” it contains no koutsu or shuntsu, so it includes no mentsu; * [4s, 3m, 3p, 4s, 5p, 4s, 5p] β€” it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu; * [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β€” it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu. Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite. Input The only line contains three strings β€” the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s. Output Print a single integer β€” the minimum number of extra suited tiles she needs to draw. Examples Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 1 Note In the first example, Tokitsukaze already has a shuntsu. In the second example, Tokitsukaze already has a koutsu. In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β€” 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p].
2
8
# http://codeforces.com/contest/1191/problem/B ''' Author - Subhajit Das University of Engineering and Management, Kolkata 14/07/2019 ''' def f(x: int) -> int: return 1 if not not x else 0 def main(): inp = input().split() idx = {'m': 0, 'p': 1, 's': 2} table = [[0]*9 for _ in range(3)] for i in inp: r = idx[i[1]] c = int(i[0])-1 table[r][c] += 1 ans = 3 for i in range(3): for j in range(9): ans = min(ans, 3-table[i][j]) if(j+2 < 9): ans = min(ans, 3 - f(table[i][j])-f(table[i][j+1])-f(table[i][j+2])) print(ans) if __name__ == "__main__": main()
PYTHON3
1191_B. Tokitsukaze and Mahjong
Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, …, 9m, 1p, 2p, …, 9p, 1s, 2s, …, 9s. In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand. Do you know the minimum number of extra suited tiles she needs to draw so that she can win? Here are some useful definitions in this game: * A mentsu, also known as meld, is formed by a koutsu or a shuntsu; * A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu; * A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu. Some examples: * [2m, 3p, 2s, 4m, 1s, 2s, 4s] β€” it contains no koutsu or shuntsu, so it includes no mentsu; * [4s, 3m, 3p, 4s, 5p, 4s, 5p] β€” it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu; * [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β€” it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu. Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite. Input The only line contains three strings β€” the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s. Output Print a single integer β€” the minimum number of extra suited tiles she needs to draw. Examples Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 1 Note In the first example, Tokitsukaze already has a shuntsu. In the second example, Tokitsukaze already has a koutsu. In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β€” 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p].
2
8
cards = input().split() m = [] s = [] p = [] for i in range(3): if cards[i][1] == "m": m.append(int(cards[i][0])) elif cards[i][1] == "s": s.append(int(cards[i][0])) elif cards[i][1] == "p": p.append(int(cards[i][0])) msp = [m, s, p] rmsp = [len(m), len(s), len(p)] if rmsp[0] == rmsp[1] == rmsp[2]: print(2) def con3(seq): seq.sort() new_seq = [seq[1]-seq[0], seq[2]-seq[0]] n_seq = new_seq[1] - new_seq[0] if new_seq == [1,2] or new_seq == [0,0]: print(0) elif new_seq[0] == 1 or new_seq[1] == 1 or n_seq == 0 or n_seq == 1 or n_seq == 2 or new_seq[0] == 2 or new_seq[1] == 2 or new_seq[0] == 0: print(1) else: print(2) def con2(seq): cons = abs(seq[1] - seq[0]) if cons == 0 or cons == 1 or cons == 2: print(1) else: print(2) for i in range(3): if rmsp[i] == 3: con3(msp[i]) break elif rmsp[i] == 2: val2 = msp[i] con2(val2) break
PYTHON3
1191_B. Tokitsukaze and Mahjong
Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, …, 9m, 1p, 2p, …, 9p, 1s, 2s, …, 9s. In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand. Do you know the minimum number of extra suited tiles she needs to draw so that she can win? Here are some useful definitions in this game: * A mentsu, also known as meld, is formed by a koutsu or a shuntsu; * A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu; * A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu. Some examples: * [2m, 3p, 2s, 4m, 1s, 2s, 4s] β€” it contains no koutsu or shuntsu, so it includes no mentsu; * [4s, 3m, 3p, 4s, 5p, 4s, 5p] β€” it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu; * [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β€” it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu. Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite. Input The only line contains three strings β€” the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s. Output Print a single integer β€” the minimum number of extra suited tiles she needs to draw. Examples Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 1 Note In the first example, Tokitsukaze already has a shuntsu. In the second example, Tokitsukaze already has a koutsu. In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β€” 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p].
2
8
arr = input().split() arr = sorted(arr,key= lambda x:x[0]) no = [int(arr[0][0]),int(arr[1][0]),int(arr[2][0])] if (arr[0] == arr[1] == arr[2]) or (no[1]-no[0]==1 and no[2]-no[1]==1 and no[2]-no[0]==2 and arr[0][1]==arr[1][1]==arr[2][1]): print('0') elif (arr[0]==arr[1] or arr[1]==arr[2] or arr[0]==arr[2]) or (no[1]-no[0]==1 and arr[0][1]==arr[1][1]) or (no[2]-no[1]==1 and arr[1][1]==arr[2][1]) or (no[1]-no[0]==2 and arr[0][1]==arr[1][1]) or (no[2]-no[1]==2 and arr[1][1]==arr[2][1]) or (no[2]-no[0]==2 and arr[2][1]==arr[0][1]) or (no[2]-no[0]==1 and arr[2][1]==arr[0][1]): print('1') else: print('2')
PYTHON3
1191_B. Tokitsukaze and Mahjong
Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, …, 9m, 1p, 2p, …, 9p, 1s, 2s, …, 9s. In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand. Do you know the minimum number of extra suited tiles she needs to draw so that she can win? Here are some useful definitions in this game: * A mentsu, also known as meld, is formed by a koutsu or a shuntsu; * A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu; * A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu. Some examples: * [2m, 3p, 2s, 4m, 1s, 2s, 4s] β€” it contains no koutsu or shuntsu, so it includes no mentsu; * [4s, 3m, 3p, 4s, 5p, 4s, 5p] β€” it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu; * [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β€” it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu. Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite. Input The only line contains three strings β€” the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s. Output Print a single integer β€” the minimum number of extra suited tiles she needs to draw. Examples Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 1 Note In the first example, Tokitsukaze already has a shuntsu. In the second example, Tokitsukaze already has a koutsu. In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β€” 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p].
2
8
# -*- coding: utf-8 -*- """ Created on Sat Jul 13 01:25:40 2019 @author: Gulam Kibria """ from collections import defaultdict st=input() st2=st.replace(" ","") g=defaultdict(list) for x in range(0,7): if(x%2==1): g[st2[x]].append(int(st2[x-1])) for x in g: g[x].sort() if(len(g)==3): print (2) elif(len(g)==2): for x in g: if(len(g[x])==2): if((g[x][0]==g[x][1]) or abs(g[x][0]-g[x][1])==1 or abs(g[x][0]-g[x][1])==2): print (1) else: print (2) elif (len(g)==1): for x in g: if((g[x][0]==g[x][1]==g[x][2]) or (abs(g[x][0]-g[x][1])==1 and abs(g[x][1]-g[x][2])==1)): print (0) elif(abs(g[x][0]-g[x][1])>2 and abs(g[x][1]-g[x][2])>2): print (2) else: print (1)
PYTHON3
1191_B. Tokitsukaze and Mahjong
Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, …, 9m, 1p, 2p, …, 9p, 1s, 2s, …, 9s. In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand. Do you know the minimum number of extra suited tiles she needs to draw so that she can win? Here are some useful definitions in this game: * A mentsu, also known as meld, is formed by a koutsu or a shuntsu; * A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu; * A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu. Some examples: * [2m, 3p, 2s, 4m, 1s, 2s, 4s] β€” it contains no koutsu or shuntsu, so it includes no mentsu; * [4s, 3m, 3p, 4s, 5p, 4s, 5p] β€” it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu; * [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β€” it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu. Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite. Input The only line contains three strings β€” the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s. Output Print a single integer β€” the minimum number of extra suited tiles she needs to draw. Examples Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 1 Note In the first example, Tokitsukaze already has a shuntsu. In the second example, Tokitsukaze already has a koutsu. In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β€” 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p].
2
8
d = { 'm': 10, 's': 30, 'p': 60 } a, b, c = list(sorted([int(i[0]) + d[i[1]] for i in input().split(' ')])) if (a == b and b == c) or (a == b - 1 and b == c - 1): print(0) exit() if a == b - 1 or b == c - 1 or a == b or b == c or a == c - 2 or a == b - 2 or b == c - 2: print(1) exit() print(2)
PYTHON3
1191_B. Tokitsukaze and Mahjong
Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, …, 9m, 1p, 2p, …, 9p, 1s, 2s, …, 9s. In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand. Do you know the minimum number of extra suited tiles she needs to draw so that she can win? Here are some useful definitions in this game: * A mentsu, also known as meld, is formed by a koutsu or a shuntsu; * A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu; * A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu. Some examples: * [2m, 3p, 2s, 4m, 1s, 2s, 4s] β€” it contains no koutsu or shuntsu, so it includes no mentsu; * [4s, 3m, 3p, 4s, 5p, 4s, 5p] β€” it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu; * [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β€” it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu. Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite. Input The only line contains three strings β€” the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s. Output Print a single integer β€” the minimum number of extra suited tiles she needs to draw. Examples Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 1 Note In the first example, Tokitsukaze already has a shuntsu. In the second example, Tokitsukaze already has a koutsu. In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β€” 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p].
2
8
#include <bits/stdc++.h> using namespace std; char a[3][3]; int p[3]; int main() { int i; cin >> a[0] >> a[1] >> a[2]; int x; for (i = 0; i < 3; i++) { x = 0; switch (a[i][1]) { case 's': x = 0; break; case 'm': x = 10; break; case 'p': x = 20; break; } p[i] = x + (a[i][0] - '0'); } sort(p, p + 3); if (p[0] == p[1] && p[1] == p[2]) { cout << 0; return 0; } if (p[2] - p[1] == 1 && p[1] - p[0] == 1) { cout << 0; return 0; } if (p[2] / 10 == p[1] / 10 && p[2] - p[1] <= 2) { cout << 1; return 0; } if (p[1] / 10 == p[0] / 10 && p[1] - p[0] <= 2) { cout << 1; return 0; } cout << 2; }
CPP
1191_B. Tokitsukaze and Mahjong
Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, …, 9m, 1p, 2p, …, 9p, 1s, 2s, …, 9s. In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand. Do you know the minimum number of extra suited tiles she needs to draw so that she can win? Here are some useful definitions in this game: * A mentsu, also known as meld, is formed by a koutsu or a shuntsu; * A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu; * A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu. Some examples: * [2m, 3p, 2s, 4m, 1s, 2s, 4s] β€” it contains no koutsu or shuntsu, so it includes no mentsu; * [4s, 3m, 3p, 4s, 5p, 4s, 5p] β€” it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu; * [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β€” it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu. Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite. Input The only line contains three strings β€” the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s. Output Print a single integer β€” the minimum number of extra suited tiles she needs to draw. Examples Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 1 Note In the first example, Tokitsukaze already has a shuntsu. In the second example, Tokitsukaze already has a koutsu. In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β€” 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p].
2
8
n=list(map(str,input().split(" "))) d=dict() mp = {"m":0 , "p":1 , "s":2} l=[0]*10 ll=[0]*10 lll=[0]*10 arr = [l,ll,lll] maxi=0 for i in n: # print(i) if (i in d.keys()): d[i]+=1 maxi = max(maxi,d[i]) else: d[i]=1 maxi = max(maxi,d[i]) arr[mp[i[1]]][int(i[0])-1] = 1 # print(arr) # print(d) if(maxi>=3): print(0) else: temp2 = 0 for i in range(3): ansi=0 prev2=arr[i][0] prev1=arr[i][1] for j in range(10): if(j>=2 and prev2==1 and prev1==0 and arr[i][j]==1): temp2=max(temp2,2) if(arr[i][j] ==0): ansi=0 if(arr[i][j]==1): ansi+=arr[i][j] temp2=max(ansi,temp2) prev2=prev1 prev1=arr[i][j] finalAns = min(3-maxi, 3-temp2) print(finalAns)
PYTHON3
1191_B. Tokitsukaze and Mahjong
Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, …, 9m, 1p, 2p, …, 9p, 1s, 2s, …, 9s. In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand. Do you know the minimum number of extra suited tiles she needs to draw so that she can win? Here are some useful definitions in this game: * A mentsu, also known as meld, is formed by a koutsu or a shuntsu; * A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu; * A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu. Some examples: * [2m, 3p, 2s, 4m, 1s, 2s, 4s] β€” it contains no koutsu or shuntsu, so it includes no mentsu; * [4s, 3m, 3p, 4s, 5p, 4s, 5p] β€” it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu; * [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β€” it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu. Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite. Input The only line contains three strings β€” the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s. Output Print a single integer β€” the minimum number of extra suited tiles she needs to draw. Examples Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 1 Note In the first example, Tokitsukaze already has a shuntsu. In the second example, Tokitsukaze already has a koutsu. In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β€” 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p].
2
8
cards=list(input().split()) lm=[0]*9 lp=[0]*9 ls=[0]*9 for item in cards: if item[1]=='m': lm[int(item[0])-1]+=1 elif item[1]=='p': lp[int(item[0])-1]+=1 else : ls[int(item[0])-1]+=1 if max(lm)==3 or max(lp)==3 or max(ls)==3: print(0) else : flag=0 def seq_checker(li): flag=0 for i in range(9): if flag==0: if lm[i]==1: flag=1 else : if lm[i]==1: flag+=1 else : break return flag if seq_checker(lm)==3 or seq_checker(lp)==3 or seq_checker(ls)==3: print(0) elif max(lm)==2 or max(lp)==2 or max(ls)==2: print(1) else : m=0 for i in range(0,7): m=max(sum(lm[i:i+3]),sum(lp[i:i+3]),sum(ls[i:i+3]),m) print(3-m)
PYTHON3
1191_B. Tokitsukaze and Mahjong
Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, …, 9m, 1p, 2p, …, 9p, 1s, 2s, …, 9s. In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand. Do you know the minimum number of extra suited tiles she needs to draw so that she can win? Here are some useful definitions in this game: * A mentsu, also known as meld, is formed by a koutsu or a shuntsu; * A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu; * A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu. Some examples: * [2m, 3p, 2s, 4m, 1s, 2s, 4s] β€” it contains no koutsu or shuntsu, so it includes no mentsu; * [4s, 3m, 3p, 4s, 5p, 4s, 5p] β€” it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu; * [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β€” it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu. Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite. Input The only line contains three strings β€” the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s. Output Print a single integer β€” the minimum number of extra suited tiles she needs to draw. Examples Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 1 Note In the first example, Tokitsukaze already has a shuntsu. In the second example, Tokitsukaze already has a koutsu. In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β€” 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p].
2
8
a = input().split() st = set([]) cnt = [[0 for i in range(9)] for i in range(3)] for e in a: cnt['mps'.index(e[1])][int(e[0]) - 1] = 1 st.add(e) answ = len(st) - 1 for i in range(3): for j in range(7): answ = min(answ, 3 - sum(cnt[i][j:j + 3])) print(answ)
PYTHON3
1191_B. Tokitsukaze and Mahjong
Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, …, 9m, 1p, 2p, …, 9p, 1s, 2s, …, 9s. In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand. Do you know the minimum number of extra suited tiles she needs to draw so that she can win? Here are some useful definitions in this game: * A mentsu, also known as meld, is formed by a koutsu or a shuntsu; * A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu; * A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu. Some examples: * [2m, 3p, 2s, 4m, 1s, 2s, 4s] β€” it contains no koutsu or shuntsu, so it includes no mentsu; * [4s, 3m, 3p, 4s, 5p, 4s, 5p] β€” it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu; * [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β€” it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu. Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite. Input The only line contains three strings β€” the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s. Output Print a single integer β€” the minimum number of extra suited tiles she needs to draw. Examples Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 1 Note In the first example, Tokitsukaze already has a shuntsu. In the second example, Tokitsukaze already has a koutsu. In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β€” 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p].
2
8
s = [0] * 10 m = [0] * 10 p = [0] * 10 D = list(input().split()) for i in D: if i[1] == 'p': p[int(i[0])] += 1 elif i[1] == 'm': m[int(i[0])] += 1 else: s[int(i[0])] += 1 need = 3 for i in range(1, 10): need = min(3 - p[i], need) need = min(3 - s[i], need) need = min(3 - m[i], need) if i <= 7: tmp = 0 tmp += min(1, p[i]) tmp += min(1, p[i + 1]) tmp += min(1, p[i + 2]) need = min(3 - tmp, need) tmp = 0 tmp += min(1, m[i]) tmp += min(1, m[i + 1]) tmp += min(1, m[i + 2]) need = min(3 - tmp, need) tmp = 0 tmp += min(1, s[i]) tmp += min(1, s[i + 1]) tmp += min(1, s[i + 2]) need = min(3 - tmp, need) print(need)
PYTHON3
1191_B. Tokitsukaze and Mahjong
Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, …, 9m, 1p, 2p, …, 9p, 1s, 2s, …, 9s. In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand. Do you know the minimum number of extra suited tiles she needs to draw so that she can win? Here are some useful definitions in this game: * A mentsu, also known as meld, is formed by a koutsu or a shuntsu; * A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu; * A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu. Some examples: * [2m, 3p, 2s, 4m, 1s, 2s, 4s] β€” it contains no koutsu or shuntsu, so it includes no mentsu; * [4s, 3m, 3p, 4s, 5p, 4s, 5p] β€” it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu; * [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β€” it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu. Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite. Input The only line contains three strings β€” the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s. Output Print a single integer β€” the minimum number of extra suited tiles she needs to draw. Examples Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 1 Note In the first example, Tokitsukaze already has a shuntsu. In the second example, Tokitsukaze already has a koutsu. In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β€” 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p].
2
8
def main(): list_str = input().split() list_str.sort() """ p = ['3p', '5s', '5s'] (after sort) sorted by decimal its mean either ['3p','5p'] or ['5p','5p'] near to koutsu, or whole string koutsu either 1,2,3.... 0 is impossible""" # if koutsu 2 or 3 then cannot be shuntsu koutsu_count = 1 if list_str[0] == list_str[1] and list_str[1] == list_str[2]: # all equal koutsu_count = 3 print('0') return # if koutsu 2 or 3 then cannot be shuntsu elif list_str[0] == list_str[1] or list_str[1] == list_str[2]: # either one equal koutsu_count = 2 print('1') return # if koutsu 2 or 3 then cannot be shuntsu ############################################### shuntsu_count = 0 """ 1s,5m,3s => after sort 1s,3s,5m 1s,5m,2s => after sort 1s,2s,5m 1s,2m,2s => after sort 1s,2m,2s so i guess better to digits 1,2,2 s,m,s """ num = [] lstr = [] for e in list_str: num.append(int(e[0])) lstr.append(e[1]) # sorted so smallest to largest if lstr[0] == lstr[1] and (num[0] == num[1]-1 or num[0] == num[1]-2): shuntsu_count += 1 if lstr[0] == lstr[2] and (num[0] == num[2]-1 or num[0] == num[2]-2): shuntsu_count += 1 if lstr[1] == lstr[2] and (num[1] == num[2]-1 or num[1] == num[2]-2): shuntsu_count += 1 if shuntsu_count >= 3: print('0') elif shuntsu_count == 0: # atleast Count one of kind already print('2') else: print('1') main()
PYTHON3
1191_B. Tokitsukaze and Mahjong
Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, …, 9m, 1p, 2p, …, 9p, 1s, 2s, …, 9s. In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand. Do you know the minimum number of extra suited tiles she needs to draw so that she can win? Here are some useful definitions in this game: * A mentsu, also known as meld, is formed by a koutsu or a shuntsu; * A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu; * A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu. Some examples: * [2m, 3p, 2s, 4m, 1s, 2s, 4s] β€” it contains no koutsu or shuntsu, so it includes no mentsu; * [4s, 3m, 3p, 4s, 5p, 4s, 5p] β€” it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu; * [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β€” it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu. Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite. Input The only line contains three strings β€” the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s. Output Print a single integer β€” the minimum number of extra suited tiles she needs to draw. Examples Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 1 Note In the first example, Tokitsukaze already has a shuntsu. In the second example, Tokitsukaze already has a koutsu. In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β€” 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p].
2
8
l = list(input().split()) d = {} d["m"] = {} d["s"] = {} d["p"] = {} for i in range(1,10): d["m"][i] = 0 d["s"][i] = 0 d["p"][i] = 0 for a in l: s = a[1] c = int(a[0]) d[s][c] += 1 #koutsu c = 0 for s in ["m","p","s"]: for i in range(1,10): c = max(c,d[s][i]) if c >= 3: print(0) exit() ans = 3 - c #shuusu t = 0 for s in ["m","p","s"]: c = 0 for i in range(1,10): if d[s][i] != 0: c += 1 else: t = max(t,c) if t >= 3: print(0) exit() c = 0 t = max(t,c) c = 0 for i in range(1,8): if d[s][i] != 0 and d[s][i+2] != 0: c = 2 break t = max(t,c) print(min(ans , 3 - t))
PYTHON3
1191_B. Tokitsukaze and Mahjong
Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, …, 9m, 1p, 2p, …, 9p, 1s, 2s, …, 9s. In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand. Do you know the minimum number of extra suited tiles she needs to draw so that she can win? Here are some useful definitions in this game: * A mentsu, also known as meld, is formed by a koutsu or a shuntsu; * A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu; * A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu. Some examples: * [2m, 3p, 2s, 4m, 1s, 2s, 4s] β€” it contains no koutsu or shuntsu, so it includes no mentsu; * [4s, 3m, 3p, 4s, 5p, 4s, 5p] β€” it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu; * [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β€” it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu. Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite. Input The only line contains three strings β€” the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s. Output Print a single integer β€” the minimum number of extra suited tiles she needs to draw. Examples Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 1 Note In the first example, Tokitsukaze already has a shuntsu. In the second example, Tokitsukaze already has a koutsu. In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β€” 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p].
2
8
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 10; const int mod = 1e9 + 7; const int seed = 1333331; long long qm(long long a, long long b, long long res = 1) { for (a %= mod; b; b >>= 1, a = a * a % mod) if (b & 1) res = res * a % mod; return res; } int n, m; long long a[maxn], res, lim[maxn]; vector<int> vec; int val[110]; string s; int main() { int tmp, res = 3; cin >> s; if (s[1] == 'm') tmp = 0; else if (s[1] == 'p') tmp = 1; else tmp = 2; val[s[0] - '0' + tmp * 10]++; cin >> s; if (s[1] == 'm') tmp = 0; else if (s[1] == 'p') tmp = 1; else tmp = 2; val[s[0] - '0' + tmp * 10]++; cin >> s; if (s[1] == 'm') tmp = 0; else if (s[1] == 'p') tmp = 1; else tmp = 2; val[s[0] - '0' + tmp * 10]++; for (int i = 1; i <= 9; i++) { int tmp = 3; if (i > 1 && val[i - 1]) tmp--; if (val[i]) tmp--; if (i < 9 && val[i + 1]) tmp--; res = min(res, tmp); res = min(res, max(3 - val[i], 0)); } for (int i = 11; i <= 19; i++) { int tmp = 3; if (i > 11 && val[i - 1]) tmp--; if (val[i]) tmp--; if (i < 19 && val[i + 1]) tmp--; res = min(res, tmp); res = min(res, max(3 - val[i], 0)); } for (int i = 21; i <= 29; i++) { int tmp = 3; if (i > 21 && val[i - 1]) tmp--; if (val[i]) tmp--; if (i < 29 && val[i + 1]) tmp--; res = min(res, tmp); res = min(res, max(3 - val[i], 0)); } cout << res << endl; }
CPP
1191_B. Tokitsukaze and Mahjong
Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, …, 9m, 1p, 2p, …, 9p, 1s, 2s, …, 9s. In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand. Do you know the minimum number of extra suited tiles she needs to draw so that she can win? Here are some useful definitions in this game: * A mentsu, also known as meld, is formed by a koutsu or a shuntsu; * A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu; * A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu. Some examples: * [2m, 3p, 2s, 4m, 1s, 2s, 4s] β€” it contains no koutsu or shuntsu, so it includes no mentsu; * [4s, 3m, 3p, 4s, 5p, 4s, 5p] β€” it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu; * [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β€” it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu. Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite. Input The only line contains three strings β€” the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s. Output Print a single integer β€” the minimum number of extra suited tiles she needs to draw. Examples Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 1 Note In the first example, Tokitsukaze already has a shuntsu. In the second example, Tokitsukaze already has a koutsu. In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β€” 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p].
2
8
#include <bits/stdc++.h> using namespace std; int main() { string a, b, c; cin >> a >> b >> c; if (a == b && a == c) cout << "0"; else if ((a == b && a != c)) cout << "1"; else if ((a == c && a != b)) cout << "1"; else if (b == c && b != a) cout << "1"; else if (a[1] == b[1] && b[1] == c[1]) { if ((a[0] + 1 == b[0]) && (b[0] + 1 == c[0]) || ((b[0] + 1 == a[0]) && (a[0] + 1 == c[0])) || ((a[0] + 1 == c[0]) && (c[0] + 1 == b[0])) || ((a[0] + 1 == b[0]) && (c[0] + 1 == a[0])) || ((b[0] + 1 == c[0]) && (c[0] + 1 == a[0]))) cout << "0"; else if ((a[0] + 1 == b[0]) || (b[0] + 1 == c[0]) || (a[0] + 1 == c[0]) || (c[0] + 1 == a[0]) || (c[0] + 1 == b[0])) cout << "1"; else if ((abs(a[0] - b[0]) == 2) || (abs(b[0] - c[0]) == 2) || (abs(a[0] - c[0]) == 2)) cout << "1"; else if (a == b && b != c) cout << "1"; else if (b == c && b != a) cout << "1"; else cout << "2"; } else if (a[1] == b[1]) { if (a[0] + 1 == b[0] || b[0] + 1 == a[0]) cout << "1"; else if (abs(b[0] - a[0]) == 2) cout << "1"; else cout << "2"; } else if (b[1] == c[1]) { if (b[0] + 1 == c[0] || c[0] + 1 == b[0]) cout << "1"; else if (abs(b[0] - c[0]) == 2) cout << "1"; else cout << "2"; } else if (a[1] == c[1]) { if (a[0] + 1 == c[0] || c[0] + 1 == a[0]) cout << "1"; else if (abs(a[0] - c[0]) == 2) cout << "1"; else cout << "2"; } else if ((a[1] != b[1] && a[1] != c[1]) && (b[1] != c[1])) cout << "2"; }
CPP
1191_B. Tokitsukaze and Mahjong
Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, …, 9m, 1p, 2p, …, 9p, 1s, 2s, …, 9s. In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand. Do you know the minimum number of extra suited tiles she needs to draw so that she can win? Here are some useful definitions in this game: * A mentsu, also known as meld, is formed by a koutsu or a shuntsu; * A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu; * A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu. Some examples: * [2m, 3p, 2s, 4m, 1s, 2s, 4s] β€” it contains no koutsu or shuntsu, so it includes no mentsu; * [4s, 3m, 3p, 4s, 5p, 4s, 5p] β€” it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu; * [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β€” it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu. Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite. Input The only line contains three strings β€” the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s. Output Print a single integer β€” the minimum number of extra suited tiles she needs to draw. Examples Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 1 Note In the first example, Tokitsukaze already has a shuntsu. In the second example, Tokitsukaze already has a koutsu. In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β€” 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p].
2
8
o = [] n = input() p = 0 for i in range(3): o.insert(i, n[i + p] + n[i + 1 + p]) p = p + 2 o = sorted(o) ans = 2 if (o[0][1] == o[1][1]) and (int(o[1][0]) - int(o[0][0]) < 3) or (o[1][1] == o[2][1]) and (int(o[2][0]) - int(o[1][0]) < 3): ans = 1 if (o[0][1] == o[2][1]) and (int(o[2][0]) - int(o[0][0]) < 3): ans = 1 if (o[0][1] == o[1][1] == o[2][1]): if ((int(o[2][0]) == int(o[1][0]) + 1) and (int(o[1][0]) == int(o[0][0]) + 1)) or ((int(o[2][0]) == int(o[1][0])) and (int(o[1][0]) == int(o[0][0]))): ans = 0 print(ans)
PYTHON3
1191_B. Tokitsukaze and Mahjong
Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, …, 9m, 1p, 2p, …, 9p, 1s, 2s, …, 9s. In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand. Do you know the minimum number of extra suited tiles she needs to draw so that she can win? Here are some useful definitions in this game: * A mentsu, also known as meld, is formed by a koutsu or a shuntsu; * A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu; * A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu. Some examples: * [2m, 3p, 2s, 4m, 1s, 2s, 4s] β€” it contains no koutsu or shuntsu, so it includes no mentsu; * [4s, 3m, 3p, 4s, 5p, 4s, 5p] β€” it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu; * [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β€” it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu. Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite. Input The only line contains three strings β€” the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s. Output Print a single integer β€” the minimum number of extra suited tiles she needs to draw. Examples Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 1 Note In the first example, Tokitsukaze already has a shuntsu. In the second example, Tokitsukaze already has a koutsu. In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β€” 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p].
2
8
import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); String s[]=new String[3]; int a[]=new int[27]; for(int i=0;i<3;i++) { s[i]=sc.next(); a[s[i].charAt(1)-'a']++; } int index='s'-'a'; if(a[index]<a['m'-'a']) { index='m'-'a'; } if(a[index]<a['p'-'a']) { index='p'-'a'; } int an[]=new int[3]; int cnt=0; for(int i=0;i<3;i++) { if(s[i].charAt(1)==index+'a') { an[cnt++]=s[i].charAt(0)-'0'; } } Arrays.sort(an,0,cnt); if(cnt==3) { if(an[0]==an[1]&&an[1]==an[2]) { System.out.println(0); return; } if(an[0]==an[1]||an[1]==an[2]) { System.out.println(1); return; } if(an[2]-an[1]==1&&an[1]-an[0]==1) { System.out.println(0); return; } if(an[2]-an[1]==1||an[1]-an[0]==1||an[2]-an[1]==2||an[1]-an[0]==2) { System.out.println(1); return; } System.out.println(2); return; }else if(cnt==2) { if(an[0]==an[1]||an[1]-an[0]==1||an[1]-an[0]==2) { System.out.println(1); return; } System.out.println(2); return; }else { System.out.println(2); return; } } }
JAVA
1191_B. Tokitsukaze and Mahjong
Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, …, 9m, 1p, 2p, …, 9p, 1s, 2s, …, 9s. In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand. Do you know the minimum number of extra suited tiles she needs to draw so that she can win? Here are some useful definitions in this game: * A mentsu, also known as meld, is formed by a koutsu or a shuntsu; * A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu; * A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu. Some examples: * [2m, 3p, 2s, 4m, 1s, 2s, 4s] β€” it contains no koutsu or shuntsu, so it includes no mentsu; * [4s, 3m, 3p, 4s, 5p, 4s, 5p] β€” it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu; * [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β€” it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu. Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite. Input The only line contains three strings β€” the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s. Output Print a single integer β€” the minimum number of extra suited tiles she needs to draw. Examples Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 1 Note In the first example, Tokitsukaze already has a shuntsu. In the second example, Tokitsukaze already has a koutsu. In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β€” 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p].
2
8
import java.util.*; public class CF573B { public static void main(String args[]) { Scanner sc = new Scanner(System.in); int[] m = new int[9]; int[] p = new int[9]; int[] s = new int[9]; for(int i=1; i<=3; i++) { String temp = sc.next(); int pos = Integer.valueOf(temp.substring(0, 1)); char c = temp.charAt(1); if(c == 's') { s[pos-1]++; } if(c == 'p') { p[pos-1]++; } if(c == 'm') { m[pos-1]++; } } int max = 0; int mc = 0; for(int i=0; i<9; i++) { max = Math.max(max, m[i]); if(i<7) { int tc = 0; tc += m[i]>0? 1:0; tc += m[i+1]>0? 1:0; tc += m[i+2]>0? 1:0; mc = Math.max(tc, mc); } } for(int i=0; i<9; i++) { max = Math.max(max, p[i]); if(i<7) { int tc = 0; tc += p[i]>0? 1:0; tc += p[i+1]>0? 1:0; tc += p[i+2]>0? 1:0; mc = Math.max(tc, mc); } } for(int i=0; i<9; i++) { max = Math.max(max, s[i]); if(i<7) { int tc = 0; tc += s[i]>0? 1:0; tc += s[i+1]>0? 1:0; tc += s[i+2]>0? 1:0; mc = Math.max(tc, mc); } } int ans = Math.max(mc, max); System.out.println(3-ans); } }
JAVA
1191_B. Tokitsukaze and Mahjong
Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, …, 9m, 1p, 2p, …, 9p, 1s, 2s, …, 9s. In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand. Do you know the minimum number of extra suited tiles she needs to draw so that she can win? Here are some useful definitions in this game: * A mentsu, also known as meld, is formed by a koutsu or a shuntsu; * A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu; * A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu. Some examples: * [2m, 3p, 2s, 4m, 1s, 2s, 4s] β€” it contains no koutsu or shuntsu, so it includes no mentsu; * [4s, 3m, 3p, 4s, 5p, 4s, 5p] β€” it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu; * [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β€” it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu. Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite. Input The only line contains three strings β€” the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s. Output Print a single integer β€” the minimum number of extra suited tiles she needs to draw. Examples Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 1 Note In the first example, Tokitsukaze already has a shuntsu. In the second example, Tokitsukaze already has a koutsu. In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β€” 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p].
2
8
# for t in range(int(input())): # s = input() # i, j = 0, 0 # cnt = 0 # ans = float('inf') # dic = {} # while j < len(s): # if len(dic) < 3: # dic[s[j]] = dic.get(s[j], 0) + 1 # # print(j) # # print(dic) # while len(dic) == 3: # ans = min(ans, j - i + 1) # dic[s[i]] -= 1 # if dic[s[i]] == 0: # del dic[s[i]] # i += 1 # # j += 1 # print((0, ans)[ans < float('inf')]) # for t in range(int(input())): # n = int(input()) # s = list(map(int, input().split())) # dp = [1] * n # for i in range(n): # k = 2 # while (i + 1) * k <= n: # j = (i + 1) * k # if s[i] < s[j - 1]: # dp[j - 1] = max(dp[j - 1], dp[i] + 1) # k += 1 # print(max(dp)) # for T in range(int(input())): # t = input() # z, o = 0, 0 # for ch in t: # z = z + 1 if ch == '0' else z # o = o + 1 if ch == '1' else o # if z > 0 and o > 0: # print('01' * len(t)) # elif o > 0 and not z: # print('1' * len(t)) # else: # print('0' * len(t)) # for t in range(int(input())): # n = int(input()) # a = list(map(int, input().split())) # a.sort() # ans = [] # while a: # ans.append(str(a.pop(len(a) // 2))) # print(' '.join(ans)) # for t in range(int(input())): # n = int(input()) # a = list(map(int, input().split())) # cnt = 0 # p = set() # l, r = 0, sum(a) # left, right = {}, {} # for i in a: # right[i] = right.get(i, 0) + 1 # for i in range(n - 1): # l += a[i] # left[a[i]] = left.get(a[i], 0) + 1 # r -= a[i] # right[a[i]] = right.get(a[i], 0) - 1 # if not right[a[i]]: # del right[a[i]] # j = n - i - 1 # if (2 + i) * (i + 1) // 2 == l and (j + 1) * j // 2 == r: # if len(left) == i + 1 and len(right) == j: # cnt += 1 # p.add((i + 1, n - i - 1)) # print(cnt) # if cnt: # for el in p: # print(*el) # for t in range(int(input())): # n = int(input()) # G = [] # taken = [False] * n # girl = -1 # for i in range(n): # g = list(map(int, input().split())) # k = g[0] # single = True # for j in range(1, k + 1): # if not taken[g[j] - 1]: # taken[g[j] - 1] = True # single = False # break # if single: # girl = i # if girl == -1: # print('OPTIMAL') # else: # print('IMPROVE') # print(girl + 1, taken.index(False) + 1) # for t in range(int(input())): # n = int(input()) # a = list(map(int, input().split())) # odd, even = [], [] # for i in range(2 * n): # if a[i] % 2: # odd.append(i + 1) # else: # even.append(i + 1) # for i in range(n - 1): # if len(odd) >= len(even): # print(odd.pop(), odd.pop()) # else: # print(even.pop(), even.pop()) # for t in range(int(input())): # n = int(input()) # a = list(map(int, input().split())) # a.sort() # ans, i, j = 0, 0, 1 # while j < n: # if a[i] < a[j]: # ans += 1 # i += 1 # j += 1 # else: # while j < n and a[i] == a[j]: # i += 1 # j += 1 # print(ans + 1) # for t in range(int(input())): # n = int(input()) # a = list(map(int, input().split())) # got = False # # b = 1 # while not got and b < 2 * n - 1: # if b % 2: # i, j = (b - 1) // 2, (b + 1) // 2 # else: # i, j = b // 2 - 1, b // 2 + 1 # left, right = set(a[:i]), set(a[j:]) # if left & right: # got = True # b += 1 # print('YES' if got else 'NO') # n, m, k = list(map(int, input().split())) # A = list(map(int, input().split())) # B = list(map(int, input().split())) # ans = 0 # a, b = [0], [0] # for el in A: # a.append(a[-1] + el) # for el in B: # b.append(b[-1] + el) # d = [(i, k//i) for i in range(1, int(k**0.5)+1) if k % i == 0] # d += [(j, i) for i, j in d if i != j] # for i in range(n): # for j in range(m): # for q, p in d: # if i + q <= n and j + p <= m: # if a[i + q] - a[i] == q and b[j + p] - b[j] == p: # ans += 1 # print(ans) # for t in range(int(input())): # n = int(input()) # s = input() # dic, se = {s: 1}, {s} # for k in range(2, n): # p = s[k - 1:] + (s[:k - 1], s[:k - 1][::-1])[(n % 2) == (k % 2)] # # print(k, p) # if p not in dic: # # print(dic, p) # dic[p] = k # se.add(p) # if s[::-1] not in dic: # dic[s[::-1]] = n # se.add(s[::-1]) # # print(dic) # ans = min(se) # print(ans) # print(dic[ans]) # for t in range(int(input())): # a, b, p = list(map(int, input().split())) # s = input() # road = [a if s[0] == 'A' else b] # st = [0] # for i in range(1, len(s) - 1): # if s[i] != s[i - 1]: # road.append(road[-1] + (a, b)[s[i] == 'B']) # st.append(i) # # print(road) # pay = road[-1] # j = 0 # while pay > p and j < len(st): # pay = road[-1] - road[j] # j += 1 # # print(j) # print(st[j] + 1 if j < len(st) else len(s)) # for t in range(int(input())): # n, x, y = list(map(int, input().split())) # print(max(1, min(x + y - n + 1, n)), min(n, x + y - 1)) # for t in range(int(input())): # n = int(input()) # a = list(map(int, input().split())) # print(' '.join(map(str, sorted(a, reverse=True)))) # s = input() # open, close = [], [] # i = 0 # for i in range(len(s)): # if s[i] == '(': # open.append(i) # else: # close.append(i) # i, j = 0, len(close) - 1 # ans = [] # while i < len(open) and j >= 0 and open[i] < close[j]: # ans += [open[i] + 1, close[j] + 1] # i += 1 # j -= 1 # ans.sort() # print('0' if not ans else '1\n{}\n{}'.format(len(ans), ' '.join([str(idx) for idx in ans]))) import collections # n, m = list(map(int, input().split())) # a = list(input() for i in range(n)) # dic = {} # for w in a: # dic[w] = dic.get(w, 0) + 1 # l, r = '', '' # for i in range(n): # for j in range(i + 1, n): # # print(i, j, a) # if a[i] == a[j][::-1] and dic[a[i]] and dic[a[j]]: # l += a[i] # r = a[j] + r # dic[a[i]] -= 1 # dic[a[j]] -= 1 # c = '' # for k, v in dic.items(): # if v and k == k[::-1]: # if c and c[-m] == k or not c: # c += k # print(f'{len(l) + len(c) + len(r)}\n{l + c + r}') # for t in range(int(input())): # n, g, b = list(map(int, input().split())) # d = n // 2 + n % 2 # full, inc = divmod(d, g) # ans = (g + b) * (full - 1, full)[inc > 0] + (g, inc)[inc > 0] # print(ans if ans >= n else n) # for t in range(int(input())): # n = int(input()) # a = list(map(int, input().split())) # a.sort() # print(a[n] - a[n - 1]) # for t in range(int(input())): # n, x = list(map(int, input().split())) # s = input() # cntz = s.count('0') # total = 2 * cntz - n # bal = 0 # ans = 0 # for i in range(n): # if not total: # if bal == x: # ans = -1 # elif not abs(x - bal) % abs(total): # if (x - bal) // total >= 0: # ans += 1 # bal += 1 if s[i] == '0' else -1 # print(ans) # n = int(input()) # ans = 0 # for i in range(1, n + 1): # ans += 1 / i # print(ans) # for t in range(int(input())): # n = int(input()) # a = list(map(int, input().split())) # p, s = 0, n - 1 # for i in range(n): # if a[i] < i: # break # p = i # for i in range(n - 1, -1, -1): # if a[i] < n - i - 1: # break # s = i # print('Yes' if s <= p else 'No') # n, k = list(map(int, input().split())) # a = [input() for i in range(n)] # c = set(a) # b = set() # for i in range(n): # for j in range(i + 1, n): # third = '' # for c1, c2 in zip(a[i], a[j]): # if c1 == c2: # third += c1 # else: # if c1 != 'S' and c2 != 'S': # third += 'S' # elif c1 != 'E' and c2 != 'E': # third += 'E' # else: # third += 'T' # if third in c: # b.add(frozenset([a[i], a[j], third])) # print(len(b)) # for t in range(int(input())): # n = int(input()) # a = list(map(int, input().split())) # total, curr = sum(a), 0 # ans, i, start = 'YES', 0, 0 # while ans == 'YES' and i < n: # if curr > 0: # curr += a[i] # else: # curr = a[i] # start = i # # print(curr, i, start, total) # if i - start + 1 < n and curr >= total: # ans = 'NO' # i += 1 # print(ans) # for t in range(int(input())): # n, p, k = list(map(int, input().split())) # a = list(map(int, input().split())) # a.sort(reverse=True) # odd, even = 0, 0 # i, j = len(a) - 1, len(a) - 2 # curr = 0 # while curr < p and i >= 0: # curr += a[i] # if curr <= p: # odd += 1 # i -= 2 # curr = 0 # while curr < p and j >= 0: # curr += a[j] # if curr <= p: # even += 1 # j -= 2 # print(max(odd * 2 - 1, even * 2)) # for t in range(int(input())): # s, c = input().split() # s = list(ch for ch in s) # sor = sorted(s) # for i in range(len(s)): # if s[i] != sor[i]: # j = max(j for j, v in enumerate(s[i:], i) if v == sor[i]) # s = s[:i] + [s[j]] + s[i + 1:j] + [s[i]] + s[j + 1:] # break # s = ''.join(s) # print(s if s < c else '---') # for t in range(int(input())): # n, s = list(map(int, input().split())) # a = list(map(int, input().split())) # if sum(a) <= s: # print(0) # else: # curr, i, j = 0, 0, 0 # for i in range(n): # if a[i] > a[j]: # j = i # s -= a[i] # if s < 0: # break # print(j + 1) # for t in range(int(input())): # a, b = list(map(int, input().split())) # a, b = (b, a) if b > a else (a, b) # if not ((1 + 8 * (a - b))**0.5 - 1) % 2 and ((1 + 8 * (a - b))**0.5 - 1) // 2 >= 0: # ans = ((1 + 8 * (a - b))**0.5 - 1) // 2 # print(int(ans)) # else: # n1 = int(((1 + 8 * (a - b))**0.5 - 1) // 2) + 1 # while (n1 * (n1 + 1) // 2) % 2 != (a - b) % 2: # n1 += 1 # print(n1) # for t in range(int(input())): # n = int(input()) # a = list(map(int, input().split())) # a.sort() # ans = 0 # l = 0 # dic = {} # for i in range(n - 1, -1, -1): # if not a[i] % 2: # l, r = 0, 30 # while l < r: # m = (l + r) // 2 # # print(l, r, m, a[i] % 2**m) # if a[i] % 2**m: # r = m # else: # l = m + 1 # dic[a[i] // 2**(l - 1)] = max(dic.get(a[i] // 2**(l - 1), 0), l - 1) # print(sum(list(dic.values()))) # n = int(input()) # s = input() # b = s.count('B') # w = n - b # if b % 2 and w % 2: # print(-1) # elif not b or not w: # print(0) # else: # ans = [] # if not b % 2: # for i in range(n - 1): # if s[i] != 'W': # ans += [str(i + 1)] # s = s[:i] + 'W' + 'BW'[s[i + 1] == 'B'] + s[i + 2:] # elif not w % 2: # for i in range(n - 1): # if s[i] != 'B': # ans += [str(i + 1)] # s = s[:i] + 'B' + 'WB'[s[i + 1] == 'W'] + s[i + 2:] # print(len(ans)) # print(' '.join(ans)) # n, m = list(map(int, input().split())) # a = list(map(int, input().split())) # b = list(map(int, input().split())) # b.sort() # ans = float('inf') # for i in range(n): # x = (b[0] - a[i]) % m # ax = [] # for j in range(n): # ax.append((a[j] + x) % m) # if b == sorted(ax): # ans = min(ans, x) # print(ans) # for t in range(int(input())): # n = int(input()) # ans = [1] + [0] * (n - 1) # p = list(map(int, input().split())) # i, j, curr, m = p.index(1), 1, 1, 1 # l, r = i, i # while l >= 0 and r < n: # if l and curr + p[l - 1] == (m + 2) * (m + 1) // 2: # ans[m] = 1 # curr += p[l - 1] # l -= 1 # # elif r + 1 < n and curr + p[r + 1] == (m + 2) * (m + 1) // 2: # ans[m] = 1 # curr += p[r + 1] # r += 1 # else: # if l and r + 1 < n: # curr, l, r = ((curr + p[l - 1], l - 1, r), # (curr + p[r + 1], l, r + 1))[p[r + 1] < p[l - 1]] # elif not l and r + 1 < n: # curr, l, r = curr + p[r + 1], l, r + 1 # elif r + 1 == n and l: # curr, l, r = curr + p[l - 1], l - 1, r # else: # break # m += 1 # print(''.join([str(i) for i in ans])) # for t in range(int(input())): # n = int(input()) # p = [input() for i in range(n)] # ans = 0 # for i in range(n): # if p[i] in p[i + 1:]: # for j in range(10): # code = p[i][:3] + str(j) # if code not in p: # p[i] = code # ans += 1 # break # print(ans) # for code in p: # print(code) # for t in range(int(input())): # a, b = list(map(int, input().split())) # if (a + b) % 3 == 0 and 2 * min(a, b) >= max(a, b): # print('YES') # else: # print('NO') # for t in range(int(input())): # x, y = list(map(int, input().split())) # if (x == 1 and y > 1) or (x == 2 and y > 3) or (x == 3 and y > 3): # print('NO') # else: # print('YES') # for t in range(int(input())): # n, m = list(map(int, input().split())) # a = list(map(int, input().split())) # if m < n or n == 2: # print(-1) # elif m == n: # print(2 * sum(a)) # for i in range(n - 1): # print(i + 1, i + 2) # print(n, 1) # else: # b = [(a[i], i + 1) for i in range(n)] # b.sort() # d = m - n # ans = sum(a) + d * (b[0][0] + b[1][0]) # for i in range(d): # print(b[0][1], b[1][1]) # for i in range(n - 1): # print(i + 1, i + 2) # print(n, 1) # n = int(input()) # a = list(map(int, input().split())) # if n % 2: # print(-1) # else: # d = 0 # c = [] # curr = 0 # came = set() # day = set() # inc = False # for i in range(n): # if a[i] > 0: # if a[i] in day: # inc = True # break # else: # day.add(a[i]) # came.add(a[i]) # else: # if abs(a[i]) not in came: # inc = True # break # else: # came.remove(abs(a[i])) # if len(came) == 0: # d += 1 # c.append(i + 1) # day = set() # if len(came) > 0: # inc = True # if inc: # print(-1) # else: # print(d) # print(c[0]) # for i in range(1, len(c)): # print(c[i] - c[i - 1]) # n = int(input()) # a = list(map(int, input().split())) # a.sort() # x, y = sum(a[:n // 2])**2, sum(a[n // 2:])**2 # print(x + y) # for t in range(int(input())): # n = int(input()) # r, p, s = list(map(int, input().split())) # b = input() # S, P, R = b.count('S'), b.count('P'), b.count('R') # cnt = 0 # ans = '' # # print(r, 'rock', p, 'paper', s, 'sc') # for i in range(n): # if b[i] == 'S': # if r > 0: # ans, r, cnt = ans + 'R', r - 1, cnt + 1 # else: # if p > R: # ans, p = ans + 'P', p - 1 # if len(ans) < i + 1 and s > P: # ans, s = ans + 'S', s - 1 # S -= 1 # elif b[i] == 'P': # if s > 0: # ans, s, cnt = ans + 'S', s - 1, cnt + 1 # else: # if p > R: # ans, p = ans + 'P', p - 1 # if len(ans) < i + 1 and r > S: # ans, r = ans + 'R', r - 1 # P -= 1 # else: # if p > 0: # ans, p, cnt = ans + 'P', p - 1, cnt + 1 # else: # if s > P: # ans, s = ans + 'S', s - 1 # if len(ans) < i + 1 and r > S: # ans, r = ans + 'R', r - 1 # R -= 1 # if cnt < (n // 2 + n % 2): # print('NO') # else: # print('YES') # # print(r, p, s) # print(ans) # for t in range(int(input())): # n = int(input()) # s = input() # f, l = s.find('1'), s.rfind('1') # f, l = max(f + 1, n - f) if f != -1 else 0, max(l + 1, n - l) if l != -1 else 0 # if not f and not l: # print(n) # else: # print(f * 2) if f > l else print(l * 2) # t = int(input()) # ans = list() # for _ in [0] * t: # n, r = map(int, input().split()) # x = sorted(set(map(int, input().split())))[::-1] # ans.append(sum([x - i * r > 0 for i, x in enumerate(x)])) # print(' '.join(map(str, ans))) # n = int(input()) # dots = [] # for i in range(n): # dots.append(sum(list(map(int, input().split())))) # print(max(dots)) # n, m = map(int, input().split()) # print(pow(2**m - 1, n, 10**9 + 7)) # n, k = map(int, input().split()) # s = input() # if not k: # print(s) # elif n == 1: # print('0') # else: # s = [int(i) for i in s] # if s[0] > 1: # s[0], k = 1, k - 1 # for i in range(1, n): # if not k: # break # if s[i] > 0: # s[i], k = 0, k - 1 # print(''.join(map(str, s))) if len(s) > 1 else print('0') # m, n = map(int, input().split()) # r = list(map(int, input().split())) # c = list(map(int, input().split())) # grid = [['ok'] * (n + 1) for i in range(m + 1)] # # for i in range(m): # row = r[i] # if row: # for j in range(row): # grid[i][j] = 1 # grid[i][row] = 0 # else: # grid[i][row] = 0 # # # inv = False # for j in range(n): # col = c[j] # if col: # for i in range(col): # if grid[i][j] == 0: # inv = True # break # else: # grid[i][j] = 1 # if grid[col][j] == 1: # inv = True # break # else: # grid[col][j] = 0 # else: # if grid[col][j] == 1: # inv = True # break # else: # grid[col][j] = 0 # # if inv: # print(0) # else: # cnt = 0 # for row in grid[:m]: # cnt += row[:n].count('ok') # print(pow(2, cnt, 10**9 + 7)) # n = int(input()) # for i in range(n): # print('BW' * (n // 2) + 'B' * (n % 2) if i % 2 else 'WB' * (n // 2) + 'W' * (n % 2)) # n = int(input()) # a = list(map(int, input().split())) # curr, odd, even = 0, 0, 0 # p = 0 # for d in a: # odd, even = ((odd, even + 1), (odd + 1, even))[curr % 2] # curr += 1 if d < 0 else 0 # p += odd if curr % 2 else even # print(n * (n + 1) // 2 - p, p) # n = int(input()) # a = list(map(int, input().split())) # p, m, z = 0, [], 0 # for d in a: # if d > 0: # p += d # elif d < 0: # m.append(d) # else: # z += 1 # ans = p - (n - z - len(m)) # # if len(m) % 2: # if z: # m.append(-1) # ans += 1 # z -= 1 # else: # m.sort(reverse=True) # x = m.pop() # ans += 1 - x # # mm = len(m) # ans += abs(sum(m)) - mm # ans += z # print(ans) # n, l, r = map(int, input().split()) # a = [2**i for i in range(r)] # print(sum(a[:l]) + n - l, sum(a) + (n - r) * a[-1]) # n = int(input()) # a = list(map(int, input().split())) # a.sort() # print('YES' if not sum(a) % 2 and a[-1] <= sum(a) - a[-1] else 'NO') # for t in range(int(input())): # n, m, k = map(int, input().split()) # h = list(map(int, input().split())) # ans = 'YES' # for i in range(n - 1): # if abs(h[i] - h[i + 1]) > k: # d = h[i] - h[i + 1] # if d < 0 and m >= abs(d) - k: # m -= -k - d # elif d > 0: # m += min(d + k, h[i]) # else: # ans = 'NO' # break # else: # d = h[i] - h[i + 1] # if d >= 0: # m += min(d + k, h[i]) # else: # m += min(k + d, h[i]) # print(ans) # h, l = map(int, input().split()) # print((h**2 + l**2) / (2 * h) - h) # n = int(input()) # a = list(map(int, input().split())) # a = {i: j for i, j in enumerate(a)} # m, idx = 0, 0 # ans = 'YES' # for i in range(n): # if a[i] > m: # m = a[i] # idx = i # for i in range(1, idx): # if a[i] < a[i - 1]: # ans = 'NO' # break # for i in range(idx + 1, n): # if a[i] > a[i - 1]: # ans = 'NO' # break # print(ans) # n, k = map(int, input().split()) # l, r = 0, n # while l <= r: # m = (l + r) // 2 # t = n - m # if m * (m + 1) // 2 - t == k: # ans = t # break # elif m * (m + 1) // 2 - t < k: # l = m + 1 # else: # r = m - 1 # print(ans) # for t in range(int(input())): # n, m = map(int, input().split()) # grid = [input() for i in range(n)] # rows = [] # cols = [0] * m # for row in grid: # rows.append(0) # for i in range(m): # if row[i] == '.': # rows[-1] += 1 # cols[i] += 1 # ans = m + n - 1 # for i in range(n): # for j in range(m): # ans = min(ans, rows[i] + cols[j] - (grid[i][j] == '.')) # print(ans) tiles = input().split() unique = {} m, p, s = set(), set(), set() m_unique = 0 for t in tiles: unique[t] = unique.get(t, 0) + 1 m_unique = max(m_unique, unique[t]) if t[1] == 'm': m.add(int(t[0])) elif t[1] == 'p': p.add(int(t[0])) else: s.add(int(t[0])) ans = 3 - m_unique for t in (m, p, s): if not t: continue else: m_sub = 0 l = list(sorted(t)) dif = [] for i in range(1, len(t)): dif.append(l[i] - l[i - 1]) if dif[-1] == 1 or dif[-1] == 2: m_sub = max(m_sub, 2) if i > 1 and dif[-1] == dif[-2] == 1: m_sub = 3 # print(l, dif, m_sub) ans = min(ans, 3 - m_sub) print(ans)
PYTHON3
1191_B. Tokitsukaze and Mahjong
Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, …, 9m, 1p, 2p, …, 9p, 1s, 2s, …, 9s. In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand. Do you know the minimum number of extra suited tiles she needs to draw so that she can win? Here are some useful definitions in this game: * A mentsu, also known as meld, is formed by a koutsu or a shuntsu; * A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu; * A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu. Some examples: * [2m, 3p, 2s, 4m, 1s, 2s, 4s] β€” it contains no koutsu or shuntsu, so it includes no mentsu; * [4s, 3m, 3p, 4s, 5p, 4s, 5p] β€” it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu; * [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β€” it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu. Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite. Input The only line contains three strings β€” the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s. Output Print a single integer β€” the minimum number of extra suited tiles she needs to draw. Examples Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 1 Note In the first example, Tokitsukaze already has a shuntsu. In the second example, Tokitsukaze already has a koutsu. In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β€” 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p].
2
8
a=list(map(str,input().split()));l=a s=m=p=0;s1,m1,p1=[],[],[] for i in a: if 's' in i:s+=1;s1+=[int(i[0])] elif 'p' in i:p+=1;p1+=[int(i[0])] else:m+=1;m1+=[int(i[0])] if m and s and p:print(2) elif len(set(a))==1:print(0) elif max(s,m,p)==3: if s==3:a=s1 elif m==3:a=m1 else:a=p1 a=sorted(a);r=2 if a[1]-a[0]==a[2]-a[1]==1:r=0 elif a[1]-a[0] in [1,2] or a[2]-a[1] in [1,2]:r=1 else:r=2 if len(set(l))==2:r=min(r,1) print(r) else: if s==2:a=s1 elif p==2:a=p1 else:a=m1 a=sorted(a) if a[1]-a[0]==1 or a[1]-a[0]==2:print(1) else: if len(set(l))==2:print(1) else:print(2)
PYTHON3
1191_B. Tokitsukaze and Mahjong
Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, …, 9m, 1p, 2p, …, 9p, 1s, 2s, …, 9s. In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand. Do you know the minimum number of extra suited tiles she needs to draw so that she can win? Here are some useful definitions in this game: * A mentsu, also known as meld, is formed by a koutsu or a shuntsu; * A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu; * A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu. Some examples: * [2m, 3p, 2s, 4m, 1s, 2s, 4s] β€” it contains no koutsu or shuntsu, so it includes no mentsu; * [4s, 3m, 3p, 4s, 5p, 4s, 5p] β€” it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu; * [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β€” it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu. Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite. Input The only line contains three strings β€” the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s. Output Print a single integer β€” the minimum number of extra suited tiles she needs to draw. Examples Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 1 Note In the first example, Tokitsukaze already has a shuntsu. In the second example, Tokitsukaze already has a koutsu. In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β€” 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p].
2
8
import bisect import collections import copy import functools import heapq import itertools import math import random import re import sys import time import string from typing import * sys.setrecursionlimit(99999) arr = list(input().split()) arr.sort() ans = 3 cs = collections.Counter(arr) def check(ap): cp = collections.Counter(ap) c = 0 for k, v in cs.items(): c += min(v, cp[k]) return 3 - c for i in range(1, 10): ans = min(ans, check([str(i) + 's', str(i) + 's', str(i) + 's'])) ans = min(ans, check([str(i) + 'p', str(i) + 'p', str(i) + 'p'])) ans = min(ans, check([str(i) + 'm', str(i) + 'm', str(i) + 'm'])) if i <= 7: ans = min(ans, check([str(i) + 's', str(i + 1) + 's', str(i + 2) + 's'])) ans = min(ans, check([str(i) + 'p', str(i + 1) + 'p', str(i + 2) + 'p'])) ans = min(ans, check([str(i) + 'm', str(i + 1) + 'm', str(i + 2) + 'm'])) print(ans)
PYTHON3
1191_B. Tokitsukaze and Mahjong
Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, …, 9m, 1p, 2p, …, 9p, 1s, 2s, …, 9s. In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand. Do you know the minimum number of extra suited tiles she needs to draw so that she can win? Here are some useful definitions in this game: * A mentsu, also known as meld, is formed by a koutsu or a shuntsu; * A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu; * A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu. Some examples: * [2m, 3p, 2s, 4m, 1s, 2s, 4s] β€” it contains no koutsu or shuntsu, so it includes no mentsu; * [4s, 3m, 3p, 4s, 5p, 4s, 5p] β€” it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu; * [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β€” it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu. Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite. Input The only line contains three strings β€” the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s. Output Print a single integer β€” the minimum number of extra suited tiles she needs to draw. Examples Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 1 Note In the first example, Tokitsukaze already has a shuntsu. In the second example, Tokitsukaze already has a koutsu. In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β€” 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p].
2
8
strInput = input() m = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] s = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] p = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] nekoHands = 0 while (nekoHands+1 <= len(strInput)): if (strInput[nekoHands] == " "): nekoHands += 1 elif ( "9" >= strInput[nekoHands] >= "1" ): if (strInput[nekoHands+1] == "m" ): m[int(strInput[nekoHands])] += 1 elif (strInput[nekoHands+1] == "s" ): s[int(strInput[nekoHands])] += 1 elif (strInput[nekoHands+1] == "p" ): p[int(strInput[nekoHands])] += 1 nekoHands += 2 else: print("error!") maxKoutsu = 0 if (3 in m+s+p): maxKoutsu = 3 elif (2 in m+s+p): maxKoutsu = 2 elif (1 in m+s+p): maxKoutsu = 1 nekoFeet = 0 maxShuntsu = 1 while (nekoFeet+1 <= 9): if (m[nekoFeet] > 0 and m[nekoFeet+1] > 0 and m[nekoFeet+2] > 0 and maxShuntsu < 3): maxShuntsu = 3 break elif (m[nekoFeet] > 0 and (m[nekoFeet+1] > 0 or m[nekoFeet+2] > 0) and maxShuntsu < 2): maxShuntsu = 2 if (s[nekoFeet] > 0 and s[nekoFeet+1] > 0 and s[nekoFeet+2] > 0 and maxShuntsu < 3): maxShuntsu = 3 break elif (s[nekoFeet] > 0 and (s[nekoFeet+1] > 0 or s[nekoFeet+2] > 0) and maxShuntsu < 2): maxShuntsu = 2 if (p[nekoFeet] > 0 and p[nekoFeet+1] > 0 and p[nekoFeet+2] > 0 and maxShuntsu < 3): maxShuntsu = 3 break elif (p[nekoFeet] > 0 and (p[nekoFeet+1] > 0 or p[nekoFeet+2] > 0) and maxShuntsu < 2): maxShuntsu = 2 nekoFeet += 1 if (maxKoutsu > maxShuntsu): print (3-maxKoutsu) else : print (3-maxShuntsu)
PYTHON3
1191_B. Tokitsukaze and Mahjong
Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, …, 9m, 1p, 2p, …, 9p, 1s, 2s, …, 9s. In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand. Do you know the minimum number of extra suited tiles she needs to draw so that she can win? Here are some useful definitions in this game: * A mentsu, also known as meld, is formed by a koutsu or a shuntsu; * A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu; * A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu. Some examples: * [2m, 3p, 2s, 4m, 1s, 2s, 4s] β€” it contains no koutsu or shuntsu, so it includes no mentsu; * [4s, 3m, 3p, 4s, 5p, 4s, 5p] β€” it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu; * [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β€” it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu. Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite. Input The only line contains three strings β€” the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s. Output Print a single integer β€” the minimum number of extra suited tiles she needs to draw. Examples Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 1 Note In the first example, Tokitsukaze already has a shuntsu. In the second example, Tokitsukaze already has a koutsu. In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β€” 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p].
2
8
import java.util.*; import java.io.*; public class TokitsukazeandMahjong { public static void main(String[] d) { int ans=2; Scanner sc=new Scanner(System.in); String s1=sc.next(); String s2=sc.next(); String s3=sc.next(); int a1=(s1.charAt(0)-'0'); int a2=(s2.charAt(0)-'0'); int a3=(s3.charAt(0)-'0'); char b1=s1.charAt(1); char b2=s2.charAt(1); char b3=s3.charAt(1); int b[]=new int[3]; b[0]=a1; b[1]=a2; b[2]=a3; Arrays.sort(b); if(b1==b2 && b2==b3) { if(a1==a2 && a2==a3) { ans=0; } else if(a1==a2 || a2==a3 || a1==a3) { ans=1; } else if(b[1]==(b[0]+1) && b[2]==(b[1]+1)) { ans=0; } else { if(b[2]==(b[1]+1) || b[1]==(b[0]+1) || b[1]==(b[0]+2) || b[1]==(b[0]+1)) ans=1; } } else { if(b1==b2) { if(a1==a2) ans=1; if(a1>a2) { if(a1==(a2+1) || a1==(a2+2)) ans=1; } else { if(a2==(a1+1) || a2==(a1+2)) ans=1; } } if(b2==b3) { if(a2==a3) ans=1; if(a2>a3) { if(a2==(a3+1) || a2==(a3+2)) ans=1; } else { if(a3==(a2+1) || a3==(a2+2)) ans=1; } } if(b1==b3) { if(a1==a3) ans=1; if(a1>a3) { if(a1==(a3+1) || a1==(a3+2)) ans=1; } else { if(a3==(a1+1) || a3==(a1+2)) ans=1; } } } System.out.println(ans); } }
JAVA
1191_B. Tokitsukaze and Mahjong
Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, …, 9m, 1p, 2p, …, 9p, 1s, 2s, …, 9s. In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand. Do you know the minimum number of extra suited tiles she needs to draw so that she can win? Here are some useful definitions in this game: * A mentsu, also known as meld, is formed by a koutsu or a shuntsu; * A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu; * A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu. Some examples: * [2m, 3p, 2s, 4m, 1s, 2s, 4s] β€” it contains no koutsu or shuntsu, so it includes no mentsu; * [4s, 3m, 3p, 4s, 5p, 4s, 5p] β€” it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu; * [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β€” it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu. Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite. Input The only line contains three strings β€” the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s. Output Print a single integer β€” the minimum number of extra suited tiles she needs to draw. Examples Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 1 Note In the first example, Tokitsukaze already has a shuntsu. In the second example, Tokitsukaze already has a koutsu. In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β€” 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p].
2
8
a=[x for x in input().split()] flag=0 m=[0 for x in range(10)] p=[0 for x in range(10)] s=[0 for x in range(10)] m1=0 for i in a: if i[1]=='s': s[int(i[0])]=1 elif i[1]=='p': p[int(i[0])]=1 elif i[1]=='m': m[int(i[0])]=1 if a.count(i)==3: flag=1 break elif a.count(i)==2: m1=1 #i=str(i) #print(s) #print(m) #print(p) if flag==0: #print(str(p)) t1="".join(str(x) for x in p) t2="".join(str(x) for x in m) t3="".join(str(x) for x in s) if "111" in t1: flag=1 elif "111" in t2: flag=1 elif "111" in t3: flag=1 elif "11" in t1 : m1=1 elif "11" in t2: m1=1 elif "11" in t3: m1=1 elif "101" in t1 : m1=1 elif "101" in t2: m1=1 elif "101" in t3: m1=1 if flag==1: print("0") elif m1==1: print("1") else: print("2")
PYTHON3
1191_B. Tokitsukaze and Mahjong
Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, …, 9m, 1p, 2p, …, 9p, 1s, 2s, …, 9s. In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand. Do you know the minimum number of extra suited tiles she needs to draw so that she can win? Here are some useful definitions in this game: * A mentsu, also known as meld, is formed by a koutsu or a shuntsu; * A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu; * A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu. Some examples: * [2m, 3p, 2s, 4m, 1s, 2s, 4s] β€” it contains no koutsu or shuntsu, so it includes no mentsu; * [4s, 3m, 3p, 4s, 5p, 4s, 5p] β€” it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu; * [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β€” it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu. Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite. Input The only line contains three strings β€” the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s. Output Print a single integer β€” the minimum number of extra suited tiles she needs to draw. Examples Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 1 Note In the first example, Tokitsukaze already has a shuntsu. In the second example, Tokitsukaze already has a koutsu. In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β€” 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p].
2
8
d = { 'm': 10, 's': 30, 'p': 60 } a, b, c = list(sorted([int(i[0]) + d[i[1]] for i in input().split(' ')])) if (a == b and b == c) or (a == b - 1 and b == c - 1): print(0) elif a == b - 1 or b == c - 1 or a == b or b == c or a == c - 2 or a == b - 2 or b == c - 2: print(1) else: print(2) ''' d={'m':10,'p':30,'s':50} #men=> kou or shu a,y,z=sorted([int(x[0])+d[x[1]] for x in (input().split())]) print(a,y,z) #kou and sust if (a==y and y==z) or (a==y-1 and y==z-1): print(0) elif a==y or y==z or a==z or a==y-1 or y==z-1 or a==y-2 or y==z-2 : print(1) else: print(2) '''
PYTHON3
1191_B. Tokitsukaze and Mahjong
Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, …, 9m, 1p, 2p, …, 9p, 1s, 2s, …, 9s. In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand. Do you know the minimum number of extra suited tiles she needs to draw so that she can win? Here are some useful definitions in this game: * A mentsu, also known as meld, is formed by a koutsu or a shuntsu; * A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu; * A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu. Some examples: * [2m, 3p, 2s, 4m, 1s, 2s, 4s] β€” it contains no koutsu or shuntsu, so it includes no mentsu; * [4s, 3m, 3p, 4s, 5p, 4s, 5p] β€” it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu; * [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β€” it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu. Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite. Input The only line contains three strings β€” the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s. Output Print a single integer β€” the minimum number of extra suited tiles she needs to draw. Examples Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 1 Note In the first example, Tokitsukaze already has a shuntsu. In the second example, Tokitsukaze already has a koutsu. In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β€” 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p].
2
8
import java.io.BufferedReader; import java.io.InputStreamReader; public class tok_and_mahjong { public static void main(String[] args) throws java.io.IOException{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String[] inputStr = br.readLine().trim().split(" "); int m[] = new int[10]; int p[] = new int[10]; int s[] = new int[10]; for(String str: inputStr){ if (str.charAt(1) == 'm'){ m[str.charAt(0)-'0']++; } else if (str.charAt(1) == 'p'){ p[str.charAt(0)-'0']++; } else { s[str.charAt(0)-'0']++; } } int max = 0; max = Math.max(checkThrees(m), max); max = Math.max(checkThrees(p), max); max = Math.max(checkThrees(s), max); max = Math.max(checkCont(m), max); max = Math.max(checkCont(p), max); max = Math.max(checkCont(s), max); if(max > 3){ max = 3; } System.out.println(3-max); } public static int checkCont(int[] arr){ int max = 0; for(int i=0; i<arr.length-2; i++){ int first=0, second=0, third=0; if(arr[i] > 0){ first = 1; } if(arr[i+1] > 0 ){ second = 1; } if(arr[i+2] > 0){ third = 1; } if(first+second+third > max){ max = first+second+third; } } return max; } public static int checkThrees(int[] arr){ int max = 0; for(int i=0; i<arr.length; i++){ if(arr[i] >= max){ max = arr[i]; } } return max; } }
JAVA
1191_B. Tokitsukaze and Mahjong
Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, …, 9m, 1p, 2p, …, 9p, 1s, 2s, …, 9s. In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand. Do you know the minimum number of extra suited tiles she needs to draw so that she can win? Here are some useful definitions in this game: * A mentsu, also known as meld, is formed by a koutsu or a shuntsu; * A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu; * A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu. Some examples: * [2m, 3p, 2s, 4m, 1s, 2s, 4s] β€” it contains no koutsu or shuntsu, so it includes no mentsu; * [4s, 3m, 3p, 4s, 5p, 4s, 5p] β€” it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu; * [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β€” it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu. Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite. Input The only line contains three strings β€” the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s. Output Print a single integer β€” the minimum number of extra suited tiles she needs to draw. Examples Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 1 Note In the first example, Tokitsukaze already has a shuntsu. In the second example, Tokitsukaze already has a koutsu. In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β€” 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p].
2
8
def main(): cards = input().split() my_list = [] if cards[0] == cards[1] == cards[2] : ans = 0 print(ans) elif cards[0][1] == cards[1][1] == cards[2][1]: first_number = int(cards[0][0]) Secound_number = int(cards[1][0]) third_nubmer = int(cards[2][0]) difference = first_number - Secound_number difference1 = first_number - third_nubmer difference2 = Secound_number - third_nubmer for i in range(0,3): my_list.append(int(cards[i][0])) my_list.sort() if (int(my_list[1]) - int(my_list[0]) == 1) and (int(my_list[2]) - int(my_list[1]) == 1): ans = 0 print(ans) elif difference == 1 or difference == -1 or difference == 0 or difference == 2 or difference == -2 or difference1 == 1 or difference1 == -1 or difference1 == 0 or difference1 == 2 or difference1 == -2 or difference2 == 1 or difference2 == -1 or difference2 == 0 or difference2 == 2 or difference2 == -2: ans = 1 print(ans) else: ans = 2 print(ans) elif cards[0][1] != cards[1][1] and cards[0][1] != cards[2][1] and cards[1][1] != cards[2][1] : ans = 2 print(ans) elif cards[0][1] == cards[1][1] or cards[0][1] == cards[2][1] or cards[1][1] == cards[2][1] : if cards[0][1] == cards[1][1]: first_number = int(cards[0][0]) Secound_number = int(cards[1][0]) difference = first_number - Secound_number if difference == 1 or difference == -1 or difference == 0 or difference == 2 or difference == -2: ans = 1 print(ans) else: ans = 2 print(ans) elif cards[0][1] == cards[2][1]: first_number = int(cards[0][0]) Secound_number = int(cards[2][0]) difference = first_number - Secound_number if difference == 1 or difference == -1 or difference == 0 or difference == 2 or difference == -2: ans = 1 print(ans) else: ans = 2 print(ans) elif cards[1][1] == cards[2][1]: first_number = int(cards[2][0]) Secound_number = int(cards[1][0]) difference = first_number - Secound_number if difference == 1 or difference == -1 or difference == 0 or difference == 2 or difference == -2: ans = 1 print(ans) else: ans = 2 print(ans) if __name__ == '__main__': main()
PYTHON3
1191_B. Tokitsukaze and Mahjong
Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, …, 9m, 1p, 2p, …, 9p, 1s, 2s, …, 9s. In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand. Do you know the minimum number of extra suited tiles she needs to draw so that she can win? Here are some useful definitions in this game: * A mentsu, also known as meld, is formed by a koutsu or a shuntsu; * A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu; * A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu. Some examples: * [2m, 3p, 2s, 4m, 1s, 2s, 4s] β€” it contains no koutsu or shuntsu, so it includes no mentsu; * [4s, 3m, 3p, 4s, 5p, 4s, 5p] β€” it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu; * [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β€” it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu. Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite. Input The only line contains three strings β€” the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s. Output Print a single integer β€” the minimum number of extra suited tiles she needs to draw. Examples Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 1 Note In the first example, Tokitsukaze already has a shuntsu. In the second example, Tokitsukaze already has a koutsu. In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β€” 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p].
2
8
# -*- coding: utf-8 -*- """ Created on Fri Jul 12 17:39:54 2019 @author: Hamadeh """ # -*- coding: utf-8 -*- """ Created on Fri Jul 12 17:33:49 2019 @author: Hamadeh """ class cinn: def __init__(self): self.x=[] def cin(self,t=int): if(len(self.x)==0): a=input() self.x=a.split() self.x.reverse() return self.get(t) def get(self,t): return t(self.x.pop()) def clist(self,n,t=int): #n is number of inputs, t is type to be casted l=[0]*n for i in range(n): l[i]=self.cin(t) return l def clist2(self,n,t1=int,t2=int,t3=int,tn=2): l=[0]*n for i in range(n): if(tn==2): a1=self.cin(t1) a2=self.cin(t2) l[i]=(a1,a2) elif (tn==3): a1=self.cin(t1) a2=self.cin(t2) a3=self.cin(t3) l[i]=(a1,a2,a3) return l def clist3(self,n,t1=int,t2=int,t3=int): return self.clist2(self,n,t1,t2,t3,3) def cout(self,i,ans=''): if(ans==''): print("Case #"+str(i+1)+":", end=' ') else: print("Case #"+str(i+1)+":",ans) def printf(self,thing): print(thing,end='') def countlist(self,l,s=0,e=None): if(e==None): e=len(l) dic={} for el in range(s,e): if l[el] not in dic: dic[l[el]]=1 else: dic[l[el]]+=1 return dic def talk (self,x): print(x,flush=True) def dp1(self,k): L=[-1]*(k) return L def dp2(self,k,kk): L=[-1]*(k) for i in range(k): L[i]=[-1]*kk return L def isprime(self,n): if(n==1 or n==0): return False for i in range(2,int(n**0.5+1)): if(n%i==0): return False return True def factors(self,n): from functools import reduce return set(reduce(list.__add__, ([i, n//i] for i in range(1, int(n**0.5) + 1) if n % i == 0))) def nthprime(self,n): #usable up to 10 thousand i=0 s=2 L=[] while(i<n): while(not self.isprime(s)): s+=1 L.append(s) s+=1 i+=1 return L def matrixin(self,m,n,t=int): L=[] for i in range(m): p=self.clist(n,t) L.append(p) return L def seive(self,k): #1000000 tops n=k+1 L=[True]*n L[1]=False L[0]=False for i in range(2,n): if(L[i]==True): for j in range(2*i,n,i): L[j]=False return L def seiven(self,n,L): i=0 for j in range(len(L)): if(L[j]==True): i+=1 if(i==n): return j def matrixin2(self,m,t=int): L=[] for i in range(m): iny=self.cin(str) lsmall=[] for el in iny: lsmall.append(t(el)) L.append(lsmall) return L c=cinn() ca1=c.cin(str) ca2=c.cin(str) ca3=c.cin(str) L=[ca1,ca2,ca3] if(ca1==ca2 and ca2==ca3): print(0) elif(ca1==ca2 or ca3==ca2 or ca1==ca3): print(1) else: a1=list(ca1) a2=list(ca2) a3=list(ca3) l=[int(a1[0]),int(a2[0]),int(a3[0])] l.sort() found1=False if(l[0]==l[1]-1 and l[1]==l[2]-1): if(a1[1]==a2[1] and a1[1]==a3[1]): print(0) found1=True if(found1==False): found=False for el in L: upel=str(int(el[0])+1)+el[1] downel=str(int(el[0])-1)+el[1] downel2=str(int(el[0])-2)+el[1] upel2=str(int(el[0])+2)+el[1] if(downel in L or upel in L or upel2 in L or downel2 in L): found=True if(found): print(1) else: print(2)
PYTHON3
1191_B. Tokitsukaze and Mahjong
Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, …, 9m, 1p, 2p, …, 9p, 1s, 2s, …, 9s. In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand. Do you know the minimum number of extra suited tiles she needs to draw so that she can win? Here are some useful definitions in this game: * A mentsu, also known as meld, is formed by a koutsu or a shuntsu; * A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu; * A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu. Some examples: * [2m, 3p, 2s, 4m, 1s, 2s, 4s] β€” it contains no koutsu or shuntsu, so it includes no mentsu; * [4s, 3m, 3p, 4s, 5p, 4s, 5p] β€” it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu; * [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β€” it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu. Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite. Input The only line contains three strings β€” the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s. Output Print a single integer β€” the minimum number of extra suited tiles she needs to draw. Examples Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 1 Note In the first example, Tokitsukaze already has a shuntsu. In the second example, Tokitsukaze already has a koutsu. In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β€” 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p].
2
8
a = input().split() r = 2 for i in range(48,58): for c in "mps": r = min(r,max(0,3-a.count(chr(i)+c))) #εˆ€ζ–­θ¦ε‡‘εˆ°ε…¨ιƒ¨η›ΈεŒ r = min(r,3-(chr(i-1)+c in a)-(chr(i)+c in a)-(chr(i+1)+c in a)) #εˆ€ζ–­ι‡Œι’ε«ζœ‰ηš„εŒθŠ±ι‘Ί print(r)
PYTHON3
1191_B. Tokitsukaze and Mahjong
Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, …, 9m, 1p, 2p, …, 9p, 1s, 2s, …, 9s. In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand. Do you know the minimum number of extra suited tiles she needs to draw so that she can win? Here are some useful definitions in this game: * A mentsu, also known as meld, is formed by a koutsu or a shuntsu; * A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu; * A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu. Some examples: * [2m, 3p, 2s, 4m, 1s, 2s, 4s] β€” it contains no koutsu or shuntsu, so it includes no mentsu; * [4s, 3m, 3p, 4s, 5p, 4s, 5p] β€” it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu; * [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β€” it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu. Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite. Input The only line contains three strings β€” the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s. Output Print a single integer β€” the minimum number of extra suited tiles she needs to draw. Examples Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 1 Note In the first example, Tokitsukaze already has a shuntsu. In the second example, Tokitsukaze already has a koutsu. In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β€” 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p].
2
8
a,b,c=input().split() def check(a,b,c): if a==b==c: return True if a[1]==b[1]==c[1]: a=int(a[0]) b=int(b[0]) c=int(c[0]) x=[a,b,c] x.sort() if x[1]==x[0]+1 and x[2]==x[1]+1: return True return False s=[] for i in range(1,10): for cc in ['m','s','p']: s.append(str(i)+cc) if check(a,b,c): print(0) quit() cur=[a,b,c] for r1 in [a,b,c]: cur.remove(r1) for n1 in s: cur.append(n1) if check(*cur): print(1) quit() cur.remove(n1) cur.append(r1) print(2) # print(a,b,c) # print(s)
PYTHON3
1191_B. Tokitsukaze and Mahjong
Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, …, 9m, 1p, 2p, …, 9p, 1s, 2s, …, 9s. In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand. Do you know the minimum number of extra suited tiles she needs to draw so that she can win? Here are some useful definitions in this game: * A mentsu, also known as meld, is formed by a koutsu or a shuntsu; * A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu; * A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu. Some examples: * [2m, 3p, 2s, 4m, 1s, 2s, 4s] β€” it contains no koutsu or shuntsu, so it includes no mentsu; * [4s, 3m, 3p, 4s, 5p, 4s, 5p] β€” it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu; * [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β€” it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu. Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite. Input The only line contains three strings β€” the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s. Output Print a single integer β€” the minimum number of extra suited tiles she needs to draw. Examples Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 1 Note In the first example, Tokitsukaze already has a shuntsu. In the second example, Tokitsukaze already has a koutsu. In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β€” 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p].
2
8
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.List; import java.util.Map; import java.util.Scanner; import java.util.HashMap; import java.util.ArrayList; /** * Built using CHelper plug-in * Actual solution is at the top * * @author ky112233 */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; Scanner in = new Scanner(inputStream); PrintWriter out = new PrintWriter(outputStream); BTokitsukazeAndMahjong solver = new BTokitsukazeAndMahjong(); solver.solve(1, in, out); out.close(); } static class BTokitsukazeAndMahjong { public void solve(int testNumber, Scanner in, PrintWriter out) { // StringTokenizer st = new StringTokenizer(in.next()); String a = in.next(); String b = in.next(); String c = in.next(); int[] cnt = new int[9]; cnt[a.charAt(0) - '1']++; cnt[b.charAt(0) - '1']++; cnt[c.charAt(0) - '1']++; for (int num : cnt) { if (num == 3) { if (a.charAt(1) == b.charAt(1) && b.charAt(1) == c.charAt(1)) { out.println(0); return; } } } for (int i = 0; i < 7; i++) { if (cnt[i] == 1 && cnt[i + 1] == 1 && cnt[i + 2] == 1) { if (a.charAt(1) == b.charAt(1) && b.charAt(1) == c.charAt(1)) { out.println(0); return; } } } Map<Character, List<Integer>> map = new HashMap<>(); List<Integer> temp = new ArrayList<>(); while (temp.size() < 9) temp.add(0); if (a.charAt(1) == 's') temp.set(a.charAt(0) - '1', temp.get(a.charAt(0) - '1') + 1); if (b.charAt(1) == 's') temp.set(b.charAt(0) - '1', temp.get(b.charAt(0) - '1') + 1); if (c.charAt(1) == 's') temp.set(c.charAt(0) - '1', temp.get(c.charAt(0) - '1') + 1); map.put('s', temp); temp = new ArrayList<>(); while (temp.size() < 9) temp.add(0); if (a.charAt(1) == 'm') temp.set(a.charAt(0) - '1', temp.get(a.charAt(0) - '1') + 1); if (b.charAt(1) == 'm') temp.set(b.charAt(0) - '1', temp.get(b.charAt(0) - '1') + 1); if (c.charAt(1) == 'm') temp.set(c.charAt(0) - '1', temp.get(c.charAt(0) - '1') + 1); map.put('m', temp); temp = new ArrayList<>(); while (temp.size() < 9) temp.add(0); if (a.charAt(1) == 'p') temp.set(a.charAt(0) - '1', temp.get(a.charAt(0) - '1') + 1); if (b.charAt(1) == 'p') temp.set(b.charAt(0) - '1', temp.get(b.charAt(0) - '1') + 1); if (c.charAt(1) == 'p') temp.set(c.charAt(0) - '1', temp.get(c.charAt(0) - '1') + 1); map.put('p', temp); for (char key : map.keySet()) { temp = map.get(key); for (int i = 0; i < temp.size(); i++) { if (temp.get(i) == 2) { out.println(1); return; } } for (int i = 0; i < temp.size() - 1; i++) { if (temp.get(i) == 1 && temp.get(i + 1) == 1) { out.println(1); return; } } for (int i = 0; i < temp.size() - 2; i++) { if (temp.get(i) == 1 && temp.get(i + 2) == 1) { out.println(1); return; } } } out.println(2); } } }
JAVA
1191_B. Tokitsukaze and Mahjong
Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, …, 9m, 1p, 2p, …, 9p, 1s, 2s, …, 9s. In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand. Do you know the minimum number of extra suited tiles she needs to draw so that she can win? Here are some useful definitions in this game: * A mentsu, also known as meld, is formed by a koutsu or a shuntsu; * A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu; * A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu. Some examples: * [2m, 3p, 2s, 4m, 1s, 2s, 4s] β€” it contains no koutsu or shuntsu, so it includes no mentsu; * [4s, 3m, 3p, 4s, 5p, 4s, 5p] β€” it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu; * [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β€” it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu. Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite. Input The only line contains three strings β€” the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s. Output Print a single integer β€” the minimum number of extra suited tiles she needs to draw. Examples Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 1 Note In the first example, Tokitsukaze already has a shuntsu. In the second example, Tokitsukaze already has a koutsu. In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β€” 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p].
2
8
li = input().split() li1 = sorted(map(lambda x: x[0], li)) li2 = sorted(map(lambda x: x[1], li)) if li[0] == li[1] and li[1] == li[2] or li2[0] == li2[2] and int(max(li1)) - int(min(li1)) == 2 and li1[0] != li1[1] and li1[1] != li1[2]: print(0) elif ((li[0] == li[1]) or (li[0] == li[2]) or (li[2] == li[1])) or (( abs(int(li[2][0]) - int(li[1][0])) == 2 or abs(int(li[2][0]) - int(li[1][0])) == 1) and li[2][1] == li[1][1]) or (( abs(int(li[2][0]) - int(li[0][0])) == 2 or abs(int(li[2][0]) - int(li[0][0])) == 1) and li[2][1] == li[0][1]) or (( abs(int(li[1][0]) - int(li[0][0])) == 2 or abs(int(li[1][0]) - int(li[0][0])) == 1) and li[1][1] == li[0][1]): print(1) else: print(2)
PYTHON3
1191_B. Tokitsukaze and Mahjong
Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, …, 9m, 1p, 2p, …, 9p, 1s, 2s, …, 9s. In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand. Do you know the minimum number of extra suited tiles she needs to draw so that she can win? Here are some useful definitions in this game: * A mentsu, also known as meld, is formed by a koutsu or a shuntsu; * A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu; * A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu. Some examples: * [2m, 3p, 2s, 4m, 1s, 2s, 4s] β€” it contains no koutsu or shuntsu, so it includes no mentsu; * [4s, 3m, 3p, 4s, 5p, 4s, 5p] β€” it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu; * [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β€” it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu. Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite. Input The only line contains three strings β€” the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s. Output Print a single integer β€” the minimum number of extra suited tiles she needs to draw. Examples Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 1 Note In the first example, Tokitsukaze already has a shuntsu. In the second example, Tokitsukaze already has a koutsu. In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β€” 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p].
2
8
#include <bits/stdc++.h> using namespace std; int s[15][5]; int k[300]; int main() { int a, b, c; char x, y, z; cin >> a >> x >> b >> y >> c >> z; k['m'] = 1; k['p'] = 2; k['s'] = 3; s[a][k[x]]++, s[b][k[y]]++, s[c][k[z]]++; int ans = 3; for (int i = 1; i <= 9; i++) for (int j = 1; j <= 3; j++) ans = min(ans, 3 - s[i][j]); for (int i = 1; i <= 7; i++) for (int j = 1; j <= 3; j++) ans = min(ans, 3 - (s[i][j] >= 1) - (s[i + 1][j] >= 1) - (s[i + 2][j] >= 1)); cout << ans << endl; return 0; }
CPP
1191_B. Tokitsukaze and Mahjong
Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, …, 9m, 1p, 2p, …, 9p, 1s, 2s, …, 9s. In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand. Do you know the minimum number of extra suited tiles she needs to draw so that she can win? Here are some useful definitions in this game: * A mentsu, also known as meld, is formed by a koutsu or a shuntsu; * A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu; * A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu. Some examples: * [2m, 3p, 2s, 4m, 1s, 2s, 4s] β€” it contains no koutsu or shuntsu, so it includes no mentsu; * [4s, 3m, 3p, 4s, 5p, 4s, 5p] β€” it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu; * [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β€” it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu. Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite. Input The only line contains three strings β€” the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s. Output Print a single integer β€” the minimum number of extra suited tiles she needs to draw. Examples Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 1 Note In the first example, Tokitsukaze already has a shuntsu. In the second example, Tokitsukaze already has a koutsu. In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β€” 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p].
2
8
li= list(input().split(" ")) li.sort() x = li ans = 2; if((int(x[1][0]) - int(x[0][0])) == 1 ): if(x[0][1] == x[1][1]): ans = ans -1 if(int(x[1][0]) == (int(x[2][0])- 1)): if(x[1][1] == x[2][1]): ans = ans -1 if(int(x[2][0])- int(x[1][0]) == 2): if(x[1][1] == x[2][1]): ans = min(ans,1) if(int(x[1][0])- int(x[0][0]) == 2): if(x[0][1] == x[1][1]): ans = min(ans,1) if(int(x[2][0])- int(x[0][0]) == 2): if(x[0][1] == x[2][1]): ans = min(ans,1) if(int(x[2][0])- int(x[0][0]) == 1): if(x[0][1] == x[2][1]): ans = min(ans,1) m = ans ans = 2 if(int(x[0][0]) == int(x[1][0])): if(x[0][1] == x[1][1]): ans = ans -1 if(int(x[1][0]) == int(x[2][0])): if(x[1][1] == x[2][1]): ans = ans -1 if(int(x[0][0]) == int(x[2][0])): if(x[0][1] == x[2][1]): ans = min(ans,1) print(min(ans,m))
PYTHON3
1191_B. Tokitsukaze and Mahjong
Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, …, 9m, 1p, 2p, …, 9p, 1s, 2s, …, 9s. In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand. Do you know the minimum number of extra suited tiles she needs to draw so that she can win? Here are some useful definitions in this game: * A mentsu, also known as meld, is formed by a koutsu or a shuntsu; * A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu; * A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu. Some examples: * [2m, 3p, 2s, 4m, 1s, 2s, 4s] β€” it contains no koutsu or shuntsu, so it includes no mentsu; * [4s, 3m, 3p, 4s, 5p, 4s, 5p] β€” it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu; * [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β€” it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu. Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite. Input The only line contains three strings β€” the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s. Output Print a single integer β€” the minimum number of extra suited tiles she needs to draw. Examples Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 1 Note In the first example, Tokitsukaze already has a shuntsu. In the second example, Tokitsukaze already has a koutsu. In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β€” 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p].
2
8
c = list(input().split()) a = [] for i in range(3): a.append([]) for n in c: if n[1]=='p': a[0].append(int(n[0])) elif n[1]=='m': a[1].append(int(n[0])) else: a[2].append(int(n[0])) m = 2 for i in range(3): a[i].sort() if len(a[i])==3 and a[i][0]==a[i][1] and a[i][1]==a[i][2]: m = 0 if len(a[i])==3 and a[i][1]-a[i][0]==1 and a[i][2]-a[i][1]==1: m = 0 if len(a[i])==3 and (a[i][1]-a[i][0]<=2 or a[i][2]-a[i][1]<=2): m = min(m,1) if len(a[i])==2 and a[i][1]-a[i][0] in [0,1,2] : m = min(m,1) if c[0]==c[1] and c[1]==c[2]: m = 0 print(m)
PYTHON3
1191_B. Tokitsukaze and Mahjong
Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, …, 9m, 1p, 2p, …, 9p, 1s, 2s, …, 9s. In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand. Do you know the minimum number of extra suited tiles she needs to draw so that she can win? Here are some useful definitions in this game: * A mentsu, also known as meld, is formed by a koutsu or a shuntsu; * A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu; * A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu. Some examples: * [2m, 3p, 2s, 4m, 1s, 2s, 4s] β€” it contains no koutsu or shuntsu, so it includes no mentsu; * [4s, 3m, 3p, 4s, 5p, 4s, 5p] β€” it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu; * [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β€” it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu. Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite. Input The only line contains three strings β€” the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s. Output Print a single integer β€” the minimum number of extra suited tiles she needs to draw. Examples Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 1 Note In the first example, Tokitsukaze already has a shuntsu. In the second example, Tokitsukaze already has a koutsu. In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β€” 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p].
2
8
inp=input().split() mps=[0,0,0] k=['m','p','s'] num=[] for i in range(3): if(inp[i][1]==k[0]): mps[0]+=1 elif(inp[i][1]==k[1]): mps[1]+=1 else: mps[2]+=1 sl=max(mps) t=mps.index(sl) for i in range(3): if(inp[i][1]==k[t]): num.append(int(inp[i][0])) if(sl==1): print(2) elif(sl==2): num.sort() kq=0 if((num[1]-num[0])<=2): kq=1 else: kq=2 print(kq) else: num.sort() kq=0 if((num[1]-num[0])==0): if((num[2]-num[1])<=1): kq=num[2]-num[1] else: kq=1 elif((num[1]-num[0])==1): if((num[2]-num[1])==1): kq=0 else: kq=1 elif((num[1]-num[0])==2): kq=1 else: if((num[2]-num[1])<=2): kq=1 else: kq=2 print(kq)
PYTHON3
1191_B. Tokitsukaze and Mahjong
Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, …, 9m, 1p, 2p, …, 9p, 1s, 2s, …, 9s. In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand. Do you know the minimum number of extra suited tiles she needs to draw so that she can win? Here are some useful definitions in this game: * A mentsu, also known as meld, is formed by a koutsu or a shuntsu; * A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu; * A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu. Some examples: * [2m, 3p, 2s, 4m, 1s, 2s, 4s] β€” it contains no koutsu or shuntsu, so it includes no mentsu; * [4s, 3m, 3p, 4s, 5p, 4s, 5p] β€” it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu; * [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β€” it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu. Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite. Input The only line contains three strings β€” the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s. Output Print a single integer β€” the minimum number of extra suited tiles she needs to draw. Examples Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 1 Note In the first example, Tokitsukaze already has a shuntsu. In the second example, Tokitsukaze already has a koutsu. In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β€” 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p].
2
8
a = input().split() a.sort() if a[0] == a[1] == a[2]: print(0) elif (int(a[0][0]) + 1 == int(a[1][0]) == int(a[2][0]) - 1) and a[0][1] == a[1][1] == a[2][1]: print(0) elif (a[0] == a[1] or a[0] == a[2] or a[1]==a[2]): print(1) elif (int(a[0][0]) + 1 == int(a[1][0]) and a[0][1] == a[1][1]) or (int(a[0][0]) + 1 == int(a[2][0]) and a[0][1] == a[2][1]) or (int(a[1][0]) + 1 == int(a[2][0]) and a[1][1] == a[2][1]): print(1) elif (int(a[0][0]) + 2 == int(a[1][0]) and a[0][1] == a[1][1]) or (int(a[0][0]) + 2 == int(a[2][0]) and a[0][1] == a[2][1]) or (int(a[1][0]) + 2 == int(a[2][0]) and a[1][1] == a[2][1]): print(1) else: print(2)
PYTHON3
1191_B. Tokitsukaze and Mahjong
Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, …, 9m, 1p, 2p, …, 9p, 1s, 2s, …, 9s. In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand. Do you know the minimum number of extra suited tiles she needs to draw so that she can win? Here are some useful definitions in this game: * A mentsu, also known as meld, is formed by a koutsu or a shuntsu; * A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu; * A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu. Some examples: * [2m, 3p, 2s, 4m, 1s, 2s, 4s] β€” it contains no koutsu or shuntsu, so it includes no mentsu; * [4s, 3m, 3p, 4s, 5p, 4s, 5p] β€” it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu; * [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β€” it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu. Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite. Input The only line contains three strings β€” the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s. Output Print a single integer β€” the minimum number of extra suited tiles she needs to draw. Examples Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 1 Note In the first example, Tokitsukaze already has a shuntsu. In the second example, Tokitsukaze already has a koutsu. In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β€” 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p].
2
8
l=input().split() l.sort() #print(l) if l[0]==l[2]: print(0) elif ((int(l[0][0])+2)==(int(l[1][0])+1)==int(l[2][0])) and (l[0][1]==l[1][1]==l[2][1]): print(0) elif l[0][1]==l[1][1] and int(l[1][0])-int(l[0][0])<=2: print(1) elif l[1][1]==l[2][1] and int(l[2][0])-int(l[1][0])<=2: print(1) elif l[0][1]==l[2][1] and int(l[2][0])-int(l[0][0])<=2: print(1) else: print(2)
PYTHON3
1191_B. Tokitsukaze and Mahjong
Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, …, 9m, 1p, 2p, …, 9p, 1s, 2s, …, 9s. In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand. Do you know the minimum number of extra suited tiles she needs to draw so that she can win? Here are some useful definitions in this game: * A mentsu, also known as meld, is formed by a koutsu or a shuntsu; * A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu; * A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu. Some examples: * [2m, 3p, 2s, 4m, 1s, 2s, 4s] β€” it contains no koutsu or shuntsu, so it includes no mentsu; * [4s, 3m, 3p, 4s, 5p, 4s, 5p] β€” it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu; * [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β€” it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu. Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite. Input The only line contains three strings β€” the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s. Output Print a single integer β€” the minimum number of extra suited tiles she needs to draw. Examples Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 1 Note In the first example, Tokitsukaze already has a shuntsu. In the second example, Tokitsukaze already has a koutsu. In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β€” 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p].
2
8
#include <bits/stdc++.h> using namespace std; int main() { map<char, int> m; m['s'] = 0; m['m'] = 1; m['p'] = 2; string a, b, c; cin >> a >> b >> c; if (a == b && b == c) { cout << 0; return 0; } if (a == b || b == c || c == a) { cout << 1; return 0; } bool p[3][10]; for (int i = 0; i < 10; i++) { p[1][i] = 0; p[2][i] = 0; p[0][i] = 0; } p[m[a[1]]][(int(a[0]) - '0')] = true; p[m[b[1]]][(int(b[0]) - '0')] = true; p[m[c[1]]][(int(c[0]) - '0')] = true; int ans = 2; int temp = 0; for (int j = 0; j <= 2; j++) { for (int i = 1; i <= 7; i++) { temp = 0; if (p[j][i]) temp++; if (p[j][i + 1]) temp++; if (p[j][i + 2]) temp++; if (temp == 3) { cout << "0"; return 0; } if (temp == 2) ans = min(ans, 1); } } cout << ans; }
CPP
1191_B. Tokitsukaze and Mahjong
Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, …, 9m, 1p, 2p, …, 9p, 1s, 2s, …, 9s. In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand. Do you know the minimum number of extra suited tiles she needs to draw so that she can win? Here are some useful definitions in this game: * A mentsu, also known as meld, is formed by a koutsu or a shuntsu; * A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu; * A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu. Some examples: * [2m, 3p, 2s, 4m, 1s, 2s, 4s] β€” it contains no koutsu or shuntsu, so it includes no mentsu; * [4s, 3m, 3p, 4s, 5p, 4s, 5p] β€” it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu; * [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β€” it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu. Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite. Input The only line contains three strings β€” the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s. Output Print a single integer β€” the minimum number of extra suited tiles she needs to draw. Examples Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 1 Note In the first example, Tokitsukaze already has a shuntsu. In the second example, Tokitsukaze already has a koutsu. In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β€” 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p].
2
8
#include <bits/stdc++.h> using namespace std; using namespace std; string a[4]; int main() { cin >> a[0] >> a[1] >> a[2]; sort(a, a + 3); if (a[0][1] == a[1][1] && a[1][1] == a[2][1]) { if (a[0][0] == a[1][0] && a[1][0] == a[2][0]) { printf("0\n"); return 0; } if (a[0][0] + 1 == a[1][0] && a[1][0] + 1 == a[2][0]) { printf("0\n"); return 0; } } if (a[0] == a[1] || a[0] == a[2] || a[1] == a[2]) { printf("1\n"); return 0; } if ((a[0][0] + 1 == a[1][0] && a[0][1] == a[1][1]) || (a[0][0] + 1 == a[2][0] && a[0][1] == a[2][1]) || (a[1][0] + 1 == a[2][0] && a[1][1] == a[2][1])) { printf("1\n"); return 0; } if ((a[0][0] + 2 == a[1][0] && a[0][1] == a[1][1]) || (a[0][0] + 2 == a[2][0] && a[0][1] == a[2][1]) || (a[1][0] + 2 == a[2][0] && a[1][1] == a[2][1])) { printf("1\n"); return 0; } printf("2\n"); return 0; }
CPP
1191_B. Tokitsukaze and Mahjong
Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, …, 9m, 1p, 2p, …, 9p, 1s, 2s, …, 9s. In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand. Do you know the minimum number of extra suited tiles she needs to draw so that she can win? Here are some useful definitions in this game: * A mentsu, also known as meld, is formed by a koutsu or a shuntsu; * A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu; * A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu. Some examples: * [2m, 3p, 2s, 4m, 1s, 2s, 4s] β€” it contains no koutsu or shuntsu, so it includes no mentsu; * [4s, 3m, 3p, 4s, 5p, 4s, 5p] β€” it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu; * [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β€” it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu. Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite. Input The only line contains three strings β€” the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s. Output Print a single integer β€” the minimum number of extra suited tiles she needs to draw. Examples Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 1 Note In the first example, Tokitsukaze already has a shuntsu. In the second example, Tokitsukaze already has a koutsu. In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β€” 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p].
2
8
a = sorted(input().split()) if a[0] == a[1] == a[2]: print(0) elif (a[0][1] == a[1][1] == a[2][1]) and (int(a[0][0]) + 1 == int(a[1][0]) and int(a[1][0]) + 1 == int(a[2][0])): print(0) elif a[0] == a[1] or a[1] == a[2]: print(1) else: s = set(a) r = None for e in s: num, word = int(e[0]), e[1] if str(str(num - 2) + word) in s or str(str(num - 1) + word) in s or str(str(num + 1) + word) in s or str( str(num + 2) + word) in s: r = 1 break if r: print(r) else: print(2)
PYTHON3
1191_B. Tokitsukaze and Mahjong
Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, …, 9m, 1p, 2p, …, 9p, 1s, 2s, …, 9s. In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand. Do you know the minimum number of extra suited tiles she needs to draw so that she can win? Here are some useful definitions in this game: * A mentsu, also known as meld, is formed by a koutsu or a shuntsu; * A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu; * A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu. Some examples: * [2m, 3p, 2s, 4m, 1s, 2s, 4s] β€” it contains no koutsu or shuntsu, so it includes no mentsu; * [4s, 3m, 3p, 4s, 5p, 4s, 5p] β€” it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu; * [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β€” it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu. Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite. Input The only line contains three strings β€” the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s. Output Print a single integer β€” the minimum number of extra suited tiles she needs to draw. Examples Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 1 Note In the first example, Tokitsukaze already has a shuntsu. In the second example, Tokitsukaze already has a koutsu. In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β€” 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p].
2
8
Input=lambda:map(str,input().split()) s = list(Input()) s.sort() num = list() for i in range(3): num.append(int(s[i][0])) #0 if (s[0] == s[1] == s[2]): print(0) exit() if (s[0][1] == s[1][1] == s[2][1]) and (num[0]+2 == num[1]+1 == num[2]): print(0) exit() #1 if (s[0] == s[1]) or (s[1] == s[2]): print(1) exit() h = str(s.copy()) if h.count('s') in [2,3]: ind = -1 if s[0][1] != 's': ind = 0 elif s[1][1] != 's': ind = 1 elif s[2][1] != 's': ind = 2 else: pass if ind == -1: # 3 ta adad x1 = num[0] x2 = num[1] x3 = num[2] if x2 - x1 <= 2: print(1) exit() if x3 - x2 <= 2: print(1) exit() else: if ind == 0: x1 = num[1] x2 = num[2] elif ind == 1: x1 = num[0] x2 = num[2] else: x1 = num[0] x2 = num[1] if x2 - x1 <= 2: print(1) exit() elif h.count('m') in [2,3]: ind = -1 if s[0][1] != 'm': ind = 0 elif s[1][1] != 'm': ind = 1 elif s[2][1] != 'm': ind = 2 else: pass if ind == -1: # 3 ta adad x1 = num[0] x2 = num[1] x3 = num[2] if x2 - x1 <= 2: print(1) exit() if x3 - x2 <= 2: print(1) exit() else: if ind == 0: x1 = num[1] x2 = num[2] elif ind == 1: x1 = num[0] x2 = num[2] else: x1 = num[0] x2 = num[1] if x2 - x1 <= 2: print(1) exit() elif h.count('p') in [2,3]: ind = -1 if s[0][1] != 'p': ind = 0 elif s[1][1] != 'p': ind = 1 elif s[2][1] != 'p': ind = 2 else: pass if ind == -1: # 3 ta adad x1 = num[0] x2 = num[1] x3 = num[2] if x2 - x1 <= 2: print(1) exit() if x3 - x2 <= 2: print(1) exit() else: if ind == 0: x1 = num[1] x2 = num[2] elif ind == 1: x1 = num[0] x2 = num[2] else: x1 = num[0] x2 = num[1] if x2 - x1 <= 2: print(1) exit() else: pass print(2) ''' openvpn vpnbook sEN6DC9 '''
PYTHON3
1191_B. Tokitsukaze and Mahjong
Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, …, 9m, 1p, 2p, …, 9p, 1s, 2s, …, 9s. In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand. Do you know the minimum number of extra suited tiles she needs to draw so that she can win? Here are some useful definitions in this game: * A mentsu, also known as meld, is formed by a koutsu or a shuntsu; * A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu; * A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu. Some examples: * [2m, 3p, 2s, 4m, 1s, 2s, 4s] β€” it contains no koutsu or shuntsu, so it includes no mentsu; * [4s, 3m, 3p, 4s, 5p, 4s, 5p] β€” it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu; * [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β€” it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu. Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite. Input The only line contains three strings β€” the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s. Output Print a single integer β€” the minimum number of extra suited tiles she needs to draw. Examples Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 1 Note In the first example, Tokitsukaze already has a shuntsu. In the second example, Tokitsukaze already has a koutsu. In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β€” 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p].
2
8
# 1191B.py l = list(map(str,input().split())) p = [] s = [] m = [] for i in range(3): if l[i][1] == 's': s.append(int(l[i][0])) if l[i][1] == 'p': p.append(int(l[i][0])) if l[i][1] == 'm': m.append(int(l[i][0])) ans = 2 m.sort() p.sort() s.sort() for i in range(3): m.append(0) s.append(0) p.append(0) if len(p)>=3: if p[0]!=0 and p[1]!=0: if p[0] == p[1] and p[1]==p[2]: ans = 0 elif p[0] == p[1] or p[1] == p[2]: # print('a') ans = min(ans,1) elif p[1]-p[0]==1 and p[2]-p[1]==1: ans = 0 elif p[1]-p[0]==1 or p[2]-p[1]==1: # print('b') ans = min(ans,1) elif p[1]-p[0] == 2 or (p[2]!=0 and p[2]-p[1]==2): ans = min(ans,1) if len(s)>=3: if s[0]!=0 and s[1]!=0: if s[0] == s[1] and s[1]==s[2]: ans = 0 elif s[0] == s[1] or s[1] == s[2]: # print('x') ans = min(ans,1) elif s[1]-s[0]==1 and s[2]-s[1]==1: ans = 0 elif s[1]-s[0]==1 or s[2]-s[1]==1: # print('y') ans = min(ans,1) elif s[1]-s[0] == 2 or (s[2]!=0 and s[2]-s[1]==2): ans = min(ans,1) if len(m)>=3: if m[0]!=0 and m[1]!=0: if m[0] == m[1] and m[1]==m[2]: ans = 0 elif m[0] == m[1] or m[1] == m[2]: ans = min(ans,1) elif m[1]-m[0]==1 and m[2]-m[1]==1: ans = 0 elif m[1]-m[0]==1 or m[2]-m[1]==1: ans = min(ans,1) elif m[1]-m[0] == 2 or (m[2]!=0 and m[2]-m[1]==2): ans = min(ans,1) print(ans) # p = [0 for x in range(10)] # m = [0 for x in range(10)] # s = [0 for x in range(10)] # # print(p,m,s) # for i in range(3): # # print(l[i][0]) # if l[i][1] == 's': # s[int(l[i][0])]+=1 # elif l[i][1] == 'm': # m[int(l[i][0])]+=1 # else: # p[int(l[i][0])]+=1 # ans = 10000 # # print(p) # # print(m) # # print(s) # for i in range(3): # if s[i] == 3 or p[i] == 3 or m[i] == 3: # ans = 0 # for i in range(3): # if s[i] == 2 or p[i] == 2 or m[i] == 2: # ans = 1 # ok = True # for i in range(8): # if s[i]<0 or s[i+1]<0 or s[i+2]<0: # ok = False # if not ok: # for i in range(8): # if p[i]<0 or p[i+1]<0 or p[i+2]<0: # ok = False # if not ok: # for i in range(8): # if m[i]<0 or m[i+1]<0 or m[i+2]<0: # ok = False # if ok: # ans = 0 # print(ans)
PYTHON3
1191_B. Tokitsukaze and Mahjong
Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, …, 9m, 1p, 2p, …, 9p, 1s, 2s, …, 9s. In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand. Do you know the minimum number of extra suited tiles she needs to draw so that she can win? Here are some useful definitions in this game: * A mentsu, also known as meld, is formed by a koutsu or a shuntsu; * A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu; * A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu. Some examples: * [2m, 3p, 2s, 4m, 1s, 2s, 4s] β€” it contains no koutsu or shuntsu, so it includes no mentsu; * [4s, 3m, 3p, 4s, 5p, 4s, 5p] β€” it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu; * [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β€” it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu. Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite. Input The only line contains three strings β€” the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s. Output Print a single integer β€” the minimum number of extra suited tiles she needs to draw. Examples Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 1 Note In the first example, Tokitsukaze already has a shuntsu. In the second example, Tokitsukaze already has a koutsu. In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β€” 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p].
2
8
import java.util.*; import java.lang.*; import java.io.*; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Scanner; import java.util.StringTokenizer; import javax.lang.model.util.ElementScanner6; /* Name of the class has to be "Main" only if the class is public. */ public class Trie { 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; } } public static void main(String args[]) { FastReader sc=new FastReader(); int s[]=new int[10]; int m[]=new int[10]; int p[]=new int[10]; for( int i=0;i<3;i++) { String str=sc.next(); char c[]=str.toCharArray(); if(c[1]=='s') { s[c[0]-'0']++; } if(c[1]=='p') p[c[0]-'0']++; if(c[1]=='m') m[c[0]-'0']++; } int min=1000; for( int i=0;i<10;i++) { min=Math.min(min,3-p[i]); min=Math.min(min,3-m[i]); min=Math.min(min,3-s[i]); } int count1=0;int count2=0;int count3=0; for( int j=1;j<10;j++) { count1=0; count2=0;count3=0; for( int i=j;i<=j+2&&j+2<10;i++) { if(p[i]>0) count1++; if(m[i]>0) count2++; if(s[i]>0) count3++; } min=Math.min(min,3-count1); min=Math.min(min,3-count2); min=Math.min(min,3-count3); } for( int j=1;j<10;j++) { count1=0; count2=0;count3=0; for( int i=j;i<=j+1&&j+1<10;i++) { if(p[i]>0) count1++; if(m[i]>0) count2++; if(s[i]>0) count3++; } min=Math.min(min,3-count1); min=Math.min(min,3-count2); min=Math.min(min,3-count3); } for( int j=1;j<10;j++) { count1=0; count2=0;count3=0; for( int i=j;i<=j&&j<10;i++) { if(p[i]>0) count1++; if(m[i]>0) count2++; if(s[i]>0) count3++; } min=Math.min(min,3-count1); min=Math.min(min,3-count2); min=Math.min(min,3-count3); } System.out.println(min); } }
JAVA
1191_B. Tokitsukaze and Mahjong
Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, …, 9m, 1p, 2p, …, 9p, 1s, 2s, …, 9s. In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand. Do you know the minimum number of extra suited tiles she needs to draw so that she can win? Here are some useful definitions in this game: * A mentsu, also known as meld, is formed by a koutsu or a shuntsu; * A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu; * A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu. Some examples: * [2m, 3p, 2s, 4m, 1s, 2s, 4s] β€” it contains no koutsu or shuntsu, so it includes no mentsu; * [4s, 3m, 3p, 4s, 5p, 4s, 5p] β€” it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu; * [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β€” it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu. Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite. Input The only line contains three strings β€” the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s. Output Print a single integer β€” the minimum number of extra suited tiles she needs to draw. Examples Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 1 Note In the first example, Tokitsukaze already has a shuntsu. In the second example, Tokitsukaze already has a koutsu. In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β€” 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p].
2
8
a,b,c = input().split(); import sys; x = int(a[0]); y = int(b[0]); z = int(c[0]); if(a==b==c): print(0); sys.exit(); else: arr = [x,y,z]; arr.sort(); if(a[1]==b[1]==c[1]): if((arr[1]-arr[0])==1 and (arr[2]-arr[1])==1): print(0); sys.exit(); if((arr[1]-arr[0])==1 or (arr[1]-arr[0])==2 or (arr[1]-arr[0])==0): print(1); sys.exit(); if((arr[2]-arr[1])==1 or (arr[2]-arr[1])==2 or (arr[2]-arr[1])==0): print(1); sys.exit(); else: print(2); sys.exit(); if(a[1]==b[1] or b[1]==c[1] or c[1]==a[1]): if(a[1]==b[1]): if(abs(x-y)==1 or abs(x-y)==2 or abs(x-y)==0): print(1); sys.exit(); else: print(2); sys.exit(); if(b[1]==c[1]): if(abs(y-z)==1 or abs(y-z)==2 or abs(y-z)==0): print(1); sys.exit(); else: print(2); sys.exit(); if(a[1]==c[1]): if(abs(x-z)==1 or abs(x-z)==2 or abs(x-z)==0): print(1); sys.exit(); else: print(2); sys.exit(); else: print(2); sys.exit(); else: print(2); sys.exit();
PYTHON3
1191_B. Tokitsukaze and Mahjong
Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, …, 9m, 1p, 2p, …, 9p, 1s, 2s, …, 9s. In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand. Do you know the minimum number of extra suited tiles she needs to draw so that she can win? Here are some useful definitions in this game: * A mentsu, also known as meld, is formed by a koutsu or a shuntsu; * A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu; * A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu. Some examples: * [2m, 3p, 2s, 4m, 1s, 2s, 4s] β€” it contains no koutsu or shuntsu, so it includes no mentsu; * [4s, 3m, 3p, 4s, 5p, 4s, 5p] β€” it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu; * [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β€” it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu. Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite. Input The only line contains three strings β€” the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s. Output Print a single integer β€” the minimum number of extra suited tiles she needs to draw. Examples Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 1 Note In the first example, Tokitsukaze already has a shuntsu. In the second example, Tokitsukaze already has a koutsu. In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β€” 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p].
2
8
# Main data = [x for x in raw_input().split(" ")] sol = None if len(set(data)) == 1: sol = 0 elif data[0][1] == data[1][1] and data[0][1] == data[2][1]: nums = [int(data[0][0]), int(data[1][0]), int(data[2][0])] nums.sort() if nums[0] == nums[1] - 1: if nums[1] == nums[2] - 1: sol = 0 else: sol = 1 else: if nums[1] == nums[2] - 1: sol = 1 elif len(set(data)) == 2: if sol != 0: sol = 1 # I think this entire block is redundants since I handled it in the # check above... That said, I don't think it'll hurt - worst case if # I'm right, it'll just fire down this branch in the case of a 2 if sol == None: if data[0][1] == data[1][1]: if abs(int(data[0][0]) - int(data[1][0])) <= 2: # they're off by one and we only need one more piece sol = 1 if data[0][1] == data[2][1]: if abs(int(data[0][0]) - int(data[2][0])) <= 2: # they're off by one and we only need one more piece sol = 1 if data[1][1] == data[2][1]: if abs(int(data[1][0]) - int(data[2][0])) <= 2: # they're off by one and we only need one more piece sol = 1 if sol == None: sol = 2 print sol
PYTHON
1191_B. Tokitsukaze and Mahjong
Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, …, 9m, 1p, 2p, …, 9p, 1s, 2s, …, 9s. In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand. Do you know the minimum number of extra suited tiles she needs to draw so that she can win? Here are some useful definitions in this game: * A mentsu, also known as meld, is formed by a koutsu or a shuntsu; * A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu; * A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu. Some examples: * [2m, 3p, 2s, 4m, 1s, 2s, 4s] β€” it contains no koutsu or shuntsu, so it includes no mentsu; * [4s, 3m, 3p, 4s, 5p, 4s, 5p] β€” it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu; * [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β€” it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu. Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite. Input The only line contains three strings β€” the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s. Output Print a single integer β€” the minimum number of extra suited tiles she needs to draw. Examples Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 1 Note In the first example, Tokitsukaze already has a shuntsu. In the second example, Tokitsukaze already has a koutsu. In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β€” 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p].
2
8
import java.util.*; import java.io.*; public class B { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String a = sc.next(); String b = sc.next(); String c = sc.next(); if(koutsu(a, b, c)) { System.out.println(0); } else if(shuntsu(a, b, c)) { System.out.println(0); } else if(almostK(a, b, c)) { System.out.println(1); } else if(almostS(a, b, c)) { System.out.println(1); } else { System.out.println(2); } } public static boolean koutsu(String a, String b, String c) { return a.equals(b) && b.equals(c); } public static boolean shuntsu(String a, String b, String c) { if(a.charAt(1) != b.charAt(1)) { return false; } if(c.charAt(1) != b.charAt(1)) { return false; } int[] nums = new int[3]; nums[0] = a.charAt(0) - '0'; nums[1] = b.charAt(0) - '0'; nums[2] = c.charAt(0) - '0'; Arrays.sort(nums); return nums[0] + 1 == nums[1] && nums[1] + 1 == nums[2]; } public static boolean almostK(String a, String b, String c) { if(a.equals(b) && !b.equals(c)) { return true; } if(a.equals(c) && !b.equals(c)) { return true; } if(b.equals(c) && !b.equals(a)) { return true; } return false; } public static boolean almostS(String a, String b, String c) { if(a.charAt(1) != b.charAt(1) && a.charAt(1) != c.charAt(1) && b.charAt(1) != c.charAt(1)) { return false; } int[] nums = new int[3]; nums[0] = a.charAt(0) - '0'; nums[1] = b.charAt(0) - '0'; nums[2] = c.charAt(0) - '0'; boolean ab = false; boolean bb = false; boolean cb = false; if(a.charAt(1) == b.charAt(1)) { if(Math.abs(nums[0] - nums[1]) < 3) { ab = true; } else { ab = false; } } if(a.charAt(1) == c.charAt(1)) { if(Math.abs(nums[0] - nums[2]) < 3) { bb = true; } else { bb = false; } } if(c.charAt(1) == b.charAt(1)) { if(Math.abs(nums[1] - nums[2]) < 3) { cb = true; } else { cb = false; } } return ab || bb || cb; } }
JAVA
1191_B. Tokitsukaze and Mahjong
Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, …, 9m, 1p, 2p, …, 9p, 1s, 2s, …, 9s. In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand. Do you know the minimum number of extra suited tiles she needs to draw so that she can win? Here are some useful definitions in this game: * A mentsu, also known as meld, is formed by a koutsu or a shuntsu; * A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu; * A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu. Some examples: * [2m, 3p, 2s, 4m, 1s, 2s, 4s] β€” it contains no koutsu or shuntsu, so it includes no mentsu; * [4s, 3m, 3p, 4s, 5p, 4s, 5p] β€” it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu; * [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β€” it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu. Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite. Input The only line contains three strings β€” the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s. Output Print a single integer β€” the minimum number of extra suited tiles she needs to draw. Examples Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 1 Note In the first example, Tokitsukaze already has a shuntsu. In the second example, Tokitsukaze already has a koutsu. In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β€” 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p].
2
8
s = input() ans = 2 s1 = s[0:2] s2 = s[3:5] s3 = s[6:8] def func(inp): ans = 2 num = int(inp[0]) c = inp[1] ans = min( ans, 2 - int(s.find(str(num + 1)+c) != -1) - int(s.find(str(num + 2)+c) != -1)) ans = min( ans, 2 - int(s.find(str(num + 1)+c) != -1) - int(s.find(str(num - 1)+c) != -1)) ans = min( ans, 2 - int(s.find(str(num - 1)+c) != -1) - int(s.find(str(num - 2)+c) != -1)) ans = min( ans, 3 - s.count(inp)) return ans ans = min(ans,func(s1)) ans = min(ans,func(s2)) ans = min(ans,func(s3)) print(ans)
PYTHON3
1191_B. Tokitsukaze and Mahjong
Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, …, 9m, 1p, 2p, …, 9p, 1s, 2s, …, 9s. In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand. Do you know the minimum number of extra suited tiles she needs to draw so that she can win? Here are some useful definitions in this game: * A mentsu, also known as meld, is formed by a koutsu or a shuntsu; * A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu; * A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu. Some examples: * [2m, 3p, 2s, 4m, 1s, 2s, 4s] β€” it contains no koutsu or shuntsu, so it includes no mentsu; * [4s, 3m, 3p, 4s, 5p, 4s, 5p] β€” it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu; * [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β€” it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu. Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite. Input The only line contains three strings β€” the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s. Output Print a single integer β€” the minimum number of extra suited tiles she needs to draw. Examples Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 1 Note In the first example, Tokitsukaze already has a shuntsu. In the second example, Tokitsukaze already has a koutsu. In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β€” 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p].
2
8
cards = input().split() c1 = cards[0] c2 = cards[1] c3 = cards[2] if c1 == c2 == c3: ans = 0 elif (c1 != c3 and c1 == c2) or (c2 != c1 and c2 == c3) or (c2 != c1 and c1 == c3): ans = 1 else: S = [int(c1[0]), int(c2[0]), int(c3[0])] if c1[1] == c2[1] == c3[1] and max(S) - min(S) == 2: ans = 0 elif c1[1] == c2[1] == c3[1]: if int(c2[0]) - int(c1[0]) == 1 or int(c1[0]) - int(c2[0]) == 1: ans = 1 elif int(c2[0]) - int(c1[0]) == 2 or int(c1[0]) - int(c2[0]) == 2: ans = 1 elif int(c3[0]) - int(c1[0]) == 1 or int(c1[0]) - int(c3[0]) == 1: ans = 1 elif int(c3[0]) - int(c1[0]) == 2 or int(c1[0]) - int(c3[0]) == 2: ans = 1 elif int(c2[0]) - int(c3[0]) == 1 or int(c3[0]) - int(c2[0]) == 1: ans = 1 elif int(c2[0]) - int(c3[0]) == 2 or int(c3[0]) - int(c2[0]) == 2: ans = 1 else: ans = 2 elif c1[1] == c2[1] and int(c2[0]) - int(c1[0]) == 1 or c1[1] ==c2[1] and int(c1[0]) - int(c2[0]) == 1: ans = 1 elif c1[1] == c2[1] and int(c2[0]) - int(c1[0]) == 2 or c1[1] ==c2[1] and int(c1[0]) - int(c2[0]) == 2: ans = 1 elif c1[1] == c3[1] and int(c3[0]) - int(c1[0]) == 1 or c1[1] ==c3[1] and int(c1[0]) - int(c3[0]) == 1: ans = 1 elif c1[1] == c3[1] and int(c3[0]) - int(c1[0]) == 2 or c1[1] ==c3[1] and int(c1[0]) - int(c3[0]) == 2: ans = 1 elif int(c2[0]) - int(c3[0]) == 1 and c2[1] ==c3[1] or int(c3[0]) - int(c2[0]) == 1 and c2[1] == c3[1]: ans = 1 elif int(c2[0]) - int(c3[0]) == 2 and c2[1] ==c3[1] or int(c3[0]) - int(c2[0]) == 2 and c2[1] == c3[1]: ans = 1 else: ans = 2 print(ans)
PYTHON3
1191_B. Tokitsukaze and Mahjong
Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, …, 9m, 1p, 2p, …, 9p, 1s, 2s, …, 9s. In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand. Do you know the minimum number of extra suited tiles she needs to draw so that she can win? Here are some useful definitions in this game: * A mentsu, also known as meld, is formed by a koutsu or a shuntsu; * A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu; * A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu. Some examples: * [2m, 3p, 2s, 4m, 1s, 2s, 4s] β€” it contains no koutsu or shuntsu, so it includes no mentsu; * [4s, 3m, 3p, 4s, 5p, 4s, 5p] β€” it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu; * [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β€” it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu. Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite. Input The only line contains three strings β€” the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s. Output Print a single integer β€” the minimum number of extra suited tiles she needs to draw. Examples Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 1 Note In the first example, Tokitsukaze already has a shuntsu. In the second example, Tokitsukaze already has a koutsu. In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β€” 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p].
2
8
arr=[(i[1],i[0]) for i in map(str,raw_input().split())] arr.sort() s,p,m=[],[],[] de={} ms=0 ps=1 cv=int(arr[0][1]) ca=arr[0][0] for i in arr: de[i]=de.get(i,[])+[i] #print 'i-',i #print 'cv-',cv,'ca-',ca if cv+1==int(i[1]) and ca==i[0]: ps+=1 else: if cv+2==int(i[1]) and ca==i[0]: ps=2 #print 'Yes' ms=max(ps,ms) ms=max(ps,ms) ps=1 cv=int(i[1]) ca=i[0] ms=max(ps,ms) me=0 for i in de.values(): me=max(me,len(i)) print max(0,3-max(me,ms))
PYTHON
1191_B. Tokitsukaze and Mahjong
Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, …, 9m, 1p, 2p, …, 9p, 1s, 2s, …, 9s. In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand. Do you know the minimum number of extra suited tiles she needs to draw so that she can win? Here are some useful definitions in this game: * A mentsu, also known as meld, is formed by a koutsu or a shuntsu; * A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu; * A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu. Some examples: * [2m, 3p, 2s, 4m, 1s, 2s, 4s] β€” it contains no koutsu or shuntsu, so it includes no mentsu; * [4s, 3m, 3p, 4s, 5p, 4s, 5p] β€” it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu; * [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β€” it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu. Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite. Input The only line contains three strings β€” the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s. Output Print a single integer β€” the minimum number of extra suited tiles she needs to draw. Examples Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 1 Note In the first example, Tokitsukaze already has a shuntsu. In the second example, Tokitsukaze already has a koutsu. In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β€” 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p].
2
8
from sys import stdin from math import fabs x = [ i for i in stdin.readline().split()] x.sort() if x[0] == x[2]: print(0) elif ((int(x[0][0])+ 2) == (int(x[1][0]) + 1) == int(x[2][0])) and (x[0][1] == x[1][1] == x[2][1]) : print(0) elif x[0][1] == x[1][1] and int(x[1][0]) - int(x[0][0]) <= 2: print(1) elif x[0][1] == x[2][1] and int(x[2][0]) - int( x[0][0]) <= 2: print(1) elif x[2][1] == x[1][1] and int(x[2][0]) - int(x[1][0]) <= 2: print(1) else: print(2)
PYTHON3
1191_B. Tokitsukaze and Mahjong
Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, …, 9m, 1p, 2p, …, 9p, 1s, 2s, …, 9s. In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand. Do you know the minimum number of extra suited tiles she needs to draw so that she can win? Here are some useful definitions in this game: * A mentsu, also known as meld, is formed by a koutsu or a shuntsu; * A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu; * A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu. Some examples: * [2m, 3p, 2s, 4m, 1s, 2s, 4s] β€” it contains no koutsu or shuntsu, so it includes no mentsu; * [4s, 3m, 3p, 4s, 5p, 4s, 5p] β€” it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu; * [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β€” it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu. Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite. Input The only line contains three strings β€” the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s. Output Print a single integer β€” the minimum number of extra suited tiles she needs to draw. Examples Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 1 Note In the first example, Tokitsukaze already has a shuntsu. In the second example, Tokitsukaze already has a koutsu. In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β€” 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p].
2
8
''' بِسْمِ Ψ§Ω„Ω„ΩŽΩ‘Ω‡Ω Ψ§Ω„Ψ±ΩŽΩ‘Ψ­Ω’Ω…ΩŽΩ°Ω†Ω Ψ§Ω„Ψ±ΩŽΩ‘Ψ­ΩΩŠΩ…Ω ''' #codeforces1191B_live gi = lambda : list(map(int,input().strip().split())) l = input().split() l.sort() if len(set(l)) == 1: print(0) exit() c = l[0][1] b = int(l[0][0]) flag = True for e in l: if e[1] != c: flag = False break for e in l: if int(e[0]) != b: flag = False break; b += 1 if flag: print(0) exit() c = l[0][1] b = int(l[0][0]) bb = int(l[1][0]) bbb = int(l[2][0]) cc = l[1][1] ccc = l[2][1] if c == cc: if bb - b <= 2: print(1) exit() if c == ccc: if bbb - b <= 2: print(1) exit() if cc == ccc: if bbb - bb <= 2: print(1) exit() print(2)
PYTHON3
1191_B. Tokitsukaze and Mahjong
Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, …, 9m, 1p, 2p, …, 9p, 1s, 2s, …, 9s. In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand. Do you know the minimum number of extra suited tiles she needs to draw so that she can win? Here are some useful definitions in this game: * A mentsu, also known as meld, is formed by a koutsu or a shuntsu; * A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu; * A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu. Some examples: * [2m, 3p, 2s, 4m, 1s, 2s, 4s] β€” it contains no koutsu or shuntsu, so it includes no mentsu; * [4s, 3m, 3p, 4s, 5p, 4s, 5p] β€” it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu; * [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β€” it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu. Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite. Input The only line contains three strings β€” the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s. Output Print a single integer β€” the minimum number of extra suited tiles she needs to draw. Examples Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 1 Note In the first example, Tokitsukaze already has a shuntsu. In the second example, Tokitsukaze already has a koutsu. In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β€” 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p].
2
8
inn = raw_input() s,m,p = [],[],[] for x in inn.split(): if x[1] == 's': s.append(int(x[0])) elif x[1] == 'm': m.append(int(x[0])) elif x[1] == 'p': p.append(int(x[0])) s.sort() m.sort() p.sort() if len(s) == 3 or len(m) == 3 or len(p) == 3: def check1(q): if (q[0] == q[1] and q[1] == q[2]) or (q[0]+1 == q[1] and q[1]+1 == q[2]): print(0) elif q[0]+1 == q[1] or q[1]+1 == q[2] or q[0]==q[1] or q[1]==q[2] or q[0]+2==q[1] or q[1]+2==q[2] or q[0]+2 == q[2]: print(1) else: print(2) if len(s)==3: check1(s) elif len(m) == 3: check1(m) else: check1(p) elif len(s) == 1 and len(m) == 1 and len(p) == 1: print(2) else:# 1,2,0 def check2(q): if q[0]+1 == q[1] or q[0] == q[1] or q[0]+2==q[1]: print(1) else: print(2) if len(m) == 2: check2(m) elif len(p) == 2: #len(p) == 2 check2(p) else: check2(s)
PYTHON
1191_B. Tokitsukaze and Mahjong
Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, …, 9m, 1p, 2p, …, 9p, 1s, 2s, …, 9s. In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand. Do you know the minimum number of extra suited tiles she needs to draw so that she can win? Here are some useful definitions in this game: * A mentsu, also known as meld, is formed by a koutsu or a shuntsu; * A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu; * A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu. Some examples: * [2m, 3p, 2s, 4m, 1s, 2s, 4s] β€” it contains no koutsu or shuntsu, so it includes no mentsu; * [4s, 3m, 3p, 4s, 5p, 4s, 5p] β€” it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu; * [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β€” it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu. Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite. Input The only line contains three strings β€” the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s. Output Print a single integer β€” the minimum number of extra suited tiles she needs to draw. Examples Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 1 Note In the first example, Tokitsukaze already has a shuntsu. In the second example, Tokitsukaze already has a koutsu. In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β€” 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p].
2
8
l = input().split() if l[0]==l[1] and l[1]==l[2]: print(0) exit(0) def shuntsu(li): li.sort() return li[0][1]==li[1][1] and li[1][1]==li[2][1] and int(li[1][0])==int(li[0][0])+1 and int(li[2][0])==int(li[1][0])+1 if shuntsu(l): print(0) exit(0) for k in l: if len([x for x in l if x==k]) > 1: print(1) exit(0) if len([x for x in l if x[1]==k[1] and int(x[0]) == int(k[0])+1]) !=0: print(1) exit(0) if len([x for x in l if x[1]==k[1] and int(x[0]) == int(k[0])+2]) != 0: print(1) exit(0) print(2)
PYTHON3
1191_B. Tokitsukaze and Mahjong
Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, …, 9m, 1p, 2p, …, 9p, 1s, 2s, …, 9s. In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand. Do you know the minimum number of extra suited tiles she needs to draw so that she can win? Here are some useful definitions in this game: * A mentsu, also known as meld, is formed by a koutsu or a shuntsu; * A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu; * A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu. Some examples: * [2m, 3p, 2s, 4m, 1s, 2s, 4s] β€” it contains no koutsu or shuntsu, so it includes no mentsu; * [4s, 3m, 3p, 4s, 5p, 4s, 5p] β€” it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu; * [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β€” it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu. Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite. Input The only line contains three strings β€” the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s. Output Print a single integer β€” the minimum number of extra suited tiles she needs to draw. Examples Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 1 Note In the first example, Tokitsukaze already has a shuntsu. In the second example, Tokitsukaze already has a koutsu. In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β€” 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p].
2
8
import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Collections; import java.util.Hashtable; public class TestClass{ public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String[] str = br.readLine().trim().split("\\s+"); Hashtable<Character, ArrayList<Integer>> ht = new Hashtable<>(); for (int i=0;i<str.length;i++){ int val = Character.getNumericValue(str[i].charAt(0)); char key = str[i].charAt(1); if(ht.containsKey(key)){ ht.get(key).add(val); } else{ ht.put(key, new ArrayList<>()); ht.get(key).add(val); } } int max=0; char ch='a'; for (char c : ht.keySet()){ if(ht.get(c).size()>1) ch = c; if(ht.get(c).size()>max){ max = ht.get(c).size(); } } if (max==1){ System.out.println("2"); } else if(max==2){ Collections.sort(ht.get(ch)); int d = ht.get(ch).get(1)-ht.get(ch).get(0); if (d ==1|| d==0 || d==2) System.out.println("1"); else System.out.println("2"); } else { Collections.sort(ht.get(ch)); int d1 = ht.get(ch).get(1)-ht.get(ch).get(0); int d2 = ht.get(ch).get(2)-ht.get(ch).get(1); if (d1 == d2 && (d1==0 || d1==1)){ System.out.println("0"); } else { if (d1==0||d2==0||d1==1 || d2==1 || d1==2 || d2==2) System.out.println("1"); else{ System.out.println("2"); } } } } }
JAVA
1191_B. Tokitsukaze and Mahjong
Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, …, 9m, 1p, 2p, …, 9p, 1s, 2s, …, 9s. In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand. Do you know the minimum number of extra suited tiles she needs to draw so that she can win? Here are some useful definitions in this game: * A mentsu, also known as meld, is formed by a koutsu or a shuntsu; * A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu; * A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu. Some examples: * [2m, 3p, 2s, 4m, 1s, 2s, 4s] β€” it contains no koutsu or shuntsu, so it includes no mentsu; * [4s, 3m, 3p, 4s, 5p, 4s, 5p] β€” it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu; * [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β€” it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu. Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite. Input The only line contains three strings β€” the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s. Output Print a single integer β€” the minimum number of extra suited tiles she needs to draw. Examples Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 1 Note In the first example, Tokitsukaze already has a shuntsu. In the second example, Tokitsukaze already has a koutsu. In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β€” 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p].
2
8
t1, t2, t3 = input().split() if t1 == t2 == t3: #print('koutsu') print(0) else: s = [] m = [] p = [] for elem in (t1, t2, t3): if elem[1] == "s": s.append(int(elem[0])) elif elem[1] == "m": m.append(int(elem[0])) else: p.append(int(elem[0])) #print(s, m, p) x = max(len(s), len(m), len(p)) if len(s) == x: mentsu = s elif len(m) == x: mentsu = m else: mentsu = p mentsu.sort() if len(mentsu) == 3 and mentsu[0] + 2 == mentsu[1] + 1 == mentsu[2]: #print('shuntsu') print(0) else: kc = [0 for i in range(11)] for elem in mentsu: elem = elem + 1 kc[elem] += 1 if kc[elem - 1] > 0 or kc[elem - 2] > 0 or kc[elem] >= 2: print(1) break else: print(2)
PYTHON3
1191_B. Tokitsukaze and Mahjong
Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, …, 9m, 1p, 2p, …, 9p, 1s, 2s, …, 9s. In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand. Do you know the minimum number of extra suited tiles she needs to draw so that she can win? Here are some useful definitions in this game: * A mentsu, also known as meld, is formed by a koutsu or a shuntsu; * A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu; * A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu. Some examples: * [2m, 3p, 2s, 4m, 1s, 2s, 4s] β€” it contains no koutsu or shuntsu, so it includes no mentsu; * [4s, 3m, 3p, 4s, 5p, 4s, 5p] β€” it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu; * [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β€” it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu. Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite. Input The only line contains three strings β€” the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s. Output Print a single integer β€” the minimum number of extra suited tiles she needs to draw. Examples Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 1 Note In the first example, Tokitsukaze already has a shuntsu. In the second example, Tokitsukaze already has a koutsu. In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β€” 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p].
2
8
import math as mt import sys,string,bisect input=sys.stdin.readline l=list(input().split()) s=set(l) for i in s: if(l.count(i)==3): print(0) exit() elif(l.count(i)==2): print(1) exit() else: g=[] for i in range(3): g.append((l[i][0],l[i][1])) g.sort(key=lambda x:x[0]) if int(g[2][0])-int(g[1][0])==1 and int(g[1][0])-int(g[0][0])==1 and g[0][1]==g[1][1] and g[1][1]==g[2][1]: print(0) exit() if int(g[2][0])-int(g[1][0])==1 and g[1][1]==g[2][1]: print(1) exit() if int(g[2][0])-int(g[0][0])==1 and g[0][1]==g[2][1]: print(1) exit() if int(g[1][0])-int(g[0][0])==1 and g[0][1]==g[1][1]: print(1) exit() else: if(int(g[2][0])-int(g[0][0])==2 and g[2][1]==g[0][1]): print(1) exit() if(int(g[2][0])-int(g[1][0])==2 and g[2][1]==g[1][1]): print(1) exit() if(int(g[1][0])-int(g[0][0])==2 and g[1][1]==g[0][1]): print(1) exit() else: print(2) exit()
PYTHON3
1191_B. Tokitsukaze and Mahjong
Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, …, 9m, 1p, 2p, …, 9p, 1s, 2s, …, 9s. In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand. Do you know the minimum number of extra suited tiles she needs to draw so that she can win? Here are some useful definitions in this game: * A mentsu, also known as meld, is formed by a koutsu or a shuntsu; * A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu; * A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu. Some examples: * [2m, 3p, 2s, 4m, 1s, 2s, 4s] β€” it contains no koutsu or shuntsu, so it includes no mentsu; * [4s, 3m, 3p, 4s, 5p, 4s, 5p] β€” it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu; * [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β€” it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu. Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite. Input The only line contains three strings β€” the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s. Output Print a single integer β€” the minimum number of extra suited tiles she needs to draw. Examples Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 1 Note In the first example, Tokitsukaze already has a shuntsu. In the second example, Tokitsukaze already has a koutsu. In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β€” 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p].
2
8
from collections import Counter a = list(input().split()) #print(a) s = set(a) r = 3 for b in a: b1,b2 = int(b[0]),b[1] for i in range(3): c1,c2,c3 = b1-2+i,b1-1+i,b1+i if c1>0 and c3<10: d = set([str(c1)+b2,str(c2)+b2,str(c3)+b2]) r = min(r,len(d-s)) if r==0: print(r) exit(0) r = min(r,3-Counter(a).get(b)) if r==0: print(r) exit(0) print(r)
PYTHON3
1191_B. Tokitsukaze and Mahjong
Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, …, 9m, 1p, 2p, …, 9p, 1s, 2s, …, 9s. In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand. Do you know the minimum number of extra suited tiles she needs to draw so that she can win? Here are some useful definitions in this game: * A mentsu, also known as meld, is formed by a koutsu or a shuntsu; * A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu; * A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu. Some examples: * [2m, 3p, 2s, 4m, 1s, 2s, 4s] β€” it contains no koutsu or shuntsu, so it includes no mentsu; * [4s, 3m, 3p, 4s, 5p, 4s, 5p] β€” it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu; * [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β€” it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu. Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite. Input The only line contains three strings β€” the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s. Output Print a single integer β€” the minimum number of extra suited tiles she needs to draw. Examples Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 1 Note In the first example, Tokitsukaze already has a shuntsu. In the second example, Tokitsukaze already has a koutsu. In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β€” 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p].
2
8
// No sorceries shall prevail. // import java.util.Scanner; import java.io.PrintWriter; import java.util.Arrays; import java.util.HashMap; import java.util.LinkedList; import java.util.Queue; public class InVoker { public static void main(String args[]) { Scanner inp=new Scanner(System.in); PrintWriter out=new PrintWriter(System.out); HashMap<Character,Integer> x=new HashMap<>(); x.put('s',0); x.put('m',1); x.put('p',2); int a[][]=new int[10][3]; String s=inp.nextLine(); a[s.charAt(0)-'0'][x.get(s.charAt(1))]++; a[s.charAt(3)-'0'][x.get(s.charAt(4))]++; a[s.charAt(6)-'0'][x.get(s.charAt(7))]++; int min=2; boolean done=false; for(int i=1;i<10;i++) { for(int j=0;j<3;j++) { //out.print(a[i][j]+" "); if(a[i][j]==2) { min=1; done=true; } else if(a[i][j]==3) { min=0; done=true; } } //out.println(); } if(!done) { int sum=0; for(int i=1;i<=3;i++) { sum+=a[i][0]==1?1:0; min=Math.min(min, 3-sum); //out.println(min); } for(int i=4;i<10;i++) { sum+=a[i][0]==1?1:0; sum-=a[i-3][0]==1?1:0; min=Math.min(min, 3-sum); } sum=0; for(int i=1;i<=3;i++) { sum+=a[i][1]==1?1:0; min=Math.min(min, 3-sum); } for(int i=4;i<10;i++) { sum+=a[i][1]==1?1:0; sum-=a[i-3][1]==1?1:0; min=Math.min(min, 3-sum); } sum=0; for(int i=1;i<=3;i++) { sum+=a[i][2]==1?1:0; min=Math.min(min, 3-sum); } for(int i=4;i<10;i++) { sum+=a[i][2]==1?1:0; sum-=a[i-3][2]==1?1:0; min=Math.min(min, 3-sum); } } out.println(min); out.close(); inp.close(); } }
JAVA
1191_B. Tokitsukaze and Mahjong
Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, …, 9m, 1p, 2p, …, 9p, 1s, 2s, …, 9s. In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand. Do you know the minimum number of extra suited tiles she needs to draw so that she can win? Here are some useful definitions in this game: * A mentsu, also known as meld, is formed by a koutsu or a shuntsu; * A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu; * A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu. Some examples: * [2m, 3p, 2s, 4m, 1s, 2s, 4s] β€” it contains no koutsu or shuntsu, so it includes no mentsu; * [4s, 3m, 3p, 4s, 5p, 4s, 5p] β€” it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu; * [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β€” it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu. Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite. Input The only line contains three strings β€” the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s. Output Print a single integer β€” the minimum number of extra suited tiles she needs to draw. Examples Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 1 Note In the first example, Tokitsukaze already has a shuntsu. In the second example, Tokitsukaze already has a koutsu. In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β€” 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p].
2
8
#from random import randint # a = list(map(int, input().split())) # a, b, c, d = map(int, input().split()) # q = int(input()) def gcd(a, b): if b == 0: return a else: return gcd(b, a % b) a = list(map(str, input().split())) m = {} for i in range(3): if a[i] in m: m[a[i]] += 1 else: m[a[i]] = 1 q = [] for i in range(3): q.append([].copy()) # 0 - p, 1 - m, 2 - s for i in range(3): if a[i][1] == "p": q[0].append(a[i][0]) elif a[i][1] == "m": q[1].append(a[i][0]) else: q[2].append(a[i][0]) best = -1 nst = 0 for i in range(3): q[i] = sorted(q[i]) st = 1 for j in range(len(q[i])- 1): if int(q[i][j + 1]) - int(q[i][j]) == 1: st += 1 if st > best: best = st else: if st > best: best = st st = 1 if int(q[i][j + 1]) - int(q[i][j]) == 2: nst = 2 if best == 3: print(0) exit(0) flag = 0 for i in m: if m[i] == 2: flag = 1 if m[i] == 3: print(0) exit(0) if best == 2 or nst == 2: print(1) elif flag == 1: print(1) else: print(2)
PYTHON3
1191_B. Tokitsukaze and Mahjong
Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, …, 9m, 1p, 2p, …, 9p, 1s, 2s, …, 9s. In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand. Do you know the minimum number of extra suited tiles she needs to draw so that she can win? Here are some useful definitions in this game: * A mentsu, also known as meld, is formed by a koutsu or a shuntsu; * A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu; * A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu. Some examples: * [2m, 3p, 2s, 4m, 1s, 2s, 4s] β€” it contains no koutsu or shuntsu, so it includes no mentsu; * [4s, 3m, 3p, 4s, 5p, 4s, 5p] β€” it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu; * [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β€” it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu. Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite. Input The only line contains three strings β€” the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s. Output Print a single integer β€” the minimum number of extra suited tiles she needs to draw. Examples Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 1 Note In the first example, Tokitsukaze already has a shuntsu. In the second example, Tokitsukaze already has a koutsu. In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β€” 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p].
2
8
import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); String s[]=new String[3]; int a[]=new int[30]; int n[]=new int[3]; for(int i=0;i<3;i++) { s[i]=sc.next(); a[s[i].charAt(1)-'a']++; n[i]=s[i].charAt(0)-'0'; } int index='s'-'a'; if(a[index]<a['p'-'a']) index='p'-'a'; if(a[index]<a['m'-'a']) index='m'-'a'; int an[]=new int[3]; int cnt=0; for(int i=0;i<3;i++) { if(s[i].charAt(1)-'a'==index) an[cnt++]=n[i]; } Arrays.sort(an,0,cnt); if(cnt==3) { if(an[0]==an[1]&&an[1]==an[2]) { System.out.println(0); return; } if(an[0]==an[1]||an[0]==an[2]||an[1]==an[2]) { System.out.println(1); return; } if(an[2]-an[1]==1&&an[1]-an[0]==1) { System.out.println(0); return; } if(an[2]-an[1]==1||an[1]-an[0]==1||an[2]-an[1]==2||an[1]-an[0]==2) { System.out.println(1); return; } System.out.println(2); return; }else if(cnt==2) { if(an[1]-an[0]==1||an[1]-an[0]==2||an[1]==an[0]) { System.out.println(1); return; } System.out.println(2); return; }else { System.out.println(2); return; } } }
JAVA
1191_B. Tokitsukaze and Mahjong
Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, …, 9m, 1p, 2p, …, 9p, 1s, 2s, …, 9s. In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand. Do you know the minimum number of extra suited tiles she needs to draw so that she can win? Here are some useful definitions in this game: * A mentsu, also known as meld, is formed by a koutsu or a shuntsu; * A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu; * A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu. Some examples: * [2m, 3p, 2s, 4m, 1s, 2s, 4s] β€” it contains no koutsu or shuntsu, so it includes no mentsu; * [4s, 3m, 3p, 4s, 5p, 4s, 5p] β€” it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu; * [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β€” it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu. Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite. Input The only line contains three strings β€” the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s. Output Print a single integer β€” the minimum number of extra suited tiles she needs to draw. Examples Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 1 Note In the first example, Tokitsukaze already has a shuntsu. In the second example, Tokitsukaze already has a koutsu. In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β€” 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p].
2
8
from collections import defaultdict as dfd d = dfd(int) arr = input().split() arr.sort() for i in arr: d[i] += 1 if max(d.values())==3 or (arr[0][1]==arr[1][1]==arr[2][1] and int(arr[0][0])+1==int(arr[1][0]) and int(arr[1][0])+1==int(arr[2][0])): print(0) elif max(d.values())==2: print(1) else: flag = True for i in range(len(arr)): for j in range(i+1, len(arr)): if arr[i][1]==arr[j][1] and (abs(int(arr[i][0])-int(arr[j][0]))==1 or abs(int(arr[i][0])-int(arr[j][0]))==2): print(1) flag = False break if not flag: break if flag: print(2)
PYTHON3
1191_B. Tokitsukaze and Mahjong
Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, …, 9m, 1p, 2p, …, 9p, 1s, 2s, …, 9s. In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand. Do you know the minimum number of extra suited tiles she needs to draw so that she can win? Here are some useful definitions in this game: * A mentsu, also known as meld, is formed by a koutsu or a shuntsu; * A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu; * A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu. Some examples: * [2m, 3p, 2s, 4m, 1s, 2s, 4s] β€” it contains no koutsu or shuntsu, so it includes no mentsu; * [4s, 3m, 3p, 4s, 5p, 4s, 5p] β€” it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu; * [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β€” it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu. Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite. Input The only line contains three strings β€” the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s. Output Print a single integer β€” the minimum number of extra suited tiles she needs to draw. Examples Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 1 Note In the first example, Tokitsukaze already has a shuntsu. In the second example, Tokitsukaze already has a koutsu. In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β€” 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p].
2
8
a=list(map(str,input().split()));l=a s=m=p=0;s1,m1,p1=[],[],[] for i in a: if 's' in i:s+=1;s1+=[int(i[0])] elif 'p' in i:p+=1;p1+=[int(i[0])] else:m+=1;m1+=[int(i[0])] if m and s and p:print(2) elif len(set(a))==1:print(0) elif max(s,m,p)==3: if s==3:a=s1 elif m==3:a=m1 else:a=p1 a=sorted(a);r=2 if a[1]-a[0]==a[2]-a[1]==1:r=0 elif a[1]-a[0] in [1,2] or a[2]-a[1] in [1,2]:r=min(r,1) else:r=min(r,2) if len(set(l))==2:r=min(r,1) print(r) else: if s==2:a=s1 elif p==2:a=p1 else:a=m1 r=2 a=sorted(a) if a[1]-a[0]==1 or a[1]-a[0]==2:r=min(r,1) else: if len(set(l))==2:r=min(r,1) else:r=min(r,2) print(r)
PYTHON3
1191_B. Tokitsukaze and Mahjong
Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, …, 9m, 1p, 2p, …, 9p, 1s, 2s, …, 9s. In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand. Do you know the minimum number of extra suited tiles she needs to draw so that she can win? Here are some useful definitions in this game: * A mentsu, also known as meld, is formed by a koutsu or a shuntsu; * A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu; * A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu. Some examples: * [2m, 3p, 2s, 4m, 1s, 2s, 4s] β€” it contains no koutsu or shuntsu, so it includes no mentsu; * [4s, 3m, 3p, 4s, 5p, 4s, 5p] β€” it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu; * [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β€” it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu. Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite. Input The only line contains three strings β€” the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s. Output Print a single integer β€” the minimum number of extra suited tiles she needs to draw. Examples Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 1 Note In the first example, Tokitsukaze already has a shuntsu. In the second example, Tokitsukaze already has a koutsu. In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β€” 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p].
2
8
#include <bits/stdc++.h> using namespace std; int main() { int num, m = 0, p = 0, s = 0; char alpha; vector<pair<int, char>> tiles; for (int i = 0; i < 3; i++) { cin >> num >> alpha; pair<int, char> temp = make_pair(num, alpha); tiles.push_back(temp); if (alpha == 'm') { m++; } else if (alpha == 'p') { p++; } else { s++; } } sort(tiles.begin(), tiles.end()); int one = abs(tiles[0].first - tiles[1].first); int two = abs(tiles[1].first - tiles[2].first); int three = abs(tiles[0].first - tiles[2].first); if (tiles[0].second == tiles[1].second && tiles[1].second == tiles[2].second) { if (one == 0 && two == 0) { cout << 0 << endl; return 0; } else if (one == 0 || two == 0 || three == 0) { cout << 1 << endl; return 0; } else { if (one == 1 && two == 1) { cout << 0 << endl; } else if (one > 1 && two == 1 || one == 1 && two > 1 || one == 2 || two == 2) { cout << 1 << endl; } else { cout << 2 << endl; } } return 0; } else if (tiles[0].second == tiles[1].second) { if (one <= 2) { cout << 1 << endl; } else { cout << 2 << endl; } return 0; } else if (tiles[1].second == tiles[2].second) { if (two <= 2) { cout << 1 << endl; } else { cout << 2 << endl; } return 0; } else if (tiles[0].second == tiles[2].second) { if (three <= 2) { cout << 1 << endl; } else { cout << 2 << endl; } return 0; } else { cout << 2 << endl; } return 0; }
CPP
1191_B. Tokitsukaze and Mahjong
Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, …, 9m, 1p, 2p, …, 9p, 1s, 2s, …, 9s. In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand. Do you know the minimum number of extra suited tiles she needs to draw so that she can win? Here are some useful definitions in this game: * A mentsu, also known as meld, is formed by a koutsu or a shuntsu; * A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu; * A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu. Some examples: * [2m, 3p, 2s, 4m, 1s, 2s, 4s] β€” it contains no koutsu or shuntsu, so it includes no mentsu; * [4s, 3m, 3p, 4s, 5p, 4s, 5p] β€” it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu; * [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β€” it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu. Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite. Input The only line contains three strings β€” the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s. Output Print a single integer β€” the minimum number of extra suited tiles she needs to draw. Examples Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 1 Note In the first example, Tokitsukaze already has a shuntsu. In the second example, Tokitsukaze already has a koutsu. In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β€” 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p].
2
8
line = input().split() line.sort() a,b,c = line if a == b and a == c: print(0) elif a == b: print(1) elif b == c: print(1) else: if a[1] == b[1] and b[1] == c[1] \ and int(b[0])-int(a[0]) == 1 and int(c[0])-int(b[0]) == 1: print(0) elif a[1] == b[1] and int(b[0])-int(a[0]) in [1,2]: print(1) elif b[1] == c[1] and int(c[0])-int(b[0]) in [1,2]: print(1) elif a[1] == c[1] and int(c[0])-int(a[0]) in [1,2]: print(1) else: print(2)
PYTHON3
1191_B. Tokitsukaze and Mahjong
Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, …, 9m, 1p, 2p, …, 9p, 1s, 2s, …, 9s. In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand. Do you know the minimum number of extra suited tiles she needs to draw so that she can win? Here are some useful definitions in this game: * A mentsu, also known as meld, is formed by a koutsu or a shuntsu; * A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu; * A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu. Some examples: * [2m, 3p, 2s, 4m, 1s, 2s, 4s] β€” it contains no koutsu or shuntsu, so it includes no mentsu; * [4s, 3m, 3p, 4s, 5p, 4s, 5p] β€” it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu; * [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β€” it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu. Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite. Input The only line contains three strings β€” the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s. Output Print a single integer β€” the minimum number of extra suited tiles she needs to draw. Examples Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 1 Note In the first example, Tokitsukaze already has a shuntsu. In the second example, Tokitsukaze already has a koutsu. In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β€” 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p].
2
8
l = input().split() suit = ['m', 'p', 's'] from collections import defaultdict cnt = defaultdict(lambda : 0) for i in range(3): cnt[l[i]] += 1 mini = 10 for i in suit: for j in range(1, 10): # shuntsu if j + 2 <= 9: cn = 0 for k in range(3): cn += int("{}{}".format(j+k, i) not in cnt) mini = min(mini, cn) # koutsu mini = min(mini, 3 - cnt["{}{}".format(j, i)]) print(mini)
PYTHON3
1191_B. Tokitsukaze and Mahjong
Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, …, 9m, 1p, 2p, …, 9p, 1s, 2s, …, 9s. In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand. Do you know the minimum number of extra suited tiles she needs to draw so that she can win? Here are some useful definitions in this game: * A mentsu, also known as meld, is formed by a koutsu or a shuntsu; * A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu; * A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu. Some examples: * [2m, 3p, 2s, 4m, 1s, 2s, 4s] β€” it contains no koutsu or shuntsu, so it includes no mentsu; * [4s, 3m, 3p, 4s, 5p, 4s, 5p] β€” it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu; * [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β€” it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu. Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite. Input The only line contains three strings β€” the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s. Output Print a single integer β€” the minimum number of extra suited tiles she needs to draw. Examples Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 1 Note In the first example, Tokitsukaze already has a shuntsu. In the second example, Tokitsukaze already has a koutsu. In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β€” 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p].
2
8
#include <bits/stdc++.h> using namespace std; int main() { vector<vector<int>> b(9); for (int i = 0; i < 9; i++) for (int j = 0; j < 3; j++) b[i].push_back(0); string s; for (int i = 0; i < 3; i++) { cin >> s; switch (s[1]) { case 'm': b[s[0] - '1'][0]++; break; case 'p': b[s[0] - '1'][1]++; break; case 's': b[s[0] - '1'][2]++; break; } } int m = 0; for (int i = 0; i < 9; i++) { int s1 = b[i][0]; int s2 = b[i][1]; int s3 = b[i][2]; m = min(max(max(max(s1, s2), s3), m), 3); } for (int i = 0; i < 7; i++) { int s1 = (b[i][0] != 0) + (b[i + 1][0] != 0) + (b[i + 2][0] != 0); int s2 = (b[i][1] != 0) + (b[i + 1][1] != 0) + (b[i + 2][1] != 0); int s3 = (b[i][2] != 0) + (b[i + 1][2] != 0) + (b[i + 2][2] != 0); m = max(max(max(s1, s2), s3), m); } cout << 3 - m; }
CPP
1191_B. Tokitsukaze and Mahjong
Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from 1 to 9). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, …, 9m, 1p, 2p, …, 9p, 1s, 2s, …, 9s. In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand. Do you know the minimum number of extra suited tiles she needs to draw so that she can win? Here are some useful definitions in this game: * A mentsu, also known as meld, is formed by a koutsu or a shuntsu; * A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu; * A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu. Some examples: * [2m, 3p, 2s, 4m, 1s, 2s, 4s] β€” it contains no koutsu or shuntsu, so it includes no mentsu; * [4s, 3m, 3p, 4s, 5p, 4s, 5p] β€” it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu; * [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] β€” it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu. Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite. Input The only line contains three strings β€” the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from 1 to 9 and the second character is m, p or s. Output Print a single integer β€” the minimum number of extra suited tiles she needs to draw. Examples Input 1s 2s 3s Output 0 Input 9m 9m 9m Output 0 Input 3p 9m 2p Output 1 Note In the first example, Tokitsukaze already has a shuntsu. In the second example, Tokitsukaze already has a koutsu. In the third example, Tokitsukaze can get a shuntsu by drawing one suited tile β€” 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p].
2
8
import collections a=input().split() a.sort() if a[0]==a[1] and a[1]==a[2]: print(0) elif a[0][1]==a[1][1] and a[1][1]==a[2][1] and abs(int(a[1][0])-int(a[0][0]))==1 and abs(int(a[2][0])-int(a[1][0]))==1: print("0") elif a[0]==a[1] or a[1]==a[2] or a[2]==a[0]: print("1") elif a[0][1]==a[1][1] and (abs(int(a[1][0])-int(a[0][0]))==1 or abs(int(a[1][0])-int(a[0][0]))==2) : print("1") elif a[1][1]==a[2][1] and (abs(int(a[2][0])-int(a[1][0]))==1 or abs(int(a[2][0])-int(a[1][0]))==2): print("1") elif a[0][1]==a[2][1] and (abs(int(a[2][0])-int(a[0][0]))==1 or abs(int(a[2][0])-int(a[0][0]))==2): print("1") else: print("2")
PYTHON3