Gym - 100814A Arcade Game(全排列)
2017-08-03 10:52
295 查看
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
output
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.
123
题意: 给你长度小于10的 数, 问 当前下 所有数字全排列 比他的 的概率是多少, 会叠加,
例如 592 比他大的有952 925 比925 大的有952 所有 概率为 1/6 + 1/6*1/6;
思路: 先 把分母 全排列总数 打表, 然后用 STL 里一个 全排列函数 next_permutation 求 下一个比他大的全排列; 返回bool型
(学习了)
#include <stdio.h>
#include <queue>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <string>
typedef long long ll;
using namespace std;
int perm[15]={1,1};
int a[15];
void init()
{
for(int i=1;i<=10;i++)
perm[i]=perm[i-1]*i;
}
int main()
{
//freopen("input.txt","r",stdin);
init();
int T;
cin>>T;
string str;
while(T--)
{
cin>>str;
int len=str.length();
for(int i=0;i<len;i++)
{
a[i]=str[i]-'0';
}
int cont=0;
while(next_permutation(a,a+len))
{
cont++;
}
if(cont==0)
{
printf("0.000000000\n");
continue;
}
else
{
double temp=1.0/perm[len];
double x,ans;
x=ans=temp;
for(int i=0;i<cont-1;i++)
{
ans=x+x*temp;
x=ans;
}
printf("%0.9f\n",ans);
}
}
}
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.
123
题意: 给你长度小于10的 数, 问 当前下 所有数字全排列 比他的 的概率是多少, 会叠加,
例如 592 比他大的有952 925 比925 大的有952 所有 概率为 1/6 + 1/6*1/6;
思路: 先 把分母 全排列总数 打表, 然后用 STL 里一个 全排列函数 next_permutation 求 下一个比他大的全排列; 返回bool型
(学习了)
#include <stdio.h>
#include <queue>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <string>
typedef long long ll;
using namespace std;
int perm[15]={1,1};
int a[15];
void init()
{
for(int i=1;i<=10;i++)
perm[i]=perm[i-1]*i;
}
int main()
{
//freopen("input.txt","r",stdin);
init();
int T;
cin>>T;
string str;
while(T--)
{
cin>>str;
int len=str.length();
for(int i=0;i<len;i++)
{
a[i]=str[i]-'0';
}
int cont=0;
while(next_permutation(a,a+len))
{
cont++;
}
if(cont==0)
{
printf("0.000000000\n");
continue;
}
else
{
double temp=1.0/perm[len];
double x,ans;
x=ans=temp;
for(int i=0;i<cont-1;i++)
{
ans=x+x*temp;
x=ans;
}
printf("%0.9f\n",ans);
}
}
}
相关文章推荐
- Gym - 100814A Arcade Game
- Gym10081 A - Arcade Game -康托展开、全排列、组合数变成递推的思想
- Gym 100814A---Arcade Game
- 【递推】【概率】Gym - 100814A - Arcade Game
- D - Collatz Gym - 101078D——数学
- Gym - 100269F Flight Boarding Optimization (dp+树状数组或线段树预处理)
- Gym - 101350G Snake Rana
- codeforce GYM 100741 A Queries
- Gym - 101246C dfs
- Gym 101086 M ACPC Headquarters : AASTMT (Stairway to Heaven)
- Gym - 101086L Chance 解答
- Gym - 101102C Bored Judge
- 【Gym - 101350 D - Magical Bamboos 】& gcd
- 【Gym - 101350A. Sherlock Bones】 & 思维
- Gym - 100543D Wheels 模拟|BFS|签到
- UVA 1394 And Then There Was One / Gym 101415A And Then There Was One / UVAlive 3882 And Then There Was One / POJ 3517 And Then There Was One / Aizu 1275 And Then There Was One (动态规划,思维题)
- [置顶] Gym 101190F Foreign Postcards
- Gym - 101158C Distribution Center 状态转移
- Gym - 101194D(二分
- 全排列的递归与非递归实现