zoukankan      html  css  js  c++  java
  • Codeforces Round #181 (Div. 2) C. Beautiful Numbers 排列组合 暴力

    C. Beautiful Numbers

    题目连接:

    http://www.codeforces.com/contest/300/problem/C

    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 (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: a, b, n (1 ≤ a < b ≤ 9, 1 ≤ n ≤ 106).

    Output

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

    Sample Input

    1 3 3

    Sample Output

    1

    Hint

    题意

    给你 a,b,n

    问你有多少个长度为n的数中只含有a,b

    并且这个数的每一个数位之和的数,也只含有a,b

    题解:

    直接暴力枚举和就好了,然后check

    如果可行的话,他的贡献为C(n,i)

    C(n,i)用逆元来做就好了

    代码

    #include<bits/stdc++.h>
    using namespace std;
    const int mod = 1e9+7;
    #define maxn 1000005
    long long fac[maxn];
    int a,b,n;
    long long qpow(long long a,long long b)
    {
        long long ans=1;a%=mod;
        for(long long i=b;i;i>>=1,a=a*a%mod)
            if(i&1)ans=ans*a%mod;
        return ans;
    }
    long long C(long long n,long long m)
    {
        if(m>n||m<0)return 0;
        long long s1=fac[n],s2=fac[n-m]*fac[m]%mod;
        return s1*qpow(s2,mod-2)%mod;
    }
    int check(long long x)
    {
        while(x)
        {
            if(x%10!=a&&x%10!=b)return 0;
            x/=10;
        }
        return 1;
    }
    int main()
    {
        fac[0]=1;
        for(int i=1;i<maxn;i++)
            fac[i]=fac[i-1]*i%mod;
        scanf("%d%d%d",&a,&b,&n);
        long long ans = 0;
        for(int i=0;i<=n;i++)
        {
            long long num = a*i+b*(n-i);
            if(check(num))
                ans = (ans+C(n,i))%mod;
        }
        cout<<ans<<endl;
    }
  • 相关阅读:
    不使用循环使用递归得到数组的值得求和
    将int转int数组并将int数组元素处理后转int,实现加密
    小程序总结
    见过的最好的Layout讲解,挺全的
    (转)Java 的swing.GroupLayout布局管理器的使用方法和实例
    三十二、Java图形化界面设计——布局管理器之CardLayout(卡片布局)
    二十六、Jcreator使用初步
    The Java™ Tutorials下载地址
    中间容器
    二十七、Java图形化界面设计——容器(JFrame)
  • 原文地址:https://www.cnblogs.com/qscqesze/p/5118636.html
Copyright © 2011-2022 走看看