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
401_D. Roman and Numbers
Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m. Number x is considered close to number n modulo m, if: * it can be obtained by rearranging the digits of number n, * it doesn't have any leading zeroes, * the remainder after dividing number x by m equals 0. Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him. Input The first line contains two integers: n (1 ≀ n < 1018) and m (1 ≀ m ≀ 100). Output In a single line print a single integer β€” the number of numbers close to number n modulo m. Examples Input 104 2 Output 3 Input 223 4 Output 1 Input 7067678 8 Output 47 Note In the first sample the required numbers are: 104, 140, 410. In the second sample the required number is 232.
2
10
import java.io.PrintWriter; import java.util.Arrays; import java.util.Scanner; public class D401 { int n, mod; int[] num; long[][] memo; public long dp(int mask, int curMod) { if(Integer.bitCount(mask) == n) return curMod == 0 ? 1 : 0; if(memo[curMod][mask] != -1) return memo[curMod][mask]; long ans = 0; int checked = 0; for(int i = 0; i < n; ++i) { if((mask & (1 << i)) != 0) continue; if(num[i] == 0 && Integer.bitCount(mask) == 0) continue; if((checked & (1 << num[i])) != 0) continue; int nextmod = (curMod * 10 + num[i]) % mod; checked |= 1 << num[i]; ans += dp(mask | (1 << i), nextmod); } return memo[curMod][mask] = ans; } public void solve(Scanner in, PrintWriter out) { char[] nn = in.next().toCharArray(); mod = in.nextInt(); n = nn.length; num = new int[n]; for(int i = 0; i < n; ++i) { num[i] = nn[i] - '0'; } memo = new long[mod][1<<n]; for(long[] a : memo) Arrays.fill(a, -1); out.println(dp(0, 0)); } public static void main(String[] args) { Scanner in = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); new D401().solve(in, out); in.close(); out.close(); } }
JAVA
401_D. Roman and Numbers
Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m. Number x is considered close to number n modulo m, if: * it can be obtained by rearranging the digits of number n, * it doesn't have any leading zeroes, * the remainder after dividing number x by m equals 0. Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him. Input The first line contains two integers: n (1 ≀ n < 1018) and m (1 ≀ m ≀ 100). Output In a single line print a single integer β€” the number of numbers close to number n modulo m. Examples Input 104 2 Output 3 Input 223 4 Output 1 Input 7067678 8 Output 47 Note In the first sample the required numbers are: 104, 140, 410. In the second sample the required number is 232.
2
10
#include <bits/stdc++.h> using namespace std; int n, m; int digit[105]; long long Num; long long dp[1 << 18][105]; int cnt[10]; int main() { scanf("%lld%d", &Num, &m); while (Num) { digit[++n] = Num % 10; cnt[Num % 10]++; Num /= 10; } dp[0][0] = 1; for (int i = (0), i_end_ = ((1 << n) - 1); i <= i_end_; i++) { for (int j = (0), j_end_ = (m - 1); j <= j_end_; j++) { if (!dp[i][j]) continue; for (int k = (1), k_end_ = (n); k <= k_end_; k++) { if (i & (1 << (k - 1))) continue; if (!i && !digit[k]) continue; int now = i | (1 << (k - 1)); dp[now][(j * 10 + digit[k]) % m] += dp[i][j]; } } } long long res = dp[(1 << n) - 1][0]; for (int i = (0), i_end_ = (9); i <= i_end_; i++) { if (cnt[i] >= 2) { for (int j = (1), j_end_ = (cnt[i]); j <= j_end_; j++) res /= j; } } printf("%lld\n", res); return 0; }
CPP
401_D. Roman and Numbers
Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m. Number x is considered close to number n modulo m, if: * it can be obtained by rearranging the digits of number n, * it doesn't have any leading zeroes, * the remainder after dividing number x by m equals 0. Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him. Input The first line contains two integers: n (1 ≀ n < 1018) and m (1 ≀ m ≀ 100). Output In a single line print a single integer β€” the number of numbers close to number n modulo m. Examples Input 104 2 Output 3 Input 223 4 Output 1 Input 7067678 8 Output 47 Note In the first sample the required numbers are: 104, 140, 410. In the second sample the required number is 232.
2
10
#include <bits/stdc++.h> using namespace std; int main() { long long n; int m; cin >> n >> m; ostringstream oss; oss << n; string const number = oss.str(); int const digits = (int)number.size(); int const all_digits_mask = ((long long)1 << digits) - 1; vector<vector<long long> > dp(all_digits_mask + 1, vector<long long>(m)); vector<int> dup_digits(10); for (int i = (0); i < ((digits)); ++i) { ++dup_digits[number[i] - '0']; } dp[0][0] = 1; for (int mask = (0); mask < ((all_digits_mask)); ++mask) { for (int mod = (0); mod < ((m)); ++mod) if (dp[mask][mod]) { for (int i = (0); i < ((digits)); ++i) if (~mask & (1 << i)) { int const digit = (number[i] - '0'); if (digit == 0 && mask == 0) continue; long long newMod = (mod * 10 + digit) % m; dp[mask | (1 << i)][newMod] += dp[mask][mod]; } } } long long res = dp[all_digits_mask][0]; for (int i = (0); i < ((10)); ++i) for (int j = (1); j < (dup_digits[i] + 1); ++j) res /= j; cout << res << endl; return 0; }
CPP
401_D. Roman and Numbers
Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m. Number x is considered close to number n modulo m, if: * it can be obtained by rearranging the digits of number n, * it doesn't have any leading zeroes, * the remainder after dividing number x by m equals 0. Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him. Input The first line contains two integers: n (1 ≀ n < 1018) and m (1 ≀ m ≀ 100). Output In a single line print a single integer β€” the number of numbers close to number n modulo m. Examples Input 104 2 Output 3 Input 223 4 Output 1 Input 7067678 8 Output 47 Note In the first sample the required numbers are: 104, 140, 410. In the second sample the required number is 232.
2
10
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; import java.util.StringTokenizer; public class Round235_D { char[] number; int len; long[] val; int M; long memo[][]; public long go(int mask, int reminder, int cnt) { if (cnt == len) { return (reminder == 0) ? 1 : 0; } if (memo[mask][reminder] != -1) return memo[mask][reminder]; long ret = 0; if (cnt == len - 1) { for (int j = 0; j < len; j++) { if ((mask & (1 << j)) == 0 && number[j] != '0') { ret += go(mask | (1 << j), (int) (((((number[j]-'0')*val[cnt]) % M) + reminder) % M), cnt+1); break; } } } else { long done =0; for (int j = 0; j < len; j++) { int digitToTake = number[j]-'0'; if ((mask & (1 << j)) == 0 && (done&(1<<digitToTake)) == 0) { ret += go(mask | (1 << j), (int) ((((digitToTake*val[cnt]) % M) + reminder) % M), cnt+1); done |= (1 << digitToTake); } } } return memo[mask][reminder] = ret; } public void solve() throws Exception { InputReader in = new InputReader(); char[] tmp = in.next().toCharArray(); len = tmp.length; number = new char[len]; for (int i = 0; i < len; i++) number[i] = tmp[len - 1 - i]; val = new long[len]; long ten = 1; for (int i = 0; i < len; i++) { val[i] = ten; ten *= 10L; } M = in.nextInt(); memo = new long[(1 << len) + 2][M + 1]; for (int i = 0; i < memo.length; i++) Arrays.fill(memo[i], -1); // System.out.println(Arrays.toString(val)); System.out.println(go(0, 0, 0)); } public static void main(String[] args) throws Exception { new Round235_D().solve(); } static class InputReader { BufferedReader in; StringTokenizer st; public InputReader() throws IOException { in = new BufferedReader(new InputStreamReader(System.in)); st = new StringTokenizer(in.readLine()); } public String next() throws IOException { while (!st.hasMoreElements()) st = new StringTokenizer(in.readLine()); return st.nextToken(); } public int nextInt() throws NumberFormatException, IOException { return Integer.parseInt(next()); } public long nextLong() throws NumberFormatException, IOException { return Long.parseLong(next()); } } }
JAVA
401_D. Roman and Numbers
Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m. Number x is considered close to number n modulo m, if: * it can be obtained by rearranging the digits of number n, * it doesn't have any leading zeroes, * the remainder after dividing number x by m equals 0. Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him. Input The first line contains two integers: n (1 ≀ n < 1018) and m (1 ≀ m ≀ 100). Output In a single line print a single integer β€” the number of numbers close to number n modulo m. Examples Input 104 2 Output 3 Input 223 4 Output 1 Input 7067678 8 Output 47 Note In the first sample the required numbers are: 104, 140, 410. In the second sample the required number is 232.
2
10
import java.io.*; import java.util.*; public class p401d { BufferedReader in; PrintWriter out; StringTokenizer st; String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(in.readLine()); } catch (Exception e) { } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } public long[][] d = new long[1 << 18][102]; public int cnt[] = new int[11]; public int f[] = new int[20]; public long ans = 0; public void run() throws Exception { in = new BufferedReader(new InputStreamReader(System.in)); // in = new BufferedReader(new FileReader(new File("./src/input.txt"))); out = new PrintWriter(System.out); long n = nextLong(); int m = nextInt(); int len = 0; while (n > 0) { cnt[(int) (n % 10)]++; f[len++] = (int) (n % 10); n /= 10; } for (int i = 0; i < len; i++) if (f[i] > 0) { d[1 << i][f[i] % m] += 1; } for (int i = 1; i < (1 << len); i++) { for (int j = 0; j < m; j++) { if (d[i][j] == 0) { continue; } for (int p = 0; p < len; p++) { if ((i & (1 << p)) == 0) { d[i | (1 << p)][(j * 10 + f[p]) % m] += d[i][j]; } } } } long ans = d[(1 << len)-1][0]; for (int i = 0; i<10; i++) for (int j = 1; j<=cnt[i]; j++) ans/=j; out.println(ans); out.close(); } public static void main(String[] args) throws Exception { new p401d().run(); } }
JAVA
401_D. Roman and Numbers
Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m. Number x is considered close to number n modulo m, if: * it can be obtained by rearranging the digits of number n, * it doesn't have any leading zeroes, * the remainder after dividing number x by m equals 0. Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him. Input The first line contains two integers: n (1 ≀ n < 1018) and m (1 ≀ m ≀ 100). Output In a single line print a single integer β€” the number of numbers close to number n modulo m. Examples Input 104 2 Output 3 Input 223 4 Output 1 Input 7067678 8 Output 47 Note In the first sample the required numbers are: 104, 140, 410. In the second sample the required number is 232.
2
10
#include <bits/stdc++.h> using namespace std; long long dp[(1 << 18)][111]; int main() { int n, m, x, y, i, pre; char str[20]; int a[20], j, k, ii; while (cin >> str >> m) { n = strlen(str); for (i = 0; i < n; i++) a[i] = str[i] - '0'; sort(a, a + n); memset(dp, 0, sizeof(dp)); dp[0][0] = 1; y = (1 << n); for (i = 0; i < y; i++) { for (j = 0; j < m; j++) if (dp[i][j]) { pre = -1; x = j * 10; for (k = 0; k < n; k++) if (!(i & (1 << k))) { if ((i == 0 && a[k] == 0) || pre == a[k]) continue; ii = (x + a[k]) % m; dp[(1 << k) | i][ii] += dp[i][j]; pre = a[k]; } } } printf("%I64d\n", dp[y - 1][0]); } return 0; }
CPP
401_D. Roman and Numbers
Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m. Number x is considered close to number n modulo m, if: * it can be obtained by rearranging the digits of number n, * it doesn't have any leading zeroes, * the remainder after dividing number x by m equals 0. Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him. Input The first line contains two integers: n (1 ≀ n < 1018) and m (1 ≀ m ≀ 100). Output In a single line print a single integer β€” the number of numbers close to number n modulo m. Examples Input 104 2 Output 3 Input 223 4 Output 1 Input 7067678 8 Output 47 Note In the first sample the required numbers are: 104, 140, 410. In the second sample the required number is 232.
2
10
#include <bits/stdc++.h> using namespace std; long long n, m, dp[300010][110]; bool vis[11]; vector<long long> plc; int main() { long long i, j, k; cin >> n >> m; while (n != 0) { plc.push_back(n % 10); n /= 10; } memset(dp, 0, sizeof(dp)); dp[0][0] = 1; for (i = 0; i < (1 << (long long)plc.size()); i++) { for (j = 0; j < m; j++) { memset(vis, false, sizeof(vis)); for (k = 0; k < plc.size(); k++) { if (vis[plc[k]] || (i & (1 << k)) || (i == 0 && plc[k] == 0)) { continue; } dp[i | (1 << k)][(j * 10 + plc[k]) % m] += dp[i][j]; vis[plc[k]] = true; } } } cout << dp[(1 << plc.size()) - 1][0] << endl; return 0; }
CPP
401_D. Roman and Numbers
Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m. Number x is considered close to number n modulo m, if: * it can be obtained by rearranging the digits of number n, * it doesn't have any leading zeroes, * the remainder after dividing number x by m equals 0. Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him. Input The first line contains two integers: n (1 ≀ n < 1018) and m (1 ≀ m ≀ 100). Output In a single line print a single integer β€” the number of numbers close to number n modulo m. Examples Input 104 2 Output 3 Input 223 4 Output 1 Input 7067678 8 Output 47 Note In the first sample the required numbers are: 104, 140, 410. In the second sample the required number is 232.
2
10
#include <bits/stdc++.h> using namespace std; int m, a[20], cnt[15]; long long n, fact[20], dp[262150][105]; int getbit(int a, int i) { return ((a >> (i - 1)) & 1); } int batbit(int a, int i) { return ((1 << (i - 1)) | a); } int main() { int i, j, k, x, len = 0; long long n1; cin >> n >> m; n1 = n; fact[0] = 1; while (n1 != 0) { x = (int)(n1 % 10); len++; a[len] = x; cnt[x]++; n1 /= 10; } for (i = 1; i <= len; i++) fact[i] = fact[i - 1] * i; dp[0][0] = 1; for (i = 0; i < (1 << len); i++) for (j = 0; j < m; j++) for (k = 1; k <= len; k++) if (getbit(i, k) == 0) if (i != 0 || a[k] != 0) dp[batbit(i, k)][(j * 10 + a[k]) % m] += dp[i][j]; long long res = dp[(1 << len) - 1][0]; for (i = 0; i < 10; i++) res /= fact[cnt[i]]; cout << res; return 0; }
CPP
401_D. Roman and Numbers
Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m. Number x is considered close to number n modulo m, if: * it can be obtained by rearranging the digits of number n, * it doesn't have any leading zeroes, * the remainder after dividing number x by m equals 0. Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him. Input The first line contains two integers: n (1 ≀ n < 1018) and m (1 ≀ m ≀ 100). Output In a single line print a single integer β€” the number of numbers close to number n modulo m. Examples Input 104 2 Output 3 Input 223 4 Output 1 Input 7067678 8 Output 47 Note In the first sample the required numbers are: 104, 140, 410. In the second sample the required number is 232.
2
10
#include <bits/stdc++.h> using namespace std; int w[20], cnt = -1, m; long long f[(1 << 18) + 10][110], n; bool vis[10]; int main() { for (cin >> n >> m; n; n /= 10) w[++cnt] = n % 10; f[0][0] = 1; for (int s = 1; s < 1 << cnt + 1; s++) { memset(vis, 0, sizeof(vis)); for (int i = 0; i <= cnt; i++) { if (s == (1 << i) && !w[i]) break; if (!(s & (1 << i)) || vis[w[i]]) continue; vis[w[i]] = true; for (int j = 0; j < m; j++) f[s][(j * 10 + w[i]) % m] = (f[s][(j * 10 + w[i]) % m] + f[s ^ (1 << i)][j]); } } cout << f[(1 << cnt + 1) - 1][0] << endl; return 0; }
CPP
401_D. Roman and Numbers
Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m. Number x is considered close to number n modulo m, if: * it can be obtained by rearranging the digits of number n, * it doesn't have any leading zeroes, * the remainder after dividing number x by m equals 0. Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him. Input The first line contains two integers: n (1 ≀ n < 1018) and m (1 ≀ m ≀ 100). Output In a single line print a single integer β€” the number of numbers close to number n modulo m. Examples Input 104 2 Output 3 Input 223 4 Output 1 Input 7067678 8 Output 47 Note In the first sample the required numbers are: 104, 140, 410. In the second sample the required number is 232.
2
10
import java.io.IOException; import java.util.Arrays; import java.io.UnsupportedEncodingException; import java.util.InputMismatchException; import java.io.OutputStream; import java.io.PrintWriter; import java.math.BigInteger; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskD solver = new TaskD(); solver.solve(1, in, out); out.close(); } } class TaskD { long n; int m; String nn; long dp[][] = new long[(1 << 18) + 1][100]; public void solve(int testNumber, InputReader in, PrintWriter out) { n = in.readLong(); m = in.readInt(); nn = "" + n; if(n == (long)1e18) { out.println(n % m == 0? 1 : 0); return; } int c[] = new int[10]; for(int i = 0;i < nn.length();i++) { c[nn.charAt(i) - '0']++; } long ct = 0; for(int i = 0;i < dp.length;i++) { Arrays.fill(dp[i], -1); } ct = count(0, 0); out.println(ct); } public long count(int mask, int r) { if(dp[mask][r] != -1) return dp[mask][r]; if(mask == (1 << nn.length()) - 1) { if(r == 0) return 1; else return 0; } long count = 0; boolean isUsed[] = new boolean[10]; for(int i = 0;i < nn.length();i++) { if(isUsed[nn.charAt(i) - '0']) continue; if(mask == 0 && nn.charAt(i) == '0') continue; if((mask & (1 << i)) == 0) { isUsed[nn.charAt(i) - '0'] = true; count += count(mask | (1 << i), (r*10 + (nn.charAt(i) - '0')) % m); } } return dp[mask][r] = count; } } class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public int readInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long readLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public static boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } }
JAVA
401_D. Roman and Numbers
Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m. Number x is considered close to number n modulo m, if: * it can be obtained by rearranging the digits of number n, * it doesn't have any leading zeroes, * the remainder after dividing number x by m equals 0. Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him. Input The first line contains two integers: n (1 ≀ n < 1018) and m (1 ≀ m ≀ 100). Output In a single line print a single integer β€” the number of numbers close to number n modulo m. Examples Input 104 2 Output 3 Input 223 4 Output 1 Input 7067678 8 Output 47 Note In the first sample the required numbers are: 104, 140, 410. In the second sample the required number is 232.
2
10
#include <bits/stdc++.h> using namespace std; const int N = 1 << 18; long long ans[N][100]; long long tot, a[25], m, n; long long dfs(int st, int yu, int flag) { if (st == (1 << tot) - 1) return yu == 0; if (!flag && ans[st][yu] != -1) return ans[st][yu]; bool v[10] = {0}; long long res = 0; for (int i = 1; i <= tot; i++) if (!v[a[i]] && !(st & (1 << (i - 1))) && (!flag || a[i])) v[a[i]] = 1, res += dfs(st | (1 << (i - 1)), (yu * 10 + a[i]) % m, (flag && a[i] == 0)); if (!flag) ans[st][yu] = res; return res; } long long gao(long long x) { while (x) a[++tot] = x % 10, x /= 10; return dfs(0, 0, 1); } int main() { memset(ans, -1, sizeof(ans)); scanf("%I64d%I64d", &n, &m); printf("%I64d\n", gao(n)); }
CPP
401_D. Roman and Numbers
Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m. Number x is considered close to number n modulo m, if: * it can be obtained by rearranging the digits of number n, * it doesn't have any leading zeroes, * the remainder after dividing number x by m equals 0. Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him. Input The first line contains two integers: n (1 ≀ n < 1018) and m (1 ≀ m ≀ 100). Output In a single line print a single integer β€” the number of numbers close to number n modulo m. Examples Input 104 2 Output 3 Input 223 4 Output 1 Input 7067678 8 Output 47 Note In the first sample the required numbers are: 104, 140, 410. In the second sample the required number is 232.
2
10
import java.io.*; import java.math.BigInteger; import java.util.*; import java.text.*; public class cf401D { static BufferedReader br; static Scanner sc; static PrintWriter out; public static void initA() { try { br = new BufferedReader(new InputStreamReader(System.in)); sc = new Scanner(System.in); out = new PrintWriter(System.out); } catch (Exception e) { } } static boolean next_permutation(Integer[] p) { for (int a = p.length - 2; a >= 0; --a) { if (p[a] < p[a + 1]) { for (int b = p.length - 1;; --b) { if (p[b] > p[a]) { int t = p[a]; p[a] = p[b]; p[b] = t; for (++a, b = p.length - 1; a < b; ++a, --b) { t = p[a]; p[a] = p[b]; p[b] = t; } return true; } } } } return false; } public static String getString() { try { return br.readLine(); } catch (Exception e) { } return ""; } public static Integer getInt() { try { return Integer.parseInt(br.readLine()); } catch (Exception e) { } return 0; } public static Integer[] getIntArr() { try { StringTokenizer temp = new StringTokenizer(br.readLine()); int n = temp.countTokens(); Integer temp2[] = new Integer[n]; for (int i = 0; i < n; i++) { temp2[i] = Integer.parseInt(temp.nextToken()); } return temp2; } catch (Exception e) { } return null; } public static Long[] getLongArr() { try { StringTokenizer temp = new StringTokenizer(br.readLine()); int n = temp.countTokens(); Long temp2[] = new Long[n]; for (int i = 0; i < n; i++) { temp2[i] = Long.parseLong(temp.nextToken()); } return temp2; } catch (Exception e) { } return null; } public static String[] getStringArr() { try { StringTokenizer temp = new StringTokenizer(br.readLine()); int n = temp.countTokens(); String temp2[] = new String[n]; for (int i = 0; i < n; i++) { temp2[i] = (temp.nextToken()); } return temp2; } catch (Exception e) { } return null; } public static void print(Object a) { System.out.println(a); } public static void print(String s, Object... a) { System.out.printf(s, a); } public static int nextInt() { return sc.nextInt(); } public static double nextDouble() { return sc.nextDouble(); } public static void main(String[] ar) { initA(); cf401D c = new cf401D(); c.solve(); out.flush(); } int inp[] = new int[25]; int n; short moder; void solve() { String in[] = getStringArr(); char cb[] = in[0].toCharArray(); n = cb.length; int ct[] = new int[10]; for (int i = 0; i < n; i++) { inp[i] = cb[i] - '0'; ct[inp[i]]++; } moder = Short.valueOf(in[1]); //print(100 * 100 * (1<<18) * 20); int end = (1 << (n)); //print(end+" "+Integer.toString(end,2)); long bef[][] = new long[end][100]; bef[0][0] = 1; int bit[] = new int[end]; for (int i = 0; i < end; i++) { int num = 0; int tv = i; while(tv > 0){ if(tv%2==1){ num++; } tv/=2; } bit[i] = num; } for (int i = 1; i <= n; i++) { for (int k = 0; k < end; k++) { if(bit[k]!=i-1)continue; for (int j = 0; j < moder; j++) { if (bef[k][j] > 0) { for (int l = 0; l < n; l++) { if ((k & (1 << l)) == 0) { if (i == 1 && inp[l] == 0) { continue; } int nv = (j * 10 + inp[l]) % moder; int nb = k | (1 << l); //print("SEBELUM ERROR" + nv+" "+nb); bef[nb][nv] += bef[k][j]; //print(nv + " " + Integer.toString(nb, 2) + " = " + bef[nb][nv]); } } } } } } long ou = bef[end - 1][0]; //print(ou); for (int i = 0; i < 10; i++) { ou /= fact(ct[i]); } print(ou); } long fact(int n) { if (n <= 1) { return 1; } return fact(n - 1) * n; } }
JAVA
401_D. Roman and Numbers
Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m. Number x is considered close to number n modulo m, if: * it can be obtained by rearranging the digits of number n, * it doesn't have any leading zeroes, * the remainder after dividing number x by m equals 0. Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him. Input The first line contains two integers: n (1 ≀ n < 1018) and m (1 ≀ m ≀ 100). Output In a single line print a single integer β€” the number of numbers close to number n modulo m. Examples Input 104 2 Output 3 Input 223 4 Output 1 Input 7067678 8 Output 47 Note In the first sample the required numbers are: 104, 140, 410. In the second sample the required number is 232.
2
10
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.math.BigInteger; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.HashSet; import java.util.PriorityQueue; import java.util.Scanner; import java.util.concurrent.PriorityBlockingQueue; public class aa { public static class node implements Comparable<node> { int x; int y; node(int x, int y) { this.x = x; this.y = y; } @Override public int compareTo(node o) { if (o.x < x) return 1; else if (o.x > x) return -1; else if (o.y < y) return 1; else return -1; } } public static int mod; public static String num; public static long dp[][]; public static int a[]; public static int n; public static long getFact() { int a[] = new int[10]; for (int i = 0; i < num.length(); i++) { a[num.charAt(i) - 48]++; } long fact = 1; for (int i = 0; i < 10; i++) { if (a[i] != 0) fact *= fact(a[i]); } return fact; } public static long fact(int n) { long ans = 1; for (int i = 1; i <= n; i++) { ans *= i; } return ans; } public static long f; public static long solve(int mask, int number, int count, int last) { if (mask == (1 << n) - 1) { if (number == 0) return 1; return 0; } if (dp[mask][number] != -1) return dp[mask][number]; long a1 = 0; for (int i = 0; i < n; i++) { // if (a[i] != last) { if (count != 0 || a[i] != 0) { if(i>0&&(mask&(1<<(i-1)))==0&&a[i-1]==a[i]) continue; if ((mask & (1 << i)) == 0) { a1 += solve(mask | (1 << i), (number * 10 + a[i]) % mod, count + 1, a[i]); } } // } } dp[mask][number] = a1; return a1; } public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out)); StringBuilder q = new StringBuilder(); String y[] = in.readLine().trim().split(" "); num = y[0]; mod = Integer.parseInt(y[1]); n = num.length(); a = new int[n]; for (int i = 0; i < n; i++) { a[i] = num.charAt(i) - 48; } Arrays.sort(a); dp = new long[1 << n + 1][mod]; for (int i = 0; i < (1 << n + 1); i++) { for (int j = 0; j < mod; j++) { dp[i][j] = -1; } } f = getFact(); // System.out.println(t); // System.out.println(f); out.println((long) solve(0, 0, 0, 0)); out.print(q); out.close(); } }
JAVA
401_D. Roman and Numbers
Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m. Number x is considered close to number n modulo m, if: * it can be obtained by rearranging the digits of number n, * it doesn't have any leading zeroes, * the remainder after dividing number x by m equals 0. Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him. Input The first line contains two integers: n (1 ≀ n < 1018) and m (1 ≀ m ≀ 100). Output In a single line print a single integer β€” the number of numbers close to number n modulo m. Examples Input 104 2 Output 3 Input 223 4 Output 1 Input 7067678 8 Output 47 Note In the first sample the required numbers are: 104, 140, 410. In the second sample the required number is 232.
2
10
#include <bits/stdc++.h> using namespace std; long long int i, j, n, m, ans, freq[20], dp[1 << 18][120], limit, mask, temp, d, fact[100]; vector<long long int> v; int main() { cin >> n >> m; temp = n; while (temp) { d = temp % 10; freq[d]++; temp = temp / 10; v.push_back(d); } fact[0] = 1; for (i = 1; i <= 19; i++) fact[i] = i * fact[i - 1]; dp[0][0] = 1; limit = 1 << v.size(); for (mask = 0; mask < limit; mask++) { for (i = 0; i < v.size(); i++) { for (j = 0; j < m; j++) { if (mask & (1 << i)) continue; if (mask == 0 && v[i] == 0) continue; dp[mask | (1 << i)][(j * 10 + v[i]) % m] += dp[mask][j]; } } } ans = dp[limit - 1][0]; for (i = 0; i < 10; i++) ans = ans / fact[freq[i]]; cout << ans; return 0; }
CPP
401_D. Roman and Numbers
Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m. Number x is considered close to number n modulo m, if: * it can be obtained by rearranging the digits of number n, * it doesn't have any leading zeroes, * the remainder after dividing number x by m equals 0. Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him. Input The first line contains two integers: n (1 ≀ n < 1018) and m (1 ≀ m ≀ 100). Output In a single line print a single integer β€” the number of numbers close to number n modulo m. Examples Input 104 2 Output 3 Input 223 4 Output 1 Input 7067678 8 Output 47 Note In the first sample the required numbers are: 104, 140, 410. In the second sample the required number is 232.
2
10
import java.io.Reader; import java.util.InputMismatchException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.BufferedReader; import java.io.OutputStream; import java.io.PrintWriter; import java.io.Writer; import java.io.IOException; /** * Built using CHelper plug-in * Actual solution is at the top * @author Niyaz Nigmatullin */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; FastScanner in = new FastScanner(inputStream); FastPrinter out = new FastPrinter(outputStream); TaskD solver = new TaskD(); solver.solve(1, in, out); out.close(); } } class TaskD { static int[] count; static int code(int[] a) { int ret = 0; for (int i = 0; i < 10; i++) { ret = ret * (count[i] + 1) + a[i]; } return ret; } static void decode(int x, int[] a) { for (int i = 9; i >= 0; i--) { a[i] = x % (count[i] + 1); x /= count[i] + 1; } } public void solve(int testNumber, FastScanner in, FastPrinter out) { String s = in.next(); int m = in.nextInt(); count = new int[10]; for (int i = 0; i < s.length(); i++) { count[s.charAt(i) - '0']++; } int[] cur = new int[10]; int all = 1; for (int i : count) all *= i + 1; long[][] dp = new long[m][all]; dp[0][all - 1] = 1; for (int state = all - 1; state > 0; state--) { decode(state, cur); for (int mod = 0; mod < m; mod++) { long val = dp[mod][state]; if (val == 0) continue; for (int d = 0; d < 10; d++) { if (state + 1 == all && d == 0 || cur[d] == 0) continue; --cur[d]; int nState = code(cur); int nMod = (mod * 10 + d) % m; dp[nMod][nState] += val; ++cur[d]; } } } out.println(dp[0][0]); } } class FastScanner extends BufferedReader { public FastScanner(InputStream is) { super(new InputStreamReader(is)); } public int read() { try { int ret = super.read(); // if (isEOF && ret < 0) { // throw new InputMismatchException(); // } // isEOF = ret == -1; return ret; } catch (IOException e) { throw new InputMismatchException(); } } public String next() { StringBuilder sb = new StringBuilder(); int c = read(); while (isWhiteSpace(c)) { c = read(); } if (c < 0) { return null; } while (c >= 0 && !isWhiteSpace(c)) { sb.appendCodePoint(c); c = read(); } return sb.toString(); } static boolean isWhiteSpace(int c) { return c >= 0 && c <= 32; } public int nextInt() { int c = read(); while (isWhiteSpace(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int ret = 0; while (c >= 0 && !isWhiteSpace(c)) { if (c < '0' || c > '9') { throw new NumberFormatException("digit expected " + (char) c + " found"); } ret = ret * 10 + c - '0'; c = read(); } return ret * sgn; } public String readLine() { try { return super.readLine(); } catch (IOException e) { return null; } } } class FastPrinter extends PrintWriter { public FastPrinter(OutputStream out) { super(out); } }
JAVA
401_D. Roman and Numbers
Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m. Number x is considered close to number n modulo m, if: * it can be obtained by rearranging the digits of number n, * it doesn't have any leading zeroes, * the remainder after dividing number x by m equals 0. Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him. Input The first line contains two integers: n (1 ≀ n < 1018) and m (1 ≀ m ≀ 100). Output In a single line print a single integer β€” the number of numbers close to number n modulo m. Examples Input 104 2 Output 3 Input 223 4 Output 1 Input 7067678 8 Output 47 Note In the first sample the required numbers are: 104, 140, 410. In the second sample the required number is 232.
2
10
import java.io.*; import java.util.*; public class Solution{ InputStream is; static PrintWriter out; String INPUT = ""; static long mod = (long)1e9+7L; long[][] dp; long[] pow10; char[] a; int N; public void solve(){ pow10 = new long[19]; pow10[0] = 1; for(int i = 1; i < 19; i++){ pow10[i] = pow10[i-1]*10; } a = ns().toCharArray(); int m = ni(), n = a.length; N =n; int mask = (1<<n)-1; dp = new long[m][mask+1]; for(int i = 0; i < m; i++)Arrays.fill(dp[i], -1); dp[0][mask] = 0; long[] ans = new long[10]; for(int i = 0; i < n; i++){ if(isSet(mask, i) && a[i] != '0' && ans[a[i]-'0'] == 0){ //out.println(i); long tmp = (pow10[n-1]*(long)(a[i]-'0'))%(long)m; long res = f(i(tmp), reset(mask, i), n-1, m); ans[a[i] - '0'] = 1; dp[0][mask] += res; //out.println("res ="+ dp[0][mask]); } } //out.println(dp[0][1]); //out.println(dp[0][2]); out.println(dp[0][mask]); } long f(int m, int mask, int n, int M){ //out.println("state "+m+" "+mask+" "+n); if(dp[m][mask] != -1)return dp[m][mask]; if(n == 0 && (mask+m)%M == 0)return dp[m][mask] = 1; dp[m][mask] = 0; int[] ans = new int[10]; for(int i = 0; i < N; i++){ if(isSet(mask, i) && ans[a[i] - '0'] == 0){ long tmp = (pow10[n-1]*(long)(a[i]-'0'))%(long)M; //out.println("hj "+mask+" "+i+" "+tmp+" "+m); tmp = (tmp + m)%M; dp[m][mask] += f(i(tmp), reset(mask, i), n-1, M); ans[a[i]-'0'] = 1; //out.println(m+" "+mask+" "+dp[m][mask]); } } return dp[m][mask]; } int reset(int num, int idx){ if(!isSet(num, idx))return num; int m = 1<<(N-idx-1); return (num ^ m); } boolean isSet(int num, int idx){ int m = 1<<(N-idx-1); return (num|m) == num; } void run(){ is = new DataInputStream(System.in); out = new PrintWriter(System.out); int t=1;while(t-->0)solve(); out.flush(); } public static void main(String[] args)throws Exception{new Solution().run();} long mod(long v, long m){if(v<0){long q=(Math.abs(v)+m-1L)/m;v=v+q*m;}return v%m;} long mod(long v){if(v<0){long q=(Math.abs(v)+mod-1L)/mod;v=v+q*mod;}return v%mod;} //Fast I/O code is copied from uwi code. private byte[] inbuf = new byte[1024]; public int lenbuf = 0, ptrbuf = 0; private int readByte(){ if(lenbuf == -1)throw new InputMismatchException(); if(ptrbuf >= lenbuf){ ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if(lenbuf <= 0)return -1; } return inbuf[ptrbuf++]; } private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; } private double nd() { return Double.parseDouble(ns()); } private char nc() { return (char)skip(); } private String ns(){ int b = skip(); StringBuilder sb = new StringBuilder(); while(!(isSpaceChar(b))){ // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } private char[] ns(int n){ char[] buf = new char[n]; int b = skip(), p = 0; while(p < n && !(isSpaceChar(b))){ buf[p++] = (char)b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } private char[][] nm(int n, int m){ char[][] map = new char[n][]; for(int i = 0;i < n;i++)map[i] = ns(m); return map; } private int[] na(int n){ int[] a = new int[n]; for(int i = 0;i < n;i++)a[i] = ni(); return a; } private int ni(){ int num = 0, b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } private long nl(){ long num = 0; int b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } static int i(long x){return (int)Math.round(x);} static class Pair implements Comparable<Pair>{ long fs,sc; Pair(long a,long b){ fs=a;sc=b; } public int compareTo(Pair p){ if(this.fs>p.fs)return 1; else if(this.fs<p.fs)return -1; else{ return i(this.sc-p.sc); } //return i(this.sc-p.sc); } public String toString(){ return "("+fs+","+sc+")"; } } }
JAVA
401_D. Roman and Numbers
Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m. Number x is considered close to number n modulo m, if: * it can be obtained by rearranging the digits of number n, * it doesn't have any leading zeroes, * the remainder after dividing number x by m equals 0. Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him. Input The first line contains two integers: n (1 ≀ n < 1018) and m (1 ≀ m ≀ 100). Output In a single line print a single integer β€” the number of numbers close to number n modulo m. Examples Input 104 2 Output 3 Input 223 4 Output 1 Input 7067678 8 Output 47 Note In the first sample the required numbers are: 104, 140, 410. In the second sample the required number is 232.
2
10
#include <bits/stdc++.h> using namespace std; string num; int m, n; long long dp[1 << 18][100]; int cnt[10], fact[20]; long long solve(int cnt, int mask, int rem) { if (cnt == n) { if (rem == 0) return 1; return 0; } long long &ret = dp[mask][rem]; if (ret != -1) return ret; ret = 0; int vis[10]; memset(vis, 0, sizeof vis); for (int i = 0; i < n; i++) { if (num[i] == '0' && cnt == 0) continue; if ((mask & 1 << i) == 0 && !vis[num[i] - '0']) { ret += solve(cnt + 1, mask | (1 << i), (rem * 10 + num[i] - '0') % m); vis[num[i] - '0'] = 1; } } return ret; } int main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); ; cin >> num >> m; n = (int)num.size(); for (int i = 0; i < n; i++) cnt[num[i] - '0']++; fact[0] = 1; for (int i = 1; i <= n; i++) fact[i] = fact[i - 1] * i; memset(dp, -1, sizeof dp); long long ret = solve(0, 0, 0); cout << ret << '\n'; }
CPP
401_D. Roman and Numbers
Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m. Number x is considered close to number n modulo m, if: * it can be obtained by rearranging the digits of number n, * it doesn't have any leading zeroes, * the remainder after dividing number x by m equals 0. Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him. Input The first line contains two integers: n (1 ≀ n < 1018) and m (1 ≀ m ≀ 100). Output In a single line print a single integer β€” the number of numbers close to number n modulo m. Examples Input 104 2 Output 3 Input 223 4 Output 1 Input 7067678 8 Output 47 Note In the first sample the required numbers are: 104, 140, 410. In the second sample the required number is 232.
2
10
#include <bits/stdc++.h> using namespace std; inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v; } template <class T> inline string toString(T x) { ostringstream sout; sout << x; return sout.str(); } template <class T> inline T sqr(T x) { return x * x; } const double EPS = 1e-10; const double PI = acos(-1.0); const int dx[] = {-1, 1, 0, 0}; const int dy[] = {0, 0, 1, -1}; long long n, ret; int mod, d; long long dp[(1 << 18) + 10][110]; vector<int> v; long long f[20]; int c[10]; int main() { cin >> n >> mod; f[0] = 1; for (int i = (0); i < (18); ++i) f[i + 1] = f[i] * (i + 1); while (n) { ++c[n % 10]; v.push_back(n % 10); n /= 10; ++d; } for (int i = (0); i < (d); ++i) if (v[i] != 0) dp[1 << i][v[i] % mod] = 1; for (int i = 1; i < (1 << d); ++i) { int mask = i; for (int j = (0); j < (d); ++j) { if ((mask >> j) & 1) continue; for (int k = (0); k < (mod); ++k) { dp[mask | (1 << j)][(k * 10 + v[j]) % mod] += dp[mask][k]; } } } ret = dp[(1 << d) - 1][0]; for (int i = (0); i < (10); ++i) ret /= f[c[i]]; cout << ret << endl; return 0; }
CPP
401_D. Roman and Numbers
Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m. Number x is considered close to number n modulo m, if: * it can be obtained by rearranging the digits of number n, * it doesn't have any leading zeroes, * the remainder after dividing number x by m equals 0. Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him. Input The first line contains two integers: n (1 ≀ n < 1018) and m (1 ≀ m ≀ 100). Output In a single line print a single integer β€” the number of numbers close to number n modulo m. Examples Input 104 2 Output 3 Input 223 4 Output 1 Input 7067678 8 Output 47 Note In the first sample the required numbers are: 104, 140, 410. In the second sample the required number is 232.
2
10
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); string s; int m; cin >> s >> m; int n = s.size(); int N = 1 << n; long long int dp[N][m]; memset(dp, 0, sizeof(dp)); dp[0][0] = 1; for (int i = 0; i < N; ++i) { for (int j = 0; j < m; ++j) { if (dp[i][j]) { for (int k = 0; k < n; ++k) { if (!(i & (1 << k))) { if (i || (s[k] - '0')) { dp[i | (1 << k)][(j * 10 + (s[k] - '0')) % m] += dp[i][j]; } } } } } } long long int ans = dp[N - 1][0]; int arr[10] = {0}; for (int i = 0; i < n; ++i) { arr[s[i] - '0']++; } long long int facts[19]; facts[0] = 1; for (int i = 1; i < 19; ++i) { facts[i] = i * facts[i - 1]; } for (int i = 0; i < 10; ++i) { ans /= facts[arr[i]]; } cout << ans << '\n'; }
CPP
401_D. Roman and Numbers
Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m. Number x is considered close to number n modulo m, if: * it can be obtained by rearranging the digits of number n, * it doesn't have any leading zeroes, * the remainder after dividing number x by m equals 0. Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him. Input The first line contains two integers: n (1 ≀ n < 1018) and m (1 ≀ m ≀ 100). Output In a single line print a single integer β€” the number of numbers close to number n modulo m. Examples Input 104 2 Output 3 Input 223 4 Output 1 Input 7067678 8 Output 47 Note In the first sample the required numbers are: 104, 140, 410. In the second sample the required number is 232.
2
10
import java.util.Scanner; import java.util.Arrays; public class D{ public static void main(String[] args){ Scanner in = new Scanner(System.in); long n = in.nextLong(); int mod = in.nextInt(); long base[] = new long[18]; int nLength = 0; //convert to base 10 while(n > 0){ base[nLength++] = n % 10L; n /= 10L; } Arrays.sort(base,0,nLength); //bitmask of each digit int numOfMask[] = new int[10]; for(int i = 0 ; i < nLength ; i++) numOfMask[(int)(base[i])] |= (1 << i); long dp[][] = new long[1 << nLength][mod]; for(int i = 0 ; i < (1 << nLength) ; i++) for(int j = 0 ; j < mod ; j++) dp[i][j] = -1; long ans = solve(true,(1 << nLength) - 1,0,mod,numOfMask,dp); System.out.println(ans); } private static long solve(boolean firstDigit,int mask,int modulo,int mod,int numOfMask[],long dp[][]){ if(mask == 0){ if(modulo == 0) return 1; return 0; } long res = dp[mask][modulo]; if(res != -1) return res; res = 0; //place new digit in the end for(int i = 0 ; i < 10 ; i++){ int cross = mask & numOfMask[i]; if((i > 0 || !firstDigit) && cross > 0){ int LSB = cross & -cross; res += solve(false,mask ^ LSB,(10 * modulo + i) % mod,mod,numOfMask,dp); } } dp[mask][modulo] = res; return res; } }
JAVA
401_D. Roman and Numbers
Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m. Number x is considered close to number n modulo m, if: * it can be obtained by rearranging the digits of number n, * it doesn't have any leading zeroes, * the remainder after dividing number x by m equals 0. Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him. Input The first line contains two integers: n (1 ≀ n < 1018) and m (1 ≀ m ≀ 100). Output In a single line print a single integer β€” the number of numbers close to number n modulo m. Examples Input 104 2 Output 3 Input 223 4 Output 1 Input 7067678 8 Output 47 Note In the first sample the required numbers are: 104, 140, 410. In the second sample the required number is 232.
2
10
#include <bits/stdc++.h> using namespace std; map<pair<vector<int>, int>, long long> ma; long long dp[(1 << 18)][101]; int main() { srand((unsigned int)time(NULL)); long long n, m; int dig[20], id = 0, ten[20], cnt[10] = {}; ten[0] = 1; cin >> n >> m; for (int i = 1; i <= 19; i++) ten[i] = (ten[i - 1] * 10) % m; while (n) { cnt[n % 10]++; dig[id++] = n % 10; n /= 10; } dp[0][0] = 1LL; for (int mask = 0; mask < (1 << id); mask++) { for (int i = 0; i < id; i++) { if (!mask && !dig[i]) continue; if (((mask >> i) & 1)) continue; for (int rem = 0; rem < m; rem++) { if (!dp[mask][rem]) continue; dp[mask ^ (1 << i)][(rem * 10 + dig[i]) % m] += dp[mask][rem]; } } } for (int i = 0; i <= 9; i++) for (int j = 1; j <= cnt[i]; j++) dp[(1 << id) - 1][0] /= j; cout << dp[(1 << id) - 1][0] << endl; }
CPP
401_D. Roman and Numbers
Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m. Number x is considered close to number n modulo m, if: * it can be obtained by rearranging the digits of number n, * it doesn't have any leading zeroes, * the remainder after dividing number x by m equals 0. Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him. Input The first line contains two integers: n (1 ≀ n < 1018) and m (1 ≀ m ≀ 100). Output In a single line print a single integer β€” the number of numbers close to number n modulo m. Examples Input 104 2 Output 3 Input 223 4 Output 1 Input 7067678 8 Output 47 Note In the first sample the required numbers are: 104, 140, 410. In the second sample the required number is 232.
2
10
//package round235; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.InputMismatchException; public class D { InputStream is; PrintWriter out; String INPUT = ""; void solve() { long n = nl(); int m = ni(); int[] ds = new int[Long.toString(n).length()]; int p = 0; for(char c : Long.toString(n).toCharArray()){ ds[p++] = c-'0'; } Arrays.sort(ds); long[][] dp = new long[1<<p][m]; dp[0][0] = 1; for(int i = 0;i < 1<<p;i++){ for(int j = 0;j < m;j++){ if(dp[i][j] == 0)continue; for(int k = 0;k < p;k++){ if(i<<31-k>=0){ if(i == 0 && ds[k] == 0)continue; if(k > 0 && ds[k] == ds[k-1] && i<<31-(k-1)>=0)continue; dp[i|1<<k][(j*10+ds[k])%m] += dp[i][j]; } } } } out.println(dp[(1<<p)-1][0]); } void run() throws Exception { is = oj ? System.in : new ByteArrayInputStream(INPUT.getBytes()); out = new PrintWriter(System.out); long s = System.currentTimeMillis(); solve(); out.flush(); tr(System.currentTimeMillis()-s+"ms"); } public static void main(String[] args) throws Exception { new D().run(); } private byte[] inbuf = new byte[1024]; private int lenbuf = 0, ptrbuf = 0; private int readByte() { if(lenbuf == -1)throw new InputMismatchException(); if(ptrbuf >= lenbuf){ ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if(lenbuf <= 0)return -1; } return inbuf[ptrbuf++]; } private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; } private double nd() { return Double.parseDouble(ns()); } private char nc() { return (char)skip(); } private String ns() { int b = skip(); StringBuilder sb = new StringBuilder(); while(!(isSpaceChar(b))){ // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } private char[] ns(int n) { char[] buf = new char[n]; int b = skip(), p = 0; while(p < n && !(isSpaceChar(b))){ buf[p++] = (char)b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } private char[][] nm(int n, int m) { char[][] map = new char[n][]; for(int i = 0;i < n;i++)map[i] = ns(m); return map; } private int[] na(int n) { int[] a = new int[n]; for(int i = 0;i < n;i++)a[i] = ni(); return a; } private int ni() { int num = 0, b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } private long nl() { long num = 0; int b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } private boolean oj = System.getProperty("ONLINE_JUDGE") != null; private void tr(Object... o) { if(!oj)System.out.println(Arrays.deepToString(o)); } }
JAVA
401_D. Roman and Numbers
Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m. Number x is considered close to number n modulo m, if: * it can be obtained by rearranging the digits of number n, * it doesn't have any leading zeroes, * the remainder after dividing number x by m equals 0. Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him. Input The first line contains two integers: n (1 ≀ n < 1018) and m (1 ≀ m ≀ 100). Output In a single line print a single integer β€” the number of numbers close to number n modulo m. Examples Input 104 2 Output 3 Input 223 4 Output 1 Input 7067678 8 Output 47 Note In the first sample the required numbers are: 104, 140, 410. In the second sample the required number is 232.
2
10
#include <bits/stdc++.h> using namespace std; int l; int mo; string n; long long dp[1 << 18][100]; long long coun[10]; int fastmod[10004]; void pre() { for (int i = (1); i <= (10004 - 1); ++i) { fastmod[i] = i % mo; } } long long solve(int mask, int m) { if (mask == (1 << l) - 1) return m == 0; if (dp[mask][m] != -1) return dp[mask][m]; long long ans = 0; for (int j = (0); j <= (l - 1); ++j) { if ((mask & (1 << j))) continue; ans += solve(mask | (1 << j), fastmod[m * 10 + (n[j] - '0')]); } return dp[mask][m] = ans; } int main() { cin >> n >> mo; l = n.size(); pre(); memset(dp, -1, sizeof(dp)); long long int i, j, ans = 0; for (i = 0; i < l; i++) { coun[n[i] - '0']++; if (n[i] - '0' > 0) ans += solve(1 << i, (n[i] - '0') % mo); } for (i = 0; i < 10; i++) { for (j = 1; j <= coun[i]; j++) ans /= j; } cout << ans; return 0; }
CPP
401_D. Roman and Numbers
Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m. Number x is considered close to number n modulo m, if: * it can be obtained by rearranging the digits of number n, * it doesn't have any leading zeroes, * the remainder after dividing number x by m equals 0. Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him. Input The first line contains two integers: n (1 ≀ n < 1018) and m (1 ≀ m ≀ 100). Output In a single line print a single integer β€” the number of numbers close to number n modulo m. Examples Input 104 2 Output 3 Input 223 4 Output 1 Input 7067678 8 Output 47 Note In the first sample the required numbers are: 104, 140, 410. In the second sample the required number is 232.
2
10
#include <bits/stdc++.h> using namespace std; int num[25], cnt; long long dp[(1 << 18)][100]; long long fac[30], bcnt[10]; int main(void) { long long n, m; scanf("%lld%lld", &n, &m); for (; n; num[++cnt] = n % 10, n /= 10) ; dp[0][0] = 1; for (register int i = 0; i < (1 << cnt); ++i) for (register int j = 0; j < m; ++j) for (register int k = 0; k < cnt; ++k) { if (i & (1 << k)) continue; if (!i && !num[k + 1]) continue; dp[i | (1 << k)][(j * 10 + num[k + 1]) % m] += dp[i][j]; } fac[0] = 1; for (register int i = 1; i <= 20; ++i) fac[i] = fac[i - 1] * i; for (register int i = 1; i <= cnt; ++i) ++bcnt[num[i]]; long long ans = dp[(1 << cnt) - 1][0]; for (register int i = 0; i <= 9; ++i) ans /= fac[bcnt[i]]; printf("%lld\n", ans); return 0; }
CPP
401_D. Roman and Numbers
Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m. Number x is considered close to number n modulo m, if: * it can be obtained by rearranging the digits of number n, * it doesn't have any leading zeroes, * the remainder after dividing number x by m equals 0. Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him. Input The first line contains two integers: n (1 ≀ n < 1018) and m (1 ≀ m ≀ 100). Output In a single line print a single integer β€” the number of numbers close to number n modulo m. Examples Input 104 2 Output 3 Input 223 4 Output 1 Input 7067678 8 Output 47 Note In the first sample the required numbers are: 104, 140, 410. In the second sample the required number is 232.
2
10
import java.io.BufferedReader; import java.io.OutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; FastScanner in = new FastScanner(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskD solver = new TaskD(); solver.solve(1, in, out); out.close(); } } class TaskD { public void solve(int testNumber, FastScanner in, PrintWriter out) { long n = in.nextLong(); int m = in.nextInt(); int[] mods = new int[18]; mods[0] = 1; for (int i = 1; i < mods.length; i++) mods[i] = mods[i - 1] * 10 % m; String val = Long.toString(n); int cnt = val.length(); int[] digits = new int[cnt]; for (int i = 0; i < cnt; i++) digits[i] = val.charAt(i) - '0'; long[][] dp = new long[1 << cnt][m]; dp[0][0] = 1; for (int mask = 0; mask < (1 << cnt) - 1; mask++) { int pos = Integer.bitCount(mask); for (int curMod = 0; curMod < m; curMod++) { if (dp[mask][curMod] == 0) continue; for (int newPos = 0; newPos < cnt; newPos++) { if ((mask & (1 << newPos)) != 0) continue; if (digits[pos] == 0 && newPos == cnt - 1) continue; dp[mask | (1 << newPos)][(curMod + mods[newPos] * digits[pos]) % m] += dp[mask][curMod]; } } } long[] fact = new long[20]; fact[0] = 1; for (int i = 1; i < 20; i++) fact[i] = fact[i - 1] * i; int[] count = new int[10]; for (int d : digits) count[d]++; long res = dp[(1 << cnt) - 1][0]; for (int d = 0; d < 10; d++) res /= fact[count[d]]; out.print(res); } } class FastScanner { BufferedReader reader; StringTokenizer tokenizer; public FastScanner(InputStream inputStream) { reader = new BufferedReader(new InputStreamReader(inputStream)); } public String nextToken() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { String line; try { line = reader.readLine(); } catch (IOException e) { return null; } tokenizer = new StringTokenizer(line); } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(nextToken()); } public long nextLong() { return Long.parseLong(nextToken()); } }
JAVA
401_D. Roman and Numbers
Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m. Number x is considered close to number n modulo m, if: * it can be obtained by rearranging the digits of number n, * it doesn't have any leading zeroes, * the remainder after dividing number x by m equals 0. Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him. Input The first line contains two integers: n (1 ≀ n < 1018) and m (1 ≀ m ≀ 100). Output In a single line print a single integer β€” the number of numbers close to number n modulo m. Examples Input 104 2 Output 3 Input 223 4 Output 1 Input 7067678 8 Output 47 Note In the first sample the required numbers are: 104, 140, 410. In the second sample the required number is 232.
2
10
#include <bits/stdc++.h> long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } int bmod(int a, int b, int m) { if (b == 0) return 1; int x = bmod(a, b / 2, m); x = (x * x) % m; if (b % 2 == 1) x = (x * a) % m; return x; } long long powr(long long n, long long m) { long long ans = 1; for (long long i = 1; i <= m; i++) ans *= n; return ans; } bool prime(long long n) { for (long long i = 2; i * i <= n; i++) { if (n % i == 0) return false; } return true; } int Setbit(long long N, long long pos) { return N = N | (1 << pos); } int resetbit(long long N, long long pos) { return N = N & ~(1 << pos); } bool checkbit(long long N, long long pos) { return (bool)(N & (1 << pos)); } void countDivisor(long long num) { for (long long i = 1; i * i <= num; i++) { if (num % i == 0) { } } } long long fx[8] = {0, +0, -1, +1, +1, -1, -1, +1}; long long fy[8] = {1, -1, +0, +0, +1, +1, -1, -1}; using namespace std; long long base, k, len; long long arr[100]; long long freq[100]; long long dp[(1 << 18) + 5][101]; long long solve(long long mask, long long rem) { if (mask == ((1 << len) - 1)) { if (rem) return 0; else return 1; } if (dp[mask][rem] != -1) return dp[mask][rem]; long long sum = 0; long long used[12]; memset(used, 0, sizeof(used)); for (long long i = 0; i < len; i++) { if ((mask & (1 << i)) == 0 && !used[arr[i]]) { if (mask == 0 && arr[i] == 0) continue; used[arr[i]] = 1; sum += solve((mask | (1 << i)), ((rem * 10 + arr[i]) % k)); } } return dp[mask][rem] = sum; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; string s; cin >> s >> k; len = s.size(); memset(dp, -1, sizeof(dp)); long long same = 1; for (long long i = 0; i < len; i++) { arr[i] = s[i] - '0'; } long long ans = solve(0, 0); cout << ans << endl; }
CPP
401_D. Roman and Numbers
Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m. Number x is considered close to number n modulo m, if: * it can be obtained by rearranging the digits of number n, * it doesn't have any leading zeroes, * the remainder after dividing number x by m equals 0. Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him. Input The first line contains two integers: n (1 ≀ n < 1018) and m (1 ≀ m ≀ 100). Output In a single line print a single integer β€” the number of numbers close to number n modulo m. Examples Input 104 2 Output 3 Input 223 4 Output 1 Input 7067678 8 Output 47 Note In the first sample the required numbers are: 104, 140, 410. In the second sample the required number is 232.
2
10
import java.io.*; import java.util.*; /* * Heart beats fast * Colors and promises * How to be brave * How can I love when I am afraid... */ public class Main { public static void main(String[]args) throws Exception { long n=nl(); int m=ni(); int te=0; int lol[]=new int[64]; long lolo=n; for(int i=0; lolo>0; i++,lolo/=10) { lol[i]=(int)(lolo%10); te++; } long[][]dp=new long[1<<te][m]; dp[0][0]=1; for(int i=0; i<1<<te; i++) for(int j=0; j<te; j++) if(((i>>j)&1)==0) if(i!=0||lol[j]!=0) for(int k=0; k<m; k++) dp[i+(1<<j)][(k*10+lol[j])%m]+=dp[i][k]; int[]co=new int[10]; for(int i=0; i<te; i++) co[lol[i]]++; for(int i=0; i<10; i++) while(co[i]!=0) dp[(1<<te)-1][0]/=co[i]--; pr(dp[(1<<te)-1][0]); System.out.println(output); } /////////////////////////////////////////// /////////////////////////////////////////// ///template from here static final int mod=1000_000_007; static final double eps=1e-9; static final long inf=100000000000000000L; static class pair { int a,b; pair(){} pair(int c,int d){a=c;b=d;} @Override public int hashCode() { return (a+" "+b).hashCode(); } public boolean equals(Object c) { return (a==(((pair)c).a)&&b==(((pair)c).b)); } } public static void sort(int[][]a) { Arrays.sort(a, new Comparator<int[]>() { public int compare(int[]a,int[]b) { if(a[0]>b[0]) return 1; if(a[0]<b[0]) return -1; return 0; } }); } static interface combiner { public int combine(int a, int b); } static void pr(Object a){output.append(a+"\n");} static void pr(){output.append("\n");} static void p(Object a){output.append(a);} static void pra(int[]a){for(int i:a)output.append(i+" ");output.append("\n");} static void pra(long[]a){for(long i:a)output.append(i+" ");output.append("\n");} static void pra(String[]a){for(String i:a)output.append(i+" ");output.append("\n");} static void pra(double[]a){for(double i:a)output.append(i+" ");output.append("\n");} static void sop(Object a){System.out.println(a);} static void flush(){System.out.print(output);output=new StringBuilder();} static int ni(){return Integer.parseInt(in.next());} static long nl(){return Long.parseLong(in.next());} static String ns(){return in.next();} static double nd(){return Double.parseDouble(in.next());} static int[] nia(int n){int a[]=new int[n];for(int i=0; i<n; i++)a[i]=ni();return a;} static int[] pnia(int n){int a[]=new int[n+1];for(int i=1; i<=n; i++)a[i]=ni();return a;} static long[] nla(int n){long a[]=new long[n];for(int i=0; i<n; i++)a[i]=nl();return a;} static String[] nsa(int n){String a[]=new String[n];for(int i=0; i<n; i++)a[i]=ns();return a;} static double[] nda(int n){double a[]=new double[n];for(int i=0; i<n; i++)a[i]=nd();return a;} static Reader in=new Reader(); static StringBuilder output=new StringBuilder(); static Random rn=new Random(); static void reverse(int[]a){for(int i=0; i<a.length/2; i++){a[i]^=a[a.length-i-1];a[a.length-i-1]^=a[i];a[i]^=a[a.length-i-1];}} static int log2n(long a) { int te=0; while(a>0) { a>>=1; ++te; } return te; } static class vectorl implements Iterable<Long> { long a[]; int size; vectorl(){a=new long[10];size=0;} vectorl(int n){a=new long[n];size=0;} public void add(long b){if(++size==a.length)a=Arrays.copyOf(a, 2*size);a[size-1]=b;} public void sort(){Arrays.sort(a, 0, size);} public void sort(int l, int r){Arrays.sort(a, l, r);} @Override public Iterator<Long> iterator() { Iterator<Long> hola=new Iterator<Long>() { int cur=0; @Override public boolean hasNext() { return cur<size; } @Override public Long next() { return a[cur++]; } }; return hola; } } static class vector implements Iterable<Integer> { int a[],size; vector(){a=new int[10];size=0;} vector(int n){a=new int[n];size=0;} public void add(int b){if(++size==a.length)a=Arrays.copyOf(a, 2*size);a[size-1]=b;} public void sort(){Arrays.sort(a, 0, size);} public void sort(int l, int r){Arrays.sort(a, l, r);} @Override public Iterator<Integer> iterator() { Iterator<Integer> hola=new Iterator<Integer>() { int cur=0; @Override public boolean hasNext() { return cur<size; } @Override public Integer next() { return a[cur++]; } }; return hola; } } static void exit(){System.out.print(output);System.exit(0);} static int min(int... a){int min=a[0];for(int i:a)min=Math.min(min, i);return min;} static int max(int... a){int max=a[0];for(int i:a)max=Math.max(max, i);return max;} static int gcd(int... a){int gcd=a[0];for(int i:a)gcd=gcd(gcd, i);return gcd;} static long min(long... a){long min=a[0];for(long i:a)min=Math.min(min, i);return min;} static long max(long... a){long max=a[0];for(long i:a)max=Math.max(max, i);return max;} static long gcd(long... a){long gcd=a[0];for(long i:a)gcd=gcd(gcd, i);return gcd;} static String pr(String a, long b){String c="";while(b>0){if(b%2==1)c=c.concat(a);a=a.concat(a);b>>=1;}return c;} static long powm(long a, long b, long m){long an=1;long c=a;while(b>0){if(b%2==1)an=(an*c)%m;c=(c*c)%m;b>>=1;}return an;} static int gcd(int a, int b){if(b==0)return a;return gcd(b, a%b);} static long gcd(long a, long b){if(b==0)return a;return gcd(b, a%b);} static class Reader{ public BufferedReader reader; public StringTokenizer tokenizer; public Reader() { reader = new BufferedReader(new InputStreamReader(System.in)); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } } }
JAVA
401_D. Roman and Numbers
Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m. Number x is considered close to number n modulo m, if: * it can be obtained by rearranging the digits of number n, * it doesn't have any leading zeroes, * the remainder after dividing number x by m equals 0. Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him. Input The first line contains two integers: n (1 ≀ n < 1018) and m (1 ≀ m ≀ 100). Output In a single line print a single integer β€” the number of numbers close to number n modulo m. Examples Input 104 2 Output 3 Input 223 4 Output 1 Input 7067678 8 Output 47 Note In the first sample the required numbers are: 104, 140, 410. In the second sample the required number is 232.
2
10
import java.io.*; import java.math.BigInteger; import java.util.*; import java.text.*; public class cf401D { static BufferedReader br; static Scanner sc; static PrintWriter out; public static void initA() { try { br = new BufferedReader(new InputStreamReader(System.in)); sc = new Scanner(System.in); out = new PrintWriter(System.out); } catch (Exception e) { } } static boolean next_permutation(Integer[] p) { for (int a = p.length - 2; a >= 0; --a) { if (p[a] < p[a + 1]) { for (int b = p.length - 1;; --b) { if (p[b] > p[a]) { int t = p[a]; p[a] = p[b]; p[b] = t; for (++a, b = p.length - 1; a < b; ++a, --b) { t = p[a]; p[a] = p[b]; p[b] = t; } return true; } } } } return false; } public static String getString() { try { return br.readLine(); } catch (Exception e) { } return ""; } public static Integer getInt() { try { return Integer.parseInt(br.readLine()); } catch (Exception e) { } return 0; } public static Integer[] getIntArr() { try { StringTokenizer temp = new StringTokenizer(br.readLine()); int n = temp.countTokens(); Integer temp2[] = new Integer[n]; for (int i = 0; i < n; i++) { temp2[i] = Integer.parseInt(temp.nextToken()); } return temp2; } catch (Exception e) { } return null; } public static Long[] getLongArr() { try { StringTokenizer temp = new StringTokenizer(br.readLine()); int n = temp.countTokens(); Long temp2[] = new Long[n]; for (int i = 0; i < n; i++) { temp2[i] = Long.parseLong(temp.nextToken()); } return temp2; } catch (Exception e) { } return null; } public static String[] getStringArr() { try { StringTokenizer temp = new StringTokenizer(br.readLine()); int n = temp.countTokens(); String temp2[] = new String[n]; for (int i = 0; i < n; i++) { temp2[i] = (temp.nextToken()); } return temp2; } catch (Exception e) { } return null; } public static void print(Object a) { System.out.println(a); } public static void print(String s, Object... a) { System.out.printf(s, a); } public static int nextInt() { return sc.nextInt(); } public static double nextDouble() { return sc.nextDouble(); } public static void main(String[] ar) { initA(); cf401D c = new cf401D(); c.solve(); out.flush(); } int inp[] = new int[25]; int n; short moder; void solve() { String in[] = getStringArr(); char cb[] = in[0].toCharArray(); n = cb.length; int ct[] = new int[10]; for (int i = 0; i < n; i++) { inp[i] = cb[i] - '0'; ct[inp[i]]++; } moder = Short.valueOf(in[1]); //print(100 * 100 * (1<<18) * 20); int end = (1 << (n)); //print(end+" "+Integer.toString(end,2)); long bef[][] = new long[end][100]; bef[0][0] = 1; ArrayList<Integer> bit[] = new ArrayList[20]; for(int i =0 ;i<20;i++){ bit[i] = new ArrayList<Integer>(); } for (int i = 0; i < end; i++) { int num = 0; int tv = i; while(tv > 0){ if(tv%2==1){ num++; } tv/=2; } bit[num].add(i); } for (int i = 1; i <= n; i++) { for (int ii = 0; ii < bit[i-1].size(); ii++) { int k = bit[i-1].get(ii); //if(bit[k]!=i-1)continue; for (int j = 0; j < moder; j++) { if (bef[k][j] > 0) { for (int l = 0; l < n; l++) { if ((k & (1 << l)) == 0) { if (i == 1 && inp[l] == 0) { continue; } int nv = (j * 10 + inp[l]) % moder; int nb = k | (1 << l); //print("SEBELUM ERROR" + nv+" "+nb); bef[nb][nv] += bef[k][j]; //print(nv + " " + Integer.toString(nb, 2) + " = " + bef[nb][nv]); } } } } } } long ou = bef[end - 1][0]; //print(ou); for (int i = 0; i < 10; i++) { ou /= fact(ct[i]); } print(ou); } long fact(int n) { if (n <= 1) { return 1; } return fact(n - 1) * n; } }
JAVA
401_D. Roman and Numbers
Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m. Number x is considered close to number n modulo m, if: * it can be obtained by rearranging the digits of number n, * it doesn't have any leading zeroes, * the remainder after dividing number x by m equals 0. Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him. Input The first line contains two integers: n (1 ≀ n < 1018) and m (1 ≀ m ≀ 100). Output In a single line print a single integer β€” the number of numbers close to number n modulo m. Examples Input 104 2 Output 3 Input 223 4 Output 1 Input 7067678 8 Output 47 Note In the first sample the required numbers are: 104, 140, 410. In the second sample the required number is 232.
2
10
#include <bits/stdc++.h> using namespace std; template <class T> T abs(T x) { return x > 0 ? x : -x; } long long n; int m; bool used[10]; int bits[1 << 18]; long long base[18]; long long f[1 << 18][100]; int main() { cin >> n >> m; base[0] = 1; for (int i = 1; i < 18; ++i) base[i] = base[i - 1] * 10; bits[0] = 0; for (int i = 1; i < (1 << 18); ++i) bits[i] = bits[i & (i - 1)] + 1; for (int i = 0; i < (1 << 18); ++i) for (int j = 0; j < 100; ++j) f[i][j] = 0; int N = 0; for (long long x = n; x > 0; x /= 10) ++N; f[0][0] = 1; for (int i = 1; i < (1 << N); ++i) { for (int k = 0; k < 10; ++k) used[k] = false; for (int k = 0; k < N; ++k) { if (i & (1 << k)) { int d = n / base[k] % 10; if (used[d]) continue; if (i == (1 << N) - 1 and d == 0) continue; for (int j = 0; j < m; ++j) f[i][(j + d * base[bits[i] - 1]) % m] += f[i ^ (1 << k)][j]; used[d] = true; } } } cout << f[(1 << N) - 1][0] << endl; return 0; }
CPP
401_D. Roman and Numbers
Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m. Number x is considered close to number n modulo m, if: * it can be obtained by rearranging the digits of number n, * it doesn't have any leading zeroes, * the remainder after dividing number x by m equals 0. Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him. Input The first line contains two integers: n (1 ≀ n < 1018) and m (1 ≀ m ≀ 100). Output In a single line print a single integer β€” the number of numbers close to number n modulo m. Examples Input 104 2 Output 3 Input 223 4 Output 1 Input 7067678 8 Output 47 Note In the first sample the required numbers are: 104, 140, 410. In the second sample the required number is 232.
2
10
#include <bits/stdc++.h> using namespace std; long long dp[1 << 18][111]; int cnt[11]; int a[11]; string s; int m; int main() { cin >> s >> m; int l = s.size(); memset(cnt, 0, sizeof(cnt)); memset(a, 0, sizeof(a)); memset(dp, 0, sizeof(dp)); for (int i = 0; i < l; i++) cnt[s[i] - '0']++; for (int(i) = (1); (i) < (11); (i)++) a[i] = a[i - 1] + cnt[i - 1]; for (int(i) = (1); (i) < (10); (i)++) if (cnt[i]) dp[1 << a[i]][i % m] = 1; for (int(i) = (1); (i) < ((1 << l)); (i)++) for (int(j) = (0); (j) < (m); (j)++) if (dp[i][j]) for (int(k) = (0); (k) < (10); (k)++) for (int(p) = (a[k]); (p) < (a[k + 1]); (p)++) { if (i & (1 << p)) continue; dp[i + (1 << p)][(j * 10 + k) % m] += dp[i][j]; break; } cout << dp[(1 << l) - 1][0] << endl; return 0; }
CPP
401_D. Roman and Numbers
Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m. Number x is considered close to number n modulo m, if: * it can be obtained by rearranging the digits of number n, * it doesn't have any leading zeroes, * the remainder after dividing number x by m equals 0. Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him. Input The first line contains two integers: n (1 ≀ n < 1018) and m (1 ≀ m ≀ 100). Output In a single line print a single integer β€” the number of numbers close to number n modulo m. Examples Input 104 2 Output 3 Input 223 4 Output 1 Input 7067678 8 Output 47 Note In the first sample the required numbers are: 104, 140, 410. In the second sample the required number is 232.
2
10
import com.sun.org.apache.xml.internal.utils.StringComparable; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintWriter; import java.text.DecimalFormat; import java.text.NumberFormat; import java.util.*; public class Main { public static void main(String[] args) { // Test.testing(); ConsoleIO io = new ConsoleIO(); new Main(io).solve(); io.close(); } ConsoleIO io; Main(ConsoleIO io) { this.io = io; } ArrayList<ArrayList<Integer>> gr; boolean[] visit; class Edge { public Edge(int u, int v, int c) { this.u = u; this.v = v; this.c = c; } public int u; public int v; public int c; } long MOD = 1_000_000_007; int N, M, K; double[][] map; public void solve() { long[] l = io.readLongArray(); long n = l[0]; need = (int)l[1]; List<List<Integer>> all = new ArrayList<>(); d = (n+"").length(); nums = new int[d]; long t = n; for(int i =0;i<d;i++){ nums[i] = (int)(t%10); t/=10; } memo = new long[need][1<<d]; for(int i =0;i<memo.length;i++) Arrays.fill(memo[i], -1); long res = go(0, 0); io.writeLine(res+""); } int need; int d; int[] nums; long[][] memo; long go(int mask, int rem) { if (memo[rem][mask] > -1) return memo[rem][mask]; long mul = 1; int have = 0; for (int i = 0; i < d; i++) { if ((mask & (1 << i)) != 0) mul *= 10; else have++; } if (have == 0) return rem == 0 ? 1 : 0; long res = 0; boolean[] done = new boolean[10]; for (int i = 0; i < d; i++) { if ((mask & (1 << i)) == 0 && (have > 1 || nums[i] != 0) && !done[nums[i]]) { res += go(mask | (1 << i), (int) ((rem + mul * nums[i]) % need)); done[nums[i]] = true; } } memo[rem][mask] = res; return res; } long gcd(long a, long b) { if (a < b) return gcd(b, a); if (b == 0) return a; return gcd(b, a % b); } } class ConsoleIO { BufferedReader br; PrintWriter out; public ConsoleIO(){br = new BufferedReader(new InputStreamReader(System.in));out = new PrintWriter(System.out);} public void flush(){this.out.flush();} public void close(){this.out.close();} public void writeLine(String s) {this.out.println(s);} public void writeInt(int a) {this.out.print(a);this.out.print(' ');} public void writeWord(String s){ this.out.print(s); } public int read(char[] buf, int len){try {return br.read(buf,0,len);}catch (Exception ex){ return -1; }} public String readLine() {try {return br.readLine();}catch (Exception ex){ return "";}} public long readLong() { return Long.parseLong(this.readLine()); } public int readInt() { return Integer.parseInt(this.readLine().trim()); } public long[] readLongArray() { String[]n=this.readLine().trim().split("\\s+");long[]r=new long[n.length]; for(int i=0;i<n.length;i++)r[i]=Long.parseLong(n[i]); return r; } public int[] readIntArray() { String[]n=this.readLine().trim().split("\\s+");int[]r=new int[n.length]; for(int i=0;i<n.length;i++)r[i]=Integer.parseInt(n[i]); return r; } public int[] readIntArray(int n) { int[] res = new int[n]; char[] all = this.readLine().toCharArray(); int cur = 0;boolean have = false; int k = 0; boolean neg = false; for(int i = 0;i<all.length;i++){ if(all[i]>='0' && all[i]<='9'){ cur = cur*10+all[i]-'0'; have = true; }else if(all[i]=='-') { neg = true; } else if(have){ res[k++] = neg?-cur:cur; cur = 0; have = false; neg = false; } } if(have)res[k++] = neg?-cur:cur; return res; } public void writeIntArray(int[] a) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < a.length; i++) {if (i > 0) sb.append(' ');sb.append(a[i]);} this.writeLine(sb.toString()); } } class Pair { public Pair(int a, int b) {this.a = a;this.b = b;} public int a; public int b; } class PairLL { public PairLL(long a, long b) {this.a = a;this.b = b;} public long a; public long b; }
JAVA
401_D. Roman and Numbers
Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m. Number x is considered close to number n modulo m, if: * it can be obtained by rearranging the digits of number n, * it doesn't have any leading zeroes, * the remainder after dividing number x by m equals 0. Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him. Input The first line contains two integers: n (1 ≀ n < 1018) and m (1 ≀ m ≀ 100). Output In a single line print a single integer β€” the number of numbers close to number n modulo m. Examples Input 104 2 Output 3 Input 223 4 Output 1 Input 7067678 8 Output 47 Note In the first sample the required numbers are: 104, 140, 410. In the second sample the required number is 232.
2
10
#include <bits/stdc++.h> const int maxn = 3e5 + 123; using namespace std; int n, m; int cnt[10]; long long dp[(1 << 18) + 123][105]; int main() { string s; cin >> s; cin >> m; for (int i = 0; i < s.size(); i++) cnt[s[i] - '0']++; for (int i = 0; i < s.size(); i++) if (s[i] != '0') dp[(1 << i)][(s[i] - '0') % m] = 1; for (int ma = 1; ma < (1 << (s.size())); ma++) for (int i = 0; i < s.size(); i++) { if (ma & (1 << i)) continue; for (int j = 0; j < m; j++) dp[ma ^ (1 << i)][(j * 10 + s[i] - '0') % m] += dp[ma][j]; } long long ans = dp[(1 << s.size()) - 1][0]; for (int i = 0; i < 10; i++) { for (int j = 1; j <= cnt[i]; j++) ans /= j; } cout << ans; return 0; }
CPP
401_D. Roman and Numbers
Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m. Number x is considered close to number n modulo m, if: * it can be obtained by rearranging the digits of number n, * it doesn't have any leading zeroes, * the remainder after dividing number x by m equals 0. Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him. Input The first line contains two integers: n (1 ≀ n < 1018) and m (1 ≀ m ≀ 100). Output In a single line print a single integer β€” the number of numbers close to number n modulo m. Examples Input 104 2 Output 3 Input 223 4 Output 1 Input 7067678 8 Output 47 Note In the first sample the required numbers are: 104, 140, 410. In the second sample the required number is 232.
2
10
#include <bits/stdc++.h> using namespace std; int n, m, limit, Now; string s; long long arr[12]; long long DP[102][(1 << 18) + 3]; long long dp(int rem, int mask) { if (mask == limit) return !rem; long long &ret = DP[rem][mask]; if (~ret) return ret; ret = 0; int d5l = 0; for (int i = 0; i < n; ++i) { Now = s[i] - 48; if (!mask && !Now) continue; if (d5l & (1 << Now)) continue; if (mask & (1 << i)) continue; d5l |= (1 << Now); ret += dp(((rem * 10) + Now) % m, mask | (1 << i)); } return ret; } int main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); ; cin >> s >> m; n = s.size(); limit = (1 << n) - 1; memset(DP, -1, sizeof DP); long long ans = dp(0, 0); cout << ans << endl; return 0; }
CPP
401_D. Roman and Numbers
Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m. Number x is considered close to number n modulo m, if: * it can be obtained by rearranging the digits of number n, * it doesn't have any leading zeroes, * the remainder after dividing number x by m equals 0. Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him. Input The first line contains two integers: n (1 ≀ n < 1018) and m (1 ≀ m ≀ 100). Output In a single line print a single integer β€” the number of numbers close to number n modulo m. Examples Input 104 2 Output 3 Input 223 4 Output 1 Input 7067678 8 Output 47 Note In the first sample the required numbers are: 104, 140, 410. In the second sample the required number is 232.
2
10
#include <bits/stdc++.h> using namespace std; int siz; long long f[(1 << 18)][105]; bool chk[20]; int getbit(int mask, int k) { return ((mask >> (k - 1)) & 1); } int cnt(int mask) { int tmp = 0; for (int i = 1; i <= siz; i++) if (getbit(mask, i)) tmp++; return tmp; } int main() { string s; int m; cin >> s >> m; siz = s.size(); f[0][0] = 1; for (int i = 0; i < (1 << siz) - 1; i++) { int num = cnt(i); for (int k = 0; k < m; k++) { if (!f[i][k]) continue; memset(chk, 0, sizeof(chk)); for (int j = 1; j <= siz; j++) if (getbit(i, j) == 0) { if ((s[j - 1] == '0' && num == 0) || chk[s[j - 1] - '0']) continue; f[i + (1 << (j - 1))][(10 * k + (s[j - 1] - '0')) % m] += f[i][k]; chk[s[j - 1] - '0'] = 1; } } } cout << f[(1 << siz) - 1][0] << endl; }
CPP
401_D. Roman and Numbers
Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m. Number x is considered close to number n modulo m, if: * it can be obtained by rearranging the digits of number n, * it doesn't have any leading zeroes, * the remainder after dividing number x by m equals 0. Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him. Input The first line contains two integers: n (1 ≀ n < 1018) and m (1 ≀ m ≀ 100). Output In a single line print a single integer β€” the number of numbers close to number n modulo m. Examples Input 104 2 Output 3 Input 223 4 Output 1 Input 7067678 8 Output 47 Note In the first sample the required numbers are: 104, 140, 410. In the second sample the required number is 232.
2
10
#include <bits/stdc++.h> using namespace std; long long d[262145][101], n, k, m, sz; long long po[1005], fc[50], G[50], PP; string s; int main() { int M = int(1e9) + 7; cin >> s >> m; fc[0] = 1; for (int i = 1; i <= 18; i++) fc[i] = (fc[i - 1] * i); n = s.size(); for (int i = 0; i < n; i++) G[s[i] - '0']++; PP = 1; for (int i = 0; i < 10; i++) PP *= fc[G[i]]; d[0][0] = 1; for (int i = 0; i < (1 << n); i++) { for (int j = 0; j < m; j++) { for (int q = 0; q < n; q++) { if (!(i & (1 << q))) if (i || (s[q] - '0') > 0) { d[i | (1 << q)][(j * 10 + s[q] - '0') % m] += d[i][j]; } } } } cout << d[(1 << n) - 1][0] / PP << endl; cin >> n; }
CPP
401_D. Roman and Numbers
Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m. Number x is considered close to number n modulo m, if: * it can be obtained by rearranging the digits of number n, * it doesn't have any leading zeroes, * the remainder after dividing number x by m equals 0. Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him. Input The first line contains two integers: n (1 ≀ n < 1018) and m (1 ≀ m ≀ 100). Output In a single line print a single integer β€” the number of numbers close to number n modulo m. Examples Input 104 2 Output 3 Input 223 4 Output 1 Input 7067678 8 Output 47 Note In the first sample the required numbers are: 104, 140, 410. In the second sample the required number is 232.
2
10
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.StringTokenizer; import java.util.TreeMap; public class Main implements Runnable { int n, m, k; static boolean use_n_tests = false; List<Integer> ls; Long[][] dp; int mod; void solve(FastScanner in, PrintWriter out, int testNumber) { long n = in.nextLong(); mod = in.nextInt(); ls = getGidits(n); dp = new Long[1 << 19][101]; long res = rec(0, 0, 0); out.println(res); } long rec(int taken, int took, int rest) { if (taken == ls.size() && rest == 0) { return 1; } if (dp[took][rest] != null) { return dp[took][rest]; } long ans = 0; long base = 1; for (int i = 0; i < taken; i++) { base *= 10L; base %= mod; } boolean[] has = new boolean[10]; for (int i = 0; i < ls.size(); i++) { int d = ls.get(i); if (taken == ls.size() - 1 && d == 0 || has[d]) { continue; } if ((took & (1 << i)) == 0) { has[d] = true; ans += rec(taken + 1, took | (1 << i), (int) (base * d + rest) % mod); } } return dp[took][rest] = ans; } // ****************************** template code *********** List<Integer> getGidits(long n) { List<Integer> res = new ArrayList<>(); while (n != 0) { res.add((int) (n % 10L)); n /= 10; } return res; } List<Integer> generatePrimes(int n) { List<Integer> res = new ArrayList<>(); boolean[] sieve = new boolean[n + 1]; for (int i = 2; i <= n; i++) { if (!sieve[i]) { res.add(i); } if ((long) i * i <= n) { for (int j = i * i; j <= n; j += i) { sieve[j] = true; } } } return res; } int ask(int l, int r) { if (l >= r) { return -1; } System.out.printf("? %d %d\n", l + 1, r + 1); System.out.flush(); return in.nextInt() - 1; } static int stack_size = 1 << 27; class Mod { long mod; Mod(long mod) { this.mod = mod; } long add(long a, long b) { a = mod(a); b = mod(b); return (a + b) % mod; } long sub(long a, long b) { a = mod(a); b = mod(b); return (a - b + mod) % mod; } long mul(long a, long b) { a = mod(a); b = mod(b); return a * b % mod; } long div(long a, long b) { a = mod(a); b = mod(b); return (a * inv(b)) % mod; } public long inv(long r) { if (r == 1) return 1; return ((mod - mod / r) * inv(mod % r)) % mod; } private long mod(long a) { return a % mod; } } class Coeff { long mod; long[][] C; long[] fact; boolean cycleWay = false; Coeff(int n, long mod) { this.mod = mod; fact = new long[n + 1]; fact[0] = 1; for (int i = 1; i <= n; i++) { fact[i] = i; fact[i] %= mod; fact[i] *= fact[i - 1]; fact[i] %= mod; } } Coeff(int n, int m, long mod) { // n > m cycleWay = true; this.mod = mod; C = new long[n + 1][m + 1]; for (int i = 0; i <= n; i++) { for (int j = 0; j <= Math.min(i, m); j++) { if (j == 0 || j == i) { C[i][j] = 1; } else { C[i][j] = C[i - 1][j - 1] + C[i - 1][j]; C[i][j] %= mod; } } } } public long C(int n, int m) { if (cycleWay) { return C[n][m]; } return fC(n, m); } private long fC(int n, int m) { return (fact[n] * inv(fact[n - m] * fact[m] % mod)) % mod; } private long inv(long r) { if (r == 1) return 1; return ((mod - mod / r) * inv(mod % r)) % mod; } } class Pair { int first; long second; Pair(int f, long s) { first = f; second = s; } public int getFirst() { return first; } public long getSecond() { return second; } } class MultisetTree<T> { int size = 0; TreeMap<T, Integer> mp = new TreeMap<>(); void add(T x) { mp.merge(x, 1, Integer::sum); size++; } void remove(T x) { if (mp.containsKey(x)) { mp.merge(x, -1, Integer::sum); if (mp.get(x) == 0) { mp.remove(x); } size--; } } boolean contains(T x) { return mp.containsKey(x); } T greatest() { return mp.lastKey(); } T smallest() { return mp.firstKey(); } int size() { return size; } int diffSize() { return mp.size(); } } class Multiset<T> { int size = 0; Map<T, Integer> mp = new HashMap<>(); void add(T x) { mp.merge(x, 1, Integer::sum); size++; } boolean contains(T x) { return mp.containsKey(x); } void remove(T x) { if (mp.containsKey(x)) { mp.merge(x, -1, Integer::sum); if (mp.get(x) == 0) { mp.remove(x); } size--; } } int size() { return size; } int diffSize() { return mp.size(); } } static class Range { int l, r; int id; public int getL() { return l; } public int getR() { return r; } public Range(int l, int r, int id) { this.l = l; this.r = r; this.id = id; } } static class Array { static Range[] readRanges(int n, FastScanner in) { Range[] result = new Range[n]; for (int i = 0; i < n; i++) { result[i] = new Range(in.nextInt(), in.nextInt(), i); } return result; } static List<List<Integer>> intInit2D(int n) { List<List<Integer>> res = new ArrayList<>(); for (int i = 0; i < n; i++) { res.add(new ArrayList<>()); } return res; } static boolean isSorted(Integer[] a) { for (int i = 0; i < a.length - 1; i++) { if (a[i] > a[i + 1]) { return false; } } return true; } static public long sum(int[] a) { long sum = 0; for (int x : a) { sum += x; } return sum; } static public long sum(long[] a) { long sum = 0; for (long x : a) { sum += x; } return sum; } static public long sum(Integer[] a) { long sum = 0; for (int x : a) { sum += x; } return sum; } static public int min(Integer[] a) { int mn = Integer.MAX_VALUE; for (int x : a) { mn = Math.min(mn, x); } return mn; } static public int min(int[] a) { int mn = Integer.MAX_VALUE; for (int x : a) { mn = Math.min(mn, x); } return mn; } static public int max(Integer[] a) { int mx = Integer.MIN_VALUE; for (int x : a) { mx = Math.max(mx, x); } return mx; } static public int max(int[] a) { int mx = Integer.MIN_VALUE; for (int x : a) { mx = Math.max(mx, x); } return mx; } static public int[] readint(int n, FastScanner in) { int[] out = new int[n]; for (int i = 0; i < out.length; i++) { out[i] = in.nextInt(); } return out; } } class Graph { List<List<Integer>> graph; Graph(int n) { create(n); } void create(int n) { List<List<Integer>> graph = new ArrayList<>(); for (int i = 0; i < n; i++) { graph.add(new ArrayList<>()); } this.graph = graph; } void readBi(int m, FastScanner in) { for (int i = 0; i < m; i++) { int v = in.nextInt() - 1; int u = in.nextInt() - 1; graph.get(v).add(u); graph.get(u).add(v); } } } class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner(InputStream io) { br = new BufferedReader(new InputStreamReader(io)); } public String line() { String result = ""; try { result = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return result; } public String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public int[] nextArray(int n) { int[] res = new int[n]; for (int i = 0; i < n; i++) { res[i] = in.nextInt(); } return res; } public long[] nextArrayL(int n) { long[] res = new long[n]; for (int i = 0; i < n; i++) { res[i] = in.nextLong(); } return res; } public Long[] nextArrayL2(int n) { Long[] res = new Long[n]; for (int i = 0; i < n; i++) { res[i] = in.nextLong(); } return res; } public Integer[] nextArray2(int n) { Integer[] res = new Integer[n]; for (int i = 0; i < n; i++) { res[i] = in.nextInt(); } return res; } public long nextLong() { return Long.parseLong(next()); } } void run_t_tests() { int t = in.nextInt(); int i = 0; while (t-- > 0) { solve(in, out, i++); } } void run_one() { solve(in, out, -1); } @Override public void run() { in = new FastScanner(System.in); out = new PrintWriter(System.out); if (use_n_tests) { run_t_tests(); } else { run_one(); } out.close(); } static FastScanner in; static PrintWriter out; public static void main(String[] args) throws InterruptedException { Thread thread = new Thread(null, new Main(), "", stack_size); thread.start(); thread.join(); } }
JAVA
401_D. Roman and Numbers
Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m. Number x is considered close to number n modulo m, if: * it can be obtained by rearranging the digits of number n, * it doesn't have any leading zeroes, * the remainder after dividing number x by m equals 0. Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him. Input The first line contains two integers: n (1 ≀ n < 1018) and m (1 ≀ m ≀ 100). Output In a single line print a single integer β€” the number of numbers close to number n modulo m. Examples Input 104 2 Output 3 Input 223 4 Output 1 Input 7067678 8 Output 47 Note In the first sample the required numbers are: 104, 140, 410. In the second sample the required number is 232.
2
10
/** * ******* Created on 20/12/19 9:09 PM******* */ import java.io.*; import java.util.*; public class D401 implements Runnable { private static final int MAX =3* (int) (1E5 + 5); private static final int MOD = (int) (1E9 + 7); private static final long Inf = (long) (1E14 + 10); long[][]dp = new long[MAX][105]; private void solve() throws IOException { long n = reader.nextLong(); int m = reader.nextInt(); int[] has = new int[20]; int[] cnt = new int[10]; int id =0; int[] pw = new int[20]; long[] fac = new long[20]; while (n > 0) { has[id++] = (int) (n % 10L); cnt[(int) (n % 10)]++; n /= 10; } pw[0] = 1 % m; for (int i = 1; i < id; i++) pw[i] = 10 * pw[i - 1] % m; fac[0] = 1; for (int i = 1; i <= id; i++) fac[i] = fac[i - 1] * i; dp[0][0] =1; for(int i=0;i<(1<<id);i++){ int b =0; for(int j=0;j<id;j++){ if( (i&(1<<j)) >0)b++; } for(int j =0;j<m;j++)if(dp[i][j]!=0){ for(int k =0;k<id;k++){ if(( !((i & (1<<k))> 0) &&(k+1< id || has[b] !=0) )) { int x = (int) ((has[b]*pw[k] + j)%m); dp[i | (1<<k)][x] = (dp[i | (1<<k)][x] + dp[i][j]); } } } } long ans = dp[(1<<id)-1][0]; for(int i =0;i<10;i++) ans /= fac[cnt[i]]; writer.println(ans); } public static void main(String[] args) throws IOException { try (Input reader = new StandardInput(); PrintWriter writer = new PrintWriter(System.out)) { new D401().run(); } } StandardInput reader; PrintWriter writer; @Override public void run() { try { reader = new StandardInput(); writer = new PrintWriter(System.out); solve(); reader.close(); writer.close(); } catch (Exception e) { e.printStackTrace(); } } interface Input extends Closeable { String next() throws IOException; default int nextInt() throws IOException { return Integer.parseInt(next()); } default long nextLong() throws IOException { return Long.parseLong(next()); } default double nextDouble() throws IOException { return Double.parseDouble(next()); } default int[] readIntArray() throws IOException { return readIntArray(nextInt()); } default int[] readIntArray(int size) throws IOException { int[] array = new int[size]; for (int i = 0; i < array.length; i++) { array[i] = nextInt(); } return array; } default long[] readLongArray(int size) throws IOException { long[] array = new long[size]; for (int i = 0; i < array.length; i++) { array[i] = nextLong(); } return array; } } private static class StandardInput implements Input { private final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); private StringTokenizer stringTokenizer; @Override public void close() throws IOException { reader.close(); } @Override public String next() throws IOException { if (stringTokenizer == null || !stringTokenizer.hasMoreTokens()) { stringTokenizer = new StringTokenizer(reader.readLine()); } return stringTokenizer.nextToken(); } } }
JAVA
401_D. Roman and Numbers
Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m. Number x is considered close to number n modulo m, if: * it can be obtained by rearranging the digits of number n, * it doesn't have any leading zeroes, * the remainder after dividing number x by m equals 0. Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him. Input The first line contains two integers: n (1 ≀ n < 1018) and m (1 ≀ m ≀ 100). Output In a single line print a single integer β€” the number of numbers close to number n modulo m. Examples Input 104 2 Output 3 Input 223 4 Output 1 Input 7067678 8 Output 47 Note In the first sample the required numbers are: 104, 140, 410. In the second sample the required number is 232.
2
10
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.util.Arrays; import java.io.BufferedWriter; import java.util.InputMismatchException; import java.io.IOException; import java.io.Writer; import java.io.OutputStreamWriter; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author Alex */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); OutputWriter out = new OutputWriter(outputStream); TaskD solver = new TaskD(); solver.solve(1, in, out); out.close(); } static class TaskD { int[] a; int m; long[][] mem; int[] powers10; long DONE; long[] dp(int used) { if (mem[used] != null) return mem[used]; if (used == DONE) { long[] res = new long[m]; res[0] = 1; return res; } int index = Integer.bitCount(used); long[] res = new long[m]; for (int pos = 0; pos < a.length; pos++) { if (index == 0 && a[pos] == 0) continue; if (((used >> pos) & 1) != 0) continue; long[] get = dp(used | (1 << pos)); int curval = powers10[a.length - index - 1]; for (int mod = 0; mod < get.length; mod++) { res[(curval * a[pos] + mod) % m] += get[mod]; } } return mem[used] = res; } public void solve(int testNumber, InputReader in, OutputWriter out) { char[] input = in.next().toCharArray(); a = new int[input.length]; for (int i = 0; i < a.length; i++) a[i] = (input[i] - '0'); Arrays.sort(a); m = in.readInt(); DONE = (1 << a.length) - 1; powers10 = new int[a.length]; powers10[0] = 1 % m; for (int i = 1; i < powers10.length; i++) { powers10[i] = powers10[i - 1] * 10; powers10[i] %= m; } mem = new long[1 << a.length][]; long[] res = dp(0); long divisor = 1; int[] count = new int[10]; for (int val : a) count[val]++; for (int c : count) divisor *= IntegerUtils.factorial(c); out.printLine(res[0] / divisor); } } static class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void close() { writer.close(); } public void printLine(long i) { writer.println(i); } } static class IntegerUtils { public static long factorial(int n) { long result = 1; for (int i = 2; i <= n; i++) result *= i; return result; } } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private InputReader.SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public int readInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public String readString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { if (Character.isValidCodePoint(c)) res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return isWhitespace(c); } public static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public String next() { return readString(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } }
JAVA
401_D. Roman and Numbers
Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m. Number x is considered close to number n modulo m, if: * it can be obtained by rearranging the digits of number n, * it doesn't have any leading zeroes, * the remainder after dividing number x by m equals 0. Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him. Input The first line contains two integers: n (1 ≀ n < 1018) and m (1 ≀ m ≀ 100). Output In a single line print a single integer β€” the number of numbers close to number n modulo m. Examples Input 104 2 Output 3 Input 223 4 Output 1 Input 7067678 8 Output 47 Note In the first sample the required numbers are: 104, 140, 410. In the second sample the required number is 232.
2
10
#include <bits/stdc++.h> using namespace std; int l = -1; long long N, M, f[262150][105], w[20], sum[10], jc[20]; int main() { cin >> N >> M; for (; N; N /= 10) w[++l] = N % 10; for (int i = 0; i <= l; ++i) { sum[w[i]]++; if (w[i]) f[1 << i][w[i] % M] = 1; } for (int i = 1; i <= (1 << l + 1) - 1; ++i) { for (int k = 0; k <= M - 1; ++k) for (int j = 0; j <= l; ++j) { if ((1 << j ^ i) > i) f[1 << j ^ i][(k * 10 + w[j]) % M] += f[i][k]; } } long long ans = f[(1 << l + 1) - 1][0]; jc[0] = 1; for (int i = 1; i <= l + 1; ++i) jc[i] = jc[i - 1] * i; for (int i = 0; i <= 9; ++i) ans /= jc[sum[i]]; cout << ans << endl; }
CPP
401_D. Roman and Numbers
Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m. Number x is considered close to number n modulo m, if: * it can be obtained by rearranging the digits of number n, * it doesn't have any leading zeroes, * the remainder after dividing number x by m equals 0. Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him. Input The first line contains two integers: n (1 ≀ n < 1018) and m (1 ≀ m ≀ 100). Output In a single line print a single integer β€” the number of numbers close to number n modulo m. Examples Input 104 2 Output 3 Input 223 4 Output 1 Input 7067678 8 Output 47 Note In the first sample the required numbers are: 104, 140, 410. In the second sample the required number is 232.
2
10
import java.util.*; import java.math.*; import java.io.*; public class CF401D { long go(int mod, int mask) { if(mask == full) return mod > 0 ? 0 : 1; if(dp[mod][mask] != -1) return dp[mod][mask]; long res = 0, tried = 0; for(int i = len - 1 ; i >= 0 ; i--) { if((mask & (1 << i)) != 0) continue; if(mask == 0 && digit[i] == 0) continue; if((tried & (1 << digit[i])) != 0) continue; res += go((mod + (pow10[len - Integer.bitCount(mask) - 1] * digit[i])) % m, mask | (1 << i)); tried |= 1 << digit[i]; } dp[mod][mask] = res; return res; } long[][] dp; int len, full, digit[], pow10[], m; public CF401D() { System.err.println("Go!"); FS scan = new FS(); char[] n = scan.next().toCharArray(); m = scan.nextInt(); len = n.length; pow10 = new int[20]; pow10[0] = 1 % m; for(int i = 1 ; i < 20 ; i++) pow10[i] = (pow10[i - 1] * 10) % m; full = (1 << len) - 1; digit = new int[n.length]; for(int i = 0 ; i < n.length ; i++) digit[i] = n[n.length - i - 1] - '0'; dp = new long[m][1 << n.length]; for(int i = 0 ; i < m ; i++) Arrays.fill(dp[i], -1); System.out.println(go(0, 0)); } class FS { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); public String next() { while(!st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch(Exception e) { e.printStackTrace(); } } return st.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } } public static void main(String[] args) { new CF401D(); } }
JAVA
401_D. Roman and Numbers
Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m. Number x is considered close to number n modulo m, if: * it can be obtained by rearranging the digits of number n, * it doesn't have any leading zeroes, * the remainder after dividing number x by m equals 0. Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him. Input The first line contains two integers: n (1 ≀ n < 1018) and m (1 ≀ m ≀ 100). Output In a single line print a single integer β€” the number of numbers close to number n modulo m. Examples Input 104 2 Output 3 Input 223 4 Output 1 Input 7067678 8 Output 47 Note In the first sample the required numbers are: 104, 140, 410. In the second sample the required number is 232.
2
10
import java.util.Arrays; import java.util.Scanner; public class cf401d_10DIMENSIONSBABY { static int[] digitcounts = new int[10]; static int len, mod; static long[][][][][][][][][][][] memo; public static long dp(int[] counts, int cmod) { if (memo[counts[0]][counts[1]][counts[2]][counts[3]][counts[4]][counts[5]][counts[6]][counts[7]][counts[8]][counts[9]][cmod] != -1) { // System.out.println(Arrays.toString(counts) + " (" + cmod + ") --> // " // + // memo[counts[0]][counts[1]][counts[2]][counts[3]][counts[4]][counts[5]][counts[6]][counts[7]][counts[8]][counts[9]][cmod]); return memo[counts[0]][counts[1]][counts[2]][counts[3]][counts[4]][counts[5]][counts[6]][counts[7]][counts[8]][counts[9]][cmod]; } long tot = 0; // try adding each digit for (int i = 0; i < 10; i++) { if (counts[i] < digitcounts[i]) { counts[i]++; // System.out.println(cmod + " --(" + i + ")--> " + (((10 * // cmod) + i) % mod)); tot += dp(counts, ((10 * cmod) + i) % mod); counts[i]--; } } memo[counts[0]][counts[1]][counts[2]][counts[3]][counts[4]][counts[5]][counts[6]][counts[7]][counts[8]][counts[9]][cmod] = tot; return tot; } public static long fact(int i) { long res = 1; for (int j = 2; j <= i; j++) res *= j; return res; } public static void main(String[] args) { Scanner s = new Scanner(System.in); long N = s.nextLong(); mod = s.nextInt(); String Nrep = "" + N; len = Nrep.length(); for (char i : Nrep.toCharArray()) digitcounts[i - '0']++; memo = new long[digitcounts[0] + 1][digitcounts[1] + 1][digitcounts[2] + 1][digitcounts[3] + 1][digitcounts[4] + 1][digitcounts[5] + 1][digitcounts[6] + 1][digitcounts[7] + 1][digitcounts[8] + 1][digitcounts[9] + 1][mod]; for (long[][][][][][][][][][] a : memo) for (long[][][][][][][][][] b : a) for (long[][][][][][][][] c : b) for (long[][][][][][][] d : c) for (long[][][][][][] e : d) for (long[][][][][] f : e) for (long[][][][] g : f) for (long[][][] h : g) for (long[][] i : h) for (long[] j : i) Arrays.fill(j, -1); Arrays.fill( memo[digitcounts[0]][digitcounts[1]][digitcounts[2]][digitcounts[3]][digitcounts[4]][digitcounts[5]][digitcounts[6]][digitcounts[7]][digitcounts[8]][digitcounts[9]], 0); memo[digitcounts[0]][digitcounts[1]][digitcounts[2]][digitcounts[3]][digitcounts[4]][digitcounts[5]][digitcounts[6]][digitcounts[7]][digitcounts[8]][digitcounts[9]][0] = 1; long res = 0; for (int i = 1; i < 10; i++) { if (digitcounts[i] != 0) { int[] count = new int[10]; count[i]++; res += dp(count, i % mod); } } // for (int g : digitcounts) { // res /= fact(g); // } System.out.println(res); // for (long[][][][][][][][][][] a : memo) // for (long[][][][][][][][][] b : a) // for (long[][][][][][][][] c : b) // for (long[][][][][][][] d : c) // for (long[][][][][][] e : d) // for (long[][][][][] f : e) // for (long[][][][] g : f) // for (long[][][] h : g) // for (long[][] i : h) // for (long[] j : i) // System.out.println(Arrays.toString(j)); s.close(); } }
JAVA
401_D. Roman and Numbers
Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m. Number x is considered close to number n modulo m, if: * it can be obtained by rearranging the digits of number n, * it doesn't have any leading zeroes, * the remainder after dividing number x by m equals 0. Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him. Input The first line contains two integers: n (1 ≀ n < 1018) and m (1 ≀ m ≀ 100). Output In a single line print a single integer β€” the number of numbers close to number n modulo m. Examples Input 104 2 Output 3 Input 223 4 Output 1 Input 7067678 8 Output 47 Note In the first sample the required numbers are: 104, 140, 410. In the second sample the required number is 232.
2
10
#include <bits/stdc++.h> using namespace std; long long f[1 << 18][100]; int a[100]; int main() { long long n, m; int N = 0; cin >> n >> m; for (; n; n /= 10) a[N++] = n % 10; sort(a, a + N); int mask, k; f[0][0] = 1; for (mask = 0; mask < (1 << N); mask++) for (k = 0; k < m; k++) if (f[mask][k]) { long long cnt = f[mask][k], pre = -1; for (int i = N - 1; i >= 0; i--) if (~mask >> i & 1) { if (a[i] == pre) continue; if (a[i] == 0 && mask == 0) break; f[mask | 1 << i][(k * 10 + a[i]) % m] += cnt; pre = a[i]; } } cout << f[(1 << N) - 1][0] << endl; return 0; }
CPP
401_D. Roman and Numbers
Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m. Number x is considered close to number n modulo m, if: * it can be obtained by rearranging the digits of number n, * it doesn't have any leading zeroes, * the remainder after dividing number x by m equals 0. Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him. Input The first line contains two integers: n (1 ≀ n < 1018) and m (1 ≀ m ≀ 100). Output In a single line print a single integer β€” the number of numbers close to number n modulo m. Examples Input 104 2 Output 3 Input 223 4 Output 1 Input 7067678 8 Output 47 Note In the first sample the required numbers are: 104, 140, 410. In the second sample the required number is 232.
2
10
import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.Arrays; import java.util.StringTokenizer; public class Main{ public static void main(String[] args) { solve(); } static int mod, state[] = new int[11]; static Long dp[][]; private static void solve() { char num[] = IN.next().toCharArray(); int cnt[] = new int[10]; for (int i = 0; i < num.length; i++) { cnt[num[i] - '0']++; } Arrays.sort(num); state[0] = 1; for (int i = 1; i <= 10; i++) { state[i] = state[i - 1] * (cnt[i - 1] + 1); } dp = new Long[mod = IN.nextInt()][state[10] + 1]; System.out.println(dp(num.length - 1, 0, state[10], num, true)); } public static long dp(int pos, int cur, int mask, char num[], boolean leadZero) { if (pos < 0) { return cur == 0 ? 1 : 0; } if (dp[cur][mask] != null) { return dp[cur][mask]; } long res = 0; int t, seen = 0; for (int i = pos; i >= 0; i--) { t = num[i] - '0'; if (leadZero && t == 0 || (seen & (1 << t)) != 0) { continue; } seen |= (1 << t); swap(num, pos, i); res += dp(pos - 1, (cur * 10 + t) % mod, mask - state[t], num, false); swap(num, pos, i); } return dp[cur][mask] = res; } public static void swap(char num[], int i, int j) { char tmp = num[i]; num[i] = num[j]; num[j] = tmp; } static class IN { private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in), 65535); private static StringTokenizer st = null; public static String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (Exception e) { break; } } return st.nextToken(); } public static int nextInt() { return Integer.valueOf(next()); } } }
JAVA
401_D. Roman and Numbers
Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m. Number x is considered close to number n modulo m, if: * it can be obtained by rearranging the digits of number n, * it doesn't have any leading zeroes, * the remainder after dividing number x by m equals 0. Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him. Input The first line contains two integers: n (1 ≀ n < 1018) and m (1 ≀ m ≀ 100). Output In a single line print a single integer β€” the number of numbers close to number n modulo m. Examples Input 104 2 Output 3 Input 223 4 Output 1 Input 7067678 8 Output 47 Note In the first sample the required numbers are: 104, 140, 410. In the second sample the required number is 232.
2
10
import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.Arrays; import java.util.HashSet; import java.util.StringTokenizer; public class RomanAndNumbers { static char[] arr; static int mod; static int[] pow; static Long[][] dp = new Long[1 << 18][101]; static long go(int mask, int m, int bits) { if (bits == arr.length) if (m == 0) return 1; else return 0; if (dp[mask][m] != null) return dp[mask][m]; long res = 0; int digits = 0; for (int j = 0; j < arr.length; j++) { if ((mask & (1 << j)) == 0) { if (mask == 0 && arr[j] == '0') continue; int d = arr[j] - '0'; if ((digits & (1 << d)) == 0) { res += go(mask | (1 << j), (m + pow[arr.length - 1 - bits] * d) % mod, bits + 1); digits |= (1 << d); } } } return dp[mask][m] = res; } public static void main(String[] args) throws Exception { InputReader r = new InputReader(System.in); PrintWriter out = new PrintWriter(System.out); arr = r.next().toCharArray(); mod = r.nextInt(); pow = new int[20]; pow[0] = 1; for (int i = 1; i < pow.length; i++) { pow[i] = 10 * pow[i - 1]; pow[i] %= mod; } out.println(go(0, 0, 0)); out.close(); } static class InputReader { private BufferedReader reader; private StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream)); tokenizer = null; } public InputReader(FileReader stream) { reader = new BufferedReader(stream); tokenizer = null; } public String nextLine() { try { return reader.readLine(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); return null; } } 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()); } } }
JAVA
401_D. Roman and Numbers
Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m. Number x is considered close to number n modulo m, if: * it can be obtained by rearranging the digits of number n, * it doesn't have any leading zeroes, * the remainder after dividing number x by m equals 0. Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him. Input The first line contains two integers: n (1 ≀ n < 1018) and m (1 ≀ m ≀ 100). Output In a single line print a single integer β€” the number of numbers close to number n modulo m. Examples Input 104 2 Output 3 Input 223 4 Output 1 Input 7067678 8 Output 47 Note In the first sample the required numbers are: 104, 140, 410. In the second sample the required number is 232.
2
10
#include <bits/stdc++.h> using namespace std; int tim[10]; int dig[19]; long long dp[(1 << 18)][100]; bool flag[10]; void solve(long long n, int mod); int main() { long long n; int mod; cin >> n >> mod; solve(n, mod); return 0; } void solve(long long n, int mod) { memset(tim, 0, sizeof tim); int len = 0; while (n) { dig[++len] = n % 10; tim[dig[len]]++; n /= 10; } memset(dp, 0, sizeof dp); dp[0][0] = 1; for (int i = 0; i < (1 << len) - 1; i++) { memset(flag, false, sizeof flag); for (int j = 1; j <= len; j++) { if (!(i & (1 << (j - 1))) && !flag[dig[j]]) { if (i == 0 && dig[j] == 0) continue; for (int k = 0; k < mod; k++) { dp[i | (1 << (j - 1))][(k * 10 + dig[j]) % mod] += dp[i][k]; } flag[dig[j]] = true; } } } cout << dp[(1 << len) - 1][0] << endl; return; }
CPP
401_D. Roman and Numbers
Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m. Number x is considered close to number n modulo m, if: * it can be obtained by rearranging the digits of number n, * it doesn't have any leading zeroes, * the remainder after dividing number x by m equals 0. Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him. Input The first line contains two integers: n (1 ≀ n < 1018) and m (1 ≀ m ≀ 100). Output In a single line print a single integer β€” the number of numbers close to number n modulo m. Examples Input 104 2 Output 3 Input 223 4 Output 1 Input 7067678 8 Output 47 Note In the first sample the required numbers are: 104, 140, 410. In the second sample the required number is 232.
2
10
#include <bits/stdc++.h> using namespace std; template <typename T> ostream &operator<<(ostream &os, const vector<T> &A) { for (long long i = 0; i < A.size(); i++) os << A[i] << " "; os << endl; return os; } template <> ostream &operator<<(ostream &os, const vector<vector<long long> > &A) { long long N = A.size(); for (long long i = 0; i < N; i++) { for (long long j = 0; j < A[i].size(); j++) os << A[i][j] << " "; os << endl; } return os; } struct edge { long long from, to, d, c; edge(long long _from = 0, long long _to = 0, long long _d = 0, long long _c = 0) { from = _from; to = _to; d = _d; c = _c; } bool operator<(const edge &rhs) const { return (d == rhs.d) ? (c < rhs.c) : (d < rhs.d); } }; struct aabb { long long x1, y1, x2, y2; aabb(long long x1, long long y1, long long x2, long long y2) : x1(x1), y1(y1), x2(x2), y2(y2) {} }; struct flow { long long to, cap, rev, cost; flow(long long to = 0, long long cap = 0, long long rev = 0, long long cost = 0) : to(to), cap(cap), rev(rev), cost(cost) {} }; const long long di[4] = {0, -1, 0, 1}; const long long dj[4] = {-1, 0, 1, 0}; const long long ci[5] = {0, 0, -1, 0, 1}; const long long cj[5] = {0, -1, 0, 1, 0}; const long long LINF = LLONG_MAX / 2; const long long INF = INT_MAX / 2; const long double PI = acos(-1); long long pow2(long long n) { return 1LL << n; } template <typename T, typename U> bool chmin(T &x, const U &y) { if (x > y) { x = y; return true; } return false; } template <typename T, typename U> bool chmax(T &x, const U &y) { if (x < y) { x = y; return true; } return false; } struct initializer { initializer() { cout << fixed << setprecision(20); } }; initializer _____; long long N, M, K, T, Q, H, W; signed main() { cin >> N >> M; vector<long long> d; while (N > 0) { d.push_back(N % 10); N /= 10; } N = d.size(); vector<vector<long long> > dp(1 << N, vector<long long>(M, 0)); dp[0][0] = 1; for (long long b = 0; b < (long long)(1 << N); b++) for (long long j = 0; j < (long long)(M); j++) { for (long long i = 0; i < (long long)(N); i++) if (((b >> i) & 1) == 0) { if (b == 0 && d[i] == 0) continue; long long nb = b | (1 << i); long long nj = (j * 10 + d[i]) % M; dp[nb][nj] += dp[b][j]; } } vector<long long> F(100, 1); for (long long i = 0; i < (long long)(99); i++) F[i + 1] = F[i] * (i + 1); long long ans = dp[(1 << N) - 1][0]; vector<long long> C(10, 0); for (long long i = 0; i < (long long)(N); i++) C[d[i]]++; for (long long i = 0; i < (long long)(10); i++) { ans /= F[C[i]]; } cout << ans << endl; return 0; }
CPP
401_D. Roman and Numbers
Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m. Number x is considered close to number n modulo m, if: * it can be obtained by rearranging the digits of number n, * it doesn't have any leading zeroes, * the remainder after dividing number x by m equals 0. Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him. Input The first line contains two integers: n (1 ≀ n < 1018) and m (1 ≀ m ≀ 100). Output In a single line print a single integer β€” the number of numbers close to number n modulo m. Examples Input 104 2 Output 3 Input 223 4 Output 1 Input 7067678 8 Output 47 Note In the first sample the required numbers are: 104, 140, 410. In the second sample the required number is 232.
2
10
#include <bits/stdc++.h> using namespace std; const int MAX = 18; long long n; int m; int digs; int dig[MAX]; int cnt[MAX]; long long dp[1 << MAX][105]; long long fact[MAX]; int main() { cin >> n >> m; fact[0] = 1; for (int i = 1; i < MAX; i++) { fact[i] = fact[i - 1] * i; } while (n > 0) { dig[digs] = n % 10; cnt[n % 10]++; digs++; n /= 10; } dp[0][0] = 1; for (int i = 0; i < (1 << digs) - 1; i++) { for (int mod = 0; mod < m; mod++) { if (!dp[i][mod]) continue; for (int j = 0; j < digs; j++) { if (i & (1 << j) || (dig[j] == 0 && i == 0)) continue; dp[i | (1 << j)][(10 * mod + dig[j]) % m] += dp[i][mod]; } } } long long ans = dp[(1 << digs) - 1][0]; for (int i = 0; i < MAX; i++) { ans /= fact[cnt[i]]; } cout << ans << endl; }
CPP
401_D. Roman and Numbers
Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m. Number x is considered close to number n modulo m, if: * it can be obtained by rearranging the digits of number n, * it doesn't have any leading zeroes, * the remainder after dividing number x by m equals 0. Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him. Input The first line contains two integers: n (1 ≀ n < 1018) and m (1 ≀ m ≀ 100). Output In a single line print a single integer β€” the number of numbers close to number n modulo m. Examples Input 104 2 Output 3 Input 223 4 Output 1 Input 7067678 8 Output 47 Note In the first sample the required numbers are: 104, 140, 410. In the second sample the required number is 232.
2
10
import java.awt.*; import java.awt.event.*; import java.awt.geom.*; import java.io.*; import java.math.*; import java.text.*; import java.util.*; import java.util.regex.*; /* br = new BufferedReader(new FileReader("input.txt")); pw = new PrintWriter(new BufferedWriter(new FileWriter("output.txt"))); br = new BufferedReader(new InputStreamReader(System.in)); pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); */ public class Main { private static BufferedReader br; private static StringTokenizer st; private static PrintWriter pw; public static void main(String[] args) throws IOException { br = new BufferedReader(new InputStreamReader(System.in)); pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); //int qq = 1; int qq = Integer.MAX_VALUE; //int qq = readInt(); for(int casenum = 1; casenum <= qq; casenum++) { String str = nextToken(); int m = readInt(); long[][] dp = new long[1<<str.length()][m]; ArrayList<Integer>[] order = new ArrayList[10]; for(int i = 0; i < order.length; i++) { order[i] = new ArrayList<Integer>(); } for(int i = 0; i < str.length(); i++) { order[str.charAt(i) - '0'].add(i); } for(int a = 1; a <= 9; a++) { if(order[a].isEmpty()) continue; int first = order[a].get(0); dp[1 << first][a%m]++; } for(int mask = 1; mask < dp.length; mask++) { for(int a = 0; a < m; a++) { if(dp[mask][a] == 0) continue; for(int b = 0; b < 10; b++) { for(int out: order[b]) { if((mask&(1<<out)) != 0) continue; dp[mask | (1 << out)][(10*a + b) % m] += dp[mask][a]; break; } } } } pw.println(dp[dp.length-1][0]); } exitImmediately(); } private static void exitImmediately() { pw.close(); System.exit(0); } private static long readLong() throws IOException { return Long.parseLong(nextToken()); } private static double readDouble() throws IOException { return Double.parseDouble(nextToken()); } private static int readInt() throws IOException { return Integer.parseInt(nextToken()); } private static String nextLine() throws IOException { if(!br.ready()) { exitImmediately(); } st = null; return br.readLine(); } private static String nextToken() throws IOException { while(st == null || !st.hasMoreTokens()) { if(!br.ready()) { exitImmediately(); } st = new StringTokenizer(br.readLine().trim()); } return st.nextToken(); } }
JAVA
401_D. Roman and Numbers
Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m. Number x is considered close to number n modulo m, if: * it can be obtained by rearranging the digits of number n, * it doesn't have any leading zeroes, * the remainder after dividing number x by m equals 0. Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him. Input The first line contains two integers: n (1 ≀ n < 1018) and m (1 ≀ m ≀ 100). Output In a single line print a single integer β€” the number of numbers close to number n modulo m. Examples Input 104 2 Output 3 Input 223 4 Output 1 Input 7067678 8 Output 47 Note In the first sample the required numbers are: 104, 140, 410. In the second sample the required number is 232.
2
10
#include <bits/stdc++.h> using namespace std; int num[20], sum[10]; long long dp[1 << 18][105], fac[20]; void init() { fac[0] = 1; for (int i = 1; i <= 17; ++i) fac[i] = fac[i - 1] * i; } int main() { int l = 0, m; long long n, res = 1; scanf("%lld%d", &n, &m); while (n) { num[l++] = n % 10; n /= 10; sum[num[l - 1]]++; } init(); for (int i = 0; i < l; ++i) if (num[i] != 0) dp[(1 << i)][num[i] % m] = 1; for (int i = 1; i < (1 << l); ++i) for (int j = 0; j < m; ++j) { for (int k = 0; k < l; ++k) if (!(i & (1 << k))) dp[i | (1 << k)][(j * 10 + num[k]) % m] += dp[i][j]; } for (int i = 0; i <= 9; ++i) res *= fac[sum[i]]; printf("%lld\n", dp[(1 << l) - 1][0] / res); }
CPP
401_D. Roman and Numbers
Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m. Number x is considered close to number n modulo m, if: * it can be obtained by rearranging the digits of number n, * it doesn't have any leading zeroes, * the remainder after dividing number x by m equals 0. Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him. Input The first line contains two integers: n (1 ≀ n < 1018) and m (1 ≀ m ≀ 100). Output In a single line print a single integer β€” the number of numbers close to number n modulo m. Examples Input 104 2 Output 3 Input 223 4 Output 1 Input 7067678 8 Output 47 Note In the first sample the required numbers are: 104, 140, 410. In the second sample the required number is 232.
2
10
import java.util.HashMap; import java.util.Scanner; public class cf401d { public static long factorial(int a) { long ret = 1; for (int i = 2; i <= a; i++) ret *= i; return ret; } public static void main(String[] args) { Scanner in = new Scanner(System.in); long n = in.nextLong(); int m = in.nextInt(); String[] Sdigs = ("" + n).split(""); int[] digs = new int[Sdigs.length]; for (int i = 0; i < digs.length; i++) digs[i] = Integer.parseInt(Sdigs[i]); long[][] dp = new long[1 << digs.length][m]; dp[0][0] = 1; for (int i = 0; i < dp.length; i++) { for (int dig = 0; dig < digs.length; dig++) { if (((i >> dig) & 1) == 1) continue; if (i == 0 && digs[dig] == 0) continue; int index = i | (1 << dig); for (int j = 0; j < m; j++) { int newmod = (10 * j + digs[dig]) % m; dp[index][newmod] += dp[i][j]; } } } long ans = dp[(1 << digs.length) - 1][0]; HashMap<Integer, Integer> hm = new HashMap<Integer, Integer>(); for (int i : digs) { if (hm.containsKey(i)) hm.put(i, hm.get(i) + 1); else hm.put(i, 1); } for (int i : hm.values()) ans /= factorial(i); System.out.println(ans); in.close(); } public static void debug(Object o) { System.out.println("\tDEBUG: " + o.toString().replaceAll("\n", "\n\tDEBUG: ")); } }
JAVA
401_D. Roman and Numbers
Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m. Number x is considered close to number n modulo m, if: * it can be obtained by rearranging the digits of number n, * it doesn't have any leading zeroes, * the remainder after dividing number x by m equals 0. Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him. Input The first line contains two integers: n (1 ≀ n < 1018) and m (1 ≀ m ≀ 100). Output In a single line print a single integer β€” the number of numbers close to number n modulo m. Examples Input 104 2 Output 3 Input 223 4 Output 1 Input 7067678 8 Output 47 Note In the first sample the required numbers are: 104, 140, 410. In the second sample the required number is 232.
2
10
import java.util.Arrays; import java.util.Comparator; import java.util.Scanner; public class P401D { static long[][] dp; public static void main(String[] args) { Scanner scan = new Scanner(System.in); char[] n = scan.next().toCharArray(); for (int i = 0; i < n.length; i++) n[i] -= '0'; int m = scan.nextInt(); dp = new long[1 << n.length][m]; Integer[] nums = new Integer[1 << n.length]; for (int i = 0; i < nums.length; i++) nums[i] = i; Arrays.sort(nums, new Comparator<Integer>() { public int compare(Integer a, Integer b) { return Integer.bitCount(a) - Integer.bitCount(b); } }); dp[0][0] = 1; for (int i = 0; i < nums.length; i++) { int num = nums[i]; int bits = Integer.bitCount(num); for (int j = 0; j < m; j++) { if (dp[num][j] == 0) continue; boolean[] done = new boolean[10]; for (int k = 0; k < n.length; k++) { if ((num & (1 << k)) == 0) { int d = n[k]; if (done[d]) continue; if (d == 0 && bits == 0) continue; dp[num | (1 << k)][(j*10+d)%m] += dp[num][j]; done[d] = true; } } } } System.out.println(dp[dp.length-1][0]); } }
JAVA
401_D. Roman and Numbers
Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m. Number x is considered close to number n modulo m, if: * it can be obtained by rearranging the digits of number n, * it doesn't have any leading zeroes, * the remainder after dividing number x by m equals 0. Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him. Input The first line contains two integers: n (1 ≀ n < 1018) and m (1 ≀ m ≀ 100). Output In a single line print a single integer β€” the number of numbers close to number n modulo m. Examples Input 104 2 Output 3 Input 223 4 Output 1 Input 7067678 8 Output 47 Note In the first sample the required numbers are: 104, 140, 410. In the second sample the required number is 232.
2
10
#include <bits/stdc++.h> using namespace std; long long fact[20]; signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; fact[0] = 1; for (long long i = 1; i < 20; i++) { fact[i] = fact[i - 1] * i; } long long n, m; cin >> n >> m; vector<long long> digits; while (n > 0) { digits.push_back(n % 10); n = n / 10; } long long l = digits.size(); map<long long, long long> count; for (long long i = 0; i < l; i++) { count[digits[i]]++; } long long z = 1; long long dp[(z << l)][m]; memset(dp, 0, sizeof(dp)); dp[0][0] = 1; for (long long i = 0; i < l; i++) { if (digits[i] != 0) { dp[(z << i)][digits[i] % m] = 1; } } for (long long i = 1; i < (z << l) - 1; i++) { for (long long j = 0; j < l; j++) { if (!(i & (z << j))) { for (long long k = 0; k < m; k++) { dp[i | (z << j)][(k * 10 + digits[j]) % m] += dp[i][k]; } } } } long long ans = dp[(z << l) - 1][0]; for (auto x : count) { ans /= fact[x.second]; } cout << ans << endl; }
CPP
401_D. Roman and Numbers
Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m. Number x is considered close to number n modulo m, if: * it can be obtained by rearranging the digits of number n, * it doesn't have any leading zeroes, * the remainder after dividing number x by m equals 0. Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him. Input The first line contains two integers: n (1 ≀ n < 1018) and m (1 ≀ m ≀ 100). Output In a single line print a single integer β€” the number of numbers close to number n modulo m. Examples Input 104 2 Output 3 Input 223 4 Output 1 Input 7067678 8 Output 47 Note In the first sample the required numbers are: 104, 140, 410. In the second sample the required number is 232.
2
10
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; const int INF = 0x3f3f3f3f; const int N = 2e6 + 10; vector<int> V[20]; int cnt[N], pos[N], dig[20], num[13]; vector<long long> f[20][105]; int main() { for (int i = 1; i <= 2000000; i++) cnt[i] = cnt[i >> 1] + (i & 1); long long n, m; cin >> n >> m; int p = 0; long long x = n; while (x) dig[p] = x % 10, p++, x /= 10; for (int i = 0; i < p; i++) num[dig[i]]++; int len = 1 << p; for (int i = 0; i < len; i++) { V[p - cnt[i]].push_back(i); } for (int i = 0; i <= p; i++) { int siz = V[i].size(); for (int j = 0; j < m; j++) f[i][j].resize(siz + 1); for (int j = 0; j < siz; j++) { pos[V[i][j]] = j; } } f[p][0][0] = 1; for (int i = p; i >= 1; i--) { int siz = V[i].size(); for (int j = 0; j < siz; j++) { for (int k = 0; k < p; k++) { if (i == p && dig[k] == 0) continue; if (((V[i][j] >> k) & 1) == 0) { for (int l = 0; l < m; l++) { f[i - 1][(l * 10 + dig[k]) % m][pos[V[i][j] | (1 << k)]] += f[i][l][j]; } } } } } long long ans = f[0][0][0]; for (int i = 0; i < 10; i++) { for (int j = 1; j <= num[i]; j++) ans /= j; } cout << ans; return 0; }
CPP
401_D. Roman and Numbers
Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m. Number x is considered close to number n modulo m, if: * it can be obtained by rearranging the digits of number n, * it doesn't have any leading zeroes, * the remainder after dividing number x by m equals 0. Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him. Input The first line contains two integers: n (1 ≀ n < 1018) and m (1 ≀ m ≀ 100). Output In a single line print a single integer β€” the number of numbers close to number n modulo m. Examples Input 104 2 Output 3 Input 223 4 Output 1 Input 7067678 8 Output 47 Note In the first sample the required numbers are: 104, 140, 410. In the second sample the required number is 232.
2
10
#include <bits/stdc++.h> using namespace std; int lowbit(int x) { return x & (-x); } const int maxn = 105; int tp, m, a[30], vis[10]; long long n, dp[1 << 18][maxn]; int main(void) { scanf("%lld%d", &n, &m); while (n) a[tp++] = n % 10, n /= 10; dp[0][0] = 1; for (int i = 1; i < (1 << tp); ++i) { memset(vis, 0, sizeof(vis)); for (int j = 0; j < tp; ++j) if (i & (1 << j)) { if (a[j] == 0 && (i ^ (1 << j)) == 0) continue; if (vis[a[j]]) continue; vis[a[j]] = 1; for (int k = 0; k < m; ++k) dp[i][(k * 10 % m + a[j]) % m] += dp[i ^ (1 << j)][k]; } } printf("%lld\n", dp[(1 << tp) - 1][0]); return 0; }
CPP
401_D. Roman and Numbers
Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m. Number x is considered close to number n modulo m, if: * it can be obtained by rearranging the digits of number n, * it doesn't have any leading zeroes, * the remainder after dividing number x by m equals 0. Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him. Input The first line contains two integers: n (1 ≀ n < 1018) and m (1 ≀ m ≀ 100). Output In a single line print a single integer β€” the number of numbers close to number n modulo m. Examples Input 104 2 Output 3 Input 223 4 Output 1 Input 7067678 8 Output 47 Note In the first sample the required numbers are: 104, 140, 410. In the second sample the required number is 232.
2
10
#include <bits/stdc++.h> using namespace std; long long dp[1 << 18][110]; int main() { int m, n; string s; cin >> s >> m; n = s.size(); sort(s.begin(), s.end()); dp[0][0] = 1; for (int i = 0; i < 1 << n; ++i) for (int j = 0; j < m; ++j) if (dp[i][j]) for (int k = 0; k < n; ++k) if (!(i & (1 << k)) && (i != 0 || s[k] != '0') && (k == 0 || s[k] != s[k - 1] || (i & 1 << (k - 1)) != 0)) dp[i | (1 << k)][(j * 10 + s[k] - '0') % m] += dp[i][j]; cout << dp[(1 << n) - 1][0] << endl; }
CPP
401_D. Roman and Numbers
Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m. Number x is considered close to number n modulo m, if: * it can be obtained by rearranging the digits of number n, * it doesn't have any leading zeroes, * the remainder after dividing number x by m equals 0. Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him. Input The first line contains two integers: n (1 ≀ n < 1018) and m (1 ≀ m ≀ 100). Output In a single line print a single integer β€” the number of numbers close to number n modulo m. Examples Input 104 2 Output 3 Input 223 4 Output 1 Input 7067678 8 Output 47 Note In the first sample the required numbers are: 104, 140, 410. In the second sample the required number is 232.
2
10
#include <bits/stdc++.h> using namespace std; const int mB = 1 << 18; const int mM = 105; long long cnt[mB][mM]; int masks[mB]; bool compareMask(int m1, int m2) { return __builtin_popcount(m1) < __builtin_popcount(m2); } int frequency[10]; int main() { long long n; int mod; cin >> n >> mod; int nBits = 0, bits[20]; while (n) { bits[++nBits] = n % 10; n /= 10; } for (int i = 0; i < (1 << nBits); i++) masks[i] = i; sort(masks, masks + (1 << nBits), compareMask); cnt[0][0] = 1; for (int i = 0; i < (1 << nBits); i++) { for (int r = 0; r < mod; r++) { for (int k = 0; k < nBits; k++) if (((masks[i] >> k) & 1) == 0) { if (masks[i] == 0 and bits[nBits - k] == 0) continue; int r_new = (r * 10 + bits[nBits - k]) % mod; cnt[masks[i] | (1 << k)][r_new] += cnt[masks[i]][r]; } } } long long ans = cnt[(1 << nBits) - 1][0]; for (int i = 1; i <= nBits; i++) frequency[bits[i]]++; for (int d = 0; d < 10; d++) { for (int j = 2; j <= frequency[d]; j++) ans /= j; } cout << ans; }
CPP
401_D. Roman and Numbers
Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m. Number x is considered close to number n modulo m, if: * it can be obtained by rearranging the digits of number n, * it doesn't have any leading zeroes, * the remainder after dividing number x by m equals 0. Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him. Input The first line contains two integers: n (1 ≀ n < 1018) and m (1 ≀ m ≀ 100). Output In a single line print a single integer β€” the number of numbers close to number n modulo m. Examples Input 104 2 Output 3 Input 223 4 Output 1 Input 7067678 8 Output 47 Note In the first sample the required numbers are: 104, 140, 410. In the second sample the required number is 232.
2
10
#include <bits/stdc++.h> using namespace std; const int W = 18, M = 100; int m, len; long long n, f[1 << W | 7][M | 7]; vector<int> d; inline void Pre() { memset(f, -1, sizeof f); } inline long long Dfs(register int w, register int st, register int sum) { if (!w) return sum == 0; if (~f[st][sum]) return f[st][sum]; register long long res = 0; for (register int i = 0; i < len; i++) if (!((1 << i) & st) && (i == 0 || d[i] != d[i - 1] || ((1 << (i - 1)) & st))) res += Dfs(w - 1, st | (1 << i), (sum * 10 + d[i]) % m); return f[st][sum] = res; } inline long long DP() { for (; n; n /= 10) d.push_back(n % 10); sort(d.begin(), d.end()), len = d.size(); register long long res = 0; for (register int i = 0; i < len; i++) if (d[i] && (i == 0 || d[i] != d[i - 1])) res += Dfs(len - 1, 1 << i, d[i] % m); return res; } int main() { scanf("%lld%d", &n, &m), Pre(); printf("%lld\n", DP()); return 0; }
CPP
401_D. Roman and Numbers
Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m. Number x is considered close to number n modulo m, if: * it can be obtained by rearranging the digits of number n, * it doesn't have any leading zeroes, * the remainder after dividing number x by m equals 0. Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him. Input The first line contains two integers: n (1 ≀ n < 1018) and m (1 ≀ m ≀ 100). Output In a single line print a single integer β€” the number of numbers close to number n modulo m. Examples Input 104 2 Output 3 Input 223 4 Output 1 Input 7067678 8 Output 47 Note In the first sample the required numbers are: 104, 140, 410. In the second sample the required number is 232.
2
10
#include <bits/stdc++.h> using namespace std; const double PI = acos(-1.0); const double eps = 1e-8; const int mod = 1e9 + 7; const int inf = 1061109567; const int dir[][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}}; int digit[20], m, cnt; long long dp[1 << 18][101]; long long dfs(int s, int num) { if (num == 0 && s == 0) return 1; if (~dp[s][num]) return dp[s][num]; int used[10]; memset(used, 0, sizeof(used)); long long ret = 0; for (int i = 0; i < cnt; i++) { if (!(s & (1 << i))) continue; if (used[digit[i]]) continue; used[digit[i]] = 1; ret += dfs(s ^ (1 << i), (num * 10 + digit[i]) % m); } return dp[s][num] = ret; } int main() { int num[12]; memset(num, 0, sizeof(num)); memset(dp, -1, sizeof(dp)); long long n; cin >> n >> m; while (n) { digit[cnt++] = n % 10; n /= 10; } long long ans = 0; long long s = (1 << cnt) - 1; for (int i = 0; i < cnt; i++) { if (digit[i]) { if (num[digit[i]]) continue; num[digit[i]] = 1; ans += dfs(s ^ (1 << i), digit[i] % m); } } cout << ans << endl; return 0; }
CPP
401_D. Roman and Numbers
Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m. Number x is considered close to number n modulo m, if: * it can be obtained by rearranging the digits of number n, * it doesn't have any leading zeroes, * the remainder after dividing number x by m equals 0. Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him. Input The first line contains two integers: n (1 ≀ n < 1018) and m (1 ≀ m ≀ 100). Output In a single line print a single integer β€” the number of numbers close to number n modulo m. Examples Input 104 2 Output 3 Input 223 4 Output 1 Input 7067678 8 Output 47 Note In the first sample the required numbers are: 104, 140, 410. In the second sample the required number is 232.
2
10
#include <bits/stdc++.h> using namespace std; string s; int n, m, a[18]; int freq[18]; long long fact[18]; long long dp[1 << 18][101]; int main() { fact[0] = 1; for (int i = 1; i < 18; ++i) { fact[i] = fact[i - 1] * i; } cin >> s >> m; n = s.length(); for (int i = 0; i < n; ++i) { a[i] = s[i] - '0'; freq[a[i]] += 1; } int l = 1 << n; dp[0][0] = 1; for (int mask = 0; mask < l; ++mask) { for (int i = 0; i < n; ++i) { if (mask & (1 << i)) { continue; } if (mask == 0 && a[i] == 0) { continue; } for (int mod = 0; mod < m; ++mod) { dp[mask ^ (1 << i)][(mod * 10 + a[i]) % m] += dp[mask][mod]; } } } long long ans = dp[l - 1][0]; for (int i = 0; i < 10; ++i) { ans /= fact[freq[i]]; } cout << ans << "\n"; return 0; }
CPP
401_D. Roman and Numbers
Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m. Number x is considered close to number n modulo m, if: * it can be obtained by rearranging the digits of number n, * it doesn't have any leading zeroes, * the remainder after dividing number x by m equals 0. Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him. Input The first line contains two integers: n (1 ≀ n < 1018) and m (1 ≀ m ≀ 100). Output In a single line print a single integer β€” the number of numbers close to number n modulo m. Examples Input 104 2 Output 3 Input 223 4 Output 1 Input 7067678 8 Output 47 Note In the first sample the required numbers are: 104, 140, 410. In the second sample the required number is 232.
2
10
#include <bits/stdc++.h> using namespace std; string S; int MOD; int len; long long dp[1 << 18][100]; int cnt[1 << 18]; int bits[1 << 18]; int DMASKS[1 << 18]; int powers[20]; int orig; long long solve(int mask, int mod) { if (mask == 0) { return mod == 0; } long long &ret = dp[mask][mod]; if (ret != -1) return ret; ret = 0; int pos = len - cnt[mask]; int dMask = pos == 0; int m = mask; while (m > 0) { int b = m & -m; if (!(dMask & (1 << (S[bits[b]] - '0')))) { ret += solve(mask ^ b, (mod + powers[len - pos - 1] * (S[bits[b]] - '0')) % MOD); dMask |= 1 << (S[bits[b]] - '0'); } m -= b; } return ret; } int main() { memset(dp, -1, sizeof dp); cin >> S >> MOD; len = S.size(); powers[0] = 1; powers[0] %= MOD; for (int i = 1; i < 20; i++) { powers[i] = powers[i - 1] * 10; powers[i] %= MOD; } for (int i = 0; i < 18; i++) { bits[1 << i] = i; } for (int i = 0, to = 1 << 18; i < to; i++) { cnt[i] = __builtin_popcount(i); } orig = (1 << len) - 1; cout << solve(orig, 0) << endl; return 0; }
CPP
401_D. Roman and Numbers
Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m. Number x is considered close to number n modulo m, if: * it can be obtained by rearranging the digits of number n, * it doesn't have any leading zeroes, * the remainder after dividing number x by m equals 0. Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him. Input The first line contains two integers: n (1 ≀ n < 1018) and m (1 ≀ m ≀ 100). Output In a single line print a single integer β€” the number of numbers close to number n modulo m. Examples Input 104 2 Output 3 Input 223 4 Output 1 Input 7067678 8 Output 47 Note In the first sample the required numbers are: 104, 140, 410. In the second sample the required number is 232.
2
10
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; int arr[19]; int md; int n; long long dp[(1 << 18)][100]; long long fact[19]; std::map<int, int> m1; long long first(int mask, int m) { if (mask == (1 << n) - 1) return m == 0; if (dp[mask][m] != -1) return dp[mask][m]; long long ans = 0; for (int j = 0; j < n; j++) { if (!(mask & (1 << j))) { if (arr[j] == 0 && mask == 0) continue; ans += first(mask | (1 << j), (m * 10 + arr[j]) % md); } } return dp[mask][m] = ans; } int main(int argc, char const *argv[]) { ios_base::sync_with_stdio(false); cin.tie(NULL); long long x; cin >> x >> md; fact[0] = 1; for (long long i = 1; i < 19; i++) { fact[i] = fact[i - 1] * i; } int i = 0; while (x) { arr[i] = x % 10; m1[x % 10] += 1; x /= 10; i++; } long long divby = 1; ; for (std::map<int, int>::iterator it = m1.begin(); it != m1.end(); it++) { divby *= fact[it->second]; } n = i; memset(dp, -1, sizeof(dp)); cout << first(0, 0) / divby; return 0; }
CPP
401_D. Roman and Numbers
Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m. Number x is considered close to number n modulo m, if: * it can be obtained by rearranging the digits of number n, * it doesn't have any leading zeroes, * the remainder after dividing number x by m equals 0. Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him. Input The first line contains two integers: n (1 ≀ n < 1018) and m (1 ≀ m ≀ 100). Output In a single line print a single integer β€” the number of numbers close to number n modulo m. Examples Input 104 2 Output 3 Input 223 4 Output 1 Input 7067678 8 Output 47 Note In the first sample the required numbers are: 104, 140, 410. In the second sample the required number is 232.
2
10
#include <bits/stdc++.h> #pragma gcc optimize("Ofast") using namespace std; unsigned long long dp[1 << 18][100] = {0}, f[10] = {0}, m, d = 1; int main() { string n; cin >> n >> m; for (int i = 0; i < n.size(); i++) d *= ++f[n[i] -= '0']; dp[0][0] = 1; unsigned long long all = (1 << n.size()) - 1; for (unsigned long long i = 0; i <= all; i++) { for (int j = 0; j < m; j++) { for (int k = 0; k < n.size(); k++) { if (i == 0 && n[k] == 0) continue; if (!(i & 1 << k)) { dp[i | (1 << k)][((j * 10) % m + n[k]) % m] += dp[i][j]; } } } } cout << dp[all][0] / d << endl; }
CPP
401_D. Roman and Numbers
Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m. Number x is considered close to number n modulo m, if: * it can be obtained by rearranging the digits of number n, * it doesn't have any leading zeroes, * the remainder after dividing number x by m equals 0. Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him. Input The first line contains two integers: n (1 ≀ n < 1018) and m (1 ≀ m ≀ 100). Output In a single line print a single integer β€” the number of numbers close to number n modulo m. Examples Input 104 2 Output 3 Input 223 4 Output 1 Input 7067678 8 Output 47 Note In the first sample the required numbers are: 104, 140, 410. In the second sample the required number is 232.
2
10
import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.Arrays; import java.util.StringTokenizer; public class Main{ public static void main(String[] args) { solve(); } static int mod, state[] = new int[11]; static long dp[][]; private static void solve() { char num[] = IN.next().toCharArray(); int cnt[] = new int[10]; for (int i = 0; i < num.length; i++) { cnt[num[i] - '0']++; } state[0] = 1; for (int i = 1; i <= 10; i++) { state[i] = state[i - 1] * (cnt[i - 1] + 1); } dp = new long[mod = IN.nextInt()][state[10] + 1]; for (long tmp[] : dp) { Arrays.fill(tmp, -1); } System.out.println(dp(num.length - 1, 0, state[10], num, true)); } public static long dp(int pos, int cur, int mask, char num[], boolean leadZero) { if (pos < 0) { return cur == 0 ? 1 : 0; } if (dp[cur][mask] != -1) { return dp[cur][mask]; } long res = 0; int t, seen = 0; for (int i = pos; i >= 0; i--) { t = num[i] - '0'; if (leadZero && t == 0 || (seen & (1 << t)) != 0) { continue; } seen |= (1 << t); swap(num, pos, i); res += dp(pos - 1, (cur * 10 + t) % mod, mask - state[t], num, false); swap(num, pos, i); } return dp[cur][mask] = res; } public static void swap(char num[], int i, int j) { char tmp = num[i]; num[i] = num[j]; num[j] = tmp; } static class IN { private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in), 65535); private static StringTokenizer st = null; public static String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (Exception e) { break; } } return st.nextToken(); } public static int nextInt() { return Integer.valueOf(next()); } } }
JAVA
401_D. Roman and Numbers
Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m. Number x is considered close to number n modulo m, if: * it can be obtained by rearranging the digits of number n, * it doesn't have any leading zeroes, * the remainder after dividing number x by m equals 0. Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him. Input The first line contains two integers: n (1 ≀ n < 1018) and m (1 ≀ m ≀ 100). Output In a single line print a single integer β€” the number of numbers close to number n modulo m. Examples Input 104 2 Output 3 Input 223 4 Output 1 Input 7067678 8 Output 47 Note In the first sample the required numbers are: 104, 140, 410. In the second sample the required number is 232.
2
10
#include <bits/stdc++.h> using namespace std; string s; long long n, mod, fact[18], dp[1 << 18][100], cnt[26], ans; int main() { cin >> s >> mod; n = s.size(); fact[0] = dp[0][0] = 1; for (int i = 1; i < 18; ++i) fact[i] = fact[i - 1] * i; for (auto &it : s) ++cnt[it - '0']; for (int mask = 0; mask < (1 << n); ++mask) { for (int i = 0; i < mod; ++i) { if (dp[mask][i] == 0) continue; for (int d = 0; d < n; ++d) { if (mask == 0 && s[d] == '0') continue; if (mask & (1 << d)) continue; dp[mask | (1 << d)][(i * 10 + s[d] - '0') % mod] += dp[mask][i]; } } } ans = dp[(1 << n) - 1][0]; for (int i = 0; i <= 9; ++i) ans /= max(1ll, fact[cnt[i]]); cout << ans; return 0; }
CPP
401_D. Roman and Numbers
Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m. Number x is considered close to number n modulo m, if: * it can be obtained by rearranging the digits of number n, * it doesn't have any leading zeroes, * the remainder after dividing number x by m equals 0. Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him. Input The first line contains two integers: n (1 ≀ n < 1018) and m (1 ≀ m ≀ 100). Output In a single line print a single integer β€” the number of numbers close to number n modulo m. Examples Input 104 2 Output 3 Input 223 4 Output 1 Input 7067678 8 Output 47 Note In the first sample the required numbers are: 104, 140, 410. In the second sample the required number is 232.
2
10
#include <bits/stdc++.h> using namespace std; long long n, k, i, f[100], j, ii, dp[(1LL << 18)][101], s[11], d[20]; char ss[100]; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cin >> ss >> k; n = strlen(ss); for (i = 0; i < n; i++) f[i] = ss[i] - '0', s[f[i]]++; sort(f, f + n); d[0] = 1; for (i = 1; i < 19; i++) d[i] = d[i - 1] * i; dp[0][0] = 1; for (i = 0; i < (1LL << n); i++) for (j = 0; j < k; j++) { if (dp[i][j] == 0) continue; for (ii = 0; ii < n; ii++) if (((1LL << ii) & i) == 0) { if (f[ii] == 0 && i == 0) continue; else dp[(i | (1LL << ii))][(j * 10 + f[ii]) % k] += dp[i][j]; } } for (i = 0; i < 10; i++) dp[(1LL << n) - 1][0] /= d[s[i]]; cout << dp[(1LL << n) - 1][0]; }
CPP
401_D. Roman and Numbers
Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m. Number x is considered close to number n modulo m, if: * it can be obtained by rearranging the digits of number n, * it doesn't have any leading zeroes, * the remainder after dividing number x by m equals 0. Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him. Input The first line contains two integers: n (1 ≀ n < 1018) and m (1 ≀ m ≀ 100). Output In a single line print a single integer β€” the number of numbers close to number n modulo m. Examples Input 104 2 Output 3 Input 223 4 Output 1 Input 7067678 8 Output 47 Note In the first sample the required numbers are: 104, 140, 410. In the second sample the required number is 232.
2
10
#include <bits/stdc++.h> using namespace std; long long n, m; long long dp[1 << 18][110]; vector<int> digits; long long solve(int mask, int k) { long long res = 0; int p = digits.size() - 1 - __builtin_popcount(mask); if (p < 0) return k == 0; if (dp[mask][k] != -1) return dp[mask][k]; long long mult = pow(10LL, p); mult %= m; for (int i = 0; i < digits.size(); i++) { if (mask & (1 << i)) continue; if (p == digits.size() - 1 && digits[i] == 0) continue; if (i > 0 && !(1 << i - 1 & mask) && digits[i] == digits[i - 1]) continue; long long r = (k + mult * digits[i]) % m; res += solve(mask | (1 << i), r); } return dp[mask][k] = res; } int main() { cin >> n >> m; long long aux = n; while (aux > 0) { digits.push_back(aux % 10); aux /= 10; } sort(digits.begin(), digits.end()); memset(dp, -1, sizeof(dp)); cout << solve(0, 0) << endl; return 0; }
CPP
401_D. Roman and Numbers
Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m. Number x is considered close to number n modulo m, if: * it can be obtained by rearranging the digits of number n, * it doesn't have any leading zeroes, * the remainder after dividing number x by m equals 0. Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him. Input The first line contains two integers: n (1 ≀ n < 1018) and m (1 ≀ m ≀ 100). Output In a single line print a single integer β€” the number of numbers close to number n modulo m. Examples Input 104 2 Output 3 Input 223 4 Output 1 Input 7067678 8 Output 47 Note In the first sample the required numbers are: 104, 140, 410. In the second sample the required number is 232.
2
10
#include <bits/stdc++.h> using namespace std; long long w[20], cnt = -1, vis[10], n, m, f[(1 << 18) + 10][105]; signed main() { cin >> n >> m; while (n) { w[++cnt] = n % 10; n /= 10; } f[0][0] = 1; for (long long s = 1; s < 1 << (cnt + 1); s++) { memset(vis, 0, sizeof(vis)); for (long long i = 0; i <= cnt; i++) { if (s == (1 << i) && !w[i]) continue; if (!(s & (1 << i)) || vis[w[i]]) continue; vis[w[i]] = 1; for (long long j = 0; j < m; j++) f[s][(j * 10 + w[i]) % m] += f[s ^ (1 << i)][j]; } } cout << f[(1 << (cnt + 1)) - 1][0]; return 0; }
CPP
401_D. Roman and Numbers
Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m. Number x is considered close to number n modulo m, if: * it can be obtained by rearranging the digits of number n, * it doesn't have any leading zeroes, * the remainder after dividing number x by m equals 0. Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him. Input The first line contains two integers: n (1 ≀ n < 1018) and m (1 ≀ m ≀ 100). Output In a single line print a single integer β€” the number of numbers close to number n modulo m. Examples Input 104 2 Output 3 Input 223 4 Output 1 Input 7067678 8 Output 47 Note In the first sample the required numbers are: 104, 140, 410. In the second sample the required number is 232.
2
10
#include <bits/stdc++.h> using namespace std; long long dp[1 << 18][110], per = 1, fact[12], cnt[12]; int main() { int m, i; char str[50]; scanf("%s%d", str, &m); int n = strlen(str); for (i = 0; i < n; i++) cnt[str[i] - '0']++; dp[0][0] = 1; for (int i = 0; i < (1ll << n); i++) { for (int j = 0; j < n; j++) { if (!(i & (1 << j))) for (int k = 0; k < m; k++) { if (i || (str[j] - '0')) dp[(i | (1 << j))][(k * 10 + (str[j] - '0')) % m] += dp[i][k]; } } } fact[0] = 1; for (i = 1; i <= n; i++) fact[i] = fact[i - 1] * i; for (i = 0; i <= 9; i++) per *= fact[cnt[i]]; printf("%I64d\n", dp[(1 << n) - 1][0] / per); }
CPP
401_D. Roman and Numbers
Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m. Number x is considered close to number n modulo m, if: * it can be obtained by rearranging the digits of number n, * it doesn't have any leading zeroes, * the remainder after dividing number x by m equals 0. Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him. Input The first line contains two integers: n (1 ≀ n < 1018) and m (1 ≀ m ≀ 100). Output In a single line print a single integer β€” the number of numbers close to number n modulo m. Examples Input 104 2 Output 3 Input 223 4 Output 1 Input 7067678 8 Output 47 Note In the first sample the required numbers are: 104, 140, 410. In the second sample the required number is 232.
2
10
#include <bits/stdc++.h> using namespace std; constexpr int N = (1 << 18) + 10; constexpr int M = 100 + 10; long long m, x, dp[N][M], h, n, fact[M], p, sz; vector<int> digit; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n >> m; while (n) { digit.push_back(n % 10); n /= 10; } sz = (int)digit.size(); fact[0] = 1; for (int i = 1; i < M; i++) fact[i] = fact[i - 1] * i; x = 1 << sz; for (int i = 0; i < sz; i++) if (digit[i]) dp[1 << i][digit[i] % m]++; for (int mask = 1; mask < x; mask++) { for (int i = 0; i < m; i++) { p = 1; for (int j = 0; j < sz; j++) { if (p & mask) { p *= 2; continue; } dp[mask + p][((10 * i) + digit[j]) % m] += dp[mask][i]; p *= 2; } } } for (int i = 0; i < 10; i++) { int cnt = 0; for (auto j : digit) if (j == i) cnt++; dp[x - 1][0] /= fact[cnt]; } cout << dp[x - 1][0] << '\n'; return 0; }
CPP
401_D. Roman and Numbers
Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m. Number x is considered close to number n modulo m, if: * it can be obtained by rearranging the digits of number n, * it doesn't have any leading zeroes, * the remainder after dividing number x by m equals 0. Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him. Input The first line contains two integers: n (1 ≀ n < 1018) and m (1 ≀ m ≀ 100). Output In a single line print a single integer β€” the number of numbers close to number n modulo m. Examples Input 104 2 Output 3 Input 223 4 Output 1 Input 7067678 8 Output 47 Note In the first sample the required numbers are: 104, 140, 410. In the second sample the required number is 232.
2
10
#include <bits/stdc++.h> using namespace std; inline long long Read() { long long res = 0, f = 1; char c; while (c = getchar(), c < 48 || c > 57) if (c == '-') f = 0; do res = (res << 3) + (res << 1) + (c ^ 48); while (c = getchar(), c >= 48 && c <= 57); return f ? res : -res; } template <class T> inline bool Min(T &a, T const &b) { return a > b ? a = b, 1 : 0; } template <class T> inline bool Max(T &a, T const &b) { return a < b ? a = b, 1 : 0; } const long long N = 3e5 + 5, M = 3e5 + 5, mod = 1e9 + 7; bool MOP1; long long dp[5 + (1 << 18)][105], vis[11], Num[N]; bool MOP2; inline void _main(void) { long long n = Read(), m = Read(), tot = 0; while (n) Num[++tot] = n % 10, n /= 10; dp[0][0] = 1; for (register long long i = (1), i_end_ = (1 << tot); i < i_end_; ++i) { memset(vis, 0, sizeof vis); for (register long long j = (1), j_end_ = (tot); j <= j_end_; ++j) if (i & (1 << (j - 1))) { if (!Num[j] && !(i ^ (1 << (j - 1)))) continue; if (vis[Num[j]]) continue; vis[Num[j]] = 1; for (register long long k = (0), k_end_ = (m); k < k_end_; ++k) dp[i][((k * 10) + Num[j]) % m] += dp[i ^ (1 << (j - 1))][k]; } } printf("%lld\n", dp[(1 << tot) - 1][0]); } signed main() { _main(); return 0; }
CPP
401_D. Roman and Numbers
Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m. Number x is considered close to number n modulo m, if: * it can be obtained by rearranging the digits of number n, * it doesn't have any leading zeroes, * the remainder after dividing number x by m equals 0. Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him. Input The first line contains two integers: n (1 ≀ n < 1018) and m (1 ≀ m ≀ 100). Output In a single line print a single integer β€” the number of numbers close to number n modulo m. Examples Input 104 2 Output 3 Input 223 4 Output 1 Input 7067678 8 Output 47 Note In the first sample the required numbers are: 104, 140, 410. In the second sample the required number is 232.
2
10
import java.io.*; import java.text.DecimalFormat; import java.util.*; public class Main { static int mod = (int)1e9+7; static int N = 18; static int S = 1<<N; int m, mx; int[] ar = new int[N]; int dep; long n; long[][] d = new long[S][105]; void work() throws Exception { //in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); // Scanner cin = new Scanner(System.in); ///////////////////////////////////////// n = cin.nextLong(); m = cin.nextInt(); dep = 0; while(n > 0) { ar[dep++] = (int)(n%10); n /= 10; } Arrays.sort(ar, 0, dep); mx = (1 << dep) - 1; for(int i = 0; i <= mx; i ++) Arrays.fill(d[i], 0); d[0][0] = 1; for (int i = 0; i <= mx; ++i) { for (int j = 0; j < m; ++j) if (d[i][j] > 0) { for (int k = 0; k < dep; ++k) { if (i == 0 && ar[k] == 0) continue; if (0 == (i >> k & 1)) { d[i | (1 << k)][(j * 10 + ar[k]) % m] += d[i][j]; while (k + 1 < dep && ar[k + 1] == ar[k]) ++ k; } } } } out.print(d[mx][0]); } // BufferedReader in; // StringTokenizer str = null; static PrintWriter out; // private String next() throws Exception{ // while (str == null || !str.hasMoreElements()) // str = new StringTokenizer(in.readLine()); // return str.nextToken(); // } // private int nextInt() throws Exception{ // return Integer.parseInt(next()); // } // private long nextLong() throws Exception{ // return Long.parseLong(next()); // } static int dx[] = {0,1,0,-1}; static int dy[] = {1,0,-1,0}; class pair implements Comparable<pair>{ int first, second; pair(int x, int y) { first = x; second = y; } public int compareTo(pair o) { // TODO auto-generated method stub if(first != o.first) return ((Integer)first).compareTo(o.first); else return ((Integer)second).compareTo(o.second); } } DecimalFormat df=new DecimalFormat("0.000000"); public static void main(String[] args) throws Exception{ Main wo = new Main(); wo.work(); out.close(); } }
JAVA
401_D. Roman and Numbers
Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m. Number x is considered close to number n modulo m, if: * it can be obtained by rearranging the digits of number n, * it doesn't have any leading zeroes, * the remainder after dividing number x by m equals 0. Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him. Input The first line contains two integers: n (1 ≀ n < 1018) and m (1 ≀ m ≀ 100). Output In a single line print a single integer β€” the number of numbers close to number n modulo m. Examples Input 104 2 Output 3 Input 223 4 Output 1 Input 7067678 8 Output 47 Note In the first sample the required numbers are: 104, 140, 410. In the second sample the required number is 232.
2
10
#include <bits/stdc++.h> using namespace std; int m, bit, sum; int a[20]; long long dp[262150][105]; long long ten[20]; long long n, ans; bool mark[15]; map<long long, int> pos; void init() { ten[0] = 1; for (int i = 1; i < 18; i++) ten[i] = ten[i - 1] * 10; for (int i = 1; i <= 18; i++) pos[1 << (i - 1)] = i; long long h = n; while (h) { a[++bit] = h % 10; h /= 10; if (!a[bit]) sum++; } } int main() { scanf("%lld%d", &n, &m); init(); dp[0][0] = 1; for (int i = 1; i < (1 << bit); i++) { int h = i, cnt = 0, num[20], sm = 0; while (h) { int o = h & (-h), nm = pos[o]; num[++cnt] = nm; h ^= o; if (!a[nm]) sm++; } if (sum - sm == bit - cnt && cnt != bit) continue; for (int j = 1; j <= cnt; j++) { int nm = num[j]; if (mark[a[nm]]) continue; mark[a[nm]] = 1; for (int k = 0; k < m; k++) { int flag = (ten[cnt - 1] * a[nm] + k) % m; dp[i][flag] += dp[i ^ (1 << (nm - 1))][k]; } } memset(mark, 0, sizeof(mark)); } printf("%lld\n", dp[(1 << bit) - 1][0]); return 0; }
CPP
401_D. Roman and Numbers
Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m. Number x is considered close to number n modulo m, if: * it can be obtained by rearranging the digits of number n, * it doesn't have any leading zeroes, * the remainder after dividing number x by m equals 0. Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him. Input The first line contains two integers: n (1 ≀ n < 1018) and m (1 ≀ m ≀ 100). Output In a single line print a single integer β€” the number of numbers close to number n modulo m. Examples Input 104 2 Output 3 Input 223 4 Output 1 Input 7067678 8 Output 47 Note In the first sample the required numbers are: 104, 140, 410. In the second sample the required number is 232.
2
10
#include <bits/stdc++.h> using namespace std; long long dp[1 << 18][100], n, m, a[20]; int waight[20], w[20][20], pre[20], endstate; long long solve(int state, int mod, int ai) { if (state == endstate) return mod == 0; long long &ret = dp[state][mod]; if (ret != -1) return ret; ret = 0; int nmod, nstate; for (int i = 0; i < n; ++i) { if (state & (1 << i)) continue; if (state & (1 << pre[i])) continue; nmod = (mod - w[ai][i]); if (nmod < 0) nmod += m; nstate = (state | (1 << i)); if (nstate == endstate && a[i] == 0) break; ret += solve(state | (1 << i), nmod, ai + 1); } return ret; } int main() { memset(dp, -1, sizeof dp); long long x, tkrar = 1, b[10] = {0}; cin >> x >> m; for (n = 0; x; ++n) { a[n] = x % 10; ++b[a[n]]; tkrar *= b[a[n]]; x /= 10; } endstate = (1 << n) - 1; waight[0] = 1; for (int i = 1; i < 20; ++i) waight[i] = (waight[i - 1] * 10) % m; for (int i = 0; i < 20; ++i) { for (int j = 0; j < n; ++j) w[i][j] = (a[j] * waight[i]) % m; } for (int i = 0; i < n; ++i) { pre[i] = n; for (int j = 0; j < i; ++j) { if (a[i] == a[j]) pre[i] = j; } } cout << solve(0, 0, 0); }
CPP
401_D. Roman and Numbers
Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m. Number x is considered close to number n modulo m, if: * it can be obtained by rearranging the digits of number n, * it doesn't have any leading zeroes, * the remainder after dividing number x by m equals 0. Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him. Input The first line contains two integers: n (1 ≀ n < 1018) and m (1 ≀ m ≀ 100). Output In a single line print a single integer β€” the number of numbers close to number n modulo m. Examples Input 104 2 Output 3 Input 223 4 Output 1 Input 7067678 8 Output 47 Note In the first sample the required numbers are: 104, 140, 410. In the second sample the required number is 232.
2
10
import java.io.*; import java.util.*; public class C235D { public static StringTokenizer st; public static void nextLine(BufferedReader br) throws IOException { st = new StringTokenizer(br.readLine()); } public static String next() { return st.nextToken(); } public static int nextInt() { return Integer.parseInt(st.nextToken()); } public static long nextLong() { return Long.parseLong(st.nextToken()); } public static double nextDouble() { return Double.parseDouble(st.nextToken()); } static long[][] dp; static long[] md; static int[] d, cd; static long[][] modx; static long m; public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); nextLine(br); long n = nextLong(); m = nextLong(); d = new int[10]; long copy = n; int nd = 0; while (copy > 0) { d[(int)(copy % 10)]++; copy /= 10; nd++; } cd = new int[10]; cd[0] = d[0]; for (int i = 1; i < 10; i++) { cd[i] = cd[i-1] + d[i]; } md = new long[10]; md[0] = d[0] + 1; for (int i = 1; i < 10; i++) { md[i] = md[i-1] * (d[i] + 1); } long total = 0; modx = new long[10][18]; for (int i = 0; i < 10; i++) { long val = i; for (int j = 0; j < 18; j++) { modx[i][j] = val % m; val *= 10; } } dp = new long[(int)md[9]][(int)m]; for (int i = 0; i < dp.length; i++) { Arrays.fill(dp[i], -1); } for (int i = 1; i <= 9; i++) { if (d[i] == 0) continue; long val = md[9] - (i == 0 ? 1 : md[i-1]) - 1; total += solve(val, (int)((m - modx[i][nd-1]) % m), nd - 1); } System.out.println(total); } private static long solve(long val, int rem, int digits) { //System.out.println("calling " + val + " " + rem + " " + digits); if (dp[(int)val][rem] != -1) { return dp[(int)val][rem]; } if (digits == 0) { return rem == 0 ? 1 : 0; } long total = 0; for (int i = 0; i < 10; i++) { long num = (val % md[i]) / (i == 0 ? 1 : md[i-1]); if (num == 0) continue; total += solve(val - (i == 0 ? 1 : md[i-1]), (int)(m + rem - modx[i][digits - 1]) % (int)m, digits - 1); } dp[(int)val][rem] = total; return total; } }
JAVA
401_D. Roman and Numbers
Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m. Number x is considered close to number n modulo m, if: * it can be obtained by rearranging the digits of number n, * it doesn't have any leading zeroes, * the remainder after dividing number x by m equals 0. Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him. Input The first line contains two integers: n (1 ≀ n < 1018) and m (1 ≀ m ≀ 100). Output In a single line print a single integer β€” the number of numbers close to number n modulo m. Examples Input 104 2 Output 3 Input 223 4 Output 1 Input 7067678 8 Output 47 Note In the first sample the required numbers are: 104, 140, 410. In the second sample the required number is 232.
2
10
import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.StringTokenizer; /** * Created by chinh on 7/19/14. */ public class R235D2D { static long factorial(int x){ long ret = 1; for(int i=2; i<=x; i++) ret*=i; return ret; } public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(br.readLine()); char[] s = st.nextToken().toCharArray(); int n = s.length; int m = Integer.parseInt(st.nextToken()); long[][] dp = new long[1<<n][m]; dp[0][0]=1; for(int i=1; i<(1<<n); i++){ for(int p=0; p<n; p++){ if((i&(1<<p))>0){ int j = i ^ (1<<p); if(j==0 && s[p]=='0') continue; for(int r=0; r<m; r++){ dp[i][(r*10+s[p]-'0')%m] += dp[j][r]; } } } } int[] count = new int[10]; for(int i=0; i<n; i++) count[s[i]-'0']++; long ret = dp[(1<<n)-1][0]; for(int i=0; i<10; i++) ret/=factorial(count[i]); System.out.println(ret); } }
JAVA
401_D. Roman and Numbers
Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m. Number x is considered close to number n modulo m, if: * it can be obtained by rearranging the digits of number n, * it doesn't have any leading zeroes, * the remainder after dividing number x by m equals 0. Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him. Input The first line contains two integers: n (1 ≀ n < 1018) and m (1 ≀ m ≀ 100). Output In a single line print a single integer β€” the number of numbers close to number n modulo m. Examples Input 104 2 Output 3 Input 223 4 Output 1 Input 7067678 8 Output 47 Note In the first sample the required numbers are: 104, 140, 410. In the second sample the required number is 232.
2
10
#include <bits/stdc++.h> using namespace std; int main() { int64_t n, m; cin >> n >> m; stringstream ss; ss << n; string ns = ss.str(); if (ns.size() == 1) { if (m > 1) { cout << 0 << endl; } else { cout << 1 << endl; } } else { vector<int64_t> m10(19); vector<int64_t> fact(19); m10[0] = 1; fact[0] = 1; for (int64_t i = (1); i < (19); ++i) { m10[i] = m10[i - 1] * 10; m10[i] %= m; fact[i] = fact[i - 1] * i; } vector<vector<int64_t>> dp(1 << 18, vector<int64_t>(m)); dp[0][0] = 1; int64_t s = ns.size(); for (int64_t i = (0); i < (1 << s); ++i) { bitset<18> bs(i); for (int64_t j = (0); j < (s); ++j) { if (bs.count() == s - 1 && ns[j] == '0') continue; if (i != (i | (1 << j))) { int64_t k = i | (1 << j); int64_t mod = (m10[bs.count()] * (ns[j] - '0')) % m; for (int64_t l = (0); l < (m); ++l) { dp[k][(l + mod) % m] += dp[i][l]; } } } } vector<int> cnt(10); for (int64_t i = (0); i < (s); ++i) { cnt[ns[i] - '0']++; } int64_t sum = dp[(1 << s) - 1][0]; for (int64_t i = (0); i < (10); ++i) { sum /= fact[cnt[i]]; } cout << sum << endl; } return 0; }
CPP
401_D. Roman and Numbers
Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m. Number x is considered close to number n modulo m, if: * it can be obtained by rearranging the digits of number n, * it doesn't have any leading zeroes, * the remainder after dividing number x by m equals 0. Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him. Input The first line contains two integers: n (1 ≀ n < 1018) and m (1 ≀ m ≀ 100). Output In a single line print a single integer β€” the number of numbers close to number n modulo m. Examples Input 104 2 Output 3 Input 223 4 Output 1 Input 7067678 8 Output 47 Note In the first sample the required numbers are: 104, 140, 410. In the second sample the required number is 232.
2
10
import java.io.*; import java.util.*; public class D { FastScanner in = new FastScanner(System.in); PrintWriter out = new PrintWriter(System.out); int count(long x) { int res = 0; while (x > 0) { if (x % 2 != 0) res++; x /= 2; } return res; } public void run() { String s = in.next(); int mod = in.nextInt(); int len = s.length(); long[][] dp = new long[(1<<len)][mod]; int ten = (int)(Math.pow(10, len - 1) % mod); for (int i = 0; i < len; i++) { int c = s.charAt(i) - '0'; if (c != 0) dp[(1 << i)][(c * ten) % mod] = 1; } for (int pos = 1; pos < len; pos++) { ten = (int)(Math.pow(10, len - pos - 1) % mod); for (int i = 0; i < (1 << len); i++) { if (count(i) != pos) continue; for (int j = 0; j < len; j++) { if ((i & (1 << j)) != 0) continue; int c = s.charAt(j) - '0'; int next = i | (1 << j); for (int k = 0; k < mod; k++) { dp[next][(k + c * ten) % mod] += dp[i][k]; } } } } long res = dp[(1<<len) - 1][0]; int[] cnt = new int[10]; for (int i = 0; i < len; i++) { cnt[s.charAt(i) - '0']++; } for (int i = 0; i < 10; i++) { for (int j = cnt[i]; j >= 2; j--) { res /= j; } } System.out.println(res); out.close(); } public static void main(String[] args) { new D().run(); } public void mapDebug(int[][] a) { System.out.println("--------map display---------"); for (int i = 0; i < a.length; i++) { for (int j = 0; j < a[i].length; j++) { System.out.printf("%3d ", a[i][j]); } System.out.println(); } System.out.println("----------------------------"); System.out.println(); } public void debug(Object... obj) { System.out.println(Arrays.deepToString(obj)); } class FastScanner { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; public FastScanner(InputStream stream) { this.stream = stream; //stream = new FileInputStream(new File("dec.in")); } int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } boolean isEndline(int c) { return c == '\n' || c == '\r' || c == -1; } int nextInt() { return Integer.parseInt(next()); } int[] nextIntArray(int n) { int[] array = new int[n]; for (int i = 0; i < n; i++) array[i] = nextInt(); return array; } int[][] nextIntMap(int n, int m) { int[][] map = new int[n][m]; for (int i = 0; i < n; i++) { map[i] = in.nextIntArray(m); } return map; } long nextLong() { return Long.parseLong(next()); } long[] nextLongArray(int n) { long[] array = new long[n]; for (int i = 0; i < n; i++) array[i] = nextLong(); return array; } long[][] nextLongMap(int n, int m) { long[][] map = new long[n][m]; for (int i = 0; i < n; i++) { map[i] = in.nextLongArray(m); } return map; } double nextDouble() { return Double.parseDouble(next()); } double[] nextDoubleArray(int n) { double[] array = new double[n]; for (int i = 0; i < n; i++) array[i] = nextDouble(); return array; } double[][] nextDoubleMap(int n, int m) { double[][] map = new double[n][m]; for (int i = 0; i < n; i++) { map[i] = in.nextDoubleArray(m); } return map; } String next() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } String[] nextStringArray(int n) { String[] array = new String[n]; for (int i = 0; i < n; i++) array[i] = next(); return array; } String nextLine() { int c = read(); while (isEndline(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isEndline(c)); return res.toString(); } } }
JAVA
401_D. Roman and Numbers
Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m. Number x is considered close to number n modulo m, if: * it can be obtained by rearranging the digits of number n, * it doesn't have any leading zeroes, * the remainder after dividing number x by m equals 0. Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him. Input The first line contains two integers: n (1 ≀ n < 1018) and m (1 ≀ m ≀ 100). Output In a single line print a single integer β€” the number of numbers close to number n modulo m. Examples Input 104 2 Output 3 Input 223 4 Output 1 Input 7067678 8 Output 47 Note In the first sample the required numbers are: 104, 140, 410. In the second sample the required number is 232.
2
10
import java.util.*; import java.io.*; import static java.lang.Math.*; public class Main { FastIO io; // File names!!! void solve() throws IOException { char[] a = io.nextCharArray(); int[] count = new int[10]; for (char c : a) { count[c - '0']++; } int n = a.length; int m = io.nextInt(); int[] pow10 = new int[19]; long[] fact = new long[19]; pow10[0] = 1; fact[0] = 1; for (int i = 1; i < 19; i++) { pow10[i] = (pow10[i - 1] * 10) % m; fact[i] = fact[i - 1] * i; } long[][] d = new long[1 << n][m]; d[0][0] = 1; for (int mask = 0; mask < (1 << n); mask++) { int p = Integer.bitCount(mask); for (int mod = 0; mod < m; mod++) { if (d[mask][mod] == 0) { continue; } for (int i = 0; i < n; i++) { if ((mask & (1 << i)) != 0) { continue; } if (p == 0 && a[i] == '0') { continue; } int nmask = mask | (1 << i); int nmod = ((pow10[n - p - 1] * (a[i] - '0') + mod) % m); d[nmask][nmod] += d[mask][mod]; } } } long ans = d[(1 << n) - 1][0]; for (int i : count) { ans /= fact[i]; } io.println(ans); } void run() { try { io = new FastIO(); solve(); io.close(); } catch (Exception e) { e.printStackTrace(); System.exit(abs(-1)); } } public static void main(String[] args) { try { Locale.setDefault(Locale.US); } catch (Exception ignore) { } new Main().run(); } class FastIO extends PrintWriter { private BufferedReader in; private StringTokenizer stok; FastIO() { super(System.out); in = new BufferedReader(new InputStreamReader(System.in)); } FastIO(String s) throws FileNotFoundException { super("".equals(s) ? "output.txt" : s + ".out"); in = new BufferedReader(new FileReader("".equals(s) ? "input.txt" : s + ".in")); } public void close() { super.close(); try { in.close(); } catch (IOException ignored) { } } String next() { while (stok == null || !stok.hasMoreTokens()) { try { stok = new StringTokenizer(in.readLine()); } catch (Exception e) { return null; } } return stok.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { try { return in.readLine(); } catch (IOException e) { return null; } } char[] nextCharArray() { return next().toCharArray(); } } }
JAVA
401_D. Roman and Numbers
Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m. Number x is considered close to number n modulo m, if: * it can be obtained by rearranging the digits of number n, * it doesn't have any leading zeroes, * the remainder after dividing number x by m equals 0. Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him. Input The first line contains two integers: n (1 ≀ n < 1018) and m (1 ≀ m ≀ 100). Output In a single line print a single integer β€” the number of numbers close to number n modulo m. Examples Input 104 2 Output 3 Input 223 4 Output 1 Input 7067678 8 Output 47 Note In the first sample the required numbers are: 104, 140, 410. In the second sample the required number is 232.
2
10
import java.io.PrintWriter; import java.util.Arrays; import java.util.Scanner; public class D { static Scanner sc = new Scanner(System.in); static PrintWriter out = new PrintWriter(System.out); static void exit() { sc.close(); out.close(); System.exit(0); } static int [] digits; static int m; public static void main(String[] args) { char [] n = sc.next().toCharArray(); digits = new int [n.length]; for(int i = 0; i < n.length; i++) digits[i] = n[i] - '0'; m = sc.nextInt(); int mask = (1 << digits.length) - 1; dp = new long [mask+1][m]; for(int i = 0; i < dp.length; i++) Arrays.fill(dp[i], -1); out.println(dp(mask, 0)); exit(); } static long dp [][]; static long dp(int mask, int re) { if(dp[mask][re] > -1) return dp[mask][re]; if(mask == 0) { if(re == 0) { dp[mask][re] = 1; } else { dp[mask][re] = 0; } return dp[mask][re]; } if(all_zero_digits(mask)) { dp[mask][re] = 0; return dp[mask][re]; } boolean visited [] = new boolean [10]; Arrays.fill(visited, false); Arrays.fill(dp[mask], 0); for(int i = 0; i < digits.length; i++) { if((mask & (1<<i)) > 0) { if(visited[digits[i]]) continue; visited[digits[i]] = true; int pre_mask = mask - (1<<i); for(int j = 0; j < m; j++) { dp[mask][ (j*10 + digits[i]) % m] += dp(pre_mask, j); } } } return dp[mask][re]; } static boolean all_zero_digits(int mask) { for(int i = 0; i < digits.length; i++) { if((mask & (1<<i)) > 0) { if(digits[i] > 0) return false; } } return true; } }
JAVA
401_D. Roman and Numbers
Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m. Number x is considered close to number n modulo m, if: * it can be obtained by rearranging the digits of number n, * it doesn't have any leading zeroes, * the remainder after dividing number x by m equals 0. Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him. Input The first line contains two integers: n (1 ≀ n < 1018) and m (1 ≀ m ≀ 100). Output In a single line print a single integer β€” the number of numbers close to number n modulo m. Examples Input 104 2 Output 3 Input 223 4 Output 1 Input 7067678 8 Output 47 Note In the first sample the required numbers are: 104, 140, 410. In the second sample the required number is 232.
2
10
#include <bits/stdc++.h> long long solve(std::string s, int mod) { const int n = (int)s.size(); std::vector<std::vector<long long>> cnt(1 << n, std::vector<long long>(mod, 0)); std::vector<long long> pow10(1 + n, 1 % mod); for (int i = 1; i <= n; ++i) { pow10[i] = pow10[i - 1] * 10 % mod; } cnt[0][0] = 1; for (int mask = 1; mask < (1 << n); ++mask) { auto pow = pow10[__builtin_popcount(mask) - 1]; for (int j = 0; j < n; ++j) { if (!((mask >> j) & 1)) continue; auto fromMask = mask ^ (1 << j); for (int rem = 0; rem < mod; ++rem) { auto nRem = (rem + (s[j] - '0') * pow) % mod; cnt[mask][nRem] += cnt[fromMask][rem]; } } } long long answ = 0; for (int j = 0; j < n; ++j) { if (s[j] == '0') continue; auto mask = ((1 << n) - 1); mask &= ~(1 << j); auto pow = pow10[__builtin_popcount(mask)]; auto rem = (mod - ((s[j] - '0') * pow) % mod) % mod; answ += cnt[mask][rem]; } std::vector<long long> fact(1 + n, 1); for (int i = 1; i <= n; ++i) { fact[i] = fact[i - 1] * i; } std::vector<long long> nDigits(10); for (auto& it : s) { nDigits[it - '0']++; } for (int d = 0; d < 10; ++d) { answ /= fact[nDigits[d]]; } return answ; } int main() { std::string s; int mod; while (std::cin >> s >> mod) { std::cout << solve(s, mod) << std::endl; } return 0; }
CPP
401_D. Roman and Numbers
Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m. Number x is considered close to number n modulo m, if: * it can be obtained by rearranging the digits of number n, * it doesn't have any leading zeroes, * the remainder after dividing number x by m equals 0. Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him. Input The first line contains two integers: n (1 ≀ n < 1018) and m (1 ≀ m ≀ 100). Output In a single line print a single integer β€” the number of numbers close to number n modulo m. Examples Input 104 2 Output 3 Input 223 4 Output 1 Input 7067678 8 Output 47 Note In the first sample the required numbers are: 104, 140, 410. In the second sample the required number is 232.
2
10
import java.util.*; import java.io.*; import java.text.*; public class CF_401D{ //SOLUTION BEGIN void pre() throws Exception{} void solve(int TC) throws Exception{ long N = nl(); M = ni(); n = ""+N; L = n.length(); long[] pow = new long[L+1]; pow[0] = 1; for(int i = 1; i<= L; i++)pow[i] = pow[i-1]*10%M; long[][] DP = new long[1<<L][M]; for(int i = 0; i< 1<<L; i++)Arrays.fill(DP[i], -1); pn(count(DP, pow, 0, 0)); } String n; int L, M; long count(long[][] DP, long[] pow, int mask, int rem){ if(mask+1 == DP.length)return rem == 0?1:0; if(DP[mask][rem] != -1)return DP[mask][rem]; int pos = L-bit(mask)-1; long ways = 0; int vis = 0; for(int i = 0; i< L; i++){ if(((mask>>i)&1) > 0)continue; int dig = n.charAt(i)-'0'; if(pos == L-1 && dig == 0)continue; if(((vis>>dig)&1) > 0)continue; vis |= 1<<dig; int newRem = (int)(rem+M-(pow[pos]*dig)%M)%M; ways += count(DP, pow, mask|(1<<i), newRem); } return DP[mask][rem] = ways; } //SOLUTION END void hold(boolean b)throws Exception{if(!b)throw new Exception("Hold right there, Sparky!");} void exit(boolean b){if(!b)System.exit(0);} static void dbg(Object... o){System.err.println(Arrays.deepToString(o));} final long IINF = (long)1e17; final int INF = (int)1e9+2; DecimalFormat df = new DecimalFormat("0.00000000000"); double PI = 3.141592653589793238462643383279502884197169399, eps = 1e-8; static boolean multipleTC = false, memory = true, fileIO = false; FastReader in;PrintWriter out; void run() throws Exception{ long ct = System.currentTimeMillis(); if (fileIO) { in = new FastReader(""); out = new PrintWriter(""); } else { in = new FastReader(); out = new PrintWriter(System.out); } //Solution Credits: Taranpreet Singh int T = multipleTC? ni():1; pre(); for (int t = 1; t <= T; t++) solve(t); out.flush(); out.close(); System.err.println(System.currentTimeMillis() - ct); } public static void main(String[] args) throws Exception{ if(memory)new Thread(null, new Runnable() {public void run(){try{new CF_401D().run();}catch(Exception e){e.printStackTrace();System.exit(1);}}}, "1", 1 << 28).start(); else new CF_401D().run(); } int[][] make(int n, int e, int[] from, int[] to, boolean f){ int[][] g = new int[n][];int[]cnt = new int[n]; for(int i = 0; i< e; i++){ cnt[from[i]]++; if(f)cnt[to[i]]++; } for(int i = 0; i< n; i++)g[i] = new int[cnt[i]]; for(int i = 0; i< e; i++){ g[from[i]][--cnt[from[i]]] = to[i]; if(f)g[to[i]][--cnt[to[i]]] = from[i]; } return g; } int[][][] makeS(int n, int e, int[] from, int[] to, boolean f){ int[][][] g = new int[n][][];int[]cnt = new int[n]; for(int i = 0; i< e; i++){ cnt[from[i]]++; if(f)cnt[to[i]]++; } for(int i = 0; i< n; i++)g[i] = new int[cnt[i]][]; for(int i = 0; i< e; i++){ g[from[i]][--cnt[from[i]]] = new int[]{to[i], i, 0}; if(f)g[to[i]][--cnt[to[i]]] = new int[]{from[i], i, 1}; } return g; } int find(int[] set, int u){return set[u] = (set[u] == u?u:find(set, set[u]));} int digit(long s){int ans = 0;while(s>0){s/=10;ans++;}return ans;} long gcd(long a, long b){return (b==0)?a:gcd(b,a%b);} int gcd(int a, int b){return (b==0)?a:gcd(b,a%b);} int bit(long n){return (n==0)?0:(1+bit(n&(n-1)));} void p(Object... o){for(Object oo:o)out.print(oo+" ");} void pn(Object... o){for(int i = 0; i< o.length; i++)out.print(o[i]+(i+1 < o.length?" ":"\n"));} void pni(Object... o){for(Object oo:o)out.print(oo+" ");out.println();out.flush();} String n()throws Exception{return in.next();} String nln()throws Exception{return in.nextLine();} int ni()throws Exception{return Integer.parseInt(in.next());} long nl()throws Exception{return Long.parseLong(in.next());} double nd()throws Exception{return Double.parseDouble(in.next());} class FastReader{ BufferedReader br; StringTokenizer st; public FastReader(){ br = new BufferedReader(new InputStreamReader(System.in)); } public FastReader(String s) throws Exception{ br = new BufferedReader(new FileReader(s)); } String next() throws Exception{ while (st == null || !st.hasMoreElements()){ try{ st = new StringTokenizer(br.readLine()); }catch (IOException e){ throw new Exception(e.toString()); } } return st.nextToken(); } String nextLine() throws Exception{ String str; try{ str = br.readLine(); }catch (IOException e){ throw new Exception(e.toString()); } return str; } } }
JAVA
401_D. Roman and Numbers
Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m. Number x is considered close to number n modulo m, if: * it can be obtained by rearranging the digits of number n, * it doesn't have any leading zeroes, * the remainder after dividing number x by m equals 0. Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him. Input The first line contains two integers: n (1 ≀ n < 1018) and m (1 ≀ m ≀ 100). Output In a single line print a single integer β€” the number of numbers close to number n modulo m. Examples Input 104 2 Output 3 Input 223 4 Output 1 Input 7067678 8 Output 47 Note In the first sample the required numbers are: 104, 140, 410. In the second sample the required number is 232.
2
10
import java.util.HashMap; import java.util.Scanner; public class cf401d { public static long factorial(int a) { long ret = 1; for (int i = 2; i <= a; i++) ret *= i; return ret; } public static void main(String[] args) { Scanner in = new Scanner(System.in); long n = in.nextLong(); int m = in.nextInt(); String[] Sdigs = ("" + n).split(""); int[] digs = new int[Sdigs.length]; for (int i = 0; i < digs.length; i++) digs[i] = Integer.parseInt(Sdigs[i]); long[][] dp = new long[1 << digs.length][m]; dp[0][0] = 1; for (int i = 0; i < dp.length; i++) { for (int dig = 0; dig < digs.length; dig++) { if (((i >> dig) & 1) == 1) continue; if (i == 0 && digs[dig] == 0) continue; int index = i | (1 << dig); for (int j = 0; j < m; j++) { int newmod = (10 * j + digs[dig]) % m; dp[index][newmod] += dp[i][j]; } } } long ans = dp[(1 << digs.length) - 1][0]; HashMap<Integer, Integer> hm = new HashMap<Integer, Integer>(); for (int i : digs) { if (hm.containsKey(i)) hm.put(i, hm.get(i) + 1); else hm.put(i, 1); } for (int i : hm.keySet()) ans /= factorial(hm.get(i)); System.out.println(ans); in.close(); } public static void debug(Object o) { System.out.println("\tDEBUG: " + o.toString().replaceAll("\n", "\n\tDEBUG: ")); } }
JAVA
401_D. Roman and Numbers
Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m. Number x is considered close to number n modulo m, if: * it can be obtained by rearranging the digits of number n, * it doesn't have any leading zeroes, * the remainder after dividing number x by m equals 0. Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him. Input The first line contains two integers: n (1 ≀ n < 1018) and m (1 ≀ m ≀ 100). Output In a single line print a single integer β€” the number of numbers close to number n modulo m. Examples Input 104 2 Output 3 Input 223 4 Output 1 Input 7067678 8 Output 47 Note In the first sample the required numbers are: 104, 140, 410. In the second sample the required number is 232.
2
10
#include <bits/stdc++.h> using namespace std; using ll = long long; using pii = pair<int, int>; using vi = vector<ll>; const int B = 18; const int N = 1 << B; const int M = 100; vector<vi> dp; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ll n, m; cin >> n >> m; ll pw = 1; vector<int> dig, cnt(10, 0), rem; while (n) { cnt[n % 10]++; dig.push_back(n % 10); rem.push_back(pw % m); n /= 10, pw *= 10; } n = (int)dig.size(); dp.assign(1 << n, vi(m, 0)); vector<int> vec(1 << n); iota((vec).begin(), (vec).end(), 0); sort((vec).begin(), (vec).end(), [](int i, int j) { int a = __builtin_popcount(i); int b = __builtin_popcount(j); return a < b; }); dp[0][0] = 1; for (int i : vec) { for (int j = 0; j < n; j++) { if ((i >> j) & 1) { int ni = i ^ (1 << j); for (int l = 0; l < m; l++) { if (!dig[j] && !ni) continue; int nl = (10 * l + dig[j]) % m; dp[i][nl] += dp[ni][l]; } } } } vector<ll> fac(20, 1); for (int i = 1; i < 20; i++) fac[i] = i * fac[i - 1]; ll ans = dp[(1 << n) - 1][0]; for (int i = 0; i < 10; i++) ans /= fac[cnt[i]]; cout << ans << '\n'; }
CPP
401_D. Roman and Numbers
Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m. Number x is considered close to number n modulo m, if: * it can be obtained by rearranging the digits of number n, * it doesn't have any leading zeroes, * the remainder after dividing number x by m equals 0. Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him. Input The first line contains two integers: n (1 ≀ n < 1018) and m (1 ≀ m ≀ 100). Output In a single line print a single integer β€” the number of numbers close to number n modulo m. Examples Input 104 2 Output 3 Input 223 4 Output 1 Input 7067678 8 Output 47 Note In the first sample the required numbers are: 104, 140, 410. In the second sample the required number is 232.
2
10
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.util.StringTokenizer; public class D { static StringTokenizer st; static BufferedReader br; static PrintWriter pw; public static void main(String[] args) throws IOException { br = new BufferedReader(new InputStreamReader(System.in)); pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); String n = next(); int m = nextInt(); int[]cnt = new int[10]; for (int i = 0; i < n.length(); i++) { cnt[n.charAt(i)-48]++; } long ans = calc(cnt, m); if (cnt[0] > 0) { cnt[0]--; ans -= calc(cnt, m); } System.out.println(ans); pw.close(); } private static long calc(int[] cnt, int m) { long[][][][][][][][][][][]d = new long[cnt[0]+1][cnt[1]+1][cnt[2]+1][cnt[3]+1][cnt[4]+1][cnt[5]+1][cnt[6]+1][cnt[7]+1][cnt[8]+1][cnt[9]+1][m]; d[0][0][0][0][0][0][0][0][0][0][0] = 1; for (int i0 = 0; i0 <= cnt[0]; i0++) { for (int i1 = 0; i1 <= cnt[1]; i1++) { for (int i2 = 0; i2 <= cnt[2]; i2++) { for (int i3 = 0; i3 <= cnt[3]; i3++) { for (int i4 = 0; i4 <= cnt[4]; i4++) { for (int i5 = 0; i5 <= cnt[5]; i5++) { for (int i6 = 0; i6 <= cnt[6]; i6++) { for (int i7 = 0; i7 <= cnt[7]; i7++) { for (int i8 = 0; i8 <= cnt[8]; i8++) { for (int i9 = 0; i9 <= cnt[9]; i9++) { for (int i = 0; i < m; i++) { if (i0 > 0) { int qol = (i*10+0) % m; d[i0][i1][i2][i3][i4][i5][i6][i7][i8][i9][qol] += d[i0-1][i1][i2][i3][i4][i5][i6][i7][i8][i9][i]; } if (i1 > 0) { int qol = (i*10+1) % m; d[i0][i1][i2][i3][i4][i5][i6][i7][i8][i9][qol] += d[i0][i1-1][i2][i3][i4][i5][i6][i7][i8][i9][i]; } if (i2 > 0) { int qol = (i*10+2) % m; d[i0][i1][i2][i3][i4][i5][i6][i7][i8][i9][qol] += d[i0][i1][i2-1][i3][i4][i5][i6][i7][i8][i9][i]; } if (i3 > 0) { int qol = (i*10+3) % m; d[i0][i1][i2][i3][i4][i5][i6][i7][i8][i9][qol] += d[i0][i1][i2][i3-1][i4][i5][i6][i7][i8][i9][i]; } if (i4 > 0) { int qol = (i*10+4) % m; d[i0][i1][i2][i3][i4][i5][i6][i7][i8][i9][qol] += d[i0][i1][i2][i3][i4-1][i5][i6][i7][i8][i9][i]; } if (i5 > 0) { int qol = (i*10+5) % m; d[i0][i1][i2][i3][i4][i5][i6][i7][i8][i9][qol] += d[i0][i1][i2][i3][i4][i5-1][i6][i7][i8][i9][i]; } if (i6 > 0) { int qol = (i*10+6) % m; d[i0][i1][i2][i3][i4][i5][i6][i7][i8][i9][qol] += d[i0][i1][i2][i3][i4][i5][i6-1][i7][i8][i9][i]; } if (i7 > 0) { int qol = (i*10+7) % m; d[i0][i1][i2][i3][i4][i5][i6][i7][i8][i9][qol] += d[i0][i1][i2][i3][i4][i5][i6][i7-1][i8][i9][i]; } if (i8 > 0) { int qol = (i*10+8) % m; d[i0][i1][i2][i3][i4][i5][i6][i7][i8][i9][qol] += d[i0][i1][i2][i3][i4][i5][i6][i7][i8-1][i9][i]; } if (i9 > 0) { int qol = (i*10+9) % m; d[i0][i1][i2][i3][i4][i5][i6][i7][i8][i9][qol] += d[i0][i1][i2][i3][i4][i5][i6][i7][i8][i9-1][i]; } } } } } } } } } } } } return d[cnt[0]][cnt[1]][cnt[2]][cnt[3]][cnt[4]][cnt[5]][cnt[6]][cnt[7]][cnt[8]][cnt[9]][0]; } private static int nextInt() throws IOException { return Integer.parseInt(next()); } private static long nextLong() throws IOException { return Long.parseLong(next()); } private static double nextDouble() throws IOException { return Double.parseDouble(next()); } private static String next() throws IOException { while (st==null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } }
JAVA
401_D. Roman and Numbers
Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m. Number x is considered close to number n modulo m, if: * it can be obtained by rearranging the digits of number n, * it doesn't have any leading zeroes, * the remainder after dividing number x by m equals 0. Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him. Input The first line contains two integers: n (1 ≀ n < 1018) and m (1 ≀ m ≀ 100). Output In a single line print a single integer β€” the number of numbers close to number n modulo m. Examples Input 104 2 Output 3 Input 223 4 Output 1 Input 7067678 8 Output 47 Note In the first sample the required numbers are: 104, 140, 410. In the second sample the required number is 232.
2
10
import java.util.*; public class D { Scanner sc = new Scanner(System.in); void doIt() { char [] c_n = sc.next().toCharArray(); int m = sc.nextInt(); int len = c_n.length; int [] n = new int[len]; int [] dcnt = new int[10]; long div = 1L; for(int i = 0; i < len; i++) { n[i] = (int)(c_n[i] - '0'); dcnt[n[i]]++; div *= (long)dcnt[n[i]]; } long dp[][] = new long[1<<len][m]; dp[0][0] = 1; int [] base = new int[len]; base[0] = 1; for(int i = 1; i < len; i++) base[i] = (base[i-1] * 10) % m; for(int mask = 0; mask < (1<<len)-1; mask++) { int cnt = Integer.bitCount(mask); for(int i = 0; i < len; i++) { if((mask & (1<<i)) == 0) { // not used int nmask = mask | (1<<i); if(n[i] == 0 && nmask == (1<<len)-1) continue; for(int v = 0; v < m; v++) { int nv = (v + n[i] * base[cnt]) % m; dp[nmask][nv] += dp[mask][v]; //System.out.println(v + " " + mask + "=>" + nmask + " " + nv + " " + dp[nmask][nv]); } } } } // for(int mask = 0; mask < (1<<len); mask++) { // System.out.println(mask + " " + Arrays.toString(dp[mask])); // } // System.out.println(Arrays.toString(dp[(1<<len)-1])); // System.out.println(div); System.out.println(dp[(1<<len)-1][0] / div); } public static void main(String[] args) { new D().doIt(); } }
JAVA
401_D. Roman and Numbers
Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m. Number x is considered close to number n modulo m, if: * it can be obtained by rearranging the digits of number n, * it doesn't have any leading zeroes, * the remainder after dividing number x by m equals 0. Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him. Input The first line contains two integers: n (1 ≀ n < 1018) and m (1 ≀ m ≀ 100). Output In a single line print a single integer β€” the number of numbers close to number n modulo m. Examples Input 104 2 Output 3 Input 223 4 Output 1 Input 7067678 8 Output 47 Note In the first sample the required numbers are: 104, 140, 410. In the second sample the required number is 232.
2
10
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintWriter; import java.math.BigDecimal; import java.math.BigInteger; import java.util.Arrays; import java.util.StringTokenizer; public class Main { public static void main(String[] args) { InputReader in = new InputReader(); PrintWriter out = new PrintWriter(System.out); while(in.hasNext()) { long n=in.nextLong(); int m=in.nextInt(); long[][] dp=new long[1<<18][105]; int[] a=new int[20]; int d=0; while(n!=0) { a[d++]=(int)(n%10); n/=10; } Arrays.sort(a, 0, d); // for(int i=0;i<(1<<18);i++) // for(int j=0;j<=100;j++) // dp[i][j]=0; dp[0][0]=1; for(int sta=0;sta<(1<<d);sta++) for(int i=0;i<m;i++) if(dp[sta][i]!=0) { long cnt=dp[sta][i], pre=-1; for(int j=d-1;j>=0;j--) if(((~sta)>>j)%2!=0) { if(a[j]==pre) continue; if(sta==0 && a[j]==0) break; dp[sta|(1<<j)][(i*10+a[j])%m]+=cnt; pre=a[j]; } } out.println(dp[(1<<d)-1][0]); } out.close(); } } class InputReader { BufferedReader buf; StringTokenizer tok; InputReader() { buf = new BufferedReader(new InputStreamReader(System.in)); } boolean hasNext() { while (tok == null || !tok.hasMoreElements()) { try { tok = new StringTokenizer(buf.readLine()); } catch (Exception e) { return false; } } return true; } String next() { if (hasNext()) return tok.nextToken(); return null; } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } BigInteger nextBigInteger() { return new BigInteger(next()); } BigDecimal nextBigDecimal() { return new BigDecimal(next()); } }
JAVA
401_D. Roman and Numbers
Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m. Number x is considered close to number n modulo m, if: * it can be obtained by rearranging the digits of number n, * it doesn't have any leading zeroes, * the remainder after dividing number x by m equals 0. Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him. Input The first line contains two integers: n (1 ≀ n < 1018) and m (1 ≀ m ≀ 100). Output In a single line print a single integer β€” the number of numbers close to number n modulo m. Examples Input 104 2 Output 3 Input 223 4 Output 1 Input 7067678 8 Output 47 Note In the first sample the required numbers are: 104, 140, 410. In the second sample the required number is 232.
2
10
import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.Arrays; import java.util.HashSet; import java.util.StringTokenizer; /** * * * 3 2 3 5 * -2 -1 4 -1 2 7 3 * * * @author pttrung */ public class D { public static long MOD = 100000000; public static long[][] dp; public static void main(String[] args) throws FileNotFoundException { PrintWriter out; Scanner in = new Scanner(); //out = new PrintWriter(new FileOutputStream(new File("output.txt"))); out = new PrintWriter(System.out); long n = in.nextLong(); int m = in.nextInt(); int d = digit(n); //System.out.println(d); dp = new long[1 << d][m]; for (long[] a : dp) { Arrays.fill(a, -1); } // int []or = new int[d]; // for(int i = 0; i < d; i++){ // or[i] = i; // } // String num = "" + n; // int test = 0; // HashSet<Long> set = new HashSet(); // do{ // long val = 0; // if(num.charAt(or[0]) == '0'){ // continue; // } // for(int i = 0; i < d; i++){ // int index = num.charAt(or[i]) - '0'; // // val *=10; // val += index; // // } // if(val % m == 0){ // set.add(val); // } // }while(nextPer(or)); // out.println(set); // out.println(set.size()); long val = cal(0, 0, m, d, "" + n); out.println(val); out.close(); } public static long cal(int mask, int mod, int m, int d, String num) { if (dp[mask][mod] != -1) { return dp[mask][mod]; } int order = 0; long add = 1; for (int i = 0; i < d; i++) { if (((1 << i) & mask) != 0) { order++; add *= 10; } } //System.out.println("TEST " + mask + " " + order + " " + mod); if (order == d && mod == 0) { return 1; } long result = 0; boolean[] check = new boolean[10]; for (int i = 0; i < num.length(); i++) { if (((1 << i) & mask) == 0) { int cur = num.charAt(i) - '0'; if (check[cur]) { continue; } check[cur] = true; int c = (int) ((cur % m) * (add % m)) % m; int temp = (c + mod) % m; // System.out.println(temp + " " + index + " " + order); if (cur != 0 || order != num.length() - 1) { result += cal(mask | (1 << i), temp, m, d, num); } } } return dp[mask][mod] = result; } public static boolean nextPer(int[] data) { int i = data.length - 1; while (i > 0 && data[i] < data[i - 1]) { i--; } if (i == 0) { return false; } int j = data.length - 1; while (data[j] < data[i - 1]) { j--; } int temp = data[i - 1]; data[i - 1] = data[j]; data[j] = temp; Arrays.sort(data, i, data.length); return true; } public static int digit(long n) { int result = 0; while (n > 0) { n /= 10; result++; } return result; } public static double dist(long a, long b, long x, long y) { double val = (b - a) * (b - a) + (x - y) * (x - y); val = Math.sqrt(val); double other = x * x + a * a; other = Math.sqrt(other); return val + other; } public static class Point implements Comparable<Point> { int index, value; public Point(int start, int end) { this.index = start; this.value = end; } @Override public int compareTo(Point o) { return value - o.value; } } public static class Node implements Comparable<Node> { int index; int val; public Node(int index, int u) { this.index = index; this.val = u; } @Override public int compareTo(Node o) { if (val != o.val) { return val - o.val; } return index - o.index; } } public static class FT { int[] data; FT(int n) { data = new int[n]; } public void update(int index, int value) { while (index < data.length) { data[index] += value; index += (index & (-index)); } } public int get(int index) { int result = 0; while (index > 0) { result += data[index]; index -= (index & (-index)); } return result; } } public static class Sign { char c; int x, y; public Sign(char c, int x, int y) { this.c = c; this.x = x; this.y = y; } } public static long gcd(long a, long b) { if (b == 0) { return a; } return gcd(b, a % b); } public static long pow(long a, long b) { if (b == 0) { return 1; } if (b == 1) { return a; } long val = pow(a, b / 2); if (b % 2 == 0) { return val * val % MOD; } else { return val * (val * a % MOD) % MOD; } } static class Scanner { BufferedReader br; StringTokenizer st; public Scanner() throws FileNotFoundException { // System.setOut(new PrintStream(new BufferedOutputStream(System.out), true)); br = new BufferedReader(new InputStreamReader(System.in)); //br = new BufferedReader(new InputStreamReader(new FileInputStream(new File("input.txt")))); } public String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (Exception e) { throw new RuntimeException(); } } return st.nextToken(); } public long nextLong() { return Long.parseLong(next()); } public int nextInt() { return Integer.parseInt(next()); } public double nextDouble() { return Double.parseDouble(next()); } public String nextLine() { st = null; try { return br.readLine(); } catch (Exception e) { throw new RuntimeException(); } } public boolean endLine() { try { String next = br.readLine(); while (next != null && next.trim().isEmpty()) { next = br.readLine(); } if (next == null) { return true; } st = new StringTokenizer(next); return st.hasMoreTokens(); } catch (Exception e) { throw new RuntimeException(); } } } }
JAVA
401_D. Roman and Numbers
Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m. Number x is considered close to number n modulo m, if: * it can be obtained by rearranging the digits of number n, * it doesn't have any leading zeroes, * the remainder after dividing number x by m equals 0. Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him. Input The first line contains two integers: n (1 ≀ n < 1018) and m (1 ≀ m ≀ 100). Output In a single line print a single integer β€” the number of numbers close to number n modulo m. Examples Input 104 2 Output 3 Input 223 4 Output 1 Input 7067678 8 Output 47 Note In the first sample the required numbers are: 104, 140, 410. In the second sample the required number is 232.
2
10
import java.io.*; import java.util.*; public class cf401d { static FastIO in = new FastIO(), out = in; static int n, MOD; static int[] v; static long[][] memo; public static void main(String[] args) { String x = in.next().trim(); n = x.length(); v = new int[n]; for(int i=0; i<n; i++) { v[i] = x.charAt(i)-'0'; } MOD = in.nextInt(); memo = new long[MOD][1<<n]; for(long[] z : memo) Arrays.fill(z, -1L); out.println(go(0,0)); out.close(); } static long go(int mask, int mod) { if(memo[mod][mask] != -1) return memo[mod][mask]; if(mask == (1<<n)-1) return mod == 0 ? 1 : 0; long ret = 0; boolean[] tried = new boolean[10]; for(int i=0; i<n; i++) if((mask&(1<<i)) == 0) { if(tried[v[i]]) continue; tried[v[i]] = true; if(mask == 0 && v[i] == 0) continue; ret += go(mask | (1<<i), (mod*10 + v[i])%MOD); } return memo[mod][mask] = ret; } static class FastIO extends PrintWriter { BufferedReader br; StringTokenizer st; public FastIO() { this(System.in, System.out); } public FastIO(InputStream in, OutputStream out) { super(new BufferedWriter(new OutputStreamWriter(out))); br = new BufferedReader(new InputStreamReader(in)); scanLine(); } public void scanLine() { try { st = new StringTokenizer(br.readLine().trim()); } catch (Exception e) { throw new RuntimeException(e.getMessage()); } } public int numTokens() { if (!st.hasMoreTokens()) { scanLine(); return numTokens(); } return st.countTokens(); } public String next() { if (!st.hasMoreTokens()) { scanLine(); return next(); } return st.nextToken(); } public double nextDouble() { return Double.parseDouble(next()); } public long nextLong() { return Long.parseLong(next()); } public int nextInt() { return Integer.parseInt(next()); } } }
JAVA
401_D. Roman and Numbers
Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m. Number x is considered close to number n modulo m, if: * it can be obtained by rearranging the digits of number n, * it doesn't have any leading zeroes, * the remainder after dividing number x by m equals 0. Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him. Input The first line contains two integers: n (1 ≀ n < 1018) and m (1 ≀ m ≀ 100). Output In a single line print a single integer β€” the number of numbers close to number n modulo m. Examples Input 104 2 Output 3 Input 223 4 Output 1 Input 7067678 8 Output 47 Note In the first sample the required numbers are: 104, 140, 410. In the second sample the required number is 232.
2
10
import java.io.BufferedInputStream; import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; import java.util.InputMismatchException; import java.util.LinkedList; import java.util.Map.Entry; import java.util.Queue; import java.util.Random; import java.util.Set; import java.util.SortedSet; import java.util.Stack; import java.util.TreeMap; import java.util.TreeSet; public class Main { public void foo() { MyScanner scan = new MyScanner(); String s = scan.next(); int m = scan.nextInt(); int n = s.length(); int[] ch = new int[n]; for(int i = 0;i < n;++i) { ch[i] = s.charAt(i) - '0'; } long[][] dp = new long[1 << n][m]; dp[0][0] = 1; for(int i = 0;i < (1 << n) - 1;++i) { for(int j = 0;j < m;++j) { boolean[] vis = new boolean[10]; for(int k = 0;k < n;++k) { if((i & (1 << k)) != 0 || vis[ch[k]] || (0 == i && 0 == ch[k])) { continue; } vis[ch[k]] = true; int iNext = i | (1 << k); int jNext = (10 * j + ch[k]) % m; dp[iNext][jNext] += dp[i][j]; } } } System.out.println(dp[(1 << n) - 1][0]); } public static void main(String[] args) { new Main().foo(); } class MyScanner { private byte[] buf = new byte[1024]; private int curChar; private int numChars; BufferedInputStream bis = new BufferedInputStream(System.in); public int read() { if (-1 == numChars) { throw new InputMismatchException(); } if (curChar >= numChars) { curChar = 0; try { numChars = bis.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) { return -1; } } return buf[curChar++]; } public int nextInt() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public String next() { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } private boolean isSpaceChar(int c) { return ' ' == c || '\n' == c || '\r' == c || '\t' == c || -1 == c; } } }
JAVA
401_D. Roman and Numbers
Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m. Number x is considered close to number n modulo m, if: * it can be obtained by rearranging the digits of number n, * it doesn't have any leading zeroes, * the remainder after dividing number x by m equals 0. Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him. Input The first line contains two integers: n (1 ≀ n < 1018) and m (1 ≀ m ≀ 100). Output In a single line print a single integer β€” the number of numbers close to number n modulo m. Examples Input 104 2 Output 3 Input 223 4 Output 1 Input 7067678 8 Output 47 Note In the first sample the required numbers are: 104, 140, 410. In the second sample the required number is 232.
2
10
#include <bits/stdc++.h> using namespace std; long long n; int m, cnt; int s[20], p[20][102], ss[10]; long long aa, f[262150][102], jc[20]; int main() { int x = 0, x1 = 0, x2 = 0; long long a = 0; cin >> n >> m; a = n; while (a) { s[++cnt] = a % 10; a /= 10; } jc[0] = 1; for (int i = 1; i <= cnt; i++) jc[i] = jc[i - 1] * i; for (int i = 1; i <= cnt; i++) for (int j = 0; j <= 100; j++) { a = s[i]; for (int k = 1; k <= j; k++) a *= 10; p[i][j] = a % m; } a = (1 << cnt) - 1; for (int j = 0; j < 10; j++) ss[j] = 0; x1 = 0; while (a) { x1++; if (a & 1) ss[s[x1]]++; a >>= 1; } aa = 1; for (int j = 0; j < 10; j++) aa *= jc[ss[j]]; for (int i = 1; i < (1 << cnt); i++) { a = i; x1 = x2 = 0; while (a) { if (a & 1) x2++; a >>= 1; x1++; } if (x2 == 1) { f[i][s[x1] % m] = 1; continue; } for (int j = 0; j < x1; j++) { a = i ^ (1 << j); if (x2 == cnt && !s[j + 1]) continue; for (int k = 0; k < m; k++) { f[i][k] += f[a][(m + k - p[j + 1][x2 - 1]) % m]; } } } cout << f[(1 << cnt) - 1][0] / aa << endl; return 0; }
CPP
401_D. Roman and Numbers
Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m. Number x is considered close to number n modulo m, if: * it can be obtained by rearranging the digits of number n, * it doesn't have any leading zeroes, * the remainder after dividing number x by m equals 0. Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him. Input The first line contains two integers: n (1 ≀ n < 1018) and m (1 ≀ m ≀ 100). Output In a single line print a single integer β€” the number of numbers close to number n modulo m. Examples Input 104 2 Output 3 Input 223 4 Output 1 Input 7067678 8 Output 47 Note In the first sample the required numbers are: 104, 140, 410. In the second sample the required number is 232.
2
10
#include <bits/stdc++.h> using namespace std; string s; int m; long long dp[1 << 18][100]; long long calc(int i, int mask, int mod) { if (i == s.size()) return mod == 0; long long &ret = dp[mask][mod]; if (ret != -1) return ret; ret = 0; for (int j = 0; j < s.size(); ++j) if (!((mask >> j) & 1)) { if (j && s[j - 1] == s[j] && !((mask >> (j - 1)) & 1)) continue; if (i == 0 && s[j] == 0) continue; ret += calc(i + 1, mask | (1 << j), (mod * 10 + s[j]) % m); } return ret; } int main() { cin >> s >> m; sort(s.begin(), s.end()); for (int i = 0; i < s.size(); ++i) s[i] -= '0'; memset(dp, -1, sizeof(dp)); printf("%lld\n", calc(0, 0, 0)); return 0; }
CPP
401_D. Roman and Numbers
Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m. Number x is considered close to number n modulo m, if: * it can be obtained by rearranging the digits of number n, * it doesn't have any leading zeroes, * the remainder after dividing number x by m equals 0. Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him. Input The first line contains two integers: n (1 ≀ n < 1018) and m (1 ≀ m ≀ 100). Output In a single line print a single integer β€” the number of numbers close to number n modulo m. Examples Input 104 2 Output 3 Input 223 4 Output 1 Input 7067678 8 Output 47 Note In the first sample the required numbers are: 104, 140, 410. In the second sample the required number is 232.
2
10
import java.io.*; import java.math.BigInteger; import java.util.*; import java.util.Map.Entry; public class Main{ public static class FastReader { BufferedReader br; StringTokenizer root; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (root == null || !root.hasMoreTokens()) { try { root = new StringTokenizer(br.readLine()); } catch (Exception addd) { addd.printStackTrace(); } } return root.nextToken(); } int nextInt() { return Integer.parseInt(next()); } double nextDouble() { return Double.parseDouble(next()); } long nextLong() { return Long.parseLong(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (Exception addd) { addd.printStackTrace(); } return str; } } public static PrintWriter out = new PrintWriter (new BufferedOutputStream(System.out)); public static FastReader sc = new FastReader(); static int mod = (int) (1e2),m,n; static List<Integer>[] edges ; static long[][] dp ; static int[] a; static long[] pre; static char[] s,t; public static void main(String[] args) { int t = 1; while(t-->0) { s = sc.next().toCharArray(); n = s.length; m = sc.nextInt(); dp = new long[1<<n][m]; for(int i=0;i<dp.length;++i) { Arrays.fill(dp[i], -1); } out.print(f(0,0)); } out.close(); } private static long f(int mask, int mod) { if(Long.bitCount(mask) == n) { if(mod == 0) return 1; // if all numbers are covered else return 0; } if(dp[mask][mod] != -1) return dp[mask][mod]; long ans = 0; boolean[] set = new boolean[10]; for(int i=0;i<s.length;++i) { int x = s[i]-'0'; if(mask == 0 && x == 0) continue; // prevent leading zeroes if(set[x]) continue; // already used if((mask&(1<<i))!=0) continue; // not used set[x] = true; ans+=f(mask|(1<<i),(mod*10+x)%m); } return dp[mask][mod] = ans; } }
JAVA
401_D. Roman and Numbers
Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m. Number x is considered close to number n modulo m, if: * it can be obtained by rearranging the digits of number n, * it doesn't have any leading zeroes, * the remainder after dividing number x by m equals 0. Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him. Input The first line contains two integers: n (1 ≀ n < 1018) and m (1 ≀ m ≀ 100). Output In a single line print a single integer β€” the number of numbers close to number n modulo m. Examples Input 104 2 Output 3 Input 223 4 Output 1 Input 7067678 8 Output 47 Note In the first sample the required numbers are: 104, 140, 410. In the second sample the required number is 232.
2
10
#include <bits/stdc++.h> using namespace std; int a[100]; long long f[1 << 18][100]; int main() { int m; long long n; cin >> n >> m; int N = 0; for (; n; n /= 10) a[N++] = n % 10; sort(a, a + N); f[0][0] = 1; for (int mask = 0; mask < (1 << N); mask++) for (int k = 0; k < m; k++) if (f[mask][k]) { long long cnt = f[mask][k], pre = -1; for (int i = N - 1; i >= 0; i--) if (~mask >> i & 1) { if (a[i] == pre) continue; if (mask == 0 && a[i] == 0) break; f[mask | 1 << i][(k * 10 + a[i]) % m] += cnt; pre = a[i]; } } cout << f[(1 << N) - 1][0] << endl; return 0; }
CPP
401_D. Roman and Numbers
Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m. Number x is considered close to number n modulo m, if: * it can be obtained by rearranging the digits of number n, * it doesn't have any leading zeroes, * the remainder after dividing number x by m equals 0. Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him. Input The first line contains two integers: n (1 ≀ n < 1018) and m (1 ≀ m ≀ 100). Output In a single line print a single integer β€” the number of numbers close to number n modulo m. Examples Input 104 2 Output 3 Input 223 4 Output 1 Input 7067678 8 Output 47 Note In the first sample the required numbers are: 104, 140, 410. In the second sample the required number is 232.
2
10
#include <bits/stdc++.h> using namespace std; long long dp[1LL << 18][101]; signed main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); string n; long long m; cin >> n >> m; sort(n.begin(), n.end()); dp[0][0] = 1; for (long long mask = 0; mask < (1LL << (long long)n.length()); mask++) { for (long long i = 0; i < m; i++) { if (dp[mask][i]) { for (long long j = 0; j < (long long)n.length(); j++) { if (mask & (1LL << j)) continue; if (mask == 0 && n[j] == '0') continue; if (j > 0 && n[j] == n[j - 1] && (mask & (1LL << (j - 1))) == 0) continue; dp[mask | (1LL << j)][(i * 10 + n[j] - '0') % m] += dp[mask][i]; } } } } cout << dp[(1 << (long long)n.length()) - 1][0]; return 0; }
CPP
401_D. Roman and Numbers
Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m. Number x is considered close to number n modulo m, if: * it can be obtained by rearranging the digits of number n, * it doesn't have any leading zeroes, * the remainder after dividing number x by m equals 0. Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him. Input The first line contains two integers: n (1 ≀ n < 1018) and m (1 ≀ m ≀ 100). Output In a single line print a single integer β€” the number of numbers close to number n modulo m. Examples Input 104 2 Output 3 Input 223 4 Output 1 Input 7067678 8 Output 47 Note In the first sample the required numbers are: 104, 140, 410. In the second sample the required number is 232.
2
10
#include <bits/stdc++.h> using namespace std; string s; int n; int m; long long cnt[1 << 18][100]; int main() { ios_base::sync_with_stdio(false); cin >> s >> m; n = s.size(); cnt[0][0] = 1; for (int mask = 0; mask < 1 << n; mask++) { for (int mod = 0; mod < m; mod++) { for (int i = 0; i < n; i++) { if (mask & 1 << i) continue; if (!mask && s[i] == '0') continue; cnt[mask | 1 << i][(mod * 10 + s[i] - '0') % m] += cnt[mask][mod]; } } } long long ans = cnt[(1 << n) - 1][0]; map<char, int> k; for (char c : s) { k[c]++; } for (auto& it : k) { long long f = 1; for (int i = 2; i <= it.second; i++) { f *= i; } ans /= f; } cout << ans << endl; return 0; }
CPP
401_D. Roman and Numbers
Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m. Number x is considered close to number n modulo m, if: * it can be obtained by rearranging the digits of number n, * it doesn't have any leading zeroes, * the remainder after dividing number x by m equals 0. Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him. Input The first line contains two integers: n (1 ≀ n < 1018) and m (1 ≀ m ≀ 100). Output In a single line print a single integer β€” the number of numbers close to number n modulo m. Examples Input 104 2 Output 3 Input 223 4 Output 1 Input 7067678 8 Output 47 Note In the first sample the required numbers are: 104, 140, 410. In the second sample the required number is 232.
2
10
#include <bits/stdc++.h> using namespace std; long long dp[(1 << 18)][101], n, m; long long v[22], gigi; string str; int main() { ios::sync_with_stdio(false); cin >> str >> m; n = str.size(); for (int i = str.size() - 1; i >= 0; i--) { if (i == str.size() - 1) v[i] = 1; else v[i] = v[i + 1] * 10; } dp[0][0] = 1; for (int mask = 0; mask <= (1 << n) - 1; mask++) { for (int r = 0; r <= m - 1; r++) { int can[10], p = 0; for (int i = 0; i <= 9; i++) can[i] = -1; for (int i = 0; i <= n - 1; i++) { if (mask & (1 << i)) { p++; continue; } int c = str[i] - '0'; can[c] = i; } for (int i = 0; i <= 9; i++) { if (!mask && !i) continue; if (can[i] == -1) continue; long long newMask = mask | (1 << can[i]), nr = (r + (i * v[p])) % m; dp[newMask][nr] += dp[mask][r]; } } } cout << dp[(1 << n) - 1][0] << endl; return 0; }
CPP
401_D. Roman and Numbers
Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m. Number x is considered close to number n modulo m, if: * it can be obtained by rearranging the digits of number n, * it doesn't have any leading zeroes, * the remainder after dividing number x by m equals 0. Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him. Input The first line contains two integers: n (1 ≀ n < 1018) and m (1 ≀ m ≀ 100). Output In a single line print a single integer β€” the number of numbers close to number n modulo m. Examples Input 104 2 Output 3 Input 223 4 Output 1 Input 7067678 8 Output 47 Note In the first sample the required numbers are: 104, 140, 410. In the second sample the required number is 232.
2
10
import java.io.BufferedInputStream; import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; import java.util.InputMismatchException; import java.util.LinkedList; import java.util.Map.Entry; import java.util.Queue; import java.util.Random; import java.util.Set; import java.util.SortedSet; import java.util.Stack; import java.util.TreeMap; import java.util.TreeSet; public class Main { public void foo() { MyScanner scan = new MyScanner(); String s = scan.next(); int m = scan.nextInt(); int n = s.length(); int[] ch = new int[n]; for(int i = 0;i < n;++i) { ch[i] = s.charAt(i) - '0'; } long[][] dp = new long[1 << n][m]; dp[0][0] = 1; boolean[] vis = new boolean[10]; for(int i = 0;i < (1 << n) - 1;++i) { for(int j = 0;j < m;++j) { Arrays.fill(vis, false); for(int k = 0;k < n;++k) { if((i & (1 << k)) != 0 || vis[ch[k]] || (0 == i && 0 == ch[k])) { continue; } vis[ch[k]] = true; int iNext = i | (1 << k); int jNext = (10 * j + ch[k]) % m; dp[iNext][jNext] += dp[i][j]; } } } System.out.println(dp[(1 << n) - 1][0]); } public static void main(String[] args) { new Main().foo(); } class MyScanner { private byte[] buf = new byte[1024]; private int curChar; private int numChars; BufferedInputStream bis = new BufferedInputStream(System.in); public int read() { if (-1 == numChars) { throw new InputMismatchException(); } if (curChar >= numChars) { curChar = 0; try { numChars = bis.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) { return -1; } } return buf[curChar++]; } public int nextInt() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public String next() { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } private boolean isSpaceChar(int c) { return ' ' == c || '\n' == c || '\r' == c || '\t' == c || -1 == c; } } }
JAVA
401_D. Roman and Numbers
Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m. Number x is considered close to number n modulo m, if: * it can be obtained by rearranging the digits of number n, * it doesn't have any leading zeroes, * the remainder after dividing number x by m equals 0. Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him. Input The first line contains two integers: n (1 ≀ n < 1018) and m (1 ≀ m ≀ 100). Output In a single line print a single integer β€” the number of numbers close to number n modulo m. Examples Input 104 2 Output 3 Input 223 4 Output 1 Input 7067678 8 Output 47 Note In the first sample the required numbers are: 104, 140, 410. In the second sample the required number is 232.
2
10
import java.io.*; import java.math.*; import java.util.*; public class Main { InputReader in; PrintWriter out; void run(){ out = new PrintWriter(new OutputStreamWriter(System.out)); in = new InputReader(System.in); solve(); out.flush(); } public static void main(String[] args){ new Main().run(); } int[] digits; int m; long[][] dp; int full; long divisible(int remainder, int taken) { if(taken == full) { return (remainder == 0 ? 1 : 0); } if(dp[taken][remainder] != -1) return dp[taken][remainder]; long res = 0; for(int i = 0; i < digits.length; i++) { if((taken & (1 << i)) == 0) { if(taken == 0 && digits[i] == 0) continue; if(i > 0 && digits[i-1] == digits[i] && (taken & (1 << (i-1))) == 0) continue; res += divisible((remainder * 10 + digits[i]) % m, taken | (1 << i)); } } return dp[taken][remainder] = res; } void solve() { String s = in.ns(); m = in.ni(); digits = new int[s.length()]; for(int i = 0; i < digits.length; i++) digits[i] = s.charAt(i) - '0'; dp = new long[1<<digits.length][101]; for(long[] x : dp) Arrays.fill(x, -1); Arrays.sort(digits); full = (1 << digits.length) - 1; out.println(divisible(0, 0)); } class InputReader { private boolean finished = false; private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public int peek() { if (numChars == -1) return -1; if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { return -1; } if (numChars <= 0) return -1; } return buf[curChar]; } public int ni() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nl() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public String ns() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return isWhitespace(c); } public boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private String readLine0() { StringBuilder buf = new StringBuilder(); int c = read(); while (c != '\n' && c != -1) { if (c != '\r') buf.appendCodePoint(c); c = read(); } return buf.toString(); } public String readLine() { String s = readLine0(); while (s.trim().length() == 0) s = readLine0(); return s; } public String readLine(boolean ignoreEmptyLines) { if (ignoreEmptyLines) return readLine(); else return readLine0(); } public BigInteger readBigInteger() { try { return new BigInteger(ns()); } catch (NumberFormatException e) { throw new InputMismatchException(); } } public char readCharacter() { int c = read(); while (isSpaceChar(c)) c = read(); return (char) c; } public double nd() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } double res = 0; while (!isSpaceChar(c) && c != '.') { if (c == 'e' || c == 'E') return res * Math.pow(10, ni()); if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } if (c == '.') { c = read(); double m = 1; while (!isSpaceChar(c)) { if (c == 'e' || c == 'E') return res * Math.pow(10, ni()); if (c < '0' || c > '9') throw new InputMismatchException(); m /= 10; res += (c - '0') * m; c = read(); } } return res * sgn; } public boolean eof() { int value; while (isSpaceChar(value = peek()) && value != -1) read(); return value == -1; } public String next() { return ns(); } public SpaceCharFilter getFilter() { return filter; } public void setFilter(SpaceCharFilter filter) { this.filter = filter; } } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } }
JAVA
401_D. Roman and Numbers
Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m. Number x is considered close to number n modulo m, if: * it can be obtained by rearranging the digits of number n, * it doesn't have any leading zeroes, * the remainder after dividing number x by m equals 0. Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him. Input The first line contains two integers: n (1 ≀ n < 1018) and m (1 ≀ m ≀ 100). Output In a single line print a single integer β€” the number of numbers close to number n modulo m. Examples Input 104 2 Output 3 Input 223 4 Output 1 Input 7067678 8 Output 47 Note In the first sample the required numbers are: 104, 140, 410. In the second sample the required number is 232.
2
10
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintWriter; import java.math.BigInteger; import java.text.DecimalFormat; import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.Comparator; import java.util.Deque; import java.util.HashMap; import java.util.Iterator; import java.util.LinkedList; import java.util.Map; import java.util.PriorityQueue; import java.util.Scanner; import java.util.Stack; import java.util.StringTokenizer; import java.util.TreeMap; import java.util.TreeSet; import java.util.Queue; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; public class Main { String s; int m, n; long[][] dp = new long[N][105]; int[] sum = new int[N], t = new int[30]; int add(int x, int y){ return (x+y)%m; } void work() throws Exception{ s = Next(); m = Int(); n = s.length(); for(int i = 0; i < (1<<n); i++)for(int j = 0; j < m; j++)dp[i][j] = 0; t[0] = 1%m; for(int i = 1; i <= n; i++)t[i] = t[i-1]*10%m; dp[0][0] = 1; for(int i = 0; i < (1<<n); i++) { sum[i] = 0; for(int j = 0; j < n; j++)if((i&(1<<j))>0)sum[i] += s.charAt(j)-'0'; } for(int i = 0; i < (1<<n); i++) { int num = 0; for(int k = 0; k < n; k++)if((i&(1<<k))>0)num++; for(int j = 0; j < m; j++) { if(dp[i][j] == 0)continue; for(int k = 0; k < n; k++) if((i&(1<<k))==0) { if(num == n-1 && s.charAt(k)=='0')continue; int now = i + (1<<k); dp[now][add(j, t[num]*(s.charAt(k)-'0'))] += dp[i][j]; } } } for(int i = 0; i < 10; i++)t[i] = 0; for(int i = 0; i < n; i++)t[s.charAt(i)-'0']++; for(int i = 0; i < 10; i++){ long ans = 1; for(int j = 1; j <= t[i]; j++)ans *= j; dp[(1<<n)-1][0] /= ans; } out.println(dp[(1<<n)-1][0]); } public static void main(String[] args) throws Exception{ Main wo = new Main(); in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); // in = new BufferedReader(new InputStreamReader(new FileInputStream(new File("input.txt")))); // out = new PrintWriter(new File("output.txt")); wo.work(); out.close(); } static int N = 300000; static int M = 101; DecimalFormat df=new DecimalFormat("0.0000"); static int inf = (int)1e9; static long inf64 = (long) 1e18; static double eps = 1e-8; static double Pi = Math.PI; static int mod = (int)1e9 + 7 ; private String Next() throws Exception{ while (str == null || !str.hasMoreElements()) str = new StringTokenizer(in.readLine()); return str.nextToken(); } private int Int() throws Exception{ return Integer.parseInt(Next()); } private long Long() throws Exception{ return Long.parseLong(Next()); } private double Double() throws Exception{ return Double.parseDouble(Next()); } StringTokenizer str; static Scanner cin = new Scanner(System.in); static BufferedReader in; static PrintWriter out; /* class Edge{ int from, to, dis, nex; Edge(){} Edge(int from, int to, int dis, int nex){ this.from = from; this.to = to; this.dis = dis; this.nex = nex; } } Edge[] edge = new Edge[M<<1]; int[] head = new int[N]; int edgenum; void init_edge(){for(int i = 0; i < N; i++)head[i] = -1; edgenum = 0;} void add(int u, int v, int dis){ edge[edgenum] = new Edge(u, v, dis, head[u]); head[u] = edgenum++; }/**/ int upper_bound(int[] A, int l, int r, int val) {// upper_bound(A+l,A+r,val)-A; int pos = r; r--; while (l <= r) { int mid = (l + r) >> 1; if (A[mid] <= val) { l = mid + 1; } else { pos = mid; r = mid - 1; } } return pos; } int Pow(int x, int y) { int ans = 1; while (y > 0) { if ((y & 1) > 0) ans *= x; y >>= 1; x = x * x; } return ans; } double Pow(double x, int y) { double ans = 1; while (y > 0) { if ((y & 1) > 0) ans *= x; y >>= 1; x = x * x; } return ans; } int Pow_Mod(int x, int y, int mod) { int ans = 1; while (y > 0) { if ((y & 1) > 0) ans *= x; ans %= mod; y >>= 1; x = x * x; x %= mod; } return ans; } long Pow(long x, long y) { long ans = 1; while (y > 0) { if ((y & 1) > 0) ans *= x; y >>= 1; x = x * x; } return ans; } long Pow_Mod(long x, long y, long mod) { long ans = 1; while (y > 0) { if ((y & 1) > 0) ans *= x; ans %= mod; y >>= 1; x = x * x; x %= mod; } return ans; } int gcd(int x, int y){ if(x>y){int tmp = x; x = y; y = tmp;} while(x>0){ y %= x; int tmp = x; x = y; y = tmp; } return y; } int max(int x, int y) { return x > y ? x : y; } int min(int x, int y) { return x < y ? x : y; } double max(double x, double y) { return x > y ? x : y; } double min(double x, double y) { return x < y ? x : y; } long max(long x, long y) { return x > y ? x : y; } long min(long x, long y) { return x < y ? x : y; } int abs(int x) { return x > 0 ? x : -x; } double abs(double x) { return x > 0 ? x : -x; } long abs(long x) { return x > 0 ? x : -x; } boolean zero(double x) { return abs(x) < eps; } double sin(double x){return Math.sin(x);} double cos(double x){return Math.cos(x);} double tan(double x){return Math.tan(x);} double sqrt(double x){return Math.sqrt(x);} }
JAVA
401_D. Roman and Numbers
Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number n, modulo m. Number x is considered close to number n modulo m, if: * it can be obtained by rearranging the digits of number n, * it doesn't have any leading zeroes, * the remainder after dividing number x by m equals 0. Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him. Input The first line contains two integers: n (1 ≀ n < 1018) and m (1 ≀ m ≀ 100). Output In a single line print a single integer β€” the number of numbers close to number n modulo m. Examples Input 104 2 Output 3 Input 223 4 Output 1 Input 7067678 8 Output 47 Note In the first sample the required numbers are: 104, 140, 410. In the second sample the required number is 232.
2
10
#include <bits/stdc++.h> using namespace std; template <typename T> void read(T &x) { int s = 0, c = getchar(); x = 0; while (isspace(c)) c = getchar(); if (c == 45) s = 1, c = getchar(); while (isdigit(c)) x = (x << 3) + (x << 1) + (c ^ 48), c = getchar(); if (s) x = -x; } template <typename T> void write(T x, char c = ' ') { int b[40], l = 0; if (x < 0) putchar(45), x = -x; while (x > 0) b[l++] = x % 10, x /= 10; if (!l) putchar(48); while (l) putchar(b[--l] | 48); putchar(c); } int qwq[20], cnt; long long f[1ll << 18][102]; long long DFS(int pos, long long cur, int mod, int m) { if (pos == -1) return (mod == 0); if (~f[cur][mod]) return f[cur][mod]; bool sign[10] = {0}; long long ans = 0; for (int i = 0; i < cnt; ++i) { if (sign[qwq[i]]) continue; if ((cur >> i) & 1ll) continue; if (pos == cnt - 1 && qwq[i] == 0) continue; sign[qwq[i]] = true; ans += DFS(pos - 1, cur | (1ll << i), (mod * 10 + qwq[i]) % m, m); } return f[cur][mod] = ans; } long long solve(long long n, int m) { cnt = 0; while (n) { qwq[cnt++] = n % 10; n /= 10; } memset(f, -1, sizeof(f)); return DFS(cnt - 1, 0, 0, m); } int main(void) { long long n; int m; read(n); read(m); printf("%lld\n", solve(n, m)); return 0; }
CPP