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,为质数,所以可以用费马小定理求逆元。组合数计算公式如下:
$$C(n,m)=\frac{n!}{(n-m)!*m!}(m≤n)$$
所以,我们需要对$(n-m)!$和$m!$求逆元
#include <bits/stdc++.h>
using namespace std; typedef long long ll;
const ll mod = 1e9+;
const int N = 1e6+;
ll n,a,b,fact[N]; bool isgood(ll x){ //判断和是否也是good
while(x){
ll res=x%;
if(res!=a&&res!=b)return false;
x/=;
}return true;
} ll pw(ll a,ll b){
if(b==)return ;
ll ans=;
while(b){
if(b&)ans=ans*a%mod;
a=a*a%mod;
b>>=;
}return ans;
}
int main(){
cin>>a>>b>>n;
fact[]=;
for(int i=;i<=n;i++)fact[i]=fact[i-]*i%mod;
ll ans=;
for(int i=;i<=n;i++){
if(isgood(a*i+b*(n-i))){
ll tmp1=pw(fact[i],mod-)%mod; //费马小定理求逆元
ll tmp2=pw(fact[n-i],mod-)%mod;
ans+=fact[n]*tmp1%mod*tmp2%mod;
}
}
printf("%lld\n",ans%mod);
}
Codeforces 300C Beautiful Numbers 【组合数】+【逆元】的更多相关文章
- CodeForces 300C Beautiful Numbers(乘法逆元/费马小定理+组合数公式+高速幂)
C. Beautiful Numbers time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- CodeForces 300C Beautiful Numbers
枚举,组合数,逆元. 枚举$a$用了$i$个,那么$b$就用了$n-i$个,这个时候和$sum=a*i+b*(n-i)$,判断$sum$是否满足条件,如果满足,那么答案加上$C(n,i)$. #inc ...
- [CodeForces 300C Beautiful Numbers]组合计数
题意:十进制的每一位仅由a和b组成的数是“X数”,求长度为n,各数位上的数的和是X数的X数的个数 思路:由于总的位数为n,每一位只能是a或b,令a有p个,则b有(n-p)个,如果 a*p+b*(n-p ...
- CodeForces 55D "Beautiful numbers"(数位DP+离散化处理)
传送门 参考资料: [1]:CodeForces 55D Beautiful numbers(数位dp&&离散化) 我的理解: 起初,我先定义一个三维数组 dp[ i ][ j ][ ...
- Codeforces 55D. Beautiful numbers(数位DP,离散化)
Codeforces 55D. Beautiful numbers 题意 求[L,R]区间内有多少个数满足:该数能被其每一位数字都整除(如12,24,15等). 思路 一开始以为是数位DP的水题,觉得 ...
- codeforces 55D - Beautiful numbers(数位DP+离散化)
D. Beautiful numbers time limit per test 4 seconds memory limit per test 256 megabytes input standar ...
- CodeForces 55D Beautiful numbers
D. Beautiful numbers time limit per test 4 seconds memory limit per test 256 megabytes input standar ...
- CodeForces - 55D Beautiful numbers —— 数位DP
题目链接:https://vjudge.net/problem/CodeForces-55D D. Beautiful numbers time limit per test 4 seconds me ...
- CodeForces - 55D - Beautiful numbers(数位DP,离散化)
链接: https://vjudge.net/problem/CodeForces-55D 题意: Volodya is an odd boy and his taste is strange as ...
随机推荐
- swift 实践- 08 -- UISegmentedControl
import UIKit class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoa ...
- The word 'localhost' is not correctly spelled 这个问题怎么解决
The word 'localhost' is not correctly spelled 这个问题怎么解决 有时工程中有下划线并提示 The word is not correctly spelle ...
- C# TTS 文字和英文
using System;using System.Globalization;using System.Linq;using System.Speech.Synthesis;using System ...
- Windows下Oracle 11g安装以及创建数据库
安装数据库 事实上Oracle安装 1.安装准备 Oracle的安装包下载以后是两个压缩包,同时选中两个压缩包右击进行解压 2.解压完成如下图所示 3.双击 setup.exe 文件进行安装,会弹出以 ...
- 关闭VirtualBox虚拟机的时钟同步
原文链接:关闭VirtualBox虚拟机的时钟同步 在VirtualBox的虚拟机上默认虚拟机的时间是会和物理机同步的,但可以通过下面的命令来关闭 1. 首先查看虚拟机列表 VBoxManage li ...
- 自定义你的 Confluence 6 站点
本部分对 Confluence 全站进行自定义的相关内容进行了说明.这部分只能是具有 Confluence 的管理员权限的用户才能进行操作 —— 系统管理员或者 Confluence 管理员. 有关对 ...
- 3790:最短路径问题(HDU)
Problem Description 给你n个点,m条无向边,每条边都有长度d和花费p,给你起点s终点t,要求输出起点到终点的最短距离及其花费,如果最短距离有多条路线,则输出花费最少的. Inp ...
- LeetCode(85):最大矩形
Hard! 题目描述: 给定一个仅包含 0 和 1 的二维二进制矩阵,找出只包含 1 的最大矩形,并返回其面积. 示例: 输入: [ ["1","0",&quo ...
- The import util cannot be resolved
代码: 明显的错误: 应改成 import java.util.*; 没有理解java的基本概念
- 开放系统的直连式存储(Direct-Attached Storage,简称DAS)
开放系统的直连式存储(Direct-Attached Storage,简称DAS)已经有近四十年的使用历史,随着用户数据的不断增长,尤其是数百GB以上时,其在备份.恢复.扩展.灾备等方面的问题变得日益 ...