Codeforces 300C Beautiful Numbers lucas求组合数逆元

本文介绍了一个算法问题,即计算特定条件下由两个指定数字组成的长度为n的优秀数的数量,并给出了解决方案。优秀数需满足其各位数字之和也是由指定的两个数字组成。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

点击打开链接

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 representation of this integer only contains digits a and b. Vitaly calls a good number excellent, if the sum of its digits is a good number.

For example, let's say that Vitaly's favourite digits are 1 and 3, then number 12 isn't good and numbers 13 or 311 are. Also, number111 is excellent and number 11 isn't.

Now Vitaly is wondering, how many excellent numbers of length exactly n are there. As this number can be rather large, he asks you to count the remainder after dividing it by 1000000007 (109 + 7).

A number's length is the number of digits in its decimal representation without leading zeroes.

Input

The first line contains three integers: abn (1 ≤ a < b ≤ 9, 1 ≤ n ≤ 106).

Output

Print a single integer — the answer to the problem modulo 1000000007 (109 + 7).

Sample test(s)
input
1 3 3
output
1
input
2 3 10
output
165

给你两个数a,b在1~9之间,让这两个数组成长度为n的数,且各个位上的数之和为sum,要求sum各个位上的数也仅由a和b组成,问一共有多少种方式。

枚举每一位,然后来个排列组合即可。

乘法逆元:(a/b)%mod=a*(b^(mod-2))   mod为素数。

//92 ms	7808 KB
#include<stdio.h>
#define mod 1000000007
#define ll long long
ll f[1000007]={1,1};
ll a,b;
bool check(ll x)
{
    while(x)
    {
        ll z=x%10;
        if(z!=a&&z!=b)return false;
        x/=10;
    }
    return true;
}
ll quick_mod(ll x,ll y)
{
    ll ans=1;
    x%=mod;
    while(y)
    {
        if(y&1)ans=(ans*x)%mod,y--;
        y>>=1;
        x=x*x%mod;
    }
    return ans;
}
int main()
{
    int n;
    for(int i=2;i<=1000007;i++)f[i]=f[i-1]*i%mod;
    while(scanf("%lld%lld%d",&a,&b,&n)!=EOF)
    {
        ll ans=0;
        for(int i=0;i<=n;i++)
        {
            ll sum=i*a+(n-i)*b;
            if(check(sum))
            {
                ll c=f[n-i]*f[i];
                ans=(ans+f[n]*quick_mod(c,mod-2))%mod;
            }
        }
        printf("%lld\n",ans);
    }
    return 0;
}



### 如何在 Codeforces 上找到数学相关的题目 Codeforces 是一个非常受欢迎的在线编程竞赛平台,其中包含了大量涉及不同算法数据结构类型的题目。为了更高效地查找特定主题(如数学类)的相关题目,可以采用以下方法: #### 方法一:通过标签筛选 Codeforces 的每道题目都会被打上相应的标签,这些标签可以帮助用户快速定位到感兴趣的领域。对于数学类题目,可以通过访问 Codeforces 题目列表页面并利用过滤功能来实现目标[^1]。 具体步骤如下: 1. 打开 Codeforces 官网 (https://codeforces.com/) 并进入 **Problems** 页面。 2. 使用左侧栏中的 “Tags” 过滤器选择与数学相关的标签,例如 `math` 或者 `number theory`。 3. 应用过滤条件后即可看到符合条件的所有题目。 这种方法的优点在于简单直观,适合初学者或者希望浏览广泛范围内的数学题目的选手。 #### 方法二:借助第三方工具或脚本自动化检索 如果想要更加精确地获取某些难度级别下的数学问题,则可能需要编写一些辅助程序来进行批量处理。比如使用 Python 编写的网络爬虫可以从网站提取所需信息,并按照自定义标准分类整理[^3]。 下面展示一段用于抓取指定类别下样本输入输出的小型示例代码片段: ```python from bs4 import BeautifulSoup as soup import requests def fetch_codeforces_problem(tag='math', count=5): url = f'https://codeforces.com/problemset?tags={tag}' response = requests.get(url) page_soup = soup(response.text, "html.parser") problems = [] problem_entries = page_soup.select('.problems tr[data-index]') for entry in problem_entries[:count]: title = entry.find('a').text.strip() link = 'https://codeforces.com' + entry.find('a')['href'] # Fetch sample input/output if necessary. prob_response = requests.get(link) prob_page = soup(prob_response.text, "html.parser") inputs = prob_page.find_all(name="div", attrs={"class": "input"}) outputs = prob_page.find_all(name="div", attrs={"class": "output"}) samples = [ ("Input", "\n".join([str(pre.contents[0]) for pre in inp.find_all("pre")])), ("Output", "\n".join([str(pre.contents[0]) for pre in outp.find_all("pre")])) ] for inp,outp in zip(inputs,outputs)] problems.append({ 'Title':title, 'Link':link, 'Samples':samples }) return problems # Example usage: print(fetch_codeforces_problem()) ``` 此函数会返回前几个带有给定标签的问题及其基本详情,包括链接以及部分测试案例的数据表示形式。 #### 方法三:参与专题训练营或比赛 除了单独练习之外,还可以参加专注于某一技能提升的比赛活动,像 CF Round 中经常会有专门针对某种技巧设计的一系列挑战项目,在实战环境中锻炼自己的能力往往事半功倍[^2]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值