Add Again

Summation of sequence of integers is always a
common problem in Computer Science. Rather than computing blindly, some
intelligent techniques make the task simpler. Here you have to find the
summation of a sequence of integers. The sequence is an interesting one and it
is the all possible permutations of a given set of digits. For example, if the
digits are <1 2 3>, then six possible permutations are <123>,
<132>, <213>, <231>, <312>, <321> and the sum of
them is 1332.

Input

Each input set will start with a positive
integerN (1≤N≤12). The next line will contain N decimal digits. Input will be
terminated by N=0. There will be at most 20000 test set.

Output

For each test set, there should be a one line
output containing the summation. The value will fit in 64-bit unsigned
integer.

Sample
Input                               Output for Sample Input

3

1 2 3

3

1 1 2

0


1332

444


Problemsetter: Md. Kamruzzaman

Special Thanks: Shahriar Manzoor

思路:对于第x位,一共有k个元素,其中第i个元素有ni个,求全排列个数:全排列个数=(n-1)!/(n1!*n2!*n3!*n4!..(ni-1)!..*nk!)算出每个数出现在每个位置的次数,然后乘以i加起来就是i元素的贡献值了

转载请注明出处:寻找&星空の孩子

 #include<stdio.h>
#include<string.h>
#define LL unsigned long long int a[];// 题目没有说很清楚,是0-9之间的数;
int b[];
LL jie[];
int N;
void init()
{
jie[]=;
for(LL i=;i<;i++)
{
jie[i]=i*jie[i-];
}
}
LL chsort(LL x)
{
LL cnt=;
for(int i=;i<;i++)
{
if(a[i])
{
if(i==x)
cnt*=jie[a[i]-];
else
cnt*=jie[a[i]];
}
}
return cnt;
}
int main()
{
// freopen("Add.txt","r",stdin); LL ans,sum;
init();
while(scanf("%d",&N),N)
{
sum=;
memset(a,,sizeof(a));
for(int i=;i<N;i++)
{
int tp;
scanf("%d",&tp);
a[tp]++;
// sum+=b[i];
}
ans=;
// ans=jie[N-1]*sum;
for(LL i=;i<;i++)
{
if(a[i])
{
ans+=jie[N-]*i/chsort(i);
}
}
LL kk=;
for(int i=;i<=N;i++)
{
kk=kk*+ans;
}
printf("%llu\n",kk);
}
return ;
}

Add Again(重复元素排序) UVA11076的更多相关文章

  1. 普林斯顿大学算法课 Algorithm Part I Week 3 重复元素排序 - 三路快排 Duplicate Keys

    很多时候排序是为了对数据进行归类,这种排序重复值特别多 通过年龄统计人口 删除邮件列表里的重复邮件 通过大学对求职者进行排序 若使用普通的快排对重复数据进行排序,会造成N^2复杂度,但是归并排序和三路 ...

  2. python 重复元素排序

    def counting_sort(array1, max_val): m = max_val + count = [] * m for a in array1: # count occurences ...

  3. 排序及重复元素去重的说明,TreeSet,HashSet

    先看下面一段代码: package 类集; import java.util.Set; import java.util.TreeSet; class Person{ private String n ...

  4. java8 stream初试,map排序,list去重,统计重复元素个数,获取map的key集合和value集合

    //定义一个100元素的集合,包含A-Z List<String> list = new LinkedList<>(); for (int i =0;i<100;i++) ...

  5. lintcode :Remove Duplicates from Sorted List 删除排序链表中的重复元素

    题目: 删除排序链表中的重复元素 给定一个排序链表,删除所有重复的元素每个元素只留下一个.   您在真实的面试中是否遇到过这个题? 样例 给出1->1->2->null,返回 1-& ...

  6. leetcode-83.删除排序链表中的重复元素

    leetcode-83.删除排序链表中的重复元素 Points 链表 题意 给定一个排序链表,删除所有重复的元素,使得每个元素只出现一次. 示例 1: 输入: 1->1->2 输出: 1- ...

  7. 【leetcode-82,83,26,80】 删除排序链表/数组中的重复元素

    83. 删除排序链表中的重复元素 (1 pass) 给定一个排序链表,删除所有重复的元素,使得每个元素只出现一次. 示例 1: 输入: 1->1->2 输出: 1->2 示例 2: ...

  8. 26. Remove Duplicates from Sorted Array(删除排序数组中的重复元素,利用排序的特性,比较大小)

      Given a sorted array, remove the duplicates in-place such that each element appear only once and r ...

  9. leetcode-217-Contains Duplicate(使用排序来判断整个数组有没有重复元素)

    题目描述: Given an array of integers, find if the array contains any duplicates. Your function should re ...

随机推荐

  1. Linux下MySQL数据库的安装

    记录详细过程以备使用 1.创建群组及用户 obd:~ # groupadd mysql obd:~ # useradd -g mysql mysql 2.创建相关目录 obd:~ # mkdir -p ...

  2. 背水一战 Windows 10 (120) - 后台任务: 后台上传任务

    [源码下载] 背水一战 Windows 10 (120) - 后台任务: 后台上传任务 作者:webabcd 介绍背水一战 Windows 10 之 后台任务 后台上传任务 示例演示 uwp 的后台上 ...

  3. JavaScript大厦之JS运算符

    运算符用于执行程序代码运算,会针对一个及以上操作数项目来进行运算.2+3,其操作数是2和3,而运算符则是“+”.上一篇我们说过变量用来存储数据,而同一个变量中的数据在不同的时刻可以不同,在程序的运行过 ...

  4. Android JNI 学习(二):JNI 设计机制

    本章我们重点说明以下JNI设计的问题,本章中提到的大多数设计问题都与native方法有关.至于调用相关的API的设计,我们会在后面进行介绍. 一.JNI接口函数和指针 native 代码通过调用JNI ...

  5. 腾迅云获取免费SSL证书并布置

    上次申请了SSL证书一直没时间布置,今天重新再来操作一次 首先需要申请SSL证书,腾迅云买的域名有免费一年的SSL证书申请,网址:https://console.cloud.tencent.com/s ...

  6. 全栈开发工程师微信小程序-中(下)

    全栈开发工程师微信小程序-中(下) 微信小程序视图层 wxml用于描述页面的结构,wxss用于描述页面的样式,组件用于视图的基本组成单元. // 绑定数据 index.wxml <view> ...

  7. itext实现pdf自动定位合同签订

    需求 需要实现如下效果(最终效果) 思考 需求方的要求就是实现签订合同,实现方法不限,但过程中又提出需要在签章的过程中把签订日期的文字也打上去,这就有点坑了~ 一开始的想法是想办法定位需要签名的位置, ...

  8. DCT(离散余弦变换)算法原理和源码(python)

    原理: 离散余弦变换(DCT for Discrete Cosine Transform)是与傅里叶变换相关的一种变换,它类似于离散傅里叶变换(DFT for Discrete Fourier Tra ...

  9. python数据抓取分析(python + mongodb)

    分享点干货!!! Python数据抓取分析 编程模块:requests,lxml,pymongo,time,BeautifulSoup 首先获取所有产品的分类网址: def step(): try: ...

  10. 安卓之必须了解的实时通信(Socket)

    Socket: 有服务器和客户端之分,其是对TCP/IP的封装,使用IP地址加端口,确定一个唯一的点.在Internet上的主机一般运行了多个服务软件,同时提供几种服务.每种服务都打开一个Socket ...