Add Again(重复元素排序) UVA11076
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的更多相关文章
- 普林斯顿大学算法课 Algorithm Part I Week 3 重复元素排序 - 三路快排 Duplicate Keys
很多时候排序是为了对数据进行归类,这种排序重复值特别多 通过年龄统计人口 删除邮件列表里的重复邮件 通过大学对求职者进行排序 若使用普通的快排对重复数据进行排序,会造成N^2复杂度,但是归并排序和三路 ...
- python 重复元素排序
def counting_sort(array1, max_val): m = max_val + count = [] * m for a in array1: # count occurences ...
- 排序及重复元素去重的说明,TreeSet,HashSet
先看下面一段代码: package 类集; import java.util.Set; import java.util.TreeSet; class Person{ private String n ...
- java8 stream初试,map排序,list去重,统计重复元素个数,获取map的key集合和value集合
//定义一个100元素的集合,包含A-Z List<String> list = new LinkedList<>(); for (int i =0;i<100;i++) ...
- lintcode :Remove Duplicates from Sorted List 删除排序链表中的重复元素
题目: 删除排序链表中的重复元素 给定一个排序链表,删除所有重复的元素每个元素只留下一个. 您在真实的面试中是否遇到过这个题? 样例 给出1->1->2->null,返回 1-& ...
- leetcode-83.删除排序链表中的重复元素
leetcode-83.删除排序链表中的重复元素 Points 链表 题意 给定一个排序链表,删除所有重复的元素,使得每个元素只出现一次. 示例 1: 输入: 1->1->2 输出: 1- ...
- 【leetcode-82,83,26,80】 删除排序链表/数组中的重复元素
83. 删除排序链表中的重复元素 (1 pass) 给定一个排序链表,删除所有重复的元素,使得每个元素只出现一次. 示例 1: 输入: 1->1->2 输出: 1->2 示例 2: ...
- 26. Remove Duplicates from Sorted Array(删除排序数组中的重复元素,利用排序的特性,比较大小)
Given a sorted array, remove the duplicates in-place such that each element appear only once and r ...
- leetcode-217-Contains Duplicate(使用排序来判断整个数组有没有重复元素)
题目描述: Given an array of integers, find if the array contains any duplicates. Your function should re ...
随机推荐
- spring-data-redis和jedis版本对应收集总结
基于spring主版本为4.3.13.RELEASE的项目,测试以下对应版本可用. spring-data-redis版本 jedis版本 备注 1.5.2.RELEASE 2.7.3 1.6.0 ...
- Redis-02.数据类型
Redis中所有数据都是以key-value存储的,value支持的数据类型包括string.hash.list.set.sorted_set 数据类型 string 是redis最基本的类型,一个k ...
- 包建强的培训课程(10):Android插件化从入门到精通
@import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/c ...
- 字符编码那点事:快速理解ASCII、Unicode、GBK和UTF-8
原作者:阮一峰(ruanyifeng.com),现重新整理发布,感谢原作者的无私分享. 1.引言 今天中午,我突然想搞清楚 Unicode 和 UTF-8 之间的关系,就开始查资料. 这个问题比我想象 ...
- JDK设计模式之——策略模式(Comparable和Comparator接口)
策略模式:其实就是java的多态...父类引用指向子类对象. 使用策略模式,改善排序算法上文中需要排序的是一个数组 让他可以对任何类型的数组进行排序 1.利用 接口 Comparable<T&g ...
- 修改openstack用户配额
修改openstack用户配额 这是我在工作中遇到的一个很有趣的小问题,当时的场景是这样的: 公司的云产品要上线数据库服务(trove),因为每创建数据库实例都要占用一个虚拟机及相关资源的配额,尤其是 ...
- [Swift]Alamofire:设置网络请求超时时间【timeout】的两种方式
两种方式作用相同,是同一套代码的两种表述. 第一种方式:集聚. 直接设置成员属性(全局属性),这种方法不能灵活修改网络请求超时时间timeout. 声明为成员属性: // MARK: - 设置为全局变 ...
- Winginx nginx 启动提示80端口被占用
第一步:查看80端口占用信息 win键+R运行命令:cmd-->netstat -aon|findstr "80" 2.结束任务 找到 pin=4272这个进程,将进程结束 ...
- 机器学习入门 - Google的机器学习速成课程
1 - MLCC 通过机器学习,可以有效地解读数据的潜在含义,甚至可以改变思考问题的方式,使用统计信息而非逻辑推理来处理问题. Google的机器学习速成课程(MLCC,machine-learnin ...
- 你不知道的JavaScript --- 作用域相关
本篇是<你不知道的JavaScript>的读书笔记 什么是作用域? 程序离不变量,那么变量存储在哪里?程序需要时如何找到他们? 这些问题说明需要一套设计良好的规则来存储变量, 并且之后可以 ...