CodeForces 300C Beautiful Numbers
枚举,组合数,逆元。
枚举$a$用了$i$个,那么$b$就用了$n-i$个,这个时候和$sum=a*i+b*(n-i)$,判断$sum$是否满足条件,如果满足,那么答案加上$C(n,i)$。
#include<cstdio> #include<iostream> #include<algorithm> #include<cstring> using namespace std; int n; long long a,b; long long mod=1e9+7; long long ans; long long f[1001000]; bool check(int x) { long long num=(long long)x*a +(long long)(n-x)*b; while(num) { if(num%10!=a&&num%10!=b) return 0; num=num/10; } return 1; } long long extend_gcd(long long a,long long b,long long &x,long long &y) { if(a==0&&b==0) return -1; if(b==0){x=1;y=0;return a;} long long d=extend_gcd(b,a%b,y,x); y-=a/b*x; return d; } long long mod_reverse(long long a,long long n) { long long x,y; long long d=extend_gcd(a,n,x,y); if(d==1) return (x%n+n)%n; else return -1; } long long C(int a,int b) { return f[a]*mod_reverse(f[b]*f[a-b],mod)%mod; } int main() { f[0]=1; for(long long i=1;i<=1000000;i++) f[i]=i*f[i-1]%mod; scanf("%lld%lld%d",&a,&b,&n); ans=0; for(int i=0;i<=n;i++) { if(check(i)) ans=(ans+C(n,i))%mod; } printf("%lld\n",ans); return 0; }