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 ...
随机推荐
- 笔记本安装win10之后,无线网有问题
开机后,连接无线网络,过一段时间,就掉线了,然后再连接也连接不上,必须重启电脑才可以. 解决办法如下: 右键无线网络连接,状态,无线属性,安全,高级设置,勾上为此网络启用联邦信息标准兼容.
- 微信机器人 index
<?phprequire_once ('src/wechat.php');$wechat = new wechat();$act = isset($_GET['act'])?$_GET['act ...
- C#基础笔记
第一章: 1.C#创建程序的基本结构 class 类名 { static void Main(string[]args) { } } 注意:1)namespace2)using3)类名命名规则:字母. ...
- Android 图片高级绘图效果---高斯模糊
高斯模糊就是将指定像素变换为其与周边像素加权平均后的值,权重就是高斯分布函数计算出来的值.高斯模糊能够将图片制作成类似磨砂的图片效果,一般这些图片都用来作为背景. 目前使用到的是RenderScrip ...
- MySQL-8.0.11 在 Windows10 上的安装
下载 MySQL8.0 For Windows zip包下载地址:https://dev.mysql.com/downloads/file/?id=476233. 进入页面后可以不登录,点击底部“No ...
- 小程序开发基础-swiper 滑块视图容器
小编 / 达叔小生 参考官方文档:https://developers.weixin.qq.com/miniprogram/dev/component/ 小程序开发基础-swiper 滑块视图容器 根 ...
- Python - GUI(Graphical User Interface,图形用户界面)
Tkinter https://docs.python.org/3/library/tkinter.html Python自带的标准GUI库,可用于快速创建GUI应用程序,能够满足基本的GUI程序要求 ...
- python(leetcode)-48旋转图像
给定一个 n × n 的二维矩阵表示一个图像. 将图像顺时针旋转 90 度. 说明: 你必须在原地旋转图像,这意味着你需要直接修改输入的二维矩阵.请不要使用另一个矩阵来旋转图像. 示例 1: 给定 m ...
- Excel 斜线表头制作方法
Excel 斜线表头制作方法
- (转)Python__slots__详解
原文:https://www.cnblogs.com/rainfd/p/slots.html#top 摘要 当一个类需要创建大量实例时,可以通过__slots__声明实例所需要的属性, 例如,clas ...