HRY and Abbas

HRY and Abbas

题目描述:
In a parallel universe, HRY is the president of the Kassel Academy Student Union. Once he and the Lionheart Association President Abdullah Abbas performed a secret mission: to investigate the mixed blood drug trafficking group in country X.

However, an accident happened and they were caught together. They were forced to play Russian gambling roulette games!

The terrorists brought a revolver which works as follows: a revolver has n places 1,2,…,n to place bullet, and these n places forms a circle. The revolver has a position it is currently pointing at, and when pulling the trigger, the revolver will shoot out the bullet (if it has) at the current position and turn to the next position. The next position of 1≤i≤n−1 is i+1, and for n the next position is 1. Now the terrorist puts m bullets in the revolver, and turned the wheel of the revolver, which means the starting position is randomly chosen. He orders HRY and Abbas shoot at their heads in turn using the same revolver. If someone is hit by bullet, then the game is over.

HRY and Abbas are both not ordinary mixed blood, so the revolver can do no harm to them. However, they still decided to play the game. Now they want to know the probability of the game ending exactly after the ith shoot.
输入描述:
The first line is an integer T, indicating the number of test cases.

For each test case there are two lines :

The first line contains two integers n,m, and the second line contains m different positive integers ai,indicating the initial positions of bullets.

1=<T<=1000,1<=m<=n<=1e5,1<=ai<=n.

The sum of n does not exceed 1e6.
输出描述:

For each test case output n lines, the ith line indicates the probability of game ending exactly after the ith shoot. The probability is in the form of irreducible fraction p/q, which means the greatest common divisor of p and q is 1. If the answer is 0, output 0 directly.
输入样例:

2
10 2
1 3
10 4
1 2 6 10

输出样例:

1/5
1/5
1/10
1/10
1/10
1/10
1/10
1/10
0
0
2/5
1/5
1/5
1/5
0
0
0
0
0
0

题目大致意思是:

左轮手枪有1,2,…,n放置子弹,这些n个地方形成一个圆圈。左轮手枪有一个当前指向的位置,当拉动扳机时,左轮手枪将射击子弹(如果有)在当前位置并转向下一个位置。下一个位置1=<i<=n-1是i + 1,对于n,下一个位置是1.现在,恐怖分子在左轮手枪中放入m子弹,转动左轮手枪的轮子,这意味着随机选择起始位置。他命令HRY和Abbas依次使用相同的左轮手枪射击他们的头部。如果有人被子弹击中,那么游戏就结束了。

输出i行,第i行表示游戏在第i次射击后完全结束的概率

分析问题发现,第i次射击后结束,也即是说从某一位置开始到第i次射击的位置有子弹,并且起点到i前面的位置不能有子弹。
如果起点到i前面的位置有子弹,游戏会在前面的位置结束,不会在第i次射击的位置结束。由此问题就转换成了,对于任意位置,求该位置后面与它距离最近的有子弹的位置的距离。每一个距离i出现的次数就是经过i次射击能满足条件的情况个数。然后除以一共n种情况即可(起点的可能性n种)。
例如:

10 4
1 2 6 10
每一个位置有无子弹
1 1 0 0 0 1 0 0 0 1
每一个位置后距离他最近的距离
1 4 3 2 1 4 3 2 1 1
其中1出现四次,也就是说,有四个起点满足从该位置开始转1下就能结束游戏。

代码如下:

#include<iostream>
#include<cstring>
using namespace std;
int gcd(int a,int b){
	return b==0?a:gcd(b,a%b);
}
int main()
{
	int t;
	scanf("%d",&t);
	while(t--){
		int n,m;
		scanf("%d %d",&n,&m);
		int arr[n+5];
		int res[n+5];//res[i] i位置后第一个有子弹的位置与它的距离 
		int a;
		int min=-1;
		memset(arr,0,sizeof arr);
		memset(res,0,sizeof res);
		for(int i=0;i<m;i++){
			scanf("%d",&a);
			if(min<a)min=a;
			arr[a]=1;
		}
		for(int i=min-1;i>=1;i--){
			if(!arr[i+1])res[i]=res[i+1]+1;
			else res[i]=1;
		}
		int k=1;
		for(int i=1;i<=n;i++){
			if(arr[i])break;
			k++;
		}
		res[n]=k;
		for(int i=n-1;i>=min;i--){
			if(!arr[i+1])res[i]=res[i+1]+1;
			else res[i]=1;
		}
		int ans[n+5];//记录i次转动结束游戏的次数
		memset(ans,0,sizeof ans);
		for(int i=1;i<=n;i++){
			ans[res[i]]++;
		}
		for(int i=1;i<=n;i++){
			if(ans[i]){
				int t=gcd(ans[i],n);
				printf("%d/%d\n",ans[i]/t,n/t);
			}
			else printf("0\n");
		}
	}
	return 0;
} 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值