Arcade Game Gym - 100814A

                          Arcade Game

  Gym - 100814A 


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 n which 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.

Example
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.


题意:商场有一个锤子游戏,当你用力砸下按钮时,会击飞n个数这n个数落下的概率相同,如果新生成的数大于旧的的数你则仍在游戏中,否则失败。

题目分析:题目给出一个数字n,问有多大的概率胜利即:952这几个数字的组合没有比他大,所以概率为0。925当你砸下按钮出现952时可以胜利,所以概率为出现952的概率1/6。592,你可以砸一下出现952或者925可以赢,还有就是当第一下砸出925继续第二下砸出952也会赢。

这个会用到康托展开计算当前这个数在全排列中是第几个。

#include<cstdio>
#include<cstring>

using namespace std;

double t,p;
long long n;
int T,sum,m,a[100]={1},b[100]={0},ans;

void aaa(){
	int i;
	for(i=1;i<11;i++){
		a[i]=i*a[i-1];	
	}	
}

int main()
{
	int d,count=0;
	scanf("%d",&T);
	aaa();
	while(T--){
		sum=0;
		ans=0;
		scanf("%lld",&n);
		int i=0;
		while(n){
			b[i++]=n%10;
			n/=10;
		}
		ans=a[i];
		p=1.0/ans;
	
		for(i=i-1;i>=0;i--){
			count=0;
			for(int j=0;j<i;j++){
				if(b[i]>b[j]){
					count++;
				}
			}
			sum=sum+a[i]*count;
		}
//		printf("%d\n",ans-sum-1);//前面有这些个数 
		m=ans-sum-1;
		t=0;
		for(i=0;i<m;i++){
			if(i==0)
				t=p;
			else
				t=t+t*p;
		}
		printf("%.9lf\n",t);
	}	
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值