< topic links >
Main topic:
Given A and B, if a number of each is a or B, then we call this number good, on the basis of good, if the sum of each of the numbers is also good, then this number is excellent. The number of excellent with a length of n is mod (1e9+7).
Problem Solving Analysis:
We can enumerate the number m of a, so the number of B is (N-M), and then determine whether the situation is feasible, that is, whether the a*m+b* (n-m) is satisfied with the good numbers, if satisfied, then the answer plus C (n,m). Because n is large, and in the process of calculating the number of combinations, it is necessary to divide a large number, so we need to find the inverse element, because the subject modulus is 1e9+7 and is prime, so we can use the Fermat theorem to find the inverse element.
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#define LL long long
using namespace std;
const LL MOD = 1e9 + 7;
const LL MAXN = 1e6 + 10;
LL a, b;
LL n;
LL fact [MAXN]; // Save the factorial of i
bool Sum_ok (LL x) {
while (x) {// Determine whether each bit consists of only a or b
LL res = x% 10;
if (res! = a && res! = b) return false;
x = x / 10;
}
return true;
}
LL pow (LL m, LL n) {
if (n == 0) return 1;
LL ans = 1;
while (n> 0) {
if (n & 1) ans = ans * m% MOD;
m = m * m% MOD;
n = n / 2;
}
return ans;
}
int main () {
scanf ("% d% d% d", & a, & b, & n);
fact [0] = 1;
for (LL i = 1; i <= n; i ++) fact [i] = fact [i-1] * i% MOD; // initialize the fact array and store the factorial values into the array, don't forget to take the remainder
LL ans = 0;
for (LL i = 0; i <= n; i ++) {// enumerate the number of a
if (is_gnum (a * i + b * (n-i))) {// Check if the enumerated number is a good number
LL t1 = pow (fact [i], MOD-2)% MOD; // Fermat's little theorem finds inverses, pay attention to the format of inverses, fact [i], fact [n-i] are divisors
LL t2 = pow (fact [n-i], MOD-2)% MOD;
ans + = fact [n] * t1% MOD * t2% MOD; // Use the formula deduced in the explanation above to calculate the answer, and use the combination number formula, n! / (m! * (n-m)!)
}
}
printf ("% lld \ n", ans% MOD);
return 0;
}
2018-10-09
Codeforces 300C Beautiful Numbers "combined number" + "inverse"