牛客练习赛13 --d(逆康拓展开)

本文详细解析了一道ACM竞赛中的复杂算法题,通过递归生成特定数字序列,并结合排列组合的方法来解决实际问题。文章重点介绍了核心算法的实现细节,包括数字序列的生成、条件检查、以及最终答案的计算过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接: 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[n]){
 		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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Blaze Jack

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值