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 ...
随机推荐
- Confluence 6 管理 Atlassian 提供的 App
Confluence 用户可以使用桌面应用来编辑一个已经上传到 Confluence 的文件,然后这个文件自动保存回 Confluence. 这个下载和上传的过程是通过 Atlassian Compa ...
- Confluence 6 白名单表达式类型
表达式类型 当添加一个 URL 到白名单列表中的时候,你可以选择采取下面的表达式进行添加. 域名名称(Domain name) 允许 URL 为一个指定的域名. http://www.example. ...
- vuex action 与mutations 的区别
面试没说清楚.这个太丢人回来整理下: 事实上在 vuex 里面 actions 只是一个架构性的概念,并不是必须的,说到底只是一个函数,你在里面想干嘛都可以,只要最后触发 mutation 就行.异步 ...
- 按照勾选 删除表格的行<tr>
需求描述:有一个产品列表,有一个删减按钮,点击删减按钮,按照产品勾选的行,删除产品列表中对应的行数据 代码: //html代码<table id="table1"> & ...
- poj1094 恶心题,,每次加边进行判断
/* 给定一组偏序关系,问最少第几步能确定次序 如果出现环,问第几步出现环 因为要求第几步确定次序或者第几步出现环,所以每次读入一个偏序关系就进行一次拓扑排序 */ #include <iost ...
- OAuth2 token
1.资源服务器 package com.ruhuanxingyun.config; import com.fasterxml.jackson.databind.ObjectMapper; import ...
- day4-list,列表
dist: 增:1.append(obj),在列表最后添加元素: 2.insert(index,object),在索引处添加元素: 3.extend,迭代添加元素,所添加元素必须可迭代. 删:1.po ...
- CF1065D
如果不喜欢过长代码的看官,请移步其他题解... 这题其实思想极其简单: 棋盘问题常见的算法都比较暴力,常用的有搜索和状压dp 而这道题显然没啥能状压的,所以我们考虑搜索 但是仅仅搜索是不够的,因为有极 ...
- Python变量的作用域
局部变量 局部变量是指在函数内部定义并使用的变量,他只在函数内部有效.即函数内部的名字只在函数运行时才会创建,在函数运行之前或者运行完毕之后,所有的名字就都不存在了.所以,如果在函数外部使用函数内部定 ...
- python的相关基本操作
1.安装第三方库:pip install requests 2.升级:pip install --upgrade library_name 3.升级所有已安装的库: pip list --outdat ...