组合数学 - 组合数的个数

组合数的个数 #

输入一个n,然后输入n个一位数,求这n个数组成的不重复出现的整数的总和。

 

Mean: 

 略

analyse:

 这样的数可以是1~n位,总共数的数目为:P(n,1)+p(n,2)+p(n,3)+.....+p(n,n)个。(其中p(n,m)表示从n个数中选m个数组成的排列的数目)。

若将这些数全部罗列出来再来求和,这不是一个好办法。其实我们可以将个位的和a1求出来,然后十位的和a2求出来,然后百位,然后千位......直到第n-1位。

那么最后的和就是:

sum=a1*1+a2*10^1+a3*10^2+a4*10^3.....an-1*10*n-2;

 

具体参看《组合数学.第三版》:P13

 

Time complexity:O(n^2)

 

Source code:

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
//Memory   Time
// 1347K   0MS
// by : Snarl_jsb
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<string>
#include<climits>
#include<cmath>
#define N 100010
#define LL long long
using namespace std;
 
LL n,sum,t;
LL a[5];
 
LL buff(LL s,LL e)
{
    LL res=1;
    for(LL i=s;i>=e;--i)
    {
        res*=i;
    }
    return res;
}
 
LL combination(LL n,LL m)
{
    LL t1=buff(n,n-m+1);
    return t1;
}
 
int main()
{
//    freopen("C:\\Users\\ASUS\\Desktop\\cin.txt","r",stdin);
//    freopen("C:\\Users\\ASUS\\Desktop\\cout.txt","w",stdout);
    while(~scanf("%I64d",&n))
    {
        sum=0;
        for(int i=1;i<=n;++i)
        {
            scanf("%I64d",&t);
            sum+=t;
        }
        LL tmp=1;
        a[0]=1;
        for(int i=1;i<n;i++)
        {
            a[i]=combination(n-1,i);
        }
        LL carry=1;
        LL ans=0;
        for(int i=0;i<n;++i)
        {
            LL tmp=0;
            for(int j=i;j<n;++j)
            {
                tmp+=a[j];
            }
            ans+=tmp*carry*sum;
            carry*=10;
        }
//        for(int i=0;i<n;i++)
//            cout<<a[i]<<endl;
        printf("%I64d\n",ans);
    }
    return 0;
}

  

posted @   北岛知寒  阅读(260)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
点击右上角即可分享
微信分享提示
主题色彩