首页 > 其他 > 详细

洛谷 P2518 [HAOI2010]计数 (组合数)

时间:2019-01-05 15:40:14      阅读:168      评论:0      收藏:0      [点我收藏+]

题面

luogu

题解

本来想练数位dp的,结果又忍不住写了组合数..

去掉一个\(0\)可以看作把\(0\)移到前面去

那么题目转化为 \(n\)有多少个排列小于\(n\)

强制某一位比\(n\)的对应位置上的数小, 后面方案组合数算一下即可

Code


#include<bits/stdc++.h>

#define LL long long
#define RG register

using namespace std;
template<class T> inline void read(T &x) {
    x = 0; RG char c = getchar(); bool f = 0;
    while (c != '-' && (c < '0' || c > '9')) c = getchar(); if (c == '-') c = getchar(), f = 1;
    while (c >= '0' && c <= '9') x = x*10+c-48, c = getchar();
    x = f ? -x : x;
    return ;
}
template<class T> inline void write(T x) {
    if (!x) {putchar(48);return ;}
    if (x < 0) x = -x, putchar('-');
    int len = -1, z[20]; while (x > 0) z[++len] = x%10, x /= 10;
    for (RG int i = len; i >= 0; i--) putchar(z[i]+48);return ;
}
char s[55];
int a[55], b[10], C[55][55];

int main() {
    //freopen(".in", "r", stdin);
    //freopen(".out", "w", stdout);
    scanf("%s", s);
    int n = strlen(s);
    for (int i = 0; i < n; i++)
        a[i+1] = s[i]-'0', b[a[i+1]]++;
    LL ans = 0;
    for (int i = 0; i <= n; i++) C[i][i] = 1, C[i][0] = 1;
    for (int i = 2; i <= n; i++)
        for (int j = 1; j < i; j++)
            C[i][j] = C[i-1][j-1]+C[i-1][j];
    for (int i = 1; i <= n; i++) {
        for (int j = 0; j < a[i]; j++)
            if (b[j] > 0) {
                LL s = 1;
                b[j]--;
                for (int k = 0, p = n-i; k < 10; p -= b[k++])
                    s *= C[p][b[k]];
                b[j]++;
                ans += s;
            }
        b[a[i]]--;
    }
    write(ans);
    return 0;
}

洛谷 P2518 [HAOI2010]计数 (组合数)

原文:https://www.cnblogs.com/zzy2005/p/10224696.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!