[Codeforces Round #491 (Div. 2) E] Bus Number

洛谷传送门
Codeforces传送门

题目描述

This night wasn’t easy on Vasya. His favorite team lost, and he didn’t find himself victorious either — although he played perfectly, his teammates let him down every time. He had to win at least one more time, but the losestreak only grew longer and longer… It’s no wonder he didn’t get any sleep this night at all.

In the morning, Vasya was waiting the bus to the university on the bus stop. Vasya’s thoughts were hazy and so he couldn’t remember the right bus’ number quite right and got onto the bus with the number n n .

In the bus, Vasya thought that he could get the order of the digits in the number of the bus wrong. Futhermore, he could “see” some digits several times, but the digits he saw were definitely in the real number of the bus. For example, if Vasya saw the number 2028, it could mean that the real bus number could be 2028 2028 , 8022 8022 , 2820 2820 or just 820 820 . However, numbers 80 80 , 22208 22208 , 52 52 definitely couldn’t be the number of the bus. Also, real bus number couldn’t start with the digit 0 0 , this meaning that, for example, number 082 couldn’t be the real bus number too.

Given n n , determine the total number of possible bus number variants.

输入输出格式

输入格式:

The first line contains one integer n ( 1n1018 1 ≤ n ≤ 10 18 ) — the number of the bus that was seen by Vasya. It is guaranteed that this number does not start with 0 0 .

输出格式:

Output a single integer — the amount of possible variants of the real bus number.

输入输出样例

输入样例#1:

97

输出样例#1:

2

输入样例#2:

2028

输出样例#2:

13

说明

In the first sample, only variants 97 and 79 79 are possible.

In the second sample, the variants (in the increasing order) are the following: 208 208 , 280 280 , 802 802 , 820 820 , 2028 2028 , 2082 2082 , 2208 2208 , 2280 2280 , 2802 2802 , 2820 2820 , 8022 8022 , 8202 8202 , 8220 8220 .

题目大意

给你一个数, 这个数的数字构成和另一个数 A A 完全相同,并且可能出现比A次数更多。 不考虑前导零的情况, 求可能的 A A 的个数。

解题分析

首先我们一发DFS下去搜索出所有可能的数字组成情况, 因为数字个数不同,方案一定不同。

先不考虑前导零的情况,设这种情况下每个数字出现的次数分别为 cnt[0],cnt[1],,cnt[9] c n t [ 0 ] , c n t [ 1 ] , ⋯ , c n t [ 9 ] , 总数量为 tot t o t 。 如果不考虑同一数字出现多次, 显然排列方案为 tot! t o t ! 。 那么有重复的情况, 对于每个数字来说就会出现 cnt[i]! c n t [ i ] ! 的等价情况, 我们除掉就好了。 最后因为没有前导零, 而第一个数字是 0 0 的情况占总情况的cnt[0]tot, 减去这部分即可得到当前数字组成情况的方案数。

代码如下:

#include <cstdio>
#include <cstring>
#include <cmath>
#include <cctype>
#include <algorithm>
#include <cstdlib>
#define R register
#define IN inline
#define W while
#define gc getchar()
#define ll unsigned long long
int cnt[10], temp[10];
ll fac[20], ans;
char buf[20];
void DFS(R int now)
{
    if(now == 10)
    {
        int tot = 0; ll ret;
        for (R int i = 0; i <= 9; ++i) tot += temp[i];
        ret = fac[tot]; if(!tot) return;
        for (R int i = 0; i <= 9; ++i) ret /= fac[temp[i]];
        if(temp[0]) ret -= ret * temp[0] / tot;
        ans += ret; return;
    }
    for (R int i = 1; i <= cnt[now]; ++i)
    {
        temp[now] = i;
        DFS(now + 1);
    }
    if(!temp[now]) DFS(now + 1);
}
int main(void)
{
    fac[0] = 1;
    for (R int i = 1; i <= 19; ++i) fac[i] = fac[i - 1] * i;
    scanf("%s", buf); int len = std::strlen(buf);
    for (R int i = 0; i < len; ++i) cnt[buf[i] - 48]++;
    DFS(0); printf("%I64u", ans);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值