A. Arcade Game

ACM International Collegiate Programming Contest, Egyptian Collegiate Programming Contest (ECPC 2015)


A. Arcade Game
time limit per test
1.0 s
memory limit per test
1024 MB
input
standard input
output
standard output

Arcade mall is a new modern mall. It has a new hammer game called "Arcade Game". In this game you're presented with a number nwhich is hanged on a wall on top of a long vertical tube, at the bottom of the tube there is a button that you should hit with your hammer.

When you hit the button with all your force (as you always do), a ball is pushed all over the tube and hit the number n. The number n flies in the air and it's digits fall back in any random permutation with uniform probability.

If the new number formed is less than or equal to the previous number, the game ends and you lose what ever the new number is. Otherwise (if the number is greater than the previous number), you are still in the game and you should hit the button again.

You win if the new number formed is greater than the previous number and it is equal to the greatest possible permutation number.

Can you compute the probability of winning?

Input

The first line of the input contains the number of test cases T. Following that there are T lines represents T test cases. In each line, there is a single integer (1 ≤ n ≤ 109) the target number. The digits of n are all unique, which means that any 2 digits of n are different.

Output

For each test case, print one line containing the answer. Print the answer rounded to exactly 9 decimal digits.

Examples
input
3
952
925
592
output
0.000000000
0.166666667
0.194444444
Note

In the first test case, the answer is 0 because 952 is greater than all 2,5 and 9 permutations so you can't win, whatever you do.

In the second test case, the answer is 0.166666667 because you may win by getting number 952 with probability 1/6.

In the third test case the answer is 0.194444444 because you may win by getting number 952 in round1 with probability 1/6 or you can win by getting number 925 in round 1 and then 952 in round 2 with probability 1/6 * 1/6.

分析:这道题目的大意是,一个不大于10^9的数,它的各个位会被冲散然后重新排列(数据保证每个位都不同)。

1、每次冲散全排列后如果比当前的小,那么you lose;

2、冲散后排列得到了最大的那个数,you win;

3、大于等于当前数,进入下一轮。

解题思路:运用康拓展开式,按照字典序的顺序,直接算出前面有几个数比当前的要大,再打表即可(当然运用next_permutation 函数将数字排回去也不超时);


代码如下:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int x;
int data[10],str[10],judge[10];
int index;
int jie[10];
double LL[500005];
int cmp(int a,int b)
{
	return a>b;
}
void caljie()
{
	jie[0]=1;
	for(int i=1;i<10;i++)
		jie[i]=i*jie[i-1];
}
void cal()//将原数拆开正序放在str中,最大情况放在judge中 
{
	index=0;
	long long tem=x;
	while(tem>0)
	{
		data[index++]=tem%10;
		tem/=10;
	}
	for(int i=0;i<index;i++)
	{
		str[i]=data[index-1-i];
		judge[i]=data[i];
	}
	sort(judge,judge+index,cmp);
}
int f()//康拓展开计算 
{
	int ans=0;
	for(int i=0;i<index;i++)
	{
		int ss=0;
		for(int j=0;j<index;j++)
		{
			if(judge[j]!=-1 && judge[j]>str[i])
				ss++;
			else if(judge[j]==str[i])
			{
				judge[j]=-1;
				break;
			}
		}
		int tem=ss*jie[index-1-i];
		ans+=tem;
	}
	return ans;
}
void fs(int count)//打表 
{
	double ll=(double)(1.0/jie[index]);
	LL[0]=ll;
	for(int i=1;i<=count;i++)
	{
		LL[i]=LL[i-1]+ll*LL[i-1];
	}
}
int main()
{
	int t;
	int pos;
	caljie();
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d",&x);
		cal();
		pos=f();
		fs(pos);
		if(pos>0)
			printf("%.9f\n",LL[pos-1]);
		else
			printf("0.000000000\n");
	}
	return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值