链接:https://ac.nowcoder.com/acm/problem/205078
来源:牛客网
题意:
求由{5、2、1、3、9}能够组成的所有数(可重复取)中,第 C(n, m)%1e9+7 (n>=m) 个数是多大?
思路:
打表可以找出规律,{1,2,3,5,9}组成的树中有
5
1
5^1
51个一位数,
5
2
5^2
52个两位数,
5
3
5^3
53个三位数……
1 2 3 5 9
11 12 13 15 19
21 22 23 25 29
31 32 33 35 39
51 52 53 55 59
91 92 93 95 99
111 112 113 115 119
121 122 123 125 129
是不是和5进制有点像
求第i个数就是求5进制中第i个数对{0,1,2,3,4……}到{1,2,3,5,9……}的映射
但是坑的是i的取值从1开始,而5进制是从0开始的。。。。。
大佬们的代码在进制转换的时候对 res=res-1 一下似乎就可以,学到了,记录下来慢慢消化。
/(ㄒoㄒ)/~~
AC代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod=1e9+7;
ll n,m,res,f[105],t;
char mp[5]={'1','2','3','5','9'};
ll qpow(ll a,ll b){
ll res=1,base=a;
while(b){
if(b&1) res = res*base%mod;
base = base*base%mod;
b>>=1;
}
return res;
}
ll C(int n,int m){
return f[n]*qpow(f[m],mod-2)%mod*qpow(f[n-m],mod-2)%mod;
}
int main(){
cin>>t;
f[0]=f[1]=1;
for(int i=2;i<=100;i++) f[i]=f[i-1]*i%mod;
while(t--){
cin>>n>>m;
res = C(n,m);
string ans="";
while(res>0){
--res;//因为第i个数i是从1开始的而不是0
ans+=mp[res%5];
res/=5;
}
reverse(ans.begin(),ans.end());
cout<<ans<<endl;
}
}