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.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.io.BufferedWriter; import java.io.Writer; import java.io.OutputStreamWriter; import java.util.InputMismatchException; import java.io.IOException; 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; long[] dp(int used) { int index = Integer.bitCount(used); if (index == a.length) { long[] res = new long[m]; res[0] = 1; return res; } if (mem[used] != null) return mem[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 = (int) (IntegerUtils.power(10, a.length - index - 1) % m); 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'); m = in.readInt(); 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; } public static long power(long base, long exponent) { if (exponent == 0) return 1; long result = power(base, exponent >> 1); result = result * result; if ((exponent & 1) != 0) result = result * base; 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; long long n, m, f[(1 << 18) + 10][105], fac[20]; int c[20], cnt[10], tot; void init() { while (n) { int x = n % 10; c[tot++] = x; cnt[x]++; n /= 10; } sort(c, c + tot); fac[0] = 1; for (int i = 1; i < 20; i++) fac[i] = fac[i - 1] * i; } int main() { cin >> n >> m; init(); int start = 0; for (int i = 0; i < tot; i++) if (c[i] == 0) start = i + 1; else f[1 << i][c[i] % m] = 1; for (int s = (1 << start); s < (1 << tot); s++) { for (int j = 0; j < tot; j++) if ((1 << j) & s) { for (int k = 0; k <= m; k++) f[s][(k * 10 + c[j]) % m] += f[s ^ (1 << j)][k]; } } long long ans = f[(1 << tot) - 1][0]; for (int i = 0; i <= 9; i++) ans /= fac[cnt[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; bool isPrime(long long int n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; for (long long int i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } int m, sz; vector<int> num; long long int dp[(1 << 18)][100]; long long int solv(int mask, int rem) { if (mask == (1 << sz) - 1) return rem == 0 ? 1 : 0; if (dp[mask][rem] != -1) return dp[mask][rem]; long long int ans = 0; for (int i = 0; i < sz; i++) { if (mask == 0 && num[i] == 0) continue; if (mask & (1 << i)) continue; ans += solv((mask | (1 << i)), (10 * rem + num[i]) % m); } return dp[mask][rem] = ans; } long long int solve(long long int n) { long long int fact[20]; fact[0] = 1; for (long long int i = 1; i < 20; i++) fact[i] = fact[i - 1] * i; int cnt[10]; memset(cnt, 0, sizeof(cnt)); while (n > 0) { num.push_back(n % 10); cnt[n % 10]++; n /= 10; } sz = num.size(); memset(dp, -1, sizeof(dp)); long long int ans = solv(0, 0); for (long long int i = 0; i < 10; i++) ans = ans / fact[cnt[i]]; return ans; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long int n; cin >> n >> m; cout << solve(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.InputStreamReader; import java.util.Arrays; import java.util.HashMap; import java.util.StringTokenizer; public class D { static class Scanner{ BufferedReader br=null; StringTokenizer tk=null; public Scanner(){ br=new BufferedReader(new InputStreamReader(System.in)); } public String next() throws IOException{ while(tk==null || !tk.hasMoreTokens()) tk=new StringTokenizer(br.readLine()); return tk.nextToken(); } public int nextInt() throws NumberFormatException, IOException{ return Integer.valueOf(next()); } public long nextLong() throws NumberFormatException, IOException{ return Long.valueOf(next()); } public double nextDouble() throws NumberFormatException, IOException{ return Double.valueOf(next()); } } static class State{ @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + Arrays.hashCode(a); result = prime * result + mod; return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; State other = (State) obj; if (!Arrays.equals(a, other.a)) return false; if (mod != other.mod) return false; return true; } int[] a; int mod; public State(int[] b, int m){ a = b; mod = m; } int getdigits(){ int ret = 0; for(int i = 0; i < a.length; i++) ret += a[i]; return ret; } } static HashMap<State, Long> dp; static int totaldigits; static int[] powmod10; static long N; static int M; static long dp(State s){ if (dp.containsKey(s)) return dp.get(s); int[] array = s.a; int mod = s.mod; int faltan = s.getdigits(); if (faltan == 0){ return (mod == 0)?1L:0L; } long ret = 0; for(int i = 0; i < array.length; i++){ if(array[i] == 0) continue; if (faltan == totaldigits && i == 0) continue; int nmod = (powmod10[faltan - 1] * i) % M; nmod = (mod + nmod) % M; array[i]--; State ns = new State(array, nmod); ret += dp(ns); array[i]++; } dp.put(s, ret); return ret; } public static void main(String args[]) throws NumberFormatException, IOException{ Scanner sc = new Scanner(); N = sc.nextLong(); M = sc.nextInt(); totaldigits = (N+"").length(); powmod10 = new int[20]; powmod10[0] = 1 % M; for(int i = 1; i < 20; i++) powmod10[i] = (powmod10[i - 1] * 10) % M; int[] array = new int[10]; Arrays.fill(array, 0); String cad = N + ""; for(int i = 0; i < cad.length(); i++) array[cad.charAt(i) - '0']++; State s = new State(array, 0); dp = new HashMap<State, Long>(); System.out.println(dp(s)); } }
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 = (1 << 18) + 10; long long f[maxn][105], N, M, w[20], vis[10]; inline long long read() { long long f = 1, r = 0; char c = getchar(); while (!isdigit(c)) { if (c == '-') { f = -1; } c = getchar(); } while (isdigit(c)) { r = 10 * r + c - '0'; c = getchar(); } return f * r; } inline void write(long long x) { if (x < 0) putchar('-'), x = -x; if (x > 9) write(x / 10); putchar(x % 10 + '0'); } inline void writesp(long long x) { write(x), putchar(' '); } inline void writeln(long long x) { write(x), puts(""); } int main() { N = read(), M = read(); int cnt = -1; while (N) { w[++cnt] = N % 10; N /= 10; } f[0][0] = 1; int up = (1 << (cnt + 1)) - 1; for (int i = 1; i <= up; ++i) { memset(vis, 0, sizeof(vis)); for (int j = 0; j <= cnt; ++j) { if (i == (1 << j) && !w[j]) { break; } if (!(i & (1 << j)) || vis[w[j]]) { continue; } vis[w[j]] = 1; for (int k = 0; k < M; ++k) { f[i][(k * 10 + w[j]) % M] += f[i ^ (1 << j)][k]; } } } writeln(f[up][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.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; /** * @author Don Li */ public class RomanNumbers { int n; int[] cnt; void solve() { char[] a = in.nextToken().toCharArray(); int m = in.nextInt(); n = a.length; cnt = new int[10]; for (int i = 0; i < n; i++) { cnt[a[i] - '0']++; } int tot = 1; for (int i = 0; i < 10; i++) tot *= cnt[i] + 1; long[][] dp = new long[m][tot]; dp[0][tot - 1] = 1; for (int s = tot - 1; s > 0; s--) { int[] rem = decode(s); for (int x = 0; x < m; x++) { if (dp[x][s] > 0) { for (int d = 0; d < 10; d++) { if (s == tot - 1 && d == 0 || rem[d] == 0) continue; rem[d]--; int ns = encode(rem); dp[(x * 10 + d) % m][ns] += dp[x][s]; rem[d]++; } } } } out.println(dp[0][0]); } int encode(int[] a) { int ret = 0; for (int i = 0; i < 10; i++) { ret = ret * (cnt[i] + 1) + a[i]; } return ret; } int[] decode(int x) { int[] ret = new int[10]; for (int i = 9; i >= 0; i--) { ret[i] = x % (cnt[i] + 1); x /= cnt[i] + 1; } return ret; } public static void main(String[] args) { in = new FastScanner(new BufferedReader(new InputStreamReader(System.in))); out = new PrintWriter(System.out); new RomanNumbers().solve(); out.close(); } static FastScanner in; static PrintWriter out; static class FastScanner { BufferedReader in; StringTokenizer st; public FastScanner(BufferedReader in) { this.in = in; } public String nextToken() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(in.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } public int nextInt() { return Integer.parseInt(nextToken()); } public long nextLong() { return Long.parseLong(nextToken()); } public double nextDouble() { return Double.parseDouble(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; inline vector<int> getDigit(long long n) { vector<int> d(10); do { d[n % 10]++; n /= 10; } while (n); return d; } inline size_t lenLong(long long n) { size_t count = 0; do { ++count; n /= 10; } while (n); return count; } inline long long getLong(const vector<int>& v) { long long result = 0; for (size_t i = 9; i < 10; --i) { for (int j = 0; j < v[i]; ++j) { result *= 10; result += i; } } return result; } long long solve(long long n, long long mod) { map<pair<long long, long long>, long long> m; long long input = getLong(getDigit(n)); m[make_pair(input, 0LL)] = 1; bool isFirst = true; for (size_t iteration = 0; iteration < lenLong(n); ++iteration) { map<pair<long long, long long>, long long> m_; for (const auto& it : m) { auto d = getDigit(it.first.first); for (size_t i = 0 + isFirst; i < 10; ++i) { if (d[i] == 0) continue; --d[i]; m_[make_pair(getLong(d), (it.first.second * 10 + i) % mod)] += it.second; ++d[i]; } } m = m_; isFirst = false; } long long result = 0; for (const auto& it : m) { if (it.first.second % mod == 0) result += it.second; } return result; } long long solve2(long long n, long long mod) { vector<int> digits; do { digits.push_back(n % 10); n /= 10; } while (n); sort(digits.begin(), digits.end()); int count = 0; do { if (digits[0] == 0) continue; long long m = 0; for (size_t i = 0; i < digits.size(); ++i) { m = (m * 10 + digits[i]) % mod; } if (m == 0) ++count; } while (next_permutation(digits.begin(), digits.end())); return count; } int main() { long long n; int mod; cin >> n >> mod; cout << solve(n, mod) << endl; if (false) { for (int n = 1; n < 1000; ++n) { for (int mod = 1; mod <= 100; ++mod) { long long result = solve(n, mod); long long res2 = solve2(n, mod); if (res2 != result) cout << "!!! " << n << ' ' << mod << ' ' << result << ' ' << res2 << 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 N, M, n; long long digits[100]; long long dp[1 << 18][100]; int main() { ios::sync_with_stdio(false); cin >> N >> M; for (n = 0; N > 0; n++) { digits[n] = N % 10; N /= 10; } sort(digits, digits + n, greater<int>()); memset(dp, 0, sizeof(dp)); dp[0][0] = 1; for (long long i = 0; i < (1 << n); i++) { for (long long first = 0; first < M; first++) { if (dp[i][first] == 0) continue; int prev = -1; for (long long j = 0; j < n; j++) { if ((i | (1 << j)) == i) continue; if (prev == digits[j]) continue; if (i == 0 && digits[j] == 0) continue; dp[i | (1 << j)][(first * 10 + digits[j]) % M] += dp[i][first]; prev = digits[j]; } } } cout << dp[(1 << n) - 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; int n, m, l, k, p; long long f[(1 << 18) + 1][101], ans; string a; int lt[20], c[10]; long get(long x, long i) { return (x >> i) & 1; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> a >> m; lt[0] = 1; for (int i = 1; i <= 18; ++i) { lt[i] = (lt[i - 1] * 10) % m; while (lt[i] < 0) lt[i] += m; } n = a.size(); f[0][0] = 1; l = (1 << n) - 1; for (int s = 0; s <= l; ++s) { k = __builtin_popcount(s); for (int j = 0; j < m; ++j) { if (f[s][j] == 0) continue; for (int i = 0; i < n; ++i) { if (get(s, i)) continue; p = (j + (a[i] - '0') * lt[k]) % m; f[s + (1 << i)][p] += f[s][j]; if (k == n - 1 && a[i] != '0' && p == 0) ans += f[s][j]; } } } for (int i = 0; i < n; ++i) c[a[i] - '0']++; for (int i = 0; i <= 9; ++i) { for (int j = 1; j <= c[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
import java.io.BufferedOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Random; import java.util.function.Consumer; public class Solution { public static void main(String[] args) { try (PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out))) { _Scanner sc = new _Scanner(System.in); // 17:34- long n = sc.nextLong(); int m = sc.nextInt(); int len = String.valueOf(n).length(); long[][] dp = new long[1 << len][m]; dp[0][0] = 1; long[] pows = new long[18]; pows[0] = 1; for (int i = 1; i < pows.length; i++) pows[i] = pows[i - 1] * 10; long[] fac = new long[18]; fac[0] = 1; for (int i = 1; i < fac.length; i++) fac[i] = i * fac[i - 1]; int[] freq = new int[10]; for (long t = n; t > 0; t /= 10) freq[(int) (t % 10)]++; long div = 1; for (int num : freq) div *= fac[num]; for (int k = 0; k < len; k++) { for (int j = 0; j < len; j++) { int d = (int) (n / pows[j] % 10); int mod = (int) (d * pows[k] % m); if (k == len - 1 && d == 0) continue; int finalJ = j; doit(0, 0, k, len, 0, num -> { if (((1 << finalJ) & num) == 0) { for (int i = 0; i < m; i++) { dp[num | (1 << finalJ)][(i + mod) % m] += dp[num][i]; } } }); } } out.println(dp[(1 << len) - 1][0] / div); } } private static void doit(int idx, int set, int target, int len, int tmp, Consumer<Integer> consumer) { if (len == idx) { if (set == target) { consumer.accept(tmp); } return; } if (set < target) doit(idx + 1, set + 1, target, len, tmp | (1 << idx), consumer); doit(idx + 1, set, target, len, tmp, consumer); } private static void reverse(int[] vs) { for (int i = 0; i < vs.length / 2; i++) { swap(vs, i, vs.length - 1 - i); } } static class _Scanner { InputStream is; _Scanner(InputStream is) { this.is = is; } byte[] bb = new byte[1 << 15]; int k, l; byte getc() { try { if (k >= l) { k = 0; l = is.read(bb); if (l < 0) return -1; } return bb[k++]; } catch (IOException e) { throw new RuntimeException(e); } } byte skip() { byte b; while ((b = getc()) <= 32) ; return b; } int nextInt() { int n = 0; int sig = 1; for (byte b = skip(); b > 32; b = getc()) { if (b == '-') { sig = -1; } else { n = n * 10 + b - '0'; } } return sig * n; } long nextLong() { long n = 0; long sig = 1; for (byte b = skip(); b > 32; b = getc()) { if (b == '-') { sig = -1; } else { n = n * 10 + b - '0'; } } return sig * n; } double nextDouble() { return Double.parseDouble(next()); } public String next() { StringBuilder sb = new StringBuilder(); for (int b = skip(); b > 32; b = getc()) { sb.append(((char) b)); } return sb.toString(); } } private static void shuffle(int[] ar) { Random rnd = new Random(); shuffle(ar, rnd); } private static void shuffle(long[] ar) { Random rnd = new Random(); shuffle(ar, rnd); } private static void shuffle(int[] ar, Random rnd) { for (int i = 0; i < ar.length; i++) { int j = i + rnd.nextInt(ar.length - i); swap(ar, i, j); } } private static void shuffle(long[] ar, Random rnd) { for (int i = 0; i < ar.length; i++) { int j = i + rnd.nextInt(ar.length - i); swap(ar, i, j); } } private static void shuffle(Object[] ar) { Random rnd = new Random(); for (int i = 0; i < ar.length; i++) { int j = i + rnd.nextInt(ar.length - i); swap(ar, i, j); } } private static void swap(int[] ar, int i, int j) { int t = ar[i]; ar[i] = ar[j]; ar[j] = t; } private static void swap(long[] ar, int i, int j) { long t = ar[i]; ar[i] = ar[j]; ar[j] = t; } private static void swap(Object[] ar, int i, int j) { Object t = ar[i]; ar[i] = ar[j]; ar[j] = t; } }
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; char s[20]; long long dp[1 << 18][100], f[10]; int main() { int m; scanf("%s%d", s, &m); int n = strlen(s); for (int i = 0; s[i]; i++) { s[i] -= '0'; } dp[0][0] = 1; for (int i = 0; i < (1 << n); i++) { for (int j = 0; j < m; j++) { memset(f, 0, sizeof(f)); for (int k = 0; k < n; k++) { if ((i >> k) & 1 || f[s[k]] || (i == 0 && s[k] == 0)) continue; f[s[k]] = 1; dp[i | (1 << k)][(j * 10 + s[k]) % m] += dp[i][j]; } } } printf("%I64d\n", dp[(1 << n) - 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
import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Scanner; public class CF401D{ static int cntOne(int s){ int ret = 0; while (s>0){ s &= (s-1); ret ++; } return ret; } public static void main(String[] args){ long[] pow10 = new long[18]; long[] fact = new long[19]; pow10[0] = 1; fact[0] = 1; for (int i=1; i<18; i++){ pow10[i] = pow10[i-1]*10; fact[i] = fact[i-1]*i; } fact[18] = fact[17]*18; Scanner scn = new Scanner(System.in); while (scn.hasNext()){ long x = scn.nextLong(); int m = scn.nextInt(); int[] cnt = new int[10]; int n = 0; Arrays.fill(cnt, 0); while (x>0){ int t = (int)(x%10); x /= 10; cnt[t] ++; n ++; } int allS = 1<<n; List<List<Integer>> stateList = new ArrayList<>(); for (int i=0; i<=n-cnt[0]; i++){ stateList.add(new ArrayList<>()); } for (int s=0; s<allS; s++){ if (cntOne(s)<stateList.size()) { stateList.get(cntOne(s)).add(s); } } long dp[][] = new long[allS][m]; for (int s=0; s<allS; s++){ Arrays.fill(dp[s], 0L); } dp[0][0] = 1; int nowCnt = 0; for (int i=1; i<=9; i++){ for (int j=1; j<=cnt[i]; j++){ nowCnt ++; for (int p=0; p<n; p++){ int now = 1<<p; long add = i*pow10[p]; for (int s : stateList.get(nowCnt-1)){ if ((s&now)!=0){ continue; } int newS = s|now; for (int r=0; r<m; r++){ if (dp[s][r]==0){ continue; } int newR = (int)((add+r)%m); dp[newS][newR] += dp[s][r]; } } } if (j==cnt[i]){ for (int s : stateList.get(nowCnt)){ for (int r=0; r<m; r++){ dp[s][r] /= fact[cnt[i]]; } } } } } long ans = 0L; for (int s : stateList.get(nowCnt)){ if (s>=(1<<(n-1))){ ans += dp[s][0]; } } System.out.println(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; const double pi = acos(-1); const double eps = 1e-10; const int inf = 0x3f3f3f3f; const long long infLL = 0x3f3f3f3f3f3f3f3fLL; const int maxn = 18 + 5; const int maxs = (1 << 18) + 5; const int maxm = 100 + 5; string s; int n, m; long long f[maxs][maxm]; int cnt[maxn]; long long fac[maxn]; int main() { ios::sync_with_stdio(false); cin >> s >> m; n = s.length(); for (int i = 0; i < (n); ++i) ++cnt[s[i] - '0']; fac[0] = 1; for (int i = 1; i < maxn; ++i) fac[i] = fac[i - 1] * i; f[0][0] = 1; for (int S = 0; S < ((1 << n)); ++S) for (int j = 0; j < (m); ++j) if (f[S][j]) { for (int i = 0; i < (n); ++i) if (~S & (1 << i)) { if (!S && s[i] == '0') continue; f[S | (1 << i)][(j * 10 + s[i] - '0') % m] += f[S][j]; } } long long res = f[(1 << n) - 1][0]; for (int i = 0; i < (10); ++i) res /= fac[cnt[i]]; 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
#include <bits/stdc++.h> using namespace std; int len; 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; } } inline long long solve(int mask, int m) { if (mask == (1 << len) - 1) return m == 0; if (dp[mask][m] != -1) return dp[mask][m]; long long ans = 0; for (int j = (0); j <= (len - 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() { ios::sync_with_stdio(0); cin.tie(0); ; cin >> n >> mo; len = n.size(); pre(); memset(dp, -1, sizeof(dp)); long long ans = 0; for (int i = (0); i <= (len - 1); ++i) { coun[n[i] - '0']++; if (n[i] - '0' > 0) ans += solve(1 << i, (n[i] - '0') % mo); } for (int i = (0); i <= (9); ++i) { for (int j = (1); j <= (coun[i]); ++j) ans /= j; } cout << ans; cout.flush(); 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 N = 18; long long dp[(1 << N)][105]; string n; int qwq; int m, pw[20]; bool used[10]; void init() { pw[0] = 1; for (int i = 1; i <= n.size(); i++) pw[i] = pw[i - 1] * 10 % m; } int main() { cin >> n >> m; dp[0][0] = 1; init(); for (int i = 1; i < (1 << n.size()); i++) { int sz = n.size() - __builtin_popcount(i); for (int k = 0; k < m; k++) { for (int j = 0; j < 10; j++) used[j] = false; for (int j = 0; j < n.size(); j++) if (((i >> j) & 1) && !used[n[j] - '0']) if (sz != (int)n.size() - 1 || n[j] != '0') dp[i][k] += dp[i - (1 << j)][(m + k - (n[j] - '0') * pw[sz] % m) % m], used[n[j] - '0'] = true; } } cout << dp[(1 << n.size()) - 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> #pragma comment(linker, "/STACK:102400000,102400000") using namespace std; char s[25]; int m; long long ans[1 << 18][105]; long long fac(int n) { long long out = 1; for (int i = 1; i <= n; i++) out *= i; return out; } int tool[10]; int main() { scanf("%s%d", s, &m); int len = strlen(s); sort(s, s + len); for (int i = 0; i < len; i++) { if (s[i] - '0') ans[1 << i][(s[i] - '0') % m] = 1; tool[s[i] - '0']++; } for (int i = 1; i < (1 << len) - 1; i++) { for (int k = 0; k < m; k++) { for (int j = 0; j < len; j++) { if (i & (1 << j)) continue; ans[i + (1 << j)][(k * 10 + (s[j] - '0')) % m] += ans[i][k]; } } } long long out = ans[(1 << len) - 1][0]; for (int i = 0; i <= 9; i++) out /= fac(tool[i]); cout << out << '\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; typedef pair<long long, long long> ll; typedef vector<long long> vl; typedef vector<ll> vll; typedef vector<vl> vvl; template <typename T> ostream &operator<<(ostream &o, vector<T> v) { if (v.size() > 0) o << v[0]; for (unsigned i = 1; i < v.size(); i++) o << " " << v[i]; return o << " "; } template <typename U, typename V> ostream &operator<<(ostream &o, pair<U, V> p) { return o << "(" << p.first << ", " << p.second << ") "; } template <typename T> istream &operator>>(istream &in, vector<T> &v) { for (unsigned i = 0; i < v.size(); i++) in >> v[i]; return in; } template <typename T> istream &operator>>(istream &in, pair<T, T> &p) { in >> p.first; in >> p.second; return in; } int main(int argc, char *argv[]) { std::ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cout.precision(20); string s; long long m; cin >> s >> m; vl fact(20, 1); for (long long i = (2); i < (long long)20; i++) fact[i] = fact[i - 1] * i; long long n = s.length(); vvl dp((1 << n), vl(m, 0)); dp[0][0] = 1; for (long long(mask) = 0; (mask) < ((1 << n)); (mask)++) { for (long long(rem) = 0; (rem) < (m); (rem)++) { if (dp[mask][rem] == 0) continue; for (long long i = (0); i < (long long)n; i++) { if (mask == 0 and s[i] == '0') continue; if (mask & (1 << i)) continue; long long x = (rem * 10 + s[i] - '0') % m; dp[mask | (1 << i)][x] += dp[mask][rem]; } } } vl cnt(10, 0); for (long long i = (0); i < (long long)s.length(); i++) cnt[s[i] - '0']++; for (long long i = (0); i < (long long)cnt.size(); i++) dp[(1 << n) - 1][0] /= fact[cnt[i]]; cout << dp[(1 << n) - 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; long long n; int m; int ara[20], len; long long dp[1 << 18][102]; long long func(int mask, int modu) { if (mask == (1 << len) - 1) { if (modu == 0) { return 1; } return 0; } if (dp[mask][modu] != -1) { return dp[mask][modu]; } long long ans = 0; bool chk[10]; memset((chk), 0, sizeof(chk)); for (int i = 0; i <= len - 1; i++) { if ((mask == 0) && ara[i] == 0) { continue; } if (chk[ara[i]]) { continue; } if (!(mask & (1 << i))) { int tmp = (modu * 10) + ara[i]; if (tmp >= m) { tmp %= m; } ans += func(mask | (1 << i), tmp); chk[ara[i]] = 1; } } return dp[mask][modu] = ans; } void init() { long long cc = n; len = 0; while (cc) { ara[len++] = cc % 10; cc /= 10; } } int main() { std::ios_base::sync_with_stdio(false); ; cin >> n >> m; init(); memset((dp), -1, sizeof(dp)); cout << func(0, 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; template <typename T> void read(T &a) { register int b = 1, c = getchar(); a = 0; for (; !isdigit(c); c = getchar()) if (c == 45) b *= -1; for (; isdigit(c); c = getchar()) a = (a << 3) + (a << 1) + c - 48; a *= b; } const int maxn = 105; const int maxm = 1 << 18; long long dp[maxm][maxn]; string str; int len; int m; int main() { cin >> str; len = int((str).size()); read(m); dp[0][0] = 1; for (int msk = 0; msk < int(1 << len); msk++) { for (int rem = 0; rem < int(m); rem++) { for (int j = 0; j < int(len); j++) if (!(msk & (1 << j))) { int nwrem = (rem * 10 + str[j] - '0') % m; int nwmsk = msk | (1 << j); if (!msk && str[j] == '0') continue; dp[nwmsk][nwrem] += dp[msk][rem]; } } } long long ans = dp[(1 << len) - 1][0]; for (int i = 0; i < int(10); i++) { int cnt = 0; for (int j = 0; j < int(len); j++) { if (str[j] == i + 48) cnt++; } long long fac = 1; for (int j = 1; j <= cnt; j++) fac *= j; ans /= fac; } printf("%I64d\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
#include <bits/stdc++.h> using namespace std; long long dp[262144][101]; queue<int> Q; int K[262144], P[19], A[19]; char S[20]; int main() { int a, b, t, l, n, la, s; cin >> S + 1 >> b, dp[0][0] = 1, a = strlen(S + 1), l = (1 << a) - 1, Q.push(0), n = 1, P[0] = 1 % b; for (int i = 1; i <= 18; i++) P[i] = P[i - 1] * 10 % b; for (int r = 1; r <= a; r++) { s = S[r] - '0', t = 0, A[s]++; for (int i = 1; i <= n; i++) { la = Q.front(), Q.pop(); for (int j = 0; j < a; j++) if (!(la & (1 << j))) { if (s == 0 && j == a - 1) continue; int nw = la | (1 << j), v = P[j] * s % b; for (int k = 0; k < b; k++) dp[nw][(v + k) % b] += dp[la][k]; if (!K[nw]) K[nw] = 1, Q.push(nw), t++; } } n = t; } for (int i = 0; i <= 9; i++) for (int j = 2; j <= A[i]; j++) dp[l][0] /= j; cout << dp[l][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 zeros = 0; long long cnt[1 << 18][100]; int m; vector<int> digit, summary; int bitcnt(int i) { int res = 0; while (i) { i >>= 1; res++; } return max(res, 1); } int mhash(vector<int> &v) { int res = 0; for (int i = 0; i < int(v.size()); ++i) { res = (res << bitcnt(summary[i])) + v[i]; } return res; } vector<int> decode(int h) { vector<int> res(summary.size()); for (int i = summary.size() - 1; i >= 0; --i) { res[i] = h & ((1 << bitcnt(summary[i])) - 1); h >>= bitcnt(summary[i]); } return res; } long long calcdp(int hash, int curmod) { if (cnt[hash][curmod] >= 0) return cnt[hash][curmod]; cnt[hash][curmod] = 0; vector<int> v = decode(hash); bool any = false; for (int i = 0; i < int(v.size()); ++i) { if (digit[i] || hash) if (v[i] < summary[i]) { any = true; vector<int> nv = v; nv[i]++; int nmod = (curmod * 10 + digit[i]) % m; cnt[hash][curmod] += calcdp(mhash(nv), nmod); } } if (!any && !curmod) cnt[hash][curmod] = 1; return cnt[hash][curmod]; } int dcnt[10]; int main() { long long n; cin >> n >> m; memset(cnt, 255, sizeof(cnt)); while (n) { dcnt[n % 10]++; n /= 10; } for (int i = 0; i < int(10); ++i) if (dcnt[i]) { digit.push_back(i); summary.push_back(dcnt[i]); } cout << calcdp(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.util.*; public class RomanAndNumbers { private static long[][]dp; private static char[]s; private static int m; public static void main(String[]args){ Scanner sc=new Scanner(System.in); s=(""+sc.nextLong()).toCharArray(); m=sc.nextInt(); sc.close(); dp=new long[(int)Math.pow(2,s.length)][m]; for(long[]a:dp) for(int i=0;i<a.length;i++) a[i]=-1; System.out.println(getDP(0,0)); } private static long getDP(int mask,int mod){ if(dp[mask][mod]>=0) return dp[mask][mod]; if(mask==dp.length-1){ dp[mask][mod]=mod==0?1:0; return dp[mask][mod]; } dp[mask][mod]=0; boolean[]used=new boolean[10]; for(int i=0;i<s.length;i++) if(!used[s[i]-'0']&&(mask|1<<i)!=mask&&(mask>0||s[i]!='0')){ dp[mask][mod]+=getDP(mask|1<<i,(10*mod+(s[i]-'0'))%m); used[s[i]-'0']=true; } return dp[mask][mod]; } }
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.PrintWriter; import java.util.Arrays; import java.util.StringTokenizer; public class D { static char[] ch; static int m; static long[][] dp; static long solve(int mask, int mod, int len) { if (len == ch.length) return mod == 0 ? 1L : 0; if (dp[mask][mod] != -1) return dp[mask][mod]; long ans = 0; for (int i = 0; i < ch.length; i++) { if ((mask & (1 << i)) == 0) { if (ch[i] == '0' && len == 0) continue; if (i > 0 && ch[i] == ch[i - 1] && (mask & (1 << (i - 1))) == 0) continue; ans += solve(mask | (1 << i), (mod * 10 + (ch[i] - '0')) % m, len + 1); } } return dp[mask][mod] = ans; } public static void main(String[] args) throws IOException { br = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); sc = new StringTokenizer(""); ch = nxtCharArr(); Arrays.sort(ch); m = nxtInt(); dp = new long[1 << ch.length][m]; for (long[] a : dp) Arrays.fill(a, -1); out.println(solve(0, 0, 0)); br.close(); out.close(); } static BufferedReader br; static StringTokenizer sc; static PrintWriter out; static String nxtTok() throws IOException { while (!sc.hasMoreTokens()) { String s = br.readLine(); if (s == null) return null; sc = new StringTokenizer(s.trim()); } return sc.nextToken(); } static int nxtInt() throws IOException { return Integer.parseInt(nxtTok()); } static long nxtLng() throws IOException { return Long.parseLong(nxtTok()); } static double nxtDbl() throws IOException { return Double.parseDouble(nxtTok()); } static int[] nxtIntArr(int n) throws IOException { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nxtInt(); return a; } static long[] nxtLngArr(int n) throws IOException { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nxtLng(); return a; } static char[] nxtCharArr() throws IOException { return nxtTok().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.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.io.FilterInputStream; import java.io.BufferedInputStream; import java.io.InputStream; /** * @author khokharnikunj8 */ public class Main { public static void main(String[] args) { new Thread(null, new Runnable() { public void run() { new Main().solve(); } }, "1", 1 << 26).start(); } void solve() { InputStream inputStream = System.in; OutputStream outputStream = System.out; ScanReader in = new ScanReader(inputStream); PrintWriter out = new PrintWriter(outputStream); DRomanAndNumbers solver = new DRomanAndNumbers(); solver.solve(1, in, out); out.close(); } static class DRomanAndNumbers { public void solve(int testNumber, ScanReader in, PrintWriter out) { char[] s = in.scanString().toCharArray(); int m = in.scanInt(); int n = s.length; long[][] dp = new long[1 << n][m]; for (int i = 0; i < n; i++) { if (s[i] != '0') dp[1 << i][(s[i] - '0') % m] = 1; } int[] si = new int[n]; for (int i = 0; i < n; i++) si[i] = s[i] - '0'; long[] fact = new long[20]; fact[0] = 1; for (int i = 1; i < 20; i++) fact[i] = fact[i - 1] * i; for (int i = 1; i < 1 << n; i++) { 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) { dp[i ^ (1 << k)][(j * 10 + (si[k])) % m] += dp[i][j]; } } } } long ans = dp[(1 << n) - 1][0]; int[] count = new int[10]; for (char c : s) count[c - '0']++; for (int i : count) ans /= fact[i]; out.println(ans); } } static class ScanReader { private byte[] buf = new byte[4 * 1024]; private int index; private BufferedInputStream in; private int total; public ScanReader(InputStream inputStream) { in = new BufferedInputStream(inputStream); } private int scan() { if (index >= total) { index = 0; try { total = in.read(buf); } catch (Exception e) { e.printStackTrace(); } if (total <= 0) return -1; } return buf[index++]; } public int scanInt() { int integer = 0; int n = scan(); while (isWhiteSpace(n)) n = scan(); int neg = 1; if (n == '-') { neg = -1; n = scan(); } while (!isWhiteSpace(n)) { if (n >= '0' && n <= '9') { integer *= 10; integer += n - '0'; n = scan(); } } return neg * integer; } public String scanString() { int c = scan(); if (c == -1) return null; while (isWhiteSpace(c)) c = scan(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = scan(); } while (!isWhiteSpace(c)); return res.toString(); } private boolean isWhiteSpace(int n) { if (n == ' ' || n == '\n' || n == '\r' || n == '\t' || n == -1) return true; else return false; } } }
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() { ios_base::sync_with_stdio(false); cin.tie(NULL); string s; cin >> s; int m; cin >> m; int n = s.size(); sort(s.begin(), s.end()); int limit = (1 << n); long long int dp[limit][m]; memset(dp, 0, sizeof(dp)); dp[0][0] = 1; for (int i = 0; i < limit; i++) { for (int j = 0; j < m; j++) { if (!dp[i][j]) continue; char prev = 'X'; for (int k = 0; k < n; k++) { if (i == 0 && s[k] == '0') continue; if (i & (1 << k)) continue; if (s[k] == prev) continue; prev = s[k]; int newmod = (j * 10 + (int)(s[k] - '0')) % m; int temp = i | (1 << k); dp[i | (1 << k)][newmod] += dp[i][j]; } } } cout << dp[limit - 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; const long long int mod = 1e9 + 7; long long int t, n, m, a[1000005] = {0}, dp[263000][105] = {0}; string s; vector<long long int> v; map<long long int, long long int> mp; int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); long long int i, j, k, x, y; cin >> s >> m; for (i = 0; i < s.size(); i++) { v.push_back(s[i] - '0'); mp[s[i] - '0']++; } n = v.size(); for (j = 0; j < n; j++) { if (v[j] == 0) continue; dp[(1ll << j)][v[j] % m] = 1; } for (i = 1; i < (1ll << n); i++) { for (j = 0; j < n; j++) { if (i & (1ll << j)) continue; x = v[j] % m; for (k = 0; k < m; k++) { x = v[j] % m; x = (x + (k * 10)) % m; dp[(i | (1ll << j))][x] += dp[i][k]; } } } x = dp[(1ll << n) - 1][0]; for (auto it = mp.begin(); it != mp.end(); it++) { for (i = 1; i <= (it->second); i++) x /= i; } cout << x; 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; bool chk(int msk, int ps) { return (bool)(msk & (1 << ps)); } int seti(int msk, int ps) { return (msk |= (1 << ps)); } string str; int m; long long int dp[(1 << 18) + 2][101]; vector<int> vt[13]; long long int rec(int msk, int num) { if (__builtin_popcount(msk) == str.size()) { if (num == 0) return 1; else return 0; } long long int &ret = dp[msk][num]; if (ret != -1) return ret; ret = 0; for (int d = 0; d < 10; d++) { if (msk == 0 and d == 0) continue; for (int x : vt[d]) { if (chk(msk, x) == 0) { ret += rec(seti(msk, x), (num * 10 + d) % m); break; } } } return ret; } int main() { ios_base::sync_with_stdio(0); cin >> str; for (int i = 0; i < str.size(); i++) { char x = str[i]; vt[x - '0'].push_back(i); } cin >> m; memset(dp, -1, sizeof dp); long long int ans = rec(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
import java.util.Scanner; public class Roman { public static void main(String[] args) { // TODO code application logic here String s; int m; Scanner sc=new Scanner(System.in); s=sc.next(); m=sc.nextInt(); int sz=s.length(); int mx=1<<sz; long dp[][]=new long[mx+10][110]; dp[0][0]=1; long fact[]=new long[19]; fact[0]=1; fact[1]=1; for(int i=2;i<=18;i++) { fact[i]=i*fact[i-1]; } int num[]=new int[10]; for(int i=0;i<s.length();i++) { num[s.charAt(i)-'0']++; } long prod=1; for(int i=0;i<10;i++) { prod*=fact[num[i]]; } for(int mask=0;mask<mx;mask++) { for(int mod=0;mod<m;mod++) { for(int k=0;k<sz;k++) { // if((mask|(1<<k))==2&&(mod*10+s.charAt(k)-'0')%m==0)//&&(mod*10+(s.charAt(k)-'0'))==0) // System.out.println(mask+" ere "+mod+" "+s.charAt(k)); if((mask&(1<<k))!=0) continue; if((mask==0)&&(s.charAt(k)-'0')==0) continue; dp[(mask|(1<<k))][(mod*10+(s.charAt(k)-'0'))%m]+=dp[mask][mod]; } } } System.out.println(dp[mx-1][0]/prod); } }
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; namespace patch { template <typename T> std::string to_string(const T& n) { std::ostringstream stm; stm << n; return stm.str(); } } // namespace patch int m, lim, len; long long dp[1 << 18][101]; string s; long long fac[19]; int freq[10]; long long solve(int mask, int mod) { if (mask == lim) { if (mod == 0) return 1; return 0; } if (dp[mask][mod] != -1) return dp[mask][mod]; long long ans = 0; for (int i = 0; i < len; ++i) { if (!(mask & (1 << i))) { if (mask == 0) { if (s[i] == '0') continue; } ans += solve(mask | (1 << i), (mod * 10 + (s[i] - '0')) % m); } } return dp[mask][mod] = ans; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); fac[0] = fac[1] = 1; for (int i = 2; i <= 18; ++i) fac[i] = fac[i - 1] * (long long)i; memset(dp, -1, sizeof dp); cin >> s >> m; len = s.size(); for (int i = 0; i < len; ++i) { freq[s[i] - '0']++; } lim = (1 << len) - 1; long long ans = solve(0, 0); for (int i = 0; i < 10; ++i) { if (freq[i]) ans /= fac[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
import java.io.*; import java.util.*; public class Main { public static void main(String[] args) throws Exception { Scanner in = new Scanner(System.in); String n = in.next(); int m = in.nextInt(); byte[] nums = new byte[10]; for(char ch: n.toCharArray()) { ++nums[Character.digit(ch, 10)]; } int total = 1; for(byte num: nums) { total *= num + 1; } long[][] dp = new long[total][m]; dp[0][0] = 1; for(int i = 1; i < total; ++i) { for(int j = 0; j < m; ++j) { int d = 1; int numCount = 0; for(int k = 0; k < 10; ++k) { if(nums[k] != 0) { numCount += i / d % (nums[k] + 1); d *= nums[k] + 1; } } d = 1; long level = Fun.power(10, numCount - 1); for(int k = 0; k < 10; ++k) { if(nums[k] != 0) { int count = i / d % (nums[k] + 1); if(count != 0) { int currem = (int) (k * level % m); dp[i][j] += dp[i / (d * (nums[k] + 1)) * (d * (nums[k] + 1)) + (count - 1) * d + i % d][(j + m - currem) % m]; } d *= nums[k] + 1; } } } } if(nums[0] == 0) { System.out.println(dp[total - 1][0]); } else { System.out.println(dp[total - 1][0] - dp[total - 1 - 1][0]); } } } // /////////////////////////////////////////////////////////// // /////////////////////////////////////////////////////////// class Fun { public static long power(long n, long e) { long result = 1; for(long i = 0; i < e; ++i) { result *= n; } return result; } public static int[] nextInts(Scanner in, int count) { int[] result = new int[count]; for (int i = 0; i < count; ++i) { result[i] = in.nextInt(); } return result; } public static boolean isPrime(int n) { boolean result = true; for(int i = 2; i * i <= n; ++i) { if(n % i == 0) { result = false; break; } } return result; } public static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } public static int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } public static long lcm(long a, long b) { return a / gcd(a, b) * b; } public static int[][] transpose(int[][] mat) { int r = mat.length; int c = mat[0].length; int[][] result = new int[c][r]; for (int i = 0; i < c; ++i) for (int j = 0; j < r; ++j) result[i][j] = mat[j][i]; return result; } public static String repeatString(String s, long count) { StringBuilder sb = new StringBuilder(); for (long i = 0; i < count; ++i) { sb.append(s); } return sb.toString(); } } class Treap<K extends Comparable<? super K>, V> { Treap() { nullNode = new TreapNode<K, V>(); nullNode.left = nullNode.right = nullNode; nullNode.priority = Integer.MAX_VALUE; rootNode = nullNode; } public V put(K key, V value) { PutResult<K, V> putResult = putHelp(rootNode, key, value); rootNode = putResult.newNode; return putResult.originalValue; } public V get(K key) { TreapNode<K, V> currentNode = rootNode; while(currentNode != nullNode && currentNode.key.compareTo(key) != 0) { if(key.compareTo(currentNode.key) < 0) { currentNode = currentNode.left; } else { currentNode = currentNode.right; } } if(currentNode == nullNode) { return null; } else { return currentNode.value; } } public V remove(K key) { V result = get(key); if(result != null) { rootNode = removeHelp(rootNode, key); } return result; } public K select(int rank) { if(rank >= size() || rank < 0) { return null; } TreapNode<K, V> currentNode = rootNode; while(rank != currentNode.left.size) { if(rank < currentNode.left.size) { currentNode = currentNode.left; } else { rank -= currentNode.left.size + 1; currentNode = currentNode.right; } } return currentNode.key; } public int rank(K key) { if(rootNode == nullNode) { return -1; } int result = rootNode.left.size; TreapNode<K, V> currentNode = rootNode; while(currentNode != nullNode && currentNode.key.compareTo(key) != 0) { if(key.compareTo(currentNode.key) < 0) { result -= currentNode.left.right.size + 1; currentNode = currentNode.left; } else { result += currentNode.right.left.size + 1; currentNode = currentNode.right; } } if(currentNode == nullNode) { return -1; } else { return result; } } public void clear() { rootNode = nullNode; } public int size() { return rootNode.size; } private static class TreapNode<K, V> { public K key; public V value; public int size; public int priority; TreapNode<K, V> left, right; } private static class PutResult<K, V> { public TreapNode<K, V> newNode; public V originalValue; } private PutResult<K, V> putHelp(TreapNode<K, V> node, K key, V value) { if(node == nullNode) { PutResult<K, V> result = new PutResult<K, V>(); result.newNode = new TreapNode<K, V>(); result.newNode.key = key; result.newNode.value = value; result.newNode.size = 1; result.newNode.priority = randomPriority.nextInt(Integer.MAX_VALUE); result.newNode.left = result.newNode.right = nullNode; return result; } else if(key.compareTo(node.key) == 0) { PutResult<K, V> result = new PutResult<K, V>(); result.originalValue = node.value; node.value = value; result.newNode = node; return result; } else if(key.compareTo(node.key) < 0) { PutResult<K, V> result = putHelp(node.left, key, value); node.left = result.newNode; if(result.originalValue == null) { ++node.size; if(node.left.priority < node.priority) { node = rightRotate(node); } } result.newNode = node; return result; } else { PutResult<K, V> result = putHelp(node.right, key, value); node.right = result.newNode; if(result.originalValue == null) { ++node.size; if(node.right.priority < node.priority) { node = leftRotate(node); } } result.newNode = node; return result; } } private TreapNode<K, V> removeHelp(TreapNode<K, V> node, K key) { if(key.compareTo(node.key) == 0) { node = removeHelp2(node); } else if(key.compareTo(node.key) < 0) { --node.size; node.left = removeHelp(node.left, key); } else { --node.size; node.right = removeHelp(node.right, key); } return node; } private TreapNode<K, V> removeHelp2(TreapNode<K, V> node) { if(node.left == nullNode && node.right == nullNode) { return nullNode; } if(node.left.priority < node.right.priority) { node = rightRotate(node); --node.size; node.right = removeHelp2(node.right); } else { node = leftRotate(node); --node.size; node.left = removeHelp2(node.left); } return node; } private TreapNode<K, V> leftRotate(TreapNode<K, V> node) { TreapNode<K, V> result = node.right; node.right = result.left; result.left = node; result.size = node.size; node.size -= 1 + result.right.size; return result; } private TreapNode<K, V> rightRotate(TreapNode<K, V> node) { TreapNode<K, V> result = node.left; node.left = result.right; result.right = node; result.size = node.size; node.size -= 1 + result.left.size; return result; } private TreapNode<K, V> nullNode; private TreapNode<K, V> rootNode; private Random randomPriority = new Random(); }
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.Scanner; // https://codeforces.com/problemset/problem/401/D public class VipulAndModulos { private static int m, l; private static char[] n; private static long[][] dp; private static long solve(int mask, int mod) { if (mask == (1 << l) - 1) return mod == 0 ? 1 : 0; if (dp[mask][mod] != -1) return dp[mask][mod]; long ans = 0; boolean[] active = new boolean[10]; for (int i = 0; i < l; i++) { if (n[i] == '0' && mask == 0) continue; if ((mask & (1 << i)) == 0 && !active[n[i] - '0']) { ans += solve(mask | (1 << i), (mod * 10 + (n[i] - '0')) % m); active[n[i] - '0'] = true; } } return dp[mask][mod] = ans; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); // int t = sc.nextInt(); // while (t-- > 0) { n = sc.next().toCharArray(); m = sc.nextInt(); l = n.length; dp = new long[(1 << l) + 1][m + 1]; for (int i = 0; i <= (1 << l); i++) for (int j = 0; j <= m; j++) dp[i][j] = -1; System.out.println(solve(0, 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
/* Keep solving problems. */ import java.util.*; import java.io.*; public class CF401D { BufferedReader br; PrintWriter out; StringTokenizer st; boolean eof; final long MOD = 1000L * 1000L * 1000L + 7; long[][] dp; char[] arr; int n; int m; void solve() throws IOException { arr = Long.toString(nextLong()).toCharArray(); m = nextInt(); n = arr.length; dp = new long[1 << n][m]; dp[0][0] = 1; for(int mask = 0; mask < (1 << n); mask++) { for(int rem = 0; rem < m; rem++) { if(dp[mask][rem] == 0) { continue; } for(int i = 0; i < n; i++) { if((mask & (1 << i)) == 0) { if(!(mask == 0 && arr[i] == '0')) { dp[mask + (1 << i)][(rem * 10 + (arr[i] - '0')) % m] += dp[mask][rem]; } } } } } int[] hist = new int[10]; for(int i = 0; i < n; i++) { hist[arr[i] - '0']++; } long[] fact = new long[20]; fact[0] = 1; for(int i = 1; i < 20; i++) { fact[i] = fact[i - 1] * i; } long res = dp[(1 << n) - 1][0]; for(int i = 0; i < hist.length; i++) { res /= fact[hist[i]]; } out(res); } void shuffle(int[] a) { int n = a.length; for(int i = 0; i < n; i++) { int r = i + (int) (Math.random() * (n - i)); int tmp = a[i]; a[i] = a[r]; a[r] = tmp; } } private void outln(Object o) { out.println(o); } private void out(Object o) { out.print(o); } public CF401D() throws IOException { br = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); solve(); out.close(); } public static void main(String[] args) throws IOException { new CF401D(); } public long[] nextLongArr(int n) throws IOException{ long[] res = new long[n]; for(int i = 0; i < n; i++) res[i] = nextLong(); return res; } public int[] nextIntArr(int n) throws IOException { int[] res = new int[n]; for(int i = 0; i < n; i++) res[i] = nextInt(); return res; } public String nextToken() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (Exception e) { eof = true; return null; } } return st.nextToken(); } public String nextString() { try { return br.readLine(); } catch (IOException e) { eof = true; return null; } } public int nextInt() throws IOException { return Integer.parseInt(nextToken()); } public long nextLong() throws IOException { return Long.parseLong(nextToken()); } public double nextDouble() throws IOException { return Double.parseDouble(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.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.Arrays; import java.util.StringTokenizer; public class Abood2C { static int N; static long memo[][][][][][][][][][][][]; static int MOD; static long solve(int i, int[] r, int m) { if(i == N && m == 0) return 1; if(i == N) return 0; if(memo[i][r[0]][r[1]][r[2]][r[3]][r[4]][r[5]][r[6]][r[7]][r[8]][r[9]][m] != -1) return memo[i][r[0]][r[1]][r[2]][r[3]][r[4]][r[5]][r[6]][r[7]][r[8]][r[9]][m]; long ans = 0; int[] v = r.clone(); int p = 1; for (int j = 0; j < i; j++) { p *= 10; p %= MOD; } for (int j = 0; j < r.length; j++) { if(r[j] == 0 || j == 0 && i == N - 1) continue; v[j]--; ans += solve(i + 1, v, (m + p * j) % MOD); v[j]++; } return memo[i][r[0]][r[1]][r[2]][r[3]][r[4]][r[5]][r[6]][r[7]][r[8]][r[9]][m] = ans; } public static void main(String[] args) throws Exception{ Scanner sc = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); String s = sc.next(); N = s.length(); MOD = sc.nextInt(); int[] r = new int[10]; for (int i = 0; i < s.length(); i++) r[s.charAt(i) - '0']++; memo = new long [N][r[0] + 1][r[1] + 1][r[2] + 1][r[3] + 1][r[4] + 1][r[5] + 1][r[6] + 1][r[7] + 1][r[8] + 1][r[9] + 1][MOD]; for (int i = 0; i < N; i++) for (int j = 0; j <= r[0]; j++) for (int j2 = 0; j2 <= r[1]; j2++) for (int k = 0; k <= r[2]; k++) for (int k2 = 0; k2 <= r[3]; k2++) for (int l = 0; l <= r[4]; l++) for (int l2 = 0; l2 <= r[5]; l2++) for (int m = 0; m <= r[6]; m++) for (int m2 = 0; m2 <= r[7]; m2++) for (int o = 0; o <= r[8]; o++) for (int o2 = 0; o2 <= r[9]; o2++) Arrays.fill(memo[i][j][j2][k][k2][l][l2][m][m2][o][o2], -1); out.println(solve(0, r, 0)); out.flush(); out.close(); } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream System){ br = new BufferedReader(new InputStreamReader(System));} public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public String nextLine()throws IOException{return br.readLine();} public int nextInt() throws IOException {return Integer.parseInt(next());} public double nextDouble() throws IOException {return Double.parseDouble(next());} public char nextChar()throws IOException{return next().charAt(0);} public Long nextLong()throws IOException{return Long.parseLong(next());} public boolean ready() throws IOException{return br.ready();} public void waitForInput(){for(long i = 0; i < 3e9; i++);} } }
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.IOException; import java.util.Arrays; 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 * @author Mahmoud Aladdin <aladdin3> */ 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 { int[] input; int m; long[][] memory = new long[(1 << 18)][101]; public void solve(int testNumber, InputReader jin, PrintWriter jout) { String token = jin.token(); m = jin.int32(); input = new int[token.length()]; for(int i = 0; i < token.length(); i++) { input[i] = token.charAt(i) - '0'; } for(long[] r: memory) Arrays.fill(r, -1); BigInteger mask = BigInteger.ZERO; BigInteger curr = BigInteger.ZERO; long res = 0; for(int i = 0; i < input.length; i++) { if(input[i] != 0) { if(curr.testBit(input[i])) continue; curr = curr.setBit(input[i]); res += bt(mask.setBit(i), input[i] % m, input[i]); } } jout.println(res); } private long bt(BigInteger mask, int rem, long val) { if(mask.intValue() == (1 << input.length) - 1) { return rem == 0? 1: 0; } else { if(memory[mask.intValue()][rem] != -1) return memory[mask.intValue()][rem]; long res = 0; BigInteger currMask = BigInteger.ZERO; for(int i = 0; i < input.length; i++) { if(mask.testBit(i)) continue; if(currMask.testBit(input[i])) continue; currMask = currMask.setBit(input[i]); res += bt(mask.setBit(i), ((rem * 10) + input[i]) % m, val * 10 + input[i]); } return memory[mask.intValue()][rem] = res; } } } class InputReader { private static final int bufferMaxLength = 1024; private InputStream in; private byte[] buffer; private int currentBufferSize; private int currentBufferTop; private static final String tokenizers = " \t\r\f\n"; public InputReader(InputStream stream) { this.in = stream; buffer = new byte[bufferMaxLength]; currentBufferSize = 0; currentBufferTop = 0; } private boolean refill() { try { this.currentBufferSize = this.in.read(this.buffer); this.currentBufferTop = 0; } catch(Exception e) {} return this.currentBufferSize > 0; } private Byte readChar() { if(currentBufferTop < currentBufferSize) { return this.buffer[this.currentBufferTop++]; } else { if(!this.refill()) { return null; } else { return readChar(); } } } public String token() { StringBuffer tok = new StringBuffer(); Byte first; while((first = readChar()) != null && (tokenizers.indexOf((char) first.byteValue()) != -1)); if(first == null) return null; tok.append((char)first.byteValue()); while((first = readChar()) != null && (tokenizers.indexOf((char) first.byteValue()) == -1)) { tok.append((char)first.byteValue()); } return tok.toString(); } public Integer int32() throws NumberFormatException { String tok = token(); return tok == null? null : Integer.parseInt(tok); } }
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 dig[20]; const int M = 262145; long long dp[262145][101]; int m; int pos; long long dfs(int state, int x) { long long ans = 0; if (state == (1 << pos) - 1) return x == 0; long long &dpAns = dp[state][x]; if (dpAns != -1) return dpAns; int vis[10] = {0}; for (int i = pos - 1; i >= 0; i--) { if ((1 << i) & state) continue; if (vis[dig[i]]) continue; vis[dig[i]] = 1; ans += dfs(state | 1 << i, (x * 10 + dig[i]) % m); } dpAns = ans; return ans; } long long solve(long long n) { pos = 0; while (n) { dig[pos++] = n % 10; n /= 10; } long long ans = 0; int vis[10] = {0}; for (int i = pos - 1; i >= 0; i--) if (dig[i] && !vis[dig[i]]) { vis[dig[i]] = 1; ans += dfs(1 << i, dig[i] % m); } return ans; } int main(void) { long long a; while (cin >> a >> m) { memset(dp, -1, sizeof(dp)); cout << solve(a) << 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 f[1 << 18][101]; bool bit(int x, int i) { return ((x >> i) & 1); } int bit1(int x, int i) { return (x | (1 << i)); } inline long long fac(int k) { long long ans = 1, i; for (i = 1; i <= k; i++) { ans *= i; } return ans; } int C[100]; int main() { int cnt = 0; string s; cin >> s; int m; cin >> m; vector<int> a; a.resize((int)s.length()); for (int i = 0; i < (int)s.length(); ++i) { a[i] = s[i] - '0'; C[a[i]]++; } long long n = (int)a.size(); f[0][0] = 1; for (int bmp = 0; bmp < (1 << ((int)a.size())); ++bmp) { for (int k = 0; k < m; ++k) { for (int i = 0; i < n; ++i) if (!bit(bmp, i)) { int new_bmp = bit1(bmp, i); if (bmp != 0 || a[i] != 0) f[new_bmp][(10 * k + a[i]) % m] += f[bmp][k]; } } } long long ans = f[(1 << (n)) - 1][0]; for (int i = 0; i <= 9; i++) { ans = ans / fac(C[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
/** * DA-IICT * Author : PARTH PATEL */ import java.io.*; import java.math.*; import java.util.*; import static java.util.Arrays.fill; import static java.lang.Math.*; import static java.util.Arrays.sort; import static java.util.Collections.sort; public class D401 { public static int mod = 1000000007; static FasterScanner in = new FasterScanner(); static PrintWriter out = new PrintWriter(System.out); public static long factorial(long n) { if(n==0) return 1; else return n*factorial(n-1); } public static void main(String[] args) { String s=in.nextString(); int m=in.nextInt(); char[] arr=s.toCharArray(); int n=arr.length; long[][] dp=new long[1<<n][m]; dp[0][0]=1; for(int i=0;i<(1<<n);i++) { for(int j=0;j<m;j++) { if(dp[i][j]==0) { //in mask i there is no element with mod value j=(i%m) continue; } for(int k=0;k<n;k++) { if((i & (1<<k))==0) { //there should not be zero initially if(i==0 && arr[k]=='0') continue; //kth bit is not there in mask i. So we can include it dp[i|(1<<k)][(j*10+arr[k]-'0')%m]+=dp[i][j]; } } } } long answer=dp[(1<<n)-1][0]; int[] frequency=new int[10]; for(int i=0;i<n;i++) { frequency[arr[i]-'0']++; } for(int i=0;i<10;i++) { answer/=(factorial(frequency[i])); } out.println(answer); out.close(); } public static long pow(long x, long n, long mod) { long res = 1; for (long p = x; n > 0; n >>= 1, p = (p * p) % mod) { if ((n & 1) != 0) { res = (res * p % mod); } } return res; } public static long gcd(long n1, long n2) { long r; while (n2 != 0) { r = n1 % n2; n1 = n2; n2 = r; } return n1; } public static long lcm(long n1, long n2) { long answer = (n1 * n2) / (gcd(n1, n2)); return answer; } static class FasterScanner { private byte[] buf = new byte[1024]; private int curChar; private int snumChars; public int read() { if (snumChars == -1) throw new InputMismatchException(); if (curChar >= snumChars) { curChar = 0; try { snumChars = System.in.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (snumChars <= 0) return -1; } return buf[curChar++]; } public String nextLine() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isEndOfLine(c)); return res.toString(); } public String nextString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public long nextLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public int nextInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public int[] nextIntArray(int n) { int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = nextInt(); } return arr; } public long[] nextLongArray(int n) { long[] arr = new long[n]; for (int i = 0; i < n; i++) { arr[i] = nextLong(); } return arr; } private boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } } }
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][100], d, f[20]; int main() { int m, l, n, i, j, k, c[10]; char a[20]; memset(c, 0, sizeof(c)); for (f[0] = 1, i = 1; i < 20; i++) f[i] = f[i - 1] * i; scanf("%s%d", a, &m); l = strlen(a), n = 1 << l; for (i = 0; i < l; i++) c[a[i] -= '0']++; for (d = 1, i = 0; i < 10; i++) d *= f[c[i]]; dp[0][0] = 1; for (i = 0; i < n; i++) for (j = 0; j < l; j++) if (!(i & 1 << j)) for (k = 0; k < m; k++) if (!(i == 0 && a[j] == 0)) dp[i | 1 << j][(k * 10 + a[j]) % m] += dp[i][k]; printf("%I64d\n", dp[n - 1][0] / d); 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; template <typename T> void read(T &a) { register int b = 1, c = getchar(); a = 0; for (; !isdigit(c); c = getchar()) if (c == 45) b *= -1; for (; isdigit(c); c = getchar()) a = (a << 3) + (a << 1) + c - 48; a *= b; } const int maxn = 105; const int maxm = 1 << 18; long long dp[maxm][maxn]; string str; int len; int m; int main() { cin >> str; len = int((str).size()); read(m); dp[0][0] = 1; for (int msk = 0; msk < int(1 << len); msk++) { for (int rem = 0; rem < int(m); rem++) { for (int i = 0; i < int(10); i++) { for (int j = 0; j < int(len); j++) if (str[j] == i + 48 && !(msk & (1 << j))) { int nwrem = (rem * 10 + i) % m; int nwmsk = msk | (1 << j); if (!msk && i == 0) continue; dp[nwmsk][nwrem] += dp[msk][rem]; break; } } } } printf("%I64d\n", dp[(1 << len) - 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; inline char readc() { char ch = getchar(); while (ch < '0' || ch > '9') ch = getchar(); return ch; } char s[20]; int n; int a[20]; int m; long long dp[1 << 18][110]; bool vis[10]; int main() { scanf("%s", s); n = strlen(s); for (register int i = 1; i <= n; i++) a[i] = s[i - 1] - '0'; scanf("%d", &m); dp[0][0] = 1; for (register int mask = 1; mask < 1 << n; mask++) { memset(vis, 0, sizeof(vis)); if (!(mask - (mask & -mask))) { int temp = mask; int c = 0; while (temp) { temp >>= 1; c++; } if (!a[c]) continue; } for (register int i = 1; i <= n; i++) { if (!(mask & (1 << (i - 1))) || vis[a[i]]) continue; vis[a[i]] = 1; for (register int j = 0; j < m; j++) { dp[mask][(j * 10 + a[i]) % m] += dp[mask ^ (1 << (i - 1))][j]; } } } printf("%lld\n", dp[(1 << n) - 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 a[20], m, dp[100][1 << 18], b[11]; string n; long long solve(long long sum, long long mask) { if (mask == (1 << n.size()) - 1) return ((sum % m) == 0); long long &ret = dp[sum][mask]; if (ret != -1) return ret; ret = 0; int u[10] = {0}; for (long long i = 0; i < n.size(); i++) { if (!(mask & (1 << i)) && !u[a[i]] && (a[i] > 0 || mask != 0)) { ret += solve((sum * 10 + a[i]) % m, (mask | (1 << i))); u[a[i]] = 1; } } return ret; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); memset(dp, -1, sizeof dp); cin >> n >> m; for (long long i = 0; i < n.size(); i++) { a[i] = n[i] - '0'; b[a[i]]++; } cout << solve(0, 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 dp[1 << 18][105]; bool vis[10]; vector<int> a; int main() { ios::sync_with_stdio(false); cin.tie(0); long long n, m; cin >> n >> m; while (n > 0) { int t = n % 10; a.push_back(t); n /= 10; } int l = a.size(); dp[0][0] = 1; for (int i = 0; i < (1 << l); i++) { for (int j = 0; j < m; j++) { if (dp[i][j] == 0) continue; memset(vis, 0, sizeof(vis)); for (int k = 0; k < l; k++) { if ((1 << k) & (i)) continue; if (i == 0 && a[k] == 0) continue; if (vis[a[k]]) continue; vis[a[k]] = 1; dp[i | (1 << k)][(j * 10 + a[k]) % m] += dp[i][j]; } } } 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
#include <bits/stdc++.h> using namespace std; template <class c> struct rge { c b, e; }; template <class c> rge<c> range(c i, c j) { return rge<c>{i, j}; } template <class c> auto dud(c* x) -> decltype(cerr << *x, 0); template <class c> char dud(...); struct debug { template <class c> debug& operator<<(const c&) { return *this; } }; using ll = long long; using pii = pair<int, int>; using pll = pair<ll, ll>; using vi = vector<int>; using vll = vector<ll>; const int N = (int)1e6 + 7; string s; int m; int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> s >> m; int n = (int)s.size(); vector<vll> dp(1 << n, vll(m)); dp[0][0] = 1; for (int mask = 0; mask < (1 << n); mask++) { for (int t = 0; t < m; t++) if (dp[mask][t]) { for (int i = 0; i < n; i++) { int x = s[i] - '0'; if (!x && !mask) continue; if (!(mask & (1 << i))) { ll u = 10 * t + x; u %= m; dp[mask | (1 << i)][u] += dp[mask][t]; } } } } vll f(20); f[0] = 1; for (int i = 1; i < 20; i++) f[i] = i * f[i - 1]; vi fr(10); for (char c : s) { fr[c - '0']++; } ll ans = dp[(1 << n) - 1][0]; for (int i = 0; i < 10; i++) ans /= f[fr[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; int set_bit(int mask, int c) { return mask |= (1UL << c); } bool get_bit(int mask, int c) { return (mask >> c) & 1U; } int fx[] = {1, -1, 0, 0, 1, 1, -1, -1, 0}; int fy[] = {0, 0, 1, -1, 1, -1, 1, -1, 0}; string fn[] = {"D", "U", "R", "L", "DR", "DL", "UR", "UL", "*"}; int kx[] = {2, 2, -2, -2, 1, -1, 1, -1}; int ky[] = {1, -1, 1, -1, 2, 2, -2, -2}; struct TYPE { int h = 0, m = 0, s = 0; bool operator<(TYPE t) const { return make_pair(h, make_pair(m, s)) < make_pair(t.h, make_pair(t.m, t.s)); } }; double linear_distance(pair<double, double> a, pair<double, double> b) { return sqrt(pow(a.first - b.first, 2.0) + pow(a.second - b.second, 2.0)); } const long long MAX = 1e6 + 5, MOD = 1e7 + 7, INF = 1e18; int KASE; int n, m; vector<int> v; long long dp[(1 << 18) + 5][100 + 5]; long long solve(int mask, int r) { if (__builtin_popcount(mask) == n) { return r == 0; } if (dp[mask][r] != -1) return dp[mask][r]; long long ret = 0; bool taken[10]; for (int i = 0; i < 10; i++) taken[i] = false; for (int i = 0; i < n; i++) { if (get_bit(mask, i) || (mask == 0 && v[i] == 0) || taken[v[i]]) continue; ret += solve(set_bit(mask, i), ((r * 10) + v[i]) % m); taken[v[i]] = true; } return dp[mask][r] = ret; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t = 1; for (int KASE = 1; KASE <= t; KASE++) { long long number; cin >> number >> m; while (number) { v.emplace_back(number % 10); number /= 10; } n = v.size(); memset(dp, -1, sizeof dp); cout << solve(0, 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; const int inf = 0x3f3f3f3f; const int maxn = 100000 + 100; long long dp[1 << 18][105]; int main() { long long n; int m; cin >> n >> m; vector<int> digit; int bmask = 1; long long dmask = 1; int tmp = 0; while (n) { digit.push_back(n % 10); n /= 10; } const int pmax = 1 << digit.size(); int nlen = digit.size(); dp[0][0] = 1; for (int k = 0; k < nlen; k++) for (int i = 0; i < pmax; i++) { int popcnt = 0; bmask = 1; while (bmask <= i) { if (bmask & i) popcnt++; bmask <<= 1; } if (popcnt != k) continue; bmask = 1; dmask = 1; for (int j = 0; j < nlen; j++, (bmask <<= 1), dmask = (dmask * 10LL) % m) { if (bmask & i) continue; if (j == nlen - 1 && digit[k] == 0) continue; int exm = digit[k] * dmask % m; for (int a = 0; a < m; a++) { dp[i | bmask][(a + exm) % m] += dp[i][a]; } } } long long ans = dp[pmax - 1][0]; sort(digit.begin(), digit.end()); int fac = 1; for (int i = 1; i < nlen; i++) { if (digit[i] == digit[i - 1]) { fac++; ans /= fac; } else { fac = 1; } } 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; vector<long long> v; long long dp[1 << 18][100], n, m; long long bt(long long mask, long long rem) { if (mask == (1LL << n) - 1LL) { if (rem == 0) return 1; return 0; } if (dp[mask][rem] != -1) return dp[mask][rem]; long long ret = 0, i, k; for (int h = 0; h <= 9; h++) { if (mask == 0 && h == 0) continue; i = -1; for (k = 0; k < n; k++) { if (((1LL << k) & mask) == 0 && v[k] == h) { i = k; break; } } if (i != -1) { ret += bt(((1LL << i) | mask), (rem * 10 + v[i]) % m); } } return dp[mask][rem] = ret; } int main() { long long x; cin >> x >> m; while (x) { v.push_back(x % 10); x /= 10; } n = v.size(); memset(dp, -1, sizeof dp); cout << bt(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; 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(); 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; long long dp[(1ll << (18)) + 1][100]; long long mod; long long a[100], cnt[100000]; string s; signed main() { cin >> s >> mod; long long n = s.size(); for (long long i = 0; i < n; i++) { a[i + 1] = s[i] - '0'; cnt[a[i + 1]]++; } dp[0][0] = 1; for (long long i = 0; i < (1ll << (n)); i++) for (long long j = 0; j < mod; j++) { if (dp[i][j]) for (long long k = 1; k <= n; k++) { if (a[k] == 0 && i == 0) continue; if (!(i & (1ll << (k - 1)))) dp[i | (1 << (k - 1))][(j * 10 + a[k]) % mod] += dp[i][j]; } } long long ans = dp[(1ll << (n)) - 1][0]; for (long long i = 0; i <= 9; i++) { if (cnt[i] > 1) { for (long long j = 2; j <= cnt[i]; 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
import java.io.*; import java.util.*; public class C implements Runnable{ public static void main (String[] args) {new Thread(null, new C(), "_cf", 1 << 28).start();} int[] nums; int n, m; long[][] dp; public void run() { FastScanner fs = new FastScanner(); PrintWriter out = new PrintWriter(System.out); String str = fs.next(); nums = new int[str.length()]; for(int i = 0; i < str.length(); i++) { nums[i] = str.charAt(i) - '0'; } n = str.length(); m = fs.nextInt(); dp = new long[m][1 << n]; for(int i = 0; i < m; i++) Arrays.fill(dp[i], -1); out.println(go(0, 0)); out.close(); } long go (int rem, int mask) { if(mask == (1 << n)-1) { // System.out.println("Finished with rem = " + rem); if(rem == 0) return 1L; return 0L; } if(dp[rem][mask] != -1) return dp[rem][mask]; // System.out.println("At " + rem + " " + Integer.toBinaryString(mask)); long res = 0; boolean[] done = new boolean[10]; for(int i = 0; i < n; i++) { int on = mask & (1 << i); if(on != 0) continue; if(Integer.bitCount(mask) == 0 && nums[i] == 0) continue; //leading zeros if(done[nums[i]]) continue; done[nums[i]] = true; int nrem = (rem*10 + nums[i]) % m; res += go(nrem, mask | (1 << i)); } return dp[rem][mask] = res; } void sort (int[] a) { int n = a.length; for(int i = 0; i < 1000; i++) { Random r = new Random(); int x = r.nextInt(n), y = r.nextInt(n); int temp = a[x]; a[x] = a[y]; a[y] = temp; } Arrays.sort(a); } class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner() { try { br = new BufferedReader(new InputStreamReader(System.in)); // br = new BufferedReader(new FileReader("cases.in")); st = new StringTokenizer(""); } catch (Exception e){e.printStackTrace();} } public String next() { if (st.hasMoreTokens()) return st.nextToken(); try {st = new StringTokenizer(br.readLine());} catch (Exception e) {e.printStackTrace();} return st.nextToken(); } public int nextInt() {return Integer.parseInt(next());} public long nextLong() {return Long.parseLong(next());} public double nextDouble() {return Double.parseDouble(next());} public String nextLine() { String line = ""; try {line = br.readLine();} catch (Exception e) {e.printStackTrace();} return line; } public Integer[] nextIntegerArray(int n) { Integer[] a = new Integer[n]; for(int i = 0; i < n; i++) a[i] = nextInt(); return a; } public int[] nextIntArray(int n) { int[] a = new int[n]; for(int i = 0; i < n; i++) a[i] = nextInt(); return a; } public char[] nextCharArray() {return nextLine().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.util.*; import java.io.*; public class Maximus1{ public static void main(String [] args)throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(br.readLine()); long n = Long.parseLong(st.nextToken()); int m = Integer.parseInt(st.nextToken()); ArrayList<Integer> list = new ArrayList<>(); while (n > 0) { int a = (int) (n % 10); list.add(a); n /= 10; } int len = list.size(); long dp[][] = new long[(1 << len)][m]; dp[0][0] = 1; for (int mask = 0; mask < (1 << len); mask++) { int cnt = Integer.bitCount(mask); for (int rem = 0; rem < m; rem++) { for (int j = 0; j < len; j++) { if ((mask & (1 << j)) > 0 || mask == 0 && list.get(j) == 0) continue; dp[mask | (1 << j)][(rem * 10 + list.get(j)) % m] += dp[mask][rem]; } } } long ans = dp[(1 << len) - 1][0]; int times[] = new int[10]; for (int temp : list) times[temp]++; for (int i = 0; i < 10; i++) { for (int j = 1; j <= times[i]; j++) ans /= j; } System.out.print(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
import java.util.*; public class D { private static long solve(long n, int m) { long mask = 0; long[] digMasks = new long[10]; long[] digFullMasks = new long[10]; for (long i = 0, t = 128; i < 10; ++i) { digMasks[(int)i] = t; digFullMasks[(int)i] = t*31; t <<= 5; } long t = n; int nd = 0; while (t > 0) { int d = (int)(t%10); mask += digMasks[d]; t /= 10; ++nd; } HashMap<Long, Long> h = new HashMap<Long, Long>(), e = new HashMap<Long, Long>(), f; h.put(mask, 1L); long l = 1; for (int i = 0; i < nd; ++i) { e.clear(); for(Long key : h.keySet()) { Long value = h.get(key); // System.out.println("" + i + " " + key + " " + value); for (int d = 0; d < 10; ++d) { if ( (key & digFullMasks[d]) == 0 ) continue; long newkey = key - digMasks[d]; long mod = newkey%128; mod = (mod + l*d)%m; newkey = newkey/128*128 + mod; if (newkey >= 128 && newkey < 128*32) continue; if (!e.containsKey(newkey)) e.put(newkey, value); else e.put(newkey, value + e.get(newkey)); } } l = 10*l%m; f = e; e = h; h = f; } return h.containsKey(0L) ? h.get(0L) : 0L; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); long n = sc.nextLong(); int m = sc.nextInt(); System.out.println(solve(n, m)); } }
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 mod = 1e9 + 7; long long dp[1 << 18][100], cnt[10], n, m, l = 0, d[20]; vector<int> masks[20]; long long calc(int mask, int sum) { if (mask == (1 << l) - 1) return sum == 0; if (dp[mask][sum] != -1) return dp[mask][sum]; long long res = 0; bool u[10] = {}; for (int i = 0; i < l; ++i) { if (!u[d[i]] && (mask & (1 << i)) == 0 && (mask > 0 || mask == 0 && d[i] > 0)) { res += calc(mask | (1 << i), (sum * 10 + d[i]) % m); u[d[i]] = 1; } } return dp[mask][sum] = res; } int main() { cin >> n >> m; for (n; n; n /= 10) { d[l++] = n % 10; cnt[n % 10]++; } memset(dp, -1, sizeof(dp)); long long ans = calc(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 cnt[10]; int m; int mod(int x) { while (x >= m) x -= m; return x; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); string n; cin >> n; for (auto c : n) { cnt[c - '0']++; } cin >> m; long long dp[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]; memset(dp, 0, sizeof(dp)); if (cnt[1]) { dp[0][1][0][0][0][0][0][0][0][0][mod(1)] = 1; } if (cnt[2]) { dp[0][0][1][0][0][0][0][0][0][0][mod(2)] = 1; } if (cnt[3]) { dp[0][0][0][1][0][0][0][0][0][0][mod(3)] = 1; } if (cnt[4]) { dp[0][0][0][0][1][0][0][0][0][0][mod(4)] = 1; } if (cnt[5]) { dp[0][0][0][0][0][1][0][0][0][0][mod(5)] = 1; } if (cnt[6]) { dp[0][0][0][0][0][0][1][0][0][0][mod(6)] = 1; } if (cnt[7]) { dp[0][0][0][0][0][0][0][1][0][0][mod(7)] = 1; } if (cnt[8]) { dp[0][0][0][0][0][0][0][0][1][0][mod(8)] = 1; } if (cnt[9]) { dp[0][0][0][0][0][0][0][0][0][1][mod(9)] = 1; } for (int a0 = 0; a0 <= cnt[0]; a0++) { for (int a1 = 0; a1 <= cnt[1]; a1++) { for (int a2 = 0; a2 <= cnt[2]; a2++) { for (int a3 = 0; a3 <= cnt[3]; a3++) { for (int a4 = 0; a4 <= cnt[4]; a4++) { for (int a5 = 0; a5 <= cnt[5]; a5++) { for (int a6 = 0; a6 <= cnt[6]; a6++) { for (int a7 = 0; a7 <= cnt[7]; a7++) { for (int a8 = 0; a8 <= cnt[8]; a8++) { for (int a9 = 0; a9 <= cnt[9]; a9++) { for (int r = 0; r < m; r++) { if (a0 + 1 <= cnt[0]) dp[a0 + 1][a1][a2][a3][a4][a5][a6][a7][a8][a9] [mod((r * 10 + 0))] += dp[a0][a1][a2][a3][a4][a5][a6][a7][a8][a9][r]; if (a1 + 1 <= cnt[1]) dp[a0][a1 + 1][a2][a3][a4][a5][a6][a7][a8][a9] [mod((r * 10 + 1))] += dp[a0][a1][a2][a3][a4][a5][a6][a7][a8][a9][r]; if (a2 + 1 <= cnt[2]) dp[a0][a1][a2 + 1][a3][a4][a5][a6][a7][a8][a9] [mod((r * 10 + 2))] += dp[a0][a1][a2][a3][a4][a5][a6][a7][a8][a9][r]; if (a3 + 1 <= cnt[3]) dp[a0][a1][a2][a3 + 1][a4][a5][a6][a7][a8][a9] [mod((r * 10 + 3))] += dp[a0][a1][a2][a3][a4][a5][a6][a7][a8][a9][r]; if (a4 + 1 <= cnt[4]) dp[a0][a1][a2][a3][a4 + 1][a5][a6][a7][a8][a9] [mod((r * 10 + 4))] += dp[a0][a1][a2][a3][a4][a5][a6][a7][a8][a9][r]; if (a5 + 1 <= cnt[5]) dp[a0][a1][a2][a3][a4][a5 + 1][a6][a7][a8][a9] [mod((r * 10 + 5))] += dp[a0][a1][a2][a3][a4][a5][a6][a7][a8][a9][r]; if (a6 + 1 <= cnt[6]) dp[a0][a1][a2][a3][a4][a5][a6 + 1][a7][a8][a9] [mod((r * 10 + 6))] += dp[a0][a1][a2][a3][a4][a5][a6][a7][a8][a9][r]; if (a7 + 1 <= cnt[7]) dp[a0][a1][a2][a3][a4][a5][a6][a7 + 1][a8][a9] [mod((r * 10 + 7))] += dp[a0][a1][a2][a3][a4][a5][a6][a7][a8][a9][r]; if (a8 + 1 <= cnt[8]) dp[a0][a1][a2][a3][a4][a5][a6][a7][a8 + 1][a9] [mod((r * 10 + 8))] += dp[a0][a1][a2][a3][a4][a5][a6][a7][a8][a9][r]; if (a9 + 1 <= cnt[9]) dp[a0][a1][a2][a3][a4][a5][a6][a7][a8][a9 + 1] [mod((r * 10 + 9))] += dp[a0][a1][a2][a3][a4][a5][a6][a7][a8][a9][r]; } } } } } } } } } } } long long ans = dp[cnt[0]][cnt[1]][cnt[2]][cnt[3]][cnt[4]][cnt[5]][cnt[6]] [cnt[7]][cnt[8]][cnt[9]][0]; 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.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.StringTokenizer; 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); Task solver = new Task(); solver.solve(1, in, out); out.close(); } } class Task { private int[] mst; long[][][] dp; public static class Digit implements Comparable<Digit> { int num; int cnt; Digit(int num, int cnt) { this.num = num; this.cnt = cnt; } @Override public int compareTo(Digit other) { // TODO Auto-generated method stub return cnt - other.cnt; } } public void solve(int testNumber, InputReader in, PrintWriter out) { long n = in.nextLong(); int m = in.nextInt(); String s = String.valueOf(n); Digit[] digits = new Digit[10]; for (int i = 0; i < digits.length; i++) digits[i] = new Digit(i, 0); for (int i = 0; i < s.length(); i++) { digits[s.charAt(i) - '0'].cnt++; } Arrays.sort(digits); int[] idx = new int[10]; for (int i = 0; i < digits.length; i++) { idx[digits[i].num] = i; } int M = digits[9].cnt + 1; int[] base = new int[10]; for (int i = 0; i < base.length; i++) base[i] = digits[i].cnt + 1; mst = new int[10]; for (int i = 0; i < 10; i++) mst[i] = digits[i].cnt; int len = s.length(); int maxStat = get(base, mst); dp = new long[len + 1][maxStat + 5][m]; for (int i = 1; i < 10; i++) { int[] st = new int[10]; int x = idx[i]; if (mst[x] > 0) { st[x]++; int newStat = get(base, st); dp[1][newStat][i % m]++; } } for (int i = 1; i < len; i++) for (int j = 0; j <= maxStat; j++) for (int k = 0; k < m; k++) if (dp[i][j][k] != 0) { int[] st = convert(base, j); for (int d = 0; d < 10; d++) { int x = idx[d]; if (mst[x] > st[x]) { st[x]++; int newStat = get(base, st); if (newStat > maxStat) throw new RuntimeException(); dp[i + 1][newStat][(k * 10 + d) % m] += dp[i][j][k]; st[x]--; } } } out.print(dp[len][maxStat][0]); } int[] convert(int[] base, int num) { int[] st = new int[10]; for (int i = 9; i >= 0; i--) { st[i] = num % base[i]; num /= base[i]; } return st; } private int get(int[] base, int[] st) { int num = 0, i; for (i = 0; i < st.length; i++) if (st[i] != 0) break; for (; i < st.length; i++) { num = num * base[i] + st[i]; } return num; } } class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream)); 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(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(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.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.Arrays; import java.util.StringTokenizer; public class A { static char [] c; static int n, m; static long [][] memo; static long dp(int z, int mod, int msk){ if(msk == 0) return mod == 0?1:0; if(memo[mod][msk] != -1) return memo[mod][msk]; long ans = 0; for(int i = 0; i < n; ++i){ if((msk & (1<<i)) != 0 && (c[i] != '0' || z == 1)){ boolean fnd = false; for (int j = 0; j < i; ++j) { if((msk & (1<<j)) != 0 && c[i] == c[j]) fnd = true; } if(fnd) continue; ans += dp(z == 1 || c[i] != '0'?1:0, (mod*10 + c[i] - '0')%m, msk ^ (1<<i)); } } return memo[mod][msk] = ans; } public static void main(String[] args) throws NumberFormatException, IOException { Scanner sc = new Scanner(); PrintWriter out = new PrintWriter(System.out); c = sc.next().toCharArray(); n = c.length; m = sc.nextInt(); memo = new long[m+1][(1<<n)+1]; for(long []i:memo) Arrays.fill(i, -1); out.println(dp(0, 0, (1<<n)-1)); out.close(); } static class Scanner { BufferedReader br; StringTokenizer st; public Scanner() { br = new BufferedReader(new InputStreamReader(System.in)); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) { st = new StringTokenizer(br.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
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; const long long inf = 1e18; const long long N = 100001; long long dp[1 << 18][100]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long n, k = 0, m; cin >> n >> m; long long rem[20], cnt[20]; memset(rem, 0, sizeof rem); memset(cnt, 0, sizeof cnt); while (n) { rem[k] = n % 10; cnt[n % 10]++; n = n / 10; k++; } memset(dp, 0, sizeof dp); dp[0][0] = 1; for (long long i = 1; i < (1 << k); i++) { for (long long j = 0; j < k; j++) { if (i & (1 << j)) { if (rem[j] || i != (1 << j)) { long long g = i - (1 << j); for (long long p = 0; p < m; p++) { dp[i][(p * 10 + rem[j]) % m] += dp[g][p]; } } } } } long long ans = dp[-1 + (1 << k)][0]; for (long long i = 0; i <= 9; i++) { for (long long j = 2; 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
import java.io.*; import java.util.*; import java.math.*; public class Main { long[][] dp; char[] str; int n; int m; public void solve() throws IOException{ str = in.next().toCharArray(); n = str.length; m = in.nextInt(); dp = new long[(int) 1L << n][(int) m]; for(int i = 0; i < dp.length; i++){ for(int j = 0; j < dp[0].length; j++){ dp[i][j] = -1; } } long res = dfs(0, 0); out.println(res); return; } public long dfs(int mask, int mod){ if(dp[mask][mod] != -1){ return dp[mask][mod]; }else if(mask == dp.length - 1 && mod == 0){ return 1; }else if(mask == dp.length - 1 && mod != 0){ return 0; } dp[mask][mod] = 0L; Set<Integer> used = new HashSet<>(); for(int i = 0; i < n; i++){ if((mask | (1 << i)) != mask && !used.contains(str[i] - '0') && (mask > 0 || str[i] != '0')){ dp[mask][mod] += dfs(mask | (1 << i), (mod * 10 + (str[i] - '0')) % m); used.add(str[i] - '0'); } } return dp[mask][mod]; } public BigInteger gcdBigInt(BigInteger a, BigInteger b){ if(a.compareTo(BigInteger.valueOf(0L)) == 0){ return b; }else{ return gcdBigInt(b.mod(a), a); } } FastScanner in; PrintWriter out; static class FastScanner { BufferedReader br; StringTokenizer st; FastScanner(InputStream in) { br = new BufferedReader(new InputStreamReader(in)); st = null; } String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } String nextLine() throws IOException { if (st == null || !st.hasMoreTokens()) return br.readLine(); StringBuilder result = new StringBuilder(st.nextToken()); while (st.hasMoreTokens()) { result.append(" "); result.append(st.nextToken()); } return result.toString(); } int nextInt() throws IOException { return Integer.parseInt(next()); } long nextLong() throws IOException { return Long.parseLong(next()); } double nextDouble() throws IOException { return Double.parseDouble(next()); } } void run() throws IOException { in = new FastScanner(System.in); out = new PrintWriter(System.out, false); solve(); out.close(); } public static void main(String[] args) throws IOException{ new Main().run(); } public void printArr(int[] arr){ for(int i = 0; i < arr.length; i++){ out.print(arr[i] + " "); } out.println(); } public long gcd(long a, long b){ if(a == 0) return b; return gcd(b % a, a); } public boolean isPrime(long num){ if(num == 0 || num == 1){ return false; } for(int i = 2; i * i <= num; i++){ if(num % i == 0){ return false; } } return true; } public class Pair<A, B>{ public A x; public B y; Pair(A x, B y){ this.x = x; this.y = y; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Pair<?, ?> pair = (Pair<?, ?>) o; if (!x.equals(pair.x)) return false; return y.equals(pair.y); } @Override public int hashCode() { int result = x.hashCode(); result = 31 * result + y.hashCode(); return result; } } class Tuple{ int x; int y; int z; Tuple(int ix, int iy, int iz){ x = ix; y = iy; z = iz; } } }
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 namespace std; long long f[(1 << 18)][100]; int a[20]; bool vis[10]; int main() { long long n; int m; while (cin >> n >> m) { memset(f, 0, sizeof f); int tot(0); while (n) { a[++tot] = n % 10; n /= 10; } memset(vis, 0, sizeof vis); for (int i = 1; i <= tot; ++i) if (a[i] && !vis[a[i]]) { vis[a[i]] = 1; f[1 << (i - 1)][a[i] % m] = 1; } for (int k = 1; k <= (1 << tot) - 2; ++k) { memset(vis, 0, sizeof vis); for (int i = 1; i <= tot; ++i) if ((k & (1 << (i - 1))) == 0) if (!vis[a[i]]) { vis[a[i]] = 1; for (int j = 0; j <= m - 1; ++j) f[(1 << (i - 1)) | k][(j * 10 + a[i]) % m] += f[k][j]; } } cout << f[(1 << tot) - 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.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.InputMismatchException; public class CF { static int mod, N; static int[] array; static long[][] memo; static int[] powpow; static int[][][] magic; public static void main(String[] args) { FasterScanner sc = new FasterScanner(); PrintWriter out = new PrintWriter(System.out); char[] str = sc.next().toCharArray(); int pow = 1; mod = sc.nextInt(); magic = new int[20][10][mod]; for(int a=0;a<20;a++){ for(int b=0;b<10;b++) for(int c=0;c<mod;c++) magic[a][b][c]=(pow*b+c)%mod; pow*=10; pow%=mod; } N = str.length; array = new int[N]; int[] freq = new int[10]; for(int a=0;a<N;a++) freq[array[a]=str[a]-'0']++; Arrays.sort(array); memo = new long[mod][1<<N]; for(int a=0;a<mod;a++)Arrays.fill(memo[a],-1); long ways = DP(0,0); long[] factorial = new long[20]; factorial[0]=1; long bad = 1; for(int a=1;a<20;a++) factorial[a]=factorial[a-1]*a; // for(int a=0;a<10;a++) // bad*=factorial[freq[a]]; out.println(ways/bad); out.close(); } private static long DP(int mask, int remain) { if(memo[remain][mask]!=-1)return memo[remain][mask]; if(mask==(1<<N)-1) return memo[remain][mask]=(remain==0?1L:0L); long ans = 0; for(int a=0;a<N;a++){ if(((mask>>a)&1)==0){ if(Integer.bitCount(mask)==0){ if(array[a]==0)continue; } if(a>0&&(mask>>(a-1)&1)==0&&array[a]==array[a-1])continue; ans+=DP(mask+(1<<a),(magic[N-Integer.bitCount(mask)-1][array[a]][remain])); // ans+=DP(mask+(1<<a),(powpow[N-Integer.bitCount(mask)-1]*array[a]+remain)%mod); } } return memo[remain][mask]=ans; } static boolean nextperm(int[] vals) { int N = vals.length; int K = -1; for(int a=N-2;a>=0;a--){ if(vals[a]<vals[a+1]){ K=a; break; } } if(K==-1)return false; for(int a=N-1;a>K;a--){ if(vals[K]<vals[a]){ swap(K,a,vals); break; } } int L = K+1; int R = N-1; while(L<R){ swap(L,R,vals); L++; R--; } return true; } public static void swap(int i, int j, int[] v) { int t = v[i]; v[i] = v[j]; v[j] = t; } static class FasterScanner { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; public FasterScanner() { stream = System.in; // 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()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } 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 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
#include <bits/stdc++.h> using namespace std; const double eps = 1e-8; const long long inf = 1e9; const long long mod = 1e9 + 7; const int maxn = 1e5 + 10; char str[20]; int a[20], cal[20]; long long f[1 << 18][100]; int main() { long long div = 1; int n, tot_n, i, j, k, m; scanf("%s%d", str, &m); n = strlen(str); for (i = 0; i < n; i++) { a[i] = str[i] - 48; cal[a[i]]++; } for (i = 0; i < 10; i++) while (cal[i]) div *= cal[i]--; f[0][0] = 1; tot_n = 1 << n; for (i = 0; i < tot_n; i++) for (j = 0; j < n; j++) if (!(i & (1 << j)) && !(i == 0 && a[j] == 0)) for (k = 0; k < m; k++) { f[i | 1 << j][(k * 10 + a[j]) % m] += f[i][k]; } printf("%lld", f[tot_n - 1][0] / div); 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.Arrays; import java.util.Scanner; /** * Created by ZCMX0557 on 2/4/2016. */ public class Main { static String s; static int m; static long [][]dp; static long solve(int bm, int md) { if(bm == 0) { return md == 0 ? 1 : 0; } if(dp[md][bm] != -1) { return dp[md][bm]; } long res = 0; int vis = 0; for(int i = 0; i < s.length(); ++i) { if(((bm >> i) & 1) != 1) { continue; } int d = s.charAt(i) - '0'; if((1 << s.length()) - 1 == bm && d == 0) { continue; } if(((vis >> d) & 1) == 1) { continue; } vis |= 1 << d; res += solve(bm ^ (1 << i), (md * 10 + d) % m); } return dp[md][bm] = res; } public static void main(String []args) { Scanner cin = new Scanner(System.in); s = cin.next(); m = cin.nextInt(); dp = new long[m][1 << s.length()]; for(int i = 0; i < m; ++i) { Arrays.fill(dp[i], -1); } System.out.println(solve((1 << s.length()) - 1, 0)); cin.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
import java.io.OutputStreamWriter; import java.io.BufferedWriter; import java.util.Comparator; import java.io.OutputStream; import java.io.PrintWriter; import java.io.Writer; import java.io.IOException; import java.util.Arrays; import java.util.InputMismatchException; import java.util.NoSuchElementException; import java.math.BigInteger; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * @author Árysson Cavalcanti */ 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(); } } class TaskD { long[][] dp; int[] value; int MOD; public void solve(int testNumber, InputReader in, OutputWriter out) { char[] str=in.readString().toCharArray(); MOD=in.readInt(); value=new int[11]; value[0]=1; int[] count=new int[10]; for (char i: str) count[i-'0']++; for (int i=0; i<10; i++) value[i+1]=value[i]*(count[i]+1); dp=new long[value[10]][MOD]; ArrayUtils.fill(dp, -1); dp(arrayToInt(count)); out.printLine(dp[arrayToInt(count)][0]); } int arrayToInt(int[] arr) { int ret=0; for (int i=0; i<10; i++) ret+=arr[i]*value[i]; return ret; } void dp(int n) { if (dp[n][0]!=-1) return; int[] arr=intToArray(n); int c=0; for (int i: arr) c+=i; Arrays.fill(dp[n], 0); if (c==1) { for (int i=1; i<10; i++) if (arr[i]>0) dp[n][i%MOD]=1; return; } for (int i=0; i<10; i++) if (arr[i]>0) { dp(n-value[i]); for (int j=0; j<MOD; j++) dp[n][(10*j+i)%MOD]+=dp[n-value[i]][j]; } } int[] intToArray(int n) { int[] ret=new int[10]; for (int i=1; i<=10; i++) ret[i-1]=n%value[i]/value[i-1]; return ret; } } class InputReader { 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 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 interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public void close() { writer.close(); } public void printLine(long i) { writer.println(i); } } class ArrayUtils { public static void fill(long[][] array, long value) { for (long[] row : array) Arrays.fill(row, value); } }
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, f[(1 << 18) + 5][101]; int w[20], b[20], m, cnt; int main() { cin >> n >> m; f[0][0] = 1; while (n) { w[++cnt] = n % 10; n /= 10; } for (int j = 1; j < (1 << cnt); ++j) { memset(b, 0, sizeof(b)); for (int i = 1; i <= cnt; ++i) { if ((1 << (i - 1)) == j && w[i] == 0) break; if (b[w[i]] || !(j & (1 << (i - 1)))) continue; b[w[i]] = 1; for (int k = 0; k < m; ++k) f[j][(k * 10 + w[i]) % m] += f[(j ^ (1 << (i - 1)))][k]; } } cout << f[(1 << cnt) - 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 int dp[1 << 18][100] = {0}, s[18]; int main() { long long int n; int m, top = 0, maxn, i, j, k; scanf("%lld%d", &n, &m); while (n) { s[top++] = n % 10; n /= 10; } maxn = 1 << top; dp[0][0] = 1; for (i = 0; i < maxn; i++) { int is[10] = {0}; for (j = 0; j < top; j++) if ((i & (1 << j)) == 0) { if (i == 0 && s[j] == 0 || is[s[j]]) continue; is[s[j]] = 1; for (k = 0; k < m; k++) { dp[i | (1 << j)][(k * 10 + s[j]) % m] += dp[i][k]; } } } printf("%lld\n", dp[maxn - 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
import java.io.File; import java.io.FileNotFoundException; import java.util.Arrays; import java.util.Scanner; import java.util.StringTokenizer; import java.util.TreeMap; import java.util.TreeSet; public class l083 { private static long[] pow10 = new long[19]; public static void main(String[] args) throws Exception { // StringTokenizer stok = new StringTokenizer(new Scanner(new File("F:/books/input.txt")).useDelimiter("\\A").next()); StringTokenizer stok = new StringTokenizer(new Scanner(System.in).useDelimiter("\\A").next()); StringBuilder sb = new StringBuilder(); Long n = Long.parseLong(stok.nextToken()); int m = Integer.parseInt(stok.nextToken()); pow10[0]=1; for(int i=1;i<=18;i++) pow10[i] = 10*pow10[i-1]; long[] a = new long[10]; int d = 0; while(n>0) { a[(int) (n%10)]++; n/=10; d++; } TreeMap<long[],long[]> mp = new TreeMap<long[],long[]>((x,y)->{ for(int i=0;i<10;i++) if(x[i]!=y[i]) return Long.compare(x[i], y[i]); return 0; }); ; System.out.println(solve(mp,a,m,false,d)[0]); } private static long[] solve(TreeMap<long[], long[]> mp, long[] a, int m, boolean useZero, int d) { if(mp.containsKey(a)) return mp.get(a); if(d==0) { long[] qret = new long[m]; qret[0]=1; mp.put(a, qret); return qret; } long[] ret = new long[m]; for(int i=0;i<10;i++) { if(i==0 && !useZero) continue; if(a[i]>0) { a[i]--; long[] f = solve(mp,Arrays.copyOf(a, a.length),m,true,d-1); a[i]++; int s = (int) ((pow10[d-1]*i)%m); for(int j=0;j<m;j++) ret[(j+s)%m] += f[j]; } } mp.put(a, ret); return 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
// practice with rainboy import java.io.*; import java.util.*; public class CF401D extends PrintWriter { CF401D() { super(System.out, true); } Scanner sc = new Scanner(System.in); public static void main(String[] $) { CF401D o = new CF401D(); o.main(); o.flush(); } void main() { long n = sc.nextLong(); int m = sc.nextInt(); int[] dd = new int[18]; int k = 0; while (n > 0) { dd[k++] = (int) (n % 10); n /= 10; } long[][] dp = new long[1 << k][m]; int used = 0; for (int h = 0; h < k; h++) if (dd[h] != 0 && (used & 1 << dd[h]) == 0) { used |= 1 << dd[h]; dp[1 << h][dd[h] % m] = 1; } for (int b = 0; b < 1 << k; b++) for (int r = 0; r < m; r++) { long x = dp[b][r]; if (x != 0) { used = 0; for (int h = 0; h < k; h++) if ((b & 1 << h) == 0 && (used & 1 << dd[h]) == 0) { used |= 1 << dd[h]; dp[b | 1 << h][(r * 10 + dd[h]) % m] += x; } } } println(dp[(1 << k) - 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; const long long N = 2e5 + 10; long long f[1 << 18][110]; signed main() { ios::sync_with_stdio(false); long long n, m; cin >> n >> m; vector<long long> a; while (n) a.push_back(n % 10), n /= 10; n = a.size(); f[0][0] = 1; for (long long i = 0; i < (1 << n); i++) { for (long long j = 0; j < m; j++) if (f[i][j]) { vector<long long> v(10); for (long long k = 0; k < n; k++) if (!v[a[k]] && !(i & (1 << k))) { if (i == 0 && a[k] == 0) continue; f[i | (1 << k)][(j * 10 + a[k]) % m] += f[i][j]; v[a[k]] = 1; } } } cout << f[(1 << n) - 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; const int maxn = 18, maxm = 100, maxl = 1 << 18; const long long powten[maxn + 5] = {1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000, 10000000000, 100000000000, 1000000000000, 10000000000000, 100000000000000, 1000000000000000, 10000000000000000, 100000000000000000}; vector<int> sta[maxn + 5]; int dig[maxn + 5]; long long f[maxl + 5][maxm + 5]; bool rep[maxn + 5]; int main() { long long n; int m; scanf("%lld%d", &n, &m); int cnt = 0; while (n) { dig[cnt++] = n % 10ll; n /= 10ll; } sort(dig, dig + cnt); for (int i = 1; i < cnt; i++) if (dig[i] == dig[i - 1]) rep[i] = true; for (int i = 1; i < 1 << cnt; i++) sta[__builtin_popcount(i)].push_back(i); f[0][0] = 1; for (int i = 1; i <= cnt; i++) { for (int j = 0; j < sta[i].size(); j++) { int now = sta[i][j]; for (int k = 0; k < cnt; k++) { if ((now & (1 << k)) == 0) continue; if (rep[k] && (now & (1 << (k - 1))) == 0) continue; if (dig[k] == 0 && i == cnt) continue; int lst = now ^ (1 << k); for (int l = 0; l < m; l++) f[now][(dig[k] * powten[i - 1] + l) % m] += f[lst][l]; } } } printf("%lld", f[(1 << cnt) - 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
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; 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 = (int) (IntegerUtils.power(10, a.length - index - 1) % m); 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; 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; } public static long power(long base, long exponent) { if (exponent == 0) return 1; long result = power(base, exponent >> 1); result = result * result; if ((exponent & 1) != 0) result = result * base; 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; 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); if (!dig[j] && !ni) continue; for (int l = 0; l < m; l++) { 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
#include <bits/stdc++.h> using namespace std; string num; long long mod; long long dp[262144][105]; signed main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> num >> mod; long long sz = num.size(); sort(num.begin(), num.end()); dp[0][0] = 1; for (long long mask = 0; mask < (1 << sz); ++mask) { for (long long i = 0; i < mod; i++) { if (dp[mask][i] == 0) continue; for (long long j = 0; j < sz; j++) { if ((mask >> j) & 1) continue; if (mask == 0 && num[j] == '0') continue; if (j > 0 & num[j] == num[j - 1] && ((mask >> (j - 1)) & 1) == 0) continue; dp[mask | (1 << j)][(i * 10 + num[j] - '0') % mod] += dp[mask][i]; } } } cout << dp[(1 << sz) - 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.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.io.Writer; import java.io.OutputStreamWriter; import java.util.InputMismatchException; import java.io.IOException; import java.util.Collections; import java.io.InputStream; import java.util.ArrayList; /** * 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 (used == DONE) { long[] res = new long[m]; res[0] = 1; return res; } if (mem[used] != null) return mem[used]; 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) { long n = in.readLong(); m = in.readInt(); a = toArray(n); Arrays.sort(a); 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); } private int[] toArray(long n) { ArrayList<Integer> al = new ArrayList<>(); while (n > 0) { int last = (int) (n % 10); al.add(last); n /= 10; } Collections.reverse(al); int[] res = new int[al.size()]; for (int i = 0; i < res.length; i++) res[i] = al.get(i); return res; } } 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 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 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 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 interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } static class IntegerUtils { public static long factorial(int n) { long result = 1; for (int i = 2; i <= n; i++) result *= i; return result; } } }
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) { 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; for (int i = 0; i < 10; i++) { if (counts[i] < digitcounts[i]) { counts[i]++; 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); } } System.out.println(res); 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 n, m; long long dp[1 << 18][100]; int num[10]; int d[19]; int len; int main() { cin >> n >> m; int len = 0; while (n) { num[n % 10]++; d[len++] = n % 10; n /= 10; } long long res = 1; for (int i = 0; i < 10; i++) { while (num[i]) { res *= num[i]; num[i]--; } } dp[0][0] = 1; for (int s = 0; s < (1 << len); s++) for (int i = 0; i < len; i++) if (s || d[i]) { if (s & (1 << i)) continue; for (int k = 0; k < m; k++) { dp[s | (1 << i)][(10 * k + d[i]) % m] += dp[s][k]; } } cout << dp[(1 << len) - 1][0] / 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.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.io.IOException; 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(); } static class TaskD { public void solve(int testNumber, InputReader in, PrintWriter out) { long n = in.nextLong(); int m = in.nextInt(); int[] number = new int[Long.toString(n).length()]; int[] freq = new int[10]; long[] fact = new long[18]; fact[0] = 1; for (int i = 1; i < fact.length; i++) fact[i] = fact[i - 1] * i; int numberLength = 0; for (char c : Long.toString(n).toCharArray()) { freq[c - '0']++; number[numberLength++] = c - '0'; } long[][] dp = new long[1 << numberLength][m]; dp[0][0] = 1; for (int mask = 0; mask < (1 << numberLength); mask++) { for (int rem = 0; rem < m; rem++) { for (int di = 0; di < numberLength; di++) { if (mask == 0 && number[di] == 0) continue; if ((mask & (1 << di)) != 0) continue; dp[mask | (1 << di)][(rem * 10 + number[di]) % m] += dp[mask][rem]; } } } long ans = dp[(1 << numberLength) - 1][0]; for (int i = 0; i < freq.length; i++) { ans /= fact[freq[i]]; } out.println(ans); } } static class InputReader { final InputStream is; final byte[] buffer = new byte[1024]; int curCharIdx; int nChars; public InputReader(InputStream is) { this.is = is; } public int read() { if (curCharIdx >= nChars) { try { curCharIdx = 0; nChars = is.read(buffer); if (nChars == -1) return -1; } catch (IOException e) { throw new RuntimeException(e); } } return buffer[curCharIdx++]; } public int nextInt() { int sign = 1; int c = skipDelims(); if (c == '-') { sign = -1; c = read(); if (isDelim(c)) throw new RuntimeException("Incorrect format"); } int val = 0; while (c != -1 && !isDelim(c)) { if (!isDigit(c)) throw new RuntimeException("Incorrect format"); val = 10 * val + (c - '0'); c = read(); } return val * sign; } public long nextLong() { int sign = 1; int c = skipDelims(); if (c == '-') { sign = -1; c = read(); if (isDelim(c)) throw new RuntimeException("Incorrect format"); } long val = 0; while (c != -1 && !isDelim(c)) { if (!isDigit(c)) throw new RuntimeException("Incorrect format"); val = 10L * val + (c - '0'); c = read(); } return val * sign; } private final int skipDelims() { int c = read(); while (isDelim(c)) { c = read(); } return c; } private static boolean isDelim(final int c) { return c == ' ' || c == '\n' || c == '\t' || c == '\r' || c == '\f'; } private static boolean isDigit(final int c) { return '0' <= c && c <= '9'; } } }
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.PrintWriter; import java.io.FilterInputStream; import java.io.BufferedInputStream; import java.io.InputStream; /** * @author khokharnikunj8 */ public class Main { public static void main(String[] args) { new Thread(null, new Runnable() { public void run() { new Main().solve(); } }, "1", 1 << 26).start(); } void solve() { InputStream inputStream = System.in; OutputStream outputStream = System.out; ScanReader in = new ScanReader(inputStream); PrintWriter out = new PrintWriter(outputStream); DRomanAndNumbers solver = new DRomanAndNumbers(); solver.solve(1, in, out); out.close(); } static class DRomanAndNumbers { public void solve(int testNumber, ScanReader in, PrintWriter out) { char[] s = in.scanString().toCharArray(); int m = in.scanInt(); long[][] dp = new long[1 << s.length][m]; for (int i = 0; i < s.length; i++) { if (s[i] != '0') dp[1 << i][(s[i] - '0') % m] = 1; } long[] fact = new long[20]; fact[0] = 1; for (int i = 1; i < 20; i++) fact[i] = fact[i - 1] * i; for (int i = 0; i < 1 << s.length; i++) { for (int j = 0; j < m; j++) { if (dp[i][j] == 0) continue; for (int k = 0; k < s.length; k++) { if ((i & (1 << k)) == 0) { dp[i ^ (1 << k)][(j * 10 + (s[k] - '0')) % m] += dp[i][j]; } } } } long ans = dp[(1 << s.length) - 1][0]; int[] count = new int[10]; for (char c : s) count[c - '0']++; for (int i : count) ans /= fact[i]; out.println(ans); } } static class ScanReader { private byte[] buf = new byte[4 * 1024]; private int index; private BufferedInputStream in; private int total; public ScanReader(InputStream inputStream) { in = new BufferedInputStream(inputStream); } private int scan() { if (index >= total) { index = 0; try { total = in.read(buf); } catch (Exception e) { e.printStackTrace(); } if (total <= 0) return -1; } return buf[index++]; } public int scanInt() { int integer = 0; int n = scan(); while (isWhiteSpace(n)) n = scan(); int neg = 1; if (n == '-') { neg = -1; n = scan(); } while (!isWhiteSpace(n)) { if (n >= '0' && n <= '9') { integer *= 10; integer += n - '0'; n = scan(); } } return neg * integer; } public String scanString() { int c = scan(); if (c == -1) return null; while (isWhiteSpace(c)) c = scan(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = scan(); } while (!isWhiteSpace(c)); return res.toString(); } private boolean isWhiteSpace(int n) { if (n == ' ' || n == '\n' || n == '\r' || n == '\t' || n == -1) return true; else return false; } } }
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; inline int read() { int num = 0; char c = getchar(); if (c == '-') return -read(); while (c >= '0' && c <= '9') { num = (num << 3) + (num << 1) + c - '0'; c = getchar(); } return num; } inline long long READ() { long long num = 0; char c = getchar(); if (c == '-') return -READ(); while (c >= '0' && c <= '9') { num = (num << 3) + (num << 1) + c - '0'; c = getchar(); } return num; } long long n; int m; int cnt[10]; long long p10[19], p10m[19]; int wei; unordered_map<long long, long long> mm; long long dfs(long long djg, long long num, long long md) { if (!djg) return md == 0; long long xz = num * 100 + md, bb = num, ans = 0; if (mm.count(xz)) return mm[xz]; for (int i = 0; i < 10; i++) { if (djg == wei && i == 0) { bb /= 10; continue; } if (bb % 10 < cnt[i]) { long long xxx = (md - i * p10[djg - 1]) % m; while (xxx < 0) xxx += m; ans += dfs(djg - 1, num + p10m[i], xxx); } bb /= 10; } return mm[xz] = ans; } int main() { n = READ(); m = read(); p10[0] = p10m[0] = 1; for (int i = 1; i < 19; i++) { p10[i] = p10[i - 1] * 10 % m; p10m[i] = p10m[i - 1] * 10; } while (n) { cnt[n % 10]++; wei++; n /= 10; } cout << dfs(wei, 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; const int MASK = (1 << 18) + 15, M = 128; long long dp[MASK][M]; long long fact[M], cnt[M]; int m; string a; int32_t main() { ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); cerr << "HELLO WORLD :)\n"; cin >> a >> m; fact[0] = 1; for (auto u : a) cnt[u - '0']++; for (int i = 1; i <= 18; i++) fact[i] = i * fact[i - 1]; int len = (int)a.size(); dp[0][0] = 1; for (int mask = 0; mask < (1 << len); mask++) for (int j = 0; j < m; j++) { bool flag = 0; for (int i = 0; i < len; i++) if ((mask >> i & 1) && (a[i] - '0')) flag = 1; if (!flag && mask) continue; for (int i = 0; i < len; i++) if (!(mask >> i & 1)) dp[mask ^ (1 << i)][(10 * j + a[i] - '0') % m] += dp[mask][j]; } long long ans = dp[(1 << len) - 1][0]; for (int i = 0; i < 10; i++) ans /= fact[cnt[i]]; return cout << ans << '\n', false; }
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 N = 100 + 10; const int M = 3e5 + 100; const long long int mod = 1e9 + 7; const long long int inf = 1e16; long long int pw[22], dp[M][N], a[22]; bool mark[10]; int main() { ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); long long int k; int m; cin >> k >> m; int n = 0; while (k != 0) { a[n++] = k % 10; k /= 10; } pw[0] = 1; for (int i = 1; i < 20; i++) pw[i] = (pw[i - 1] * 10) % m; dp[0][0] = 1; for (int mask = 1; mask < (1 << n); mask++) { for (int r = 0; r < m; r++) { int bi = __builtin_popcount(mask); for (int i = 0; i < 10; i++) mark[i] = false; for (int i = 0; i < n; i++) { if ((mask >> i) & 1 && (mask != (1 << n) - 1 || a[i] != 0) && !mark[a[i]]) { mark[a[i]] = true; int mask2 = mask ^ (1 << i); int r2 = (r - (pw[bi - 1] * a[i]) % m + m) % m; dp[mask][r] += dp[mask2][r2]; } } } } cout << dp[(1 << n) - 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
import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.StringTokenizer; public class Main{ public static void main(String[] args) { solve(); } static int mod; static int id[]; static Long dp[][] = new Long[100][1 << 19]; private static void solve() { char num[] = IN.next().toCharArray(); mod = IN.nextInt(); id = new int[num.length]; for (int i = 0; i < num.length; i++) { id[i] = i; } System.out.println(dp(num.length - 1, 0, 0, num, true)); } public static long dp(int pos, int state, int cur, char num[], boolean leadZero) { if (pos < 0) { return cur == 0 ? 1 : 0; } if (dp[cur][state] != null) { return dp[cur][state]; } 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, state | (1 << id[pos]), (cur * 10 + t) % mod, num, false); swap(num, pos, i); } return dp[cur][state] = res; } public static void swap(char num[], int i, int j) { char tmp = num[i]; num[i] = num[j]; num[j] = tmp; int t = id[i]; id[i] = id[j]; id[j] = t; } 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; int len; 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 << len) - 1) return m == 0; if (dp[mask][m] != -1) return dp[mask][m]; long long ans = 0; for (int j = (0); j <= (len - 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() { ios::sync_with_stdio(0); cin.tie(0); ; cin >> n >> mo; len = n.size(); pre(); memset(dp, -1, sizeof(dp)); long long ans = 0; for (int i = (0); i <= (len - 1); ++i) { coun[n[i] - '0']++; if (n[i] - '0' > 0) ans += solve(1 << i, (n[i] - '0') % mo); } for (int i = (0); i <= (9); ++i) { for (int j = (1); j <= (coun[i]); ++j) ans /= j; } cout << ans; cout.flush(); 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; long long dp[1 << 18][105]; long long power(int x, int p) { if (p == 0) return 1; return x * power(x, p - 1); } long long getCount(int mask, int mod, int c) { if (mask == (1 << n) - 1) if (mod == 0) return 1; else return 0; long long &ret = dp[mask][mod]; if (ret != -1) return ret; ret = 0; int f[10] = {0}; for (int i = 0; i < n; i++) { if (mask == 0 && s[i] == '0') continue; if (((mask >> i) & 1) == 0 && !f[s[i] - '0']) ret += getCount(mask | (1 << i), (mod + (s[i] - '0') * power(10, c)) % m, c - 1), f[s[i] - '0'] = 1; } return ret; } int main() { cin >> s >> m; n = s.length(); memset(dp, -1, sizeof dp); cout << getCount(0, 0, n - 1) << 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][100]; int co[1 << 18]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); string s; cin >> s; long long m; cin >> m; long long n = s.size(); long long c[10]; memset(c, 0, sizeof c); for (long long i = 0; i < n; i++) c[s[i] - '0']++; vector<long long> v[1 << n]; for (long long i = 0; i < (1 << n); i++) { int r = 0; for (long long j = 0; j < n; j++) { if (i & (1 << j)) { r++; } else v[i].push_back(j); } co[i] = r; } dp[(1 << n) - 1][0] = 1; long long pw[20]; pw[0] = 1 % m; int e[20]; int r = -1; for (int i = 0; i < n; i++) { int r = 0; for (int j = 0; j <= 9; j++) { r += c[j]; if (i < r) { e[i] = j; break; } } } for (long long i = 1; i < 20; i++) { pw[i] = (pw[i - 1] * 10) % m; } for (long long mask = (1 << n) - 1; mask >= 0; mask--) { for (long long mod = 0; mod < m; mod++) { int now = e[co[mask]]; for (int j = 0; j < v[mask].size(); j++) { if (now == 0 && v[mask][j] == n - 1) continue; int notun = (mod + pw[v[mask][j]] * now) % m; dp[mask][mod] += dp[mask | (1 << v[mask][j])][notun]; } } } for (long long i = 0; i < 10; i++) { long long r = 1; for (long long j = 1; j <= c[i]; j++) r *= j; dp[0][0] /= r; } cout << dp[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
#include <bits/stdc++.h> using namespace std; long long setBit(long long num, long long i) { return num | (1 << i); } long long unsetBit(long long num, long long i) { num ^= 1 << i; return num; } bool checkBit(long long num, long long i) { if (num & 1 << i) return true; else return false; } long long bp(long long num) { return __builtin_popcount(num); } long long giaithua(long long m) { if (m == 1) return 1; return m * giaithua(m - 1); } long long k, n, m, a[20], count1 = 0, dp[300000][105], used[105]; int main() { cin >> n >> m; k = n; memset(used, 0, sizeof(used)); while (k != 0) { a[count1] = k % 10; used[a[count1]]++; k = k / 10; count1++; } memset(dp, 0, sizeof(dp)); for (int i = 0; i < count1; i++) { if (a[i] != 0) { dp[setBit(0, i)][a[i] % m] = 1; } else { dp[setBit(0, i)][a[i] % m] = 0; } } int k = 1 << count1; for (int i = 0; i < k; i++) { for (int ij = 0; ij < m; ij++) { if (dp[i][ij]) { for (int j = 0; j < count1; j++) { if (checkBit(i, j) == 0) { dp[setBit(i, j)][(ij * 10 + a[j]) % m] += dp[i][ij]; } } } } } for (int i = 0; i <= 9; i++) { if (used[i] != 0) dp[k - 1][0] /= giaithua(used[i]); } cout << dp[k - 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
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; public class D { public static void main(String[] args) { try { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int tc = 0; while (true) { String line = br.readLine(); if (line == null) break; System.err.println("--------------TEST CASE " + ++tc + "---------------"); System.err.println("INPUT"); System.err.println(line); String[] s = line.split(" "); char[] n = s[0].toCharArray(); int m = Integer.parseInt(s[1]); long t = System.currentTimeMillis(); System.err.println("\nOUTPUT"); System.out.println(new D().solve(n, m)); t = System.currentTimeMillis() - t; System.err.println("\nT=" + t); } } catch (IOException io) { io.printStackTrace(); } } private int[] n; private int m, all; private long[] mlt; private long[][] ways; private long solve(char[] pn, int m) { all = (1<<pn.length)-1; ways = new long[m+1][all+1]; for (int i = 0; i < ways.length; i++) { Arrays.fill(ways[i],-1); } mlt = new long[pn.length]; mlt[mlt.length-1] = 1; for (int i = mlt.length-2; i >=0; i--) { mlt[i]=10*mlt[i+1]; } n = new int[pn.length]; for (int i = 0; i < n.length; i++) { n[i] = pn[i]-'0'; } Arrays.sort(n); this.m = m; return go(0,0,0); } long go(int mask, int rem, int size) { if (size == n.length) return rem==0?1:0; if (ways[rem][mask]>=0) return ways[rem][mask]; long mult = mlt[size]; int used = 0; long ret = 0; for (int i = 0; i < n.length; i++) { int curr = 1<<i; if ((mask&curr)!=0) continue; int v = n[i]; if (mask == 0 && v==0) continue; int digit = 1<<v; if ((used&digit)!=0) continue; used |= digit; ret += go(mask|curr, (int)((rem+v*mult)%m), size+1); } return ways[rem][mask] = 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; char s[20]; int m; long long dp[1 << 18][110]; int cnt[20]; int main() { ios::sync_with_stdio(false); cin >> s >> m; memset(dp, 0, sizeof(dp)); memset(cnt, 0, sizeof(cnt)); dp[0][0] = 1; int l = strlen(s); long long x = 1 << l, d = 1; for (int i = 0; i < l; i++) d *= ++cnt[s[i] - '0']; for (int i = 0; i < x; i++) { for (int j = 0; j < l; j++) { if (!(i & (1 << j)) && (i || s[j] - '0')) { for (int k = 0; k < m; k++) { if (dp[i][k]) dp[i | (1 << j)][(k * 10 + (s[j] - '0')) % m] += dp[i][k]; } } } } cout << dp[x - 1][0] / d << 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.io.PrintWriter; import java.util.Arrays; import java.util.StringTokenizer; public class _D { static long dp[][],ten[]; static int len,arr[],m; static long solve(int index, int mod,int mask){ if(index == len){ if(mod%m==0){ return 1; } return 0; } else if(dp[mod][mask]!=-1){ return dp[mod][mask]; } else { long way =0; int red[] = new int [10]; for(int i= 0;i<len;i++){ if((mask&(1<<i))==0){ if((index==0&&arr[i]==0)||red[arr[i]]==1) continue; red[arr[i]] = 1; way+=solve(index+1, (int) (mod+(arr[i]*(ten[len-index-1]%m))%m)%m, mask|(1<<i)); } } return dp[mod][mask]= way; } } public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(System.out); StringTokenizer st = new StringTokenizer(in.readLine()); long n = Long.parseLong(st.nextToken()); ten = new long [19]; ten[0] = 1; for(int i=1;i<ten.length;i++) ten[i]=ten[i-1]*10; m = Integer.parseInt(st.nextToken()); long temp = n; while(temp>0){ temp/=10; len++; } arr= new int[len]; int i = 0; int digit[] = new int [10]; while(n>0){ arr[i] = (int) (n%10); digit[arr[i]]++; i++; n/=10; } dp = new long [101][1<<18]; for(i = 0;i<dp.length;i++) Arrays.fill(dp[i], -1); out.println(solve(0,0, 0)); 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
d = '0123456789ABCDEFGH' prev = {d[i]: d[i - 1] for i in range(1, 18)} next = {d[i]: d[i + 1] for i in range(17)} n, m = raw_input().split() p, m = ['0'] * 10, int(m) k, l = 1, len(n) for x in map(int, n): p[x] = next[p[x]] a = ''.join(p) u, v = {a: [0] * m}, {} u[a][0] = 1 for r in range(1, l + 1): if r == l and '0' in n: u.pop('1000000000') for a in u: for i in range(10): if a[i] != '0': b = a[: i] + prev[a[i]] + a[i + 1: ] if not b in v: v[b] = [0] * m i = (k * i) % m - m x, y = v[b], u[a] for j in range(m): x[j + i] += y[j] u, v = v, {} k = 10 * k % m print u['0000000000'][0]
PYTHON
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 v; int n, m; long long dp[1 << 18][100]; int num[100], cnt[100]; int N; void solve() { N = 1 << n; dp[0][0] = 1; for (int i = 0; i < n; i++) { if (num[i] > 0) dp[1 << i][num[i] % m] += 1; } for (int i = 1; i < N; i++) { 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) continue; dp[i | (1 << k)][(j * 10 + num[k]) % m] += dp[i][j]; } } } long long ans = dp[N - 1][0]; for (int i = 0; i <= 9; i++) { for (int j = 1; j <= cnt[i]; j++) { ans /= j; } } cout << ans << endl; } int main() { ios::sync_with_stdio(false); cin >> v >> m; n = 0; while (v) { num[n++] = v % 10; cnt[v % 10]++; v /= 10; } solve(); 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 = 1e5 + 15; const int inf = 1e9 + 7; const long long MOD = 1e9 + 7; double eps = 1e-9; long long f[20]; int digit[20]; int a[20]; long long dp[1 << 18][105]; int m; void init() { f[0] = 1; for (int i = 1; i <= 18; i++) { f[i] = f[i - 1] * i; } memset(dp, -1, sizeof(dp)); } long long dfs(int cur, int mod, int len) { if (dp[cur][mod] != -1) return dp[cur][mod]; if (cur == 0) { if (mod == 0) return 1; else return 0; } long long res = 0; bool vis[10]; memset(vis, 0, sizeof(vis)); for (int i = 0; i < len; i++) { if (cur & (1 << i)) { if (vis[digit[len - i]]) continue; vis[digit[len - i]] = true; res += dfs(cur ^ (1 << i), ((10 * mod - digit[len - i]) % m + m) % m, len); } } dp[cur][mod] = res; return res; } int main() { init(); long long n; cin >> n >> m; int len = 0; while (n) { len++; digit[len] = n % 10; a[n % 10]++; n /= 10; } long long ans = 0; dp[0][0] = 1; bool vis[10]; memset(vis, 0, sizeof(vis)); for (int i = 0; i < len; i++) { if (digit[len - i] && vis[digit[len - i]] == false) { vis[digit[len - i]] = true; ans += dfs(((1 << len) - 1) ^ (1 << i), ((-digit[len - i]) % m + m) % m, len); } } 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.InputStreamReader; import java.io.IOException; import java.util.InputMismatchException; import java.io.PrintStream; import java.io.BufferedReader; import java.io.OutputStream; import java.io.PrintWriter; import java.io.Reader; import java.io.Writer; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * @author Nipuna Samarasekara */ 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 { ///////////////////////////////////////////////////////////// public void solve(int testNumber, FastScanner in, FastPrinter out) { long n=in.nextLong(); int m=in.nextInt(); String S=""+n; int len=S.length(); long[][] dp= new long[(1<<len)+1][m]; dp[0][0]=1; int[] pow2= new int[30]; pow2[0]=1; for (int i = 1; i < 30; i++) { pow2[i]=2*pow2[i-1]; } long[] pow10= new long[30]; pow10[0]=1; for (int i = 1; i < 30; i++) { pow10[i]=(pow10[i-1]*10)%m; } for (int i = 0; i < len; i++) { int cur=(int)S.charAt(i)-(int)'0'; for (int j = 0; j <= (1<<len) ; j++) { // int[] dd=new int[len]; // for (int l = 0; l < len; l++) { // // if((j&pow2[l])>0) // dd[l]=1 ; // } if(Integer.bitCount(j)==i) for (int k = 0; k < m; k++) { if(dp[j][k]!=0){ for (int l = 0; l < len; l++) { if(cur==0&&l==0)continue; if((j&pow2[l])==0) { int newj=(j|pow2[l]),newk= (int)((k+pow10[len-1-l]*cur)); while(newk>=m)newk-=m; dp[newj][newk]+=dp[j][k]; } } } } } } long ans= dp[(1<<len)-1][0]; // System.out.println(ans); int[] aa= new int[10]; for (int i = 0; i < len; i++) { aa[(int)S.charAt(i)-(int)'0']++; } long[] fact= new long[20]; fact[0]=1; for (int i = 1; i < 20; i++) { fact[i]=i*fact[i-1]; } for (int i = 0; i < 10; i++) { if(aa[i]>1)ans/=fact[aa[i]]; } // deb(dp[31]); out.println(ans); // deb(dp); } } 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 long nextLong() { return Long.parseLong(next()); } public String readLine() { try { return super.readLine(); } catch (IOException e) { return null; } } } class FastPrinter extends PrintWriter { public FastPrinter(OutputStream out) { super(out); } public FastPrinter(Writer 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.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.Arrays; import java.util.Comparator; public class D { static long n; static int m; static int cnt_bits(int i) { int ret=0; while(i>0) { ret++; i-=Integer.lowestOneBit(i); } return ret; } static int digit(char[] s, int i) { return s[i]-'0'; } static int pos(int i) { int ret=-1; while(i>0) { ret++; i=i>>1; } return ret; } static long fac(int i) { long ret=1; for (int j=2; j<=i; j++) { ret*=j; } return ret; } static long solve() { char[] s=Long.toString(n).toCharArray(); Arrays.sort(s); int l=s.length, k=(1<<l)-1; Integer[] sets=new Integer[k+1]; for (int i=0; i<=k; i++) { sets[i]=i; } Arrays.sort(sets, new Comparator<Integer>() { public int compare(Integer o1, Integer o2) { int i1=cnt_bits(o1), i2=cnt_bits(o2); return i1-i2; }}); long[][] f=new long[k+1][m]; for (int i=0; i<l; i++) { int d=digit(s, i); if(d>0) { f[1<<i][d%m]=1; } } for (int i=0; i<=k; i++) { int set=sets[i], bcnt=cnt_bits(set), set2=set; for (int j=0; j<bcnt; j++) { int lo=Integer.lowestOneBit(set2), loidx=pos(lo); set2-=lo; for (int r=0; r<m; r++) { if(f[set-lo][r]>0) { int r2=(10*r+digit(s,loidx))%m; f[set][r2]+=f[set-lo][r]; } } } } int[] perms=new int[10]; for (int i=0; i<s.length; i++) { perms[digit(s,i)]++; } long p=1; for (int i=0; i<perms.length; i++) { p*=fac(perms[i]); } return f[k][0]/p; } static void run_stream(InputStream ins) throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); String[] nm=br.readLine().split(" "); n=Long.parseLong(nm[0]); m=Integer.parseInt(nm[1]); System.out.println(solve()); } public static void main(String[] args) throws IOException { run_stream(System.in); } }
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 INF = 0x3f3f3f3f; long long n, x, dp[1 << 18][100]; int m, p[20], len; long long dfs(int pos, int status, bool zero, int rev) { if (!pos) return rev == 0; if (!zero && dp[status][rev] != -1) return dp[status][rev]; long long ans = 0; int f[10]; memset(f, 0, sizeof(f)); for (int i = 1; i <= len; i++) if (!((status >> i - 1) & 1)) { if (zero && p[i] == 0) continue; if (f[p[i]]) continue; ans += dfs(pos - 1, status | (1 << i - 1), zero && p[i] == 0, (rev * 10 + p[i]) % m); f[p[i]] = 1; } if (!zero) dp[status][rev] = ans; return ans; } int main() { ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); ; memset(dp, -1, sizeof(dp)); cin >> n >> m; x = n; while (x) { p[++len] = x % 10; x /= 10; } long long ans = dfs(len, 0, true, 0); 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.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Scanner; public class CF401D{ public static void main(String[] args){ long[] fact = new long[19]; fact[0] = 1; for (int i=1; i<19; i++){ fact[i] = fact[i-1]*i; } Scanner scn = new Scanner(System.in); while (scn.hasNext()){ String line = scn.nextLine(); String[] tmp = line.split(" "); int m = Integer.parseInt(tmp[1]); int[] cnt = new int[10]; int n = tmp[0].length(); Arrays.fill(cnt, 0); for (int i=0; i<n; i++){ cnt[tmp[0].charAt(i)-'0'] ++; } int allS = 1<<n; long[][] dp = new long[allS][m]; for (int s=0; s<allS; s++){ Arrays.fill(dp[s], 0); } dp[0][0] = 1; for (int s=0; s<allS; s++){ for (int i=0; i<n; i++){ int now = tmp[0].charAt(i)-'0'; if ((now==0&&s==0)||(s&(1<<i))!=0){ continue; } for (int r=0; r<m; r++){ if (dp[s][r]>0){ int newS = s|(1<<i); int newR = (10*r+now)%m; dp[newS][newR] += dp[s][r]; } } } } long ans = dp[allS-1][0]; for (int i=0; i<=9; i++){ ans /= fact[cnt[i]]; } System.out.println(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
import java.io.IOException; import java.util.Arrays; import java.util.InputMismatchException; import java.io.OutputStream; import java.io.PrintWriter; import java.math.BigInteger; import java.util.HashSet; 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 dp[][]; char no[]; int m; int len; public void solve(int testNumber, InputReader in, PrintWriter out) { no = (in.readLong() + "").toCharArray(); len = no.length; m = in.readInt(); dp = new long[1 << 18][100]; for (long[] a : dp) Arrays.fill(a, -1); out.println(go(0, 0)); } long go(int mask, int mod) { if (mask == (1 << len) - 1){ if (mod == 0) return 1; return 0; } HashSet<Character> hs = new HashSet<Character>(); if (dp[mask][mod] != -1) return dp[mask][mod]; long ans = 0; for (int i = 0; i < len; i++) { if (((mask >> i) & 1) != 0) continue; if (mask == 0 && no[i] == '0') continue; if (hs.contains(no[i])) continue; hs.add(no[i]); ans += go(mask | (1 << i), ((mod * 10) + no[i] - '0') % m); } return dp[mask][mod] = ans; } } class InputReader { 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 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 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 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; 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
#include <bits/stdc++.h> using namespace std; long long n; int m; long long dp[1 << 18][101]; int num[20]; int c[10]; int digitLen; long long Fac(int x) { return x == 0 ? 1 : Fac(x - 1) * x; } int main() { cin >> n >> m; while (n) { int t = (int)(n % 10); num[digitLen++] = t; c[t]++; n /= 10; } long long div = 1; for (int i = 0; i < 10; ++i) div *= Fac(c[i]); dp[0][0] = 1; for (int i = 0; i < (1 << digitLen); ++i) { for (int j = 0; j < m; ++j) { if (dp[i][j] == 0) continue; for (int k = 0; k < digitLen; ++k) { if (1 & (i >> k)) continue; if (i || num[k]) { int mask = i | (1 << k), mod = (j * 10 + num[k]) % m; dp[i | (1 << k)][(j * 10 + num[k]) % m] += dp[i][j]; } } } } printf("%I64d\n", dp[(1 << digitLen) - 1][0] / div); 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 Main { static StringBuilder st = new StringBuilder(); static int n, m, a[]; static long[][][][][][][][][][][][] memo;// idx , taken , mod , zero , one , two , three , four , five , six , seven , eight , nine static int mult(int a , int b , int MOD) {return (int)((1l * a * b) % MOD) ; } static int add(int a , int b , int MOD) {return (a+b) % MOD;} static long dp(int idx, int mod, int[] cnt) { if (idx == n && mod == 0) return 1; if (idx == n) return 0; if (check(idx, mod, cnt)) return get(idx, mod, cnt); long ans = 0 ; for (int i = 0; i < 10; i++) { if (cnt[i] > 0) { int[] newCnt = cnt.clone(); newCnt[i]--; if (i == 0 && idx == 0) continue; ans += dp(idx + 1, add(i, mult(10, mod, m), m), newCnt ); } } return set(idx, mod, cnt, ans); } public static void main(String[] args) throws Exception { Scanner sc = new Scanner(); PrintWriter out = new PrintWriter(System.out); char[] c = sc.next().toCharArray(); n = c.length; m = sc.nextInt(); init(n, m, c); int [] cnt = new int [10]; for(int x : a) cnt[x]++; out.println(dp(0, m, cnt)); out.flush(); out.close(); } static class Scanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st; Scanner() { } Scanner(String path) throws Exception { br = new BufferedReader(new FileReader(path)); } String next() throws Exception { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } int nextInt() throws Exception { return Integer.parseInt(next()); } long nextLong() throws Exception { return Long.parseLong(next()); } double nextDouble() throws Exception { return Double.parseDouble(next()); } } static void shuffle(int[] a) { int n = a.length; for (int i = 0; i < n; i++) { int r = i + (int) (Math.random() * (n - i)); int tmp = a[i]; a[i] = a[r]; a[r] = tmp; } } static void shuffle(char[] a) { int n = a.length; for (int i = 0; i < n; i++) { int r = i + (int) (Math.random() * (n - i)); char tmp = a[i]; a[i] = a[r]; a[r] = tmp; } } static void shuffle(long[] a) { int n = a.length; for (int i = 0; i < n; i++) { int r = i + (int) (Math.random() * (n - i)); long tmp = a[i]; a[i] = a[r]; a[r] = tmp; } } private static boolean oj = System.getProperty("ONLINE_JUDGE") != null; private static void tr(Object... o) { if (!oj) System.out.println(Arrays.deepToString(o)); } static boolean check(int idx, int mod, int[] cnt) { return (memo[idx][cnt[0]][cnt[1]][cnt[2]][cnt[3]][cnt[4]][cnt[5]][cnt[6]][cnt[7]][cnt[8]][cnt[9]][mod] != -1); } static long get(int idx, int mod, int[] cnt) { return memo[idx][cnt[0]][cnt[1]][cnt[2]][cnt[3]][cnt[4]][cnt[5]][cnt[6]][cnt[7]][cnt[8]][cnt[9]][mod]; } static long set(int idx, int mod, int[] cnt, long val) { return memo[idx][cnt[0]][cnt[1]][cnt[2]][cnt[3]][cnt[4]][cnt[5]][cnt[6]][cnt[7]][cnt[8]][cnt[9]][mod] = val; } static void init(int n, int m, char[] c) { a = new int[n]; for (int i = 0; i < n; i++) a[n - i - 1] = c[i] - '0'; int [] cnt = new int [10]; for(int x : a) cnt[x]++; memo = new long[n][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+1]; for (long[][][][][][][][][][][] xx : memo) for (long[][][][][][][][][][] xxx : xx) for (long[][][][][][][][][] xxxx : xxx) for (long[][][][][][][][] xxxxx : xxxx) for (long[][][][][][][] xxxxxx : xxxxx) for (long[][][][][][] xxxxxxx : xxxxxx) for (long[][][][][] xxxxxxxx : xxxxxxx) for (long[][][][] xxxxxxxxx : xxxxxxxx) for (long[][][] xxxxxxxxxx : xxxxxxxxx) for (long[][] xxxxxxxxxxx : xxxxxxxxxx) for (long[] xxxxxxxxxxxx : xxxxxxxxxxx) Arrays.fill(xxxxxxxxxxxx, -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
import java.util.*; import java.io.*; import static java.util.Arrays.*; import static java.util.Collections.*; import static java.lang.Math.*; public class D { int INF = 1 << 28; //long INF = 1L << 62; double EPS = 1e-10; ExMap[] maps; void run() { maps = new ExMap[2]; maps[0] = new ExMap(); maps[1] = new ExMap(); Scanner sc = new Scanner(System.in); String n = sc.next();int m = sc.nextInt(); int[] ns = new int[10]; int l = n.length(); for(int i=0;i<l;i++) ns[n.charAt(i) - '0']++; long[] pow = new long[10]; pow[0] = 1; for(int i=1;i<10;i++) pow[i] = pow[i-1] * 20; long ndig = 0; for(int i=0;i<10;i++) ndig += pow[i] * ns[i]; long[] dp = new long[m]; dp[0] = 1; maps[0].put(ndig, dp); int x = 1; long[] val, tar; long key; for(int i=0;i<l;i++) { maps[x].clear(); for(Map.Entry<Long, long[]> entry: maps[1-x].entrySet()) { key = entry.getKey(); tar = entry.getValue(); for(int j=(i==0? 1: 0);j<10;j++) if((key / pow[j]) % 20 > 0) { if(maps[x].containsKey(key - pow[j])) val = maps[x].get(key - pow[j]); else val = new long[m]; for(int k=0;k<m;k++) val[(k*10 + j) % m] += tar[k]; maps[x].put(key - pow[j], val); } } x = 1-x; } for(Map.Entry<Long, long[]> entry: maps[1-x].entrySet()) { tar = entry.getValue(); long ans = tar[0]; System.out.println(ans); } } class ExMap extends HashMap<Long, long[]>{} void debug(Object... os) { System.err.println(Arrays.deepToString(os)); } public static void main(String[] args) { new D().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; ifstream fin("A.in"); ofstream fout("A.out"); long long n; int m, t; int a[20]; bool upd[10]; long long dp[300000][101]; int main() { cin >> n >> m; while (n) { a[t++] = n % 10; n /= 10; } sort(a, a + t); for (int i = 0; i < t; ++i) { if (a[i] && (i == 0 || a[i] != a[i - 1])) dp[(1 << i)][a[i] % m]++; } int nn = (1 << t); for (int i = 1; i < nn; ++i) { for (int j = 0; j < m; ++j) { if (dp[i][j] == 0) continue; memset(upd, 0, sizeof(upd)); for (int k = 0; k < t; ++k) { if (((1 << k) | i) != i && !upd[a[k]]) { dp[(1 << k) | i][(j * 10 + a[k]) % m] += dp[i][j]; upd[a[k]] = 1; } } } } cout << dp[nn - 1][0]; }
CPP