codeforce 991E (组合数递归搜索)

本文介绍了一种算法,用于计算根据模糊观察得到的车牌号可能的真实情况数量。通过记录每个数字出现的次数,并采用递归方式遍历所有组合,利用组合数学原理计算合法车牌号的数量。

题目链接:点击打开链接

给出小明在迷糊的时候看到的车牌号数字,和真实数字相比:

①真实出现的数字,小明都看到了

②小明只可能把同一个数字看多,不可能看少

③车牌号没有前导零

问真实的数字有可能有多少种情况

先把每个数字出现的次数记录下来,这就是在搜索时每个数字取到的上限

从0到9遍历(相当于10层循环,在这里用递归的方式实现,每层循环的遍历次数即为该层数字出现的次数,代表取多少个这个数)

在最后一层统计用当前这些数字能组成合法数字的个数 ans=总共数字总和的阶乘/(∏每个数字出现次数的阶乘)

#include<iostream>
#include<cstdio>
#include<vector>
#include<set>
#include<map>
#include<string.h>
#include<cmath>
#include<algorithm>
#include<queue>
#include<stack>
#define LL long long
#define mod 1000000007
#define inf 0x3f3f3f3f
#define sqr(a) (a)*(a)
#define For(i,m,n) for(int i=m;i<=n;i++)
#define Dor(i,n,m) for(int i=n;i>=m;i--)
#define lan(a,b) memset(a,b,sizeof(a))
#define maxn 100010

using namespace std;

LL num[10];
LL fact[20];
LL nownum[10];
LL ans;

void init()
{
    lan(num,0);
    lan(fact,0);
    lan(nownum,0);
    fact[0]=1;
    for(int i=1;i<=19;i++)
        fact[i]=i*fact[i-1];
}

LL cal()
{
    LL tem=0;
    for(int i=0;i<=9;i++)
        tem+=nownum[i];
        //printf("%lld\n",tem);
    tem=fact[tem];
        //printf("%lld\n",tem);
    for(int i=0;i<=9;i++)
    {
        tem/=fact[nownum[i]];
    }
        //printf("%lld\n",tem);
    return tem;
}

void pou(int x)
{
   // printf("x=%d\n",x);
    if(x>9)
    {
        ans+=cal();
        //printf("ans=%lld\n",ans);
        if(nownum[0]>=1)
        {
            nownum[0]--;
            ans-=cal();
            nownum[0]++;
       // printf("ans=%lld\n",ans);
        }
        return;
    }
    if(num[x])
    {
        for(int i=1;i<=num[x];i++)
            nownum[x]=i,pou(x+1);
    }
    else
        pou(x+1);
}

int main()
{
    LL n;
    while(~scanf("%lld",&n))
    {
        init();
        ans=0;
        while(n)
        {
            num[n%10]++;
            n/=10;
        }
        pou(0);
        printf("%lld\n",ans);
    }
    return 0;
}

### Codeforces Problem or Contest 998 Information For the specific details on Codeforces problem or contest numbered 998, direct references were not provided within the available citations. However, based on similar structures observed in other contests such as those described where configurations often include constraints like `n` representing numbers of elements with defined ranges[^1], it can be inferred that contest or problem 998 would follow a comparable format. Typically, each Codeforces contest includes several problems labeled from A to F or beyond depending on the round size. Each problem comes with its own set of rules, input/output formats, and constraint descriptions. For instance, some problems specify conditions involving integer inputs for calculations or logical deductions, while others might involve more complex algorithms or data processing tasks[^3]. To find detailed information regarding contest or problem 998 specifically: - Visit the official Codeforces website. - Navigate through past contests until reaching contest 998. - Review individual problem statements under this contest for precise requirements and examples. Additionally, competitive programming platforms usually provide comprehensive documentation alongside community discussions which serve valuable resources when exploring particular challenges or learning algorithmic solutions[^2]. ```cpp // Example C++ code snippet demonstrating how contestants interact with input/output during competitions #include <iostream> using namespace std; int main() { int n; cin >> n; // Process according to problem statement specifics } ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值