UPC 2017 Summer Training 1

来源:互联网 发布:联合智业怎么样知乎 编辑:程序博客网 时间:2024/05/19 03:24

A - Arcade Game

 

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
3952925592
Output
0.0000000000.1666666670.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.


题目大概是:求给你的这个数的全排列中大于当前数。本来以为简单的大于的数除以全排列的数就搞定,看了第三个实例的给的解释,发现自己想简单了,除了第一次就拿到最大的除外,还可以拿一个比当前的数值大一点的,然后下一次拿一个更大一点的,直到你拿到最大的那个数了。
自己打全排列肯定超时,毕竟数值到了十的九次方,所以用到了C++STL中全排列函数next_permutation
 C++STL中全排列函数next_permutation连接:点击打开链接
#include<stdio.h>#include <algorithm>#include<string.h>using namespace std;int main(){int b[15]={0,1,2,6,24,120,720,5040,40320,362880,3628800};//先将前十个打个表int a[15];char c[15];int n,y,cont;double sum,q,p;while(scanf("%d",&n)!=EOF){while(n--){scanf("%s",&c);cont=0;int l=strlen(c);for(int i=0;i<l;i++){a[i]=c[i]-'0';}do{cont++;}while(next_permutation(a,a+l));  cont--;//这样会包括等于的情况 减去sum=1.0/b[l];if(cont==0)//如果没有比他大的排序printf("0.000000000\n");else{p=q=sum;for(int i=0;i<cont-1;i++)// 此处看下边的解释{p=p+p*sum;}printf("%.9f\n",p);}}}return 0;} 
PS:  第一次取比给的数大一点的数值,第二次的话就是第一次取的那个数概率*比第一次取的数大一点的概率+直接取这个比第一次大一点的概率~~~~~~~~~~一直到后来,画画,想想就行了~主要是C++的那个函数很给力。