Codeforces 300C Beautiful Numbers (数论)

本文解决了一个复杂的数学问题,即计算长度为n的优秀数的数量,其中优秀数的定义基于两个特定数字a和b,且其各位数字之和也只包含a和b。文章详细介绍了如何使用组合数学和模运算来解决这个问题,包括快速幂、阶乘预处理、检查数字和以及计算组合数等关键步骤。

原题传送门


题面:

Beautiful Numbers

                             time limit per test : 2 seconds
                           memory limit per test : 256 megabytes
                                   input : standard input
                                  output : standard output

Problem Description

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, number 111 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 (1e9 + 7).

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

Input

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

Output

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

Sample Input (1)

1 3 3

Sample Output (1)

1

Sample Input (2)

2 3 10

Sample Output (2)

165

题面描述

Vitaly对a和b这两个数字很喜欢,所以他认为每个位都由a或b的数字是good的,而当这些位上的数字的和各位上的数是a或者b中的一个时,他就会认为这个数是excellent的,现在他想知道位数为n的数中有多少个数是excellent的.(例如原题面中 , a = 1, b = 3 , 111 是good的, 而1+1+1=3 == b,所以111是excellent)

题目分析

因为Vitaly认为每个位上仅是a或b的数是good的,并在good的基础上才会出现excellent.所以可以先用a和b构造出一个位数为n的数,那么这个数必定是good的,由于对于每个位置上,既可以是a,也可以是b.那么对于由i个a,n-i个b构成的数就会有C(i , n)个.(i = 0~n)因为n很大,而且i是从0到n的,不能用杨辉三角递推出C(i , n).那么就要用阶乘来求C(i , n)了,又因为要取模,对于除法取模就要计算逆元.这题的模数是一个素数,所以可以用费马小定理来求出逆元(而不需要用欧几里得拓展).

具体代码

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
typedef long long int ll;
const int MOD = 1e9+7;
const int maxn = 1e6+5;
ll fact[maxn];
ll a, b, n;

ll qpow(ll x, ll n, ll mod)//快速幂
{
    x = x%mod;
    ll ans = 1;
    while(n)
    {
        if(n&1)
        {
            ans = ans*x%mod;
        }
        x = x*x%mod;
        n >>= 1;
    }
    return ans;
}

void pre_fact()//阶乘
{
    fact[0] = 1;
    for(ll i = 1; i <= maxn-5; i++)
    {
        fact[i] = fact[i-1]*i%MOD;
    }
}

bool check_sum(ll x)
{
    while(x)
    {
        if(x%10 != (ll)a && x%10 != (ll)b)//当某一位不是a或b时,check失败
        {
            return false;
        }
        x /= 10;
    }
    return true;
}

ll C(ll i, ll j)
{
    ll temp = fact[i]*fact[j-i]%MOD;
    return fact[j]*qpow(temp , MOD-2 , MOD)%MOD;//费马小定理求逆元
}

int main()
{
    pre_fact();//预处理出阶乘
    cin >> a >> b >> n ;
    ll ans = 0;
    for(ll i = 0; i <= n; i++)
    {
        if(check_sum(i*a+(n-i)*b))//
        {
            ans = (ans+C(i , n)%MOD)%MOD;
        }
    }
    cout << ans << endl;
    return 0;
}
内容概要:本文系统介绍了算术优化算法(AOA)的基本原理、核心思想及Python实现方法,并通过图像分割的实际案例展示了其应用价值。AOA是一种基于种群的元启发式算法,其核心思想来源于四则运算,利用乘除运算进行全局勘探,加减运算进行局部开发,通过数学优化器加速函数(MOA)和数学优化概率(MOP)动态控制搜索过程,在全局探索与局部开发之间实现平衡。文章详细解析了算法的初始化、勘探与开发阶段的更新策略,并提供了完整的Python代码实现,结合Rastrigin函数进行测试验证。进一步地,以Flask框架搭建前后端分离系统,将AOA应用于图像分割任务,展示了其在实际工程中的可行性与高效性。最后,通过收敛速度、寻优精度等指标评估算法性能,并提出自适应参数调整、模型优化和并行计算等改进策略。; 适合人群:具备一定Python编程基础和优化算法基础知识的高校学生、科研人员及工程技术人员,尤其适合从事人工智能、图像处理、智能优化等领域的从业者;; 使用场景及目标:①理解元启发式算法的设计思想与实现机制;②掌握AOA在函数优化、图像分割等实际问题中的建模与求解方法;③学习如何将优化算法集成到Web系统中实现工程化应用;④为算法性能评估与改进提供实践参考; 阅读建议:建议读者结合代码逐行调试,深入理解算法流程中MOA与MOP的作用机制,尝试在不同测试函数上运行算法以观察性能差异,并可进一步扩展图像分割模块,引入更复杂的预处理或后处理技术以提升分割效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值