Combinatorial mathematics codefoeces300c Beautiful Numbers

Source: Internet
Author: User

... At first did not see test instructions, the original premise of the excellent is good ...


We think that the number of the n-bit length for each of the a,a is called B.

First of all, we need to discuss it in two different situations.


In the first case, when a equals B, it means that the n digits in a are all a.

That is, the value of B equals a*n, and we just need to verify that the number of each digit in B is equal to a.

If all equals, then the answer is 1, otherwise the answer is 0.


In the second case, when a is not equal to B

Set a appears x times in A, and b appears in a For Y times.

Because the length of a is n, there must be x+y = n


B is the sum of each digit of a, so B can only be 9 * N, that is, every bit of n digits is 9

However, B can only have A and B two numbers, stating that the number of B is not too much, up to thousands of, so it is possible to direct violence to cite b


Also know that a in a in the appearance of x times, b in a in the occurrence of Y. B is the sum of each number in a, and a is only A and b ...

Speaking of which, it's obvious, b=a*x+b*y.

So, X+y = N,b=a*x+b*y, you can solve the x and y directly

If you find that x and y are not integers or have a less than 0, then it means that this type of B is not sufficient to ignore the situation.


Well, now we know that when B is a case, the number of x and Y, how to ask for the number of species?

In fact, in the n position to choose X position to place the number A, the other position must also only put the number B, so is C (n,x)


It's a big circle, and the final idea is clear.

1. Special break

2.DFS Enumeration Number B

3. Solution equation, get x and Y

4. C (n,x) for all conditions of satisfaction, and add modulus is the answer


So how to ask C,, this can Baidu a bit inverse

Here I directly affixed to the template, and later encountered the number of combinations, even if the provisional understanding of the principle, you can directly hit the template

const int MX = 1000000 + 5;//n size const int mod = 1e9 + 7;//If the modulo is not prime, this template is not available!! LL F[MX], invf[mx]; ll Power (ll A, ll b) {    LL ret = 1;    while (b) {        if (b & 1) ret = (ret * a)% mod;        A = (A * a)% mod;        b >>= 1;    }    return ret;} void init () {//This is used for initialization, remember    f[0] = 1;    for (int i = 1; i < MX; i++) {        F[i] = (f[i-1] * i)% mod;    }    INVF[MX-1] = Power (f[mx-1], mod-2);    for (int i = MX-2; I >= 0; i--) {        Invf[i] = invf[i + 1] * (i + 1)% mod;    }} LL C (int n, int m) {    if (N < 0 | | m < 0 | | m > N) return 0;    if (m = = 0 | | m = = N)    return 1;    return F[n] * invf[n-m]% mod * invf[m]% MoD;} LL A (int n, int m) {    if (N < 0 | | m < 0 | | m > N) return 0;    return F[n] * invf[n-m]% MoD;}

Here is the code to apply the template AC this topic

#include <map> #include <set> #include <cmath> #include <stack> #include <queue> #include <cstdio> #include <string> #include <vector> #include <cstring> #include <iostream># Include<algorithm> #include <functional>using namespace std;typedef long long ll;typedef pair<int, int > pii;const int MX = 1000000 + 5;const int mod = 1e9 + 7; LL F[MX], invf[mx];    ll Power (ll A, ll b) {ll ret = 1;        while (b) {if (b & 1) ret = (ret * a)% mod;        A = (A * a)% mod;    b >>= 1; } return ret;}    void init () {//This is used for initialization, remember f[0] = 1;    for (int i = 1; i < MX; i++) {F[i] = (f[i-1] * i)% mod;    } Invf[mx-1] = Power (f[mx-1], mod-2);    for (int i = MX-2; I >= 0; i--) {Invf[i] = invf[i + 1] * (i + 1)% MoD;    }}ll C (int n, int m) {if (N < 0 | | m < 0 | | m > N) return 0;    if (m = = 0 | | m = = n) return 1; return F[n] * invf[n-m]% mod * invf[m]% MoD;} LL A (int n,int m) {if (N < 0 | | m < 0 | | m > N) return 0; return F[n] * invf[n-m]% MoD;}        BOOL Check (int n, int x) {while (n) {if (n%! = x) return false;    n/= 10; } return true; int A, b, N;    LL solve (int z) {if ((Z-A * n)% (b-a)) return 0;    int y = (Z-A * N)/(B-A); Return C (n, y);}    LL DFS (int x) {if (x > 9 * N) return 0;    LL ans = solve (x);    Ans + = DFS (x + a);    Ans + = DFS (x + b);    Ans%= MoD; return ans;}    int main () {//freopen ("Input.txt", "R", stdin);    Init ();    scanf ("%d%d%d", &a, &b, &n);        if (a = = b) {if (check (A * n, a)) printf ("1\n");        else printf ("0\n");    return 0;    } printf ("%i64d\n", DFS (0)); return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Combinatorial mathematics codefoeces300c Beautiful Numbers

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.