UVA 11076 - Add Again

看题传送门

求各个数字(是数字哦,本来还想用map的)组成的排列的和。如1,2,3,可以<123>,<132>, <213>, <231>, <312>, <321> 和为1332。

 

如果直接枚举每个排列的话。那会TLE的感觉。

想想其他的办法。

先考虑个位数上的。每个数都有机会被安排到个位上去。

举个例子n=5,    2,2,2,3,4现将2排列到个位数,那么剩下的数的全排列为 4!/ (2!*1!*1!)

就是高中的东西啦。

将2 *全排列就可以得到 2在个位数上能得到不同数的和。

然后考虑到个位十位都是一样的,只不过大了10倍。

详见代码:

 

#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int MAXN=13;
int num[MAXN],len,cnt[MAXN];
typedef unsigned long long ull;
ull ans,A[MAXN],sum;

inline ull permulation(const int &n,const int &cur)
{
	ull temp=1;
	for(int i=0;i<len;i++)
	{
		if(i==cur) 
		{
			temp*=(A[ cnt[ num[i] ] -1 ]);
			continue;
		}
		temp*=A[ cnt[ num[i] ]  ];
	}
	return A[n-1]/temp;
}

inline void solve(const int &n)
{
	
	for(int i=0;i<len;i++)
	{
		ans=ans + num[i] * permulation(n,i);
	}
	ull t=ans;;
	for(int i=1;i<n;i++)
	{
		ans+=t*10;
		t*=10;
	}

}

int main()
{
	A[0]=1;
	for(int i=1;i<13;i++)
		A[i]=A[i-1]*i;
	int n;
	while(scanf("%d",&n),n)
	{		
		memset(cnt,0,sizeof(cnt));
		len=ans=sum=0;
		int temp;
		for(int i=0;i<n;i++)
		{
			scanf("%d",&temp);			
			if(cnt[temp]==0)
			{
				num[len++]=temp;
				sum+=temp;
			}
			cnt[temp]++;
		}
		solve(n);
		printf("%llu\n",ans);
	}

}


There were different types of fish in your aquarium. But they did not go along well with each other. So there had been Fish-War-1 among them. It was a complete mess. Lot of fishes died, many of them hid in some mountain, some were eaten by other fishes and so on. So you decided to compartmentalize your aquarium. You divided your aquarium into R x C grids, that is R rows and C columns. Then you inserted walls into each cell. The walls are slanted, that is it goes from north-east corner to south-west corner or north-west corner to south-east corner. They look like “/” or “\” respectively. Many years passed since the war. Now the fishes want to unite again. They want to bring down the walls. They measured the strength of each of the walls. What is the minimum amount of strength they need to spend to unite all the compartments? For example, in the following 2 x 2 grid, they can spend 7 + 9 + 12 = 28 unit strength to unite the 4 compartments. And this is the minimum. First line of the input contains number of test case T (<= 20). Hence follows T test cases. First line of the test case describes number of row R and number of columns C (1 <= R, C <= 100). Next R lines describe the walls. Each of these lines contains C characters and the characters are either “/” or “\”. Next R lines contain C positive integers, each describes the strength of the wall at the corresponding cell. The strength of a wall would be at most 10,000. For each test case output the case number and the minimum amount of strength to unite all the compartments in the aquarium. 输入样例 2 2 2 \/ \\ 7 10 12 9 1 3 /\\ 3 4 5 输出样例 Case 1: 28 Case 2: 12
最新发布
12-23
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值