Codeforces 300C Beautiful Numbers 【组合数】+【逆元】

<题目链接>

题目大意:

给出a和b,如果一个数每一位都是a或b,那么我们称这个数为good,在good的基础上,如果这个数的每一位之和也是good,那么这个数是excellent。求长度为n的excellent数的个数mod(1e9+7)。

解题分析:

我们可以枚举a的个数m,所以b的个数为(n-m),然后判断这种情况是否可行,即,是否满足a*m+b*(n-m)为good number ,如果满足的话,则答案加上C(n,m)。因为n很大,并且在计算组合数的过程中,需要除以很大的数,所以需要求逆元,因为本题模数为1e9+7,为质数,所以可以用费马小定理求逆元。

#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];//存i的阶乘

bool Sum_ok(LL x){
    while(x){    //判断每一位是否只由a或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;//将fact数组初始化将阶乘的值存进数组,不要忘了取余
    LL ans=0;
    for(LL i=0;i<=n;i++){    //枚举a的个数
        if(is_gnum(a*i+b*(n-i))){    //判断枚举出的数是不是good number
            LL t1=pow(fact[i],MOD-2)%MOD;       //费马小定理求逆元,注意求逆元的格式,fact[i]、fact[n-i]均为除数
            LL t2=pow(fact[n-i],MOD-2)%MOD;
            ans += fact[n]*t1%MOD*t2%MOD;//利用上面讲解中推导出的公式计算出答案,利用组合数公式,n!/(m!*(n-m)!)
        }
    }
    printf("%lld\n",ans%MOD);
    return 0;
}

2018-10-09

原文地址:https://www.cnblogs.com/00isok/p/9762172.html

时间: 2024-10-10 12:55:22

Codeforces 300C Beautiful Numbers 【组合数】+【逆元】的相关文章

CodeForces 300C Beautiful Numbers(乘法逆元/费马小定理+组合数公式+快速幂)

C. Beautiful Numbers time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Vitaly is a very weird man. He's got two favorite digits a and b. Vitaly calls a positive integer good, if the decimal

CodeForces 55D Beautiful numbers 数位DP+数学

题意大概是,判断一个正整数区间内有多少个整数能被它自身的每一个非零的数字整除. 因为每一个位置上的整数集s = {0,1,2,3,4,5,6,7,8,9} lcm(s) = 2520 现在有一个整数t是由s中一个或者多个数字构成的,记为abcde,显然t = a*10^4+b*10^3+c*10^2+d*10^1+e 要使得t能被a,b,c,d,e整除,必然有t % lcm(a,b,c,d,e) = 0 因为a,b,c,d,e去重之后一定是s的一个子集,所以lcm(s)一定是lcm(a,b,c,

Codeforces 55D. Beautiful numbers(数位DP,离散化)

Codeforces 55D. Beautiful numbers 题意 求[L,R]区间内有多少个数满足:该数能被其每一位数字都整除(如12,24,15等). 思路 一开始以为是数位DP的水题,觉得只需要记录搜到当前位出现了哪些数字作为状态即可,明显是假算法...感觉这是一道数位DP好题.可以这样思考:一个数要想被其各位数字分别都整除,等价于它被那些数字的LCM整除.因此记录当前位,当前数对(1~9的LCM)取模的结果,当前出现的数字的LCM这三个值作为状态才合理,即dp[pos][sum][

CodeForces 55D Beautiful numbers(数位dp&amp;&amp;离散化)

题目链接:[kuangbin带你飞]专题十五 数位DP A - Beautiful numbers 题意 ps:第一道数位dp,题真好,虽然是参考大牛方法悟过才a,但仍收获不少. 求一个区间内的Beautiful numbers有多少个.Beautiful numbers指:一个数能整除所有组成它的非0数字. 例如15可以被1和5整除,所以15是Beautiful numbers. 思路 Beautiful numbers指:一个数能整除所有组成它的非0数字. 等同于 一个数能整除 所有组成它的

CodeForces 55D Beautiful numbers

Beautiful numbers 题目链接 Description Volodya is an odd boy and his taste is strange as well. It seems to him that a positive integer number is beautiful if and only if it is divisible by each of its nonzero digits. We will not argue with this and just

CodeForces 55D - Beautiful numbers - [数位DP+离散化]

题目链接:https://cn.vjudge.net/problem/CodeForces-55D Volodya is an odd boy and his taste is strange as well. It seems to him that a positive integer number is beautiful if and only if it is divisible by each of its nonzero digits. We will not argue with

CodeForces - 55D Beautiful numbers (数位DP)

Volodya is an odd boy and his taste is strange as well. It seems to him that a positive integer number is beautiful if and only if it is divisible by each of its nonzero digits. We will not argue with this and just count the quantity of beautiful num

CodeForces - 55D - Beautiful numbers(数位DP,离散化)

链接: https://vjudge.net/problem/CodeForces-55D 题意: Volodya is an odd boy and his taste is strange as well. It seems to him that a positive integer number is beautiful if and only if it is divisible by each of its nonzero digits. We will not argue with

CodeForces 55D Beautiful numbers(数位dp+数学)

题目链接:http://codeforces.com/problemset/problem/55/D 题意:一个美丽数就是可以被它的每一位的数字整除的数. 给定一个区间,求美丽数的个数. 显然这是一道数位dp,就是满足一个数能被所有位数的lcm整除即可. 一般都会设dp[len][mod][LCM],mod表示余数,LCM表示前len位的lcm. 但是如果直接裸mod会很复杂,于是再想lcm{0,1,2,3,4,5,6,7,8,9}=2520; 而且lcm{a,b,c,d....}{a,b,c,