Add Again(重复元素排序)

Add Again

Input: Standard Input

Output: Standard Output

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

/*
题意大概就是给你n个数,用这n个数能组成m个整数,求m个整数的和
这题知识点就一个就是有重复元素的全排列个数:
有k个元素,其中第i个元素有ni个,求全排列个数:
全排列个数=n!/(n1!*n2!*n3!*n4!....*nk!)
算出每个数出现在每个位置的次数,然后加起来就可以了
ps:这道题会卡long long 要用unsigned long long ,貌似uva以前的题经常有这种蛋疼的问题啊
*/
#include<stdio.h>
#include<string.h>
#define LL unsigned long long

int a[10];// 题目没有说很清楚,是0-9之间的数;
int b[15];
LL jie[15];
int N;
void init()
{
    jie[0]=1;
    for(LL i=1;i<15;i++)
    {
        jie[i]=i*jie[i-1];
    }
}

int main()
{
    freopen("Add.txt","r",stdin);

    LL ans,sum,tp;
    init();
    while(scanf("%d",&N),N)
    {
        sum=0;
        memset(a,0,sizeof(a));
        for(int i=0;i<N;i++)
        {
            int tp;
            scanf("%d",&tp);
            a[tp]++;
            sum+=tp;
        }
        ans=jie[N-1]*sum;
        for(LL i=0;i<10;i++)
        {
            if(a[i])
            ans/=jie[a[i]];

        }
        LL kk=0;
        for(int i=1;i<=N;i++)
        {
            kk=kk*10+ans;
        }
        printf("%llu\n",kk);
    }
    return 0;
}

对于第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[10];// 题目没有说很清楚,是0-9之间的数;
int b[15];
LL jie[15];
int N;
void init()
{
    jie[0]=1;
    for(LL i=1;i<15;i++)
    {
        jie[i]=i*jie[i-1];
    }
}
LL chsort(LL x)
{
    LL cnt=1;
    for(int i=0;i<10;i++)
    {
        if(a[i])
        {
            if(i==x)
                cnt*=jie[a[i]-1];
            else
                cnt*=jie[a[i]];
        }
    }
    return cnt;
}
int main()
{
   // freopen("Add.txt","r",stdin);

    LL ans,sum;
    init();
    while(scanf("%d",&N),N)
    {
        sum=0;
        memset(a,0,sizeof(a));
        for(int i=0;i<N;i++)
        {
            int tp;
            scanf("%d",&tp);
            a[tp]++;
 //           sum+=b[i];
        }
        ans=0;
 //       ans=jie[N-1]*sum;
        for(LL i=0;i<10;i++)
        {
            if(a[i])
            {
                ans+=jie[N-1]*i/chsort(i);
            }
        }
        LL kk=0;
        for(int i=1;i<=N;i++)
        {
            kk=kk*10+ans;
        }
        printf("%llu\n",kk);
    }
    return 0;
}
时间: 2024-08-06 20:05:26

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

UVA - 11076 Add Again (重复元素的排列)

Summation of sequence of integersis always a common problem in Computer Science. Rather than computing blindly,some intelligent techniques make the task simpler. Here you have to find thesummation of a sequence of integers. The sequence is an interes

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

很多时候排序是为了对数据进行归类,这种排序重复值特别多 通过年龄统计人口 删除邮件列表里的重复邮件 通过大学对求职者进行排序 若使用普通的快排对重复数据进行排序,会造成N^2复杂度,但是归并排序和三路快排就没有这样的问题. 归并排序对重复数据排序的比较在1/2NlgN和NlgN之间 三路快排 目标:将数据分成三个区间(3-way partitioning) lt和gt区间内的元素都和比较元素v相等 lt左边的元素都比v小 gt右边的元素都比v大 性能 三路快排的复杂度比普通快排小,主要取决于数据

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

先看下面一段代码: package 类集; import java.util.Set; import java.util.TreeSet; class Person{ private String name ; private int age ; public Person(String name,int age){ this.name = name ; this.age = age ; } public String gtoString(){ return "姓名:" + this.

Leetcode(无重复字符的最长子串;删除排序链表中的重复元素II;加一;最后一个单词的长度;相同的树)

1.无重复字符的最长子串 这题需要用到滑动窗口法,有许多问题都可以考虑使用滑动窗口法:https://www.geeksforgeeks.org/tag/sliding-window/ 因为用c++,所以用到set容器:std::count 2.删除排序链表中的重复元素II 3.加一 1 class Solution { 2 public: 3 vector<int> plusOne(vector<int>& digits) { 4 int n=digits.size()-

【LeetCode-面试算法经典-Java实现】【026-Remove Duplicates from Sorted Array(删除排序数组中的重复元素)】

[026-Remove Duplicates from Sorted Array(删除排序数组中的重复元素)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocate extra space for anot

【LeetCode-面试算法经典-Java实现】【082-Remove Duplicates from Sorted List II(排序链表中删除重复元素II)】

[082-Remove Duplicates from Sorted List II(排序链表中删除重复元素II)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list. For example, Given 1->2->

list双向链表容器应用基础(创建、遍历、插入、删除、归并、排序及连续重复元素剔除等)

不同于采用线性表顺序存储结构的vector和deque容器,list双向链表中任一位置的元素差值.插入和删除,都具有高效的常数阶算法时间复杂度O(1). 头文件 #include<list> 创建list对象 1)list();//创建一个没有任何元素的list对象. list<int>l 2)list(size_type n);//创建一个具有n个元素的list对象,每个元素采用它的类型下的默认值. list<int>l(10);//list对象l有10个元素,每个元

leetcode 题解:Remove Duplicates from Sorted Array II(已排序数组去三次及以上重复元素)

题目: Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For example,Given sorted array A = [1,1,1,2,2,3], Your function should return length = 5, and A is now [1,1,2,2,3]. 说明: 1)设个标志可实现 实现: 1 class Solution { 2 public

[leetcode] 83. 删除排序链表中的重复元素

83. 删除排序链表中的重复元素 链表操作 class Solution { public ListNode deleteDuplicates(ListNode head) { if (head == null) return head; if (head.next == null) return head; ListNode now = head.next; ListNode last = head; while (now != null) { while (now != null &&