牛客练习赛13 --d(逆康拓展开)
2018-03-18 00:02
323 查看
题目链接:https://www.nowcoder.com/acm/contest/70/D
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<vector> #define ll long long const int mx = 55; //const int mod = 1e9 + 7; using namespace std; ll num[2049],ans; int add; static const int FAC[] = {1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880,3628800,39916800,479001600}; void da(ll x){ ll a = 1LL * 10 * x + 4, b = 1LL * 10 * x + 7; if(a > 1e10) return; da(a); num[add++] = a; // cout<<a<<endl; if(b > 1e10) return; da(b); num[add++] = b; // cout<<b<<endl; } ll cmp(int l, int r){ ll sum = 0; int le =lower_bound(num, num+add, l) - num; int ri =lower_bound(num, num+add, r) - num; if(num[ri] >r) ri--; //if() //cout<<le<<endl; sum = (ri - le + 1); // cout<<sum<<endl; return sum; } bool check(int x){ int a; while(x){ a = x %10; if(a == 4 || a ==7){ x /= 10; } else break; } if(x) return false; else return true; } void decantor(int x, int n, int qi) { x -=1; //最小从一开始 vector<int> v; // 存放当前可选数 // vector<int> a; // 所求排列组合 for(int i= 0;i<=n;i++) v.push_back(qi + i); for(int i=n;i>=1;i--) { int r = x % FAC[i-1]; int t = x / FAC[i-1]; x = r; sort(v.begin(),v.end());// 从小到大排序 // a.push_back(v[t]); // 剩余数里第t+1个数为当前位 if(check(v[t]) && check (qi + n - i)) ans++; v.erase(v.begin()+t); // 移除选做当前位的数 } } int main(){ ll n, k; add = 0; da(0); //add = 1022; sort(num, num + add); while(~scanf("%d%d",&n,&k)){ if(n < 13 &&k > FAC ){ puts("-1"); continue; } int cnt = lower_bound(FAC,FAC+13,k) - FAC; if(FAC[cnt] != k) cnt--; ans = 0; ans += cmp(1,n-cnt-1); decantor(k, cnt+1, n - cnt); cout<<ans<<endl; } return 0; }
相关文章推荐
- 牛客练习赛13-幸运数字I
- 牛客练习赛13 幸运数系列
- 牛客练习赛13-D-幸运数字Ⅳ(康托展开 & 逆康托展开)
- 牛客练习赛13 D 幸运数字Ⅳ[康拓展开逆运算]
- 牛客练习赛13
- 牛客练习赛13 --E(dp)
- 牛客练习赛13 A 幸运数字Ⅰ 【暴力】
- 【牛客练习赛13】 A B C D【康拓展开】 E【DP or 记忆化搜索】 F 【思维】
- 牛客练习赛13 E-龟兔跑步
- 牛客练习赛13--幸运数字I II III
- 牛客练习赛13
- 牛客练习赛13
- 牛客练习赛13 D 题 幸运数字IV 【康拓逆展开 + 思维】
- 牛客练习赛13 B 幸运数字Ⅱ 【暴力】【二分】
- 牛客练习赛13-B,幸运数字2
- 牛客练习赛13 D-幸运数字IV(逆康托展开)
- 牛客练习赛13-D:幸运数字Ⅳ(思维)
- 牛客练习赛13-C题幸运数字III
- 牛客练习赛13题目题解
- 牛客网 牛客练习赛13 - 幸运数字Ⅱ