Concise and clear CodeForces - 991F(dfs 有重复元素的全排列)

就是有重复元素的全排列

 

#include <bits/stdc++.h>
#define mem(a, b) memset(a, b, sizeof(a))
using namespace std;
typedef long long LL;
const int maxn = 10010, INF = 0x7fffffff;
char str[maxn];
int vis[maxn], v[maxn];
LL num[maxn];
LL res = 0;
void init()
{
    num[0] = 1;
    for(int i=1; i<maxn; i++)
        num[i] = num[i-1] * i;
}
void dfs(LL x, LL cnt)
{
    if(cnt == 10)
    {
        if(x > 0) return;
        LL ans1 = 1, ans2 = 0, ans3 = 1;
        for(int i=0; i<=9; i++)
        {
            ans2 += v[i];
        }
        LL temp = ans2;
        ans2 = num[ans2];
        for(int i=0; i<=9; i++)
            ans2 /= num[v[i]];
    //    cout<< ans2 <<endl;
        if(v[0])
        {
            ans3 = num[temp-1];
            for(int i=0; i<=9; i++)
                if(i == 0) ans3 /= num[v[i]-1];
                else ans3 /= num[v[i]];

            ans2 -= ans3;
        }

        res += ans2;
        return;
    }
    if(!vis[cnt])
        dfs(x, cnt+1);
    else
        for(int i=0; i<vis[cnt] && i<=x; i++)
        {
            v[cnt] += i;
            dfs(x-i, cnt+1);
            v[cnt] -= i;
        }
}
int main()
{
    init();
    cin>> str;
    int len = strlen(str);
    int maxx = -INF;
    LL sum = 0, ans = 0, flag = 0;
    for(int i=0; i<len; i++)
    {
        if(i != 0)
            if(str[i] != str[i-1])
                flag = 1;
        vis[str[i]-'0']++;
        if(vis[str[i]-'0'] == 1) ans++;
    }
    if(!flag)
    {
        cout<< len <<endl;
        return 0;
    }
    res += num[ans];
    if(vis[0])
        res -= num[ans-1];
    for(int i=0; i<=9; i++)
        if(vis[i])
            sum += vis[i]-1;
    for(int i=1; i<=sum; i++)
    {
        for(int i=0; i<=9; i++)
            if(vis[i])
                v[i] = 1;
            else
                v[i] = 0;
        dfs(i, 0);

    }
    cout<< res <<endl;

    return 0;
}

 

转载于:https://www.cnblogs.com/WTSRUVF/p/9556318.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值