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 ...
随机推荐
- Android-Java-普通类与抽象类(覆盖)&方法重载
覆盖都是子类与父类之间 & 接口与实现类之间 才会产生:覆盖 有很多名称,覆盖,复写,重写 都是一个意思: 注意:重载都是方法之间 方法同名 不同参数,就属于重载: 普通类-覆盖: 描述An ...
- C# WebSocket Fleck 调用非托管C++ DLL 实现通信(使用stringbuilder接收)
[DllImport(@"XXX.dll", CallingConvention = CallingConvention.StdCall)]public static exter ...
- 目标检测算法(1)目标检测中的问题描述和R-CNN算法
目标检测(object detection)是计算机视觉中非常具有挑战性的一项工作,一方面它是其他很多后续视觉任务的基础,另一方面目标检测不仅需要预测区域,还要进行分类,因此问题更加复杂.最近的5年使 ...
- vs2012升级vs2017后的一些坑
异常信息:未能加载文件或程序集"System.Web.Helpers... 未能加载文件或程序集"System.Web.Helpers, Version=2.0.0.0, Cult ...
- 基于kNN的手写字体识别——《机器学习实战》笔记
看完一节<机器学习实战>,算是踏入ML的大门了吧!这里就详细讲一下一个demo:使用kNN算法实现手写字体的简单识别 kNN 先简单介绍一下kNN,就是所谓的K-近邻算法: [作用原理]: ...
- 微信小程序实现显示和隐藏控件-头像-取值-bindblur事件
微信小程序实现显示和隐藏控件 .wxml: <view class=" {{showOrHidden?'show':'hidden'}}"></view> ...
- linux下 几个常用makefile模板,亲测可用
一 生成动态链接库的模板: ####################### # Makefile ####################### # compile and lib parameter ...
- ionic3 ionic serve build fail Error: webpackJsonP is not defined
ionic升级后发现 ionic serve 跑起来项目出现一下错误: Runtime Error: webpackJsonP is not definedStack: @http://localho ...
- .Net 并发写入文件的多种方式
1.简介 本文主要演示日常开发中利用多线程写入文件存在的问题,以及解决方案,本文使用最常用的日志案例! 2.使用File.AppendAllText写入日志 这是种常规的做法,通过File定位到日志文 ...
- mysql 开发基础系列19 触发器
触发器是与表有关的数据库对象,触发器只能是针对创建的永久表,而不能是临时表. 1.1 创建触发器 -- 语法: CREATE TRIGGER trigger_name trigger_time tri ...