2018.10.22-ssoi3979荔枝丹(litchi)

算法:搜索?????

思路:

经计算,共仅有C(27,9)=4686824种情况会对答案造成贡献,于是我们暴力枚举所有情况,再暴力在l,r中按位数f(枚举到的位数,bool 是否在左边界,bool是否在右边界)来爆搜,效率不会算玄学。

代码:

#include<bits/stdc++.h>
#define il inline
#define LL long long
#define _(d) while(d(isdigit(ch=getchar())))
using namespace std;
LL l,r,tmp,tt,c[12],lb[20],rb[20];int ans;bool pd;
il LL read(){LL x;char ch;_(!);x=ch-48;_()x=(x<<1)+(x<<3)+(ch^48);return x;}
int f(int x,bool lpd,bool rpd){
    if(x==0)return 1;if(lpd==0&&rpd==0)return 1;
    if(lpd&&rpd&&lb[x]==rb[x]){if(!c[lb[x]])return 0;c[lb[x]]--;int res=f(x-1,1,1);c[lb[x]]++;return res;}
    if(lpd&&rpd){
        for(int i=lb[x]+1;i<=rb[x]-1;i++){
            if(!c[i])continue;c[i]--;int res=f(x-1,0,0);c[i]++;
            if(res>0)return 1;
        }
        if(c[lb[x]]){c[lb[x]]--;int res=f(x-1,1,0);c[lb[x]]++;if(res>0)return 1;}
        if(c[rb[x]]){c[rb[x]]--;int res=f(x-1,0,1);c[rb[x]]++;if(res>0)return 1;}return 0;
    }
    if(lpd){
        if(c[lb[x]]){c[lb[x]]--;int res=f(x-1,1,0);c[lb[x]]++;if(res>0)return 1;}
        for(int i=lb[x]+1;i<=9;i++){
            if(!c[i])continue;c[i]--;int res=f(x-1,0,0);c[i]++;
            if(res>0)return 1;
        }return 0;
    }
    if(rpd){
        if(c[rb[x]]){c[rb[x]]--;int res=f(x-1,0,1);c[rb[x]]++;if(res>0)return 1;}
        for(int i=0;i<=rb[x]-1;i++){
            if(!c[i])continue;c[i]--;int res=f(x-1,0,0);c[i]++;
            if(res>0)return 1;
        }return 0;
    }
    for(int i=0;i<=9;i++){
        if(!c[i])continue;c[i]--;int res=f(x-1,0,0);c[i]++;
        if(res>0)return 1;
    }return 0;
}
void dfs(int x,int now){
    if(x==tt+1){if(pd&&c[0]==tt-1&&c[1]==1)ans++;else if(c[0]!=tt)ans+=f(tt,1,1);return;}
    for(int j=now;j<=9;j++){c[j]++;dfs(x+1,j);c[j]--;}
}
int main()
{
    l=read();r=read();if(r==1e18)pd=1,r--;tmp=l;while(tmp)lb[++tt]=tmp%10ll,tmp/=10ll;
    tt=0;tmp=r;while(tmp)rb[++tt]=tmp%10ll,tmp/=10ll;
    dfs(1,0);printf("%d
",ans);
  return 0;
}
View Code

(测试,总方案数算错,总体爆炸心痛唉...)

原文地址:https://www.cnblogs.com/Jessie-/p/9830450.html