Fighting Monsters

**

Fighting Monsters(German Collegiate Programming Contest 2018)

**

Emma just discovered a new card game called Gwint: A wizard’s game. There are two types of cards: monster cards and spell cards. Monster cards are used to score points, while spell cards typically interact with the monsters in some way.

On each monster card there is an integer value, the power of the monster. Monsters can fight each other, and during these fights the power acts as both the strength and the health of the monster. The monsters take turns hitting each other until one of them dies. Whenever a monster AA hits a monster BB, this causes BB to lose an amount of power equal to the power of AA. Conversely, if BB hits AA, AA loses power equal to the power of BB (see the example below). This continues until one of the two monsters has a power of zero or less, at which point this monster is considered dead.

One of Emma’s most beloved cards in the game is a spell called Fight! which states:

Pick two monsters. They fight each other to the death. If the surviving monster has a power of exactly 11 left, return this card to your hand.

Of course, Emma would like to play as efficiently as possible by picking two monsters such that Fight! is returned to her hand. However, there are often a lot of monsters on the board, which makes it very time consuming to figure out whether this can be done or not. Can you help her find two monsters she can pick so that she gets the card back?

Input Format
The input consists of:one line with an integer nn (2 \le n \le 10^5)(2≤n≤10 5), the number of monsters;one line with nn integers m_1, \cdots, m_nm 1 ,⋯,m n(1 \le m_i \le 10^6)(1≤m i ≤10 6 ), giving the power of each monster.

Output Format
If there is no pair of monsters that Emma can pick, output impossible. Otherwise, output two distinct integers ii, jj (1 \le i, j \le n)(1≤i,j≤n), where ii is the index of the monster that starts the fight and jj is the index of the other monster. If multiple solutions exist, print the minimum pair(i,j)(i,j).

样例输入1
4
1 12 67 8
样例输出1
impossible
样例输入2
5
1 1 12 67 8
样例输出2
2 1
样例输入3
6
1 5 6 7 90 8
样例输出3
2 6

题意

有n个怪兽,每个怪兽有一个数值ai,如果该怪兽攻击另一个怪兽那么另一个怪兽的数值aj会减少ai,低于0的怪兽死亡。问你给出的n只怪兽,能否选其中两只,使得两只怪兽相互攻击后一只死亡,一只的数值只剩下1.

思路

我们可以从后面往前推1 0, 1 1, 2 1, 2 3, 3 5,这样我们不难看出就是求是否存在两个连续的斐波那契数。

代码

#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long ll;
const int maxn=10000050;
bool isprime[maxn];
ll prime[maxn];
int cnt;

ll gcd(ll a,ll b){
	return b==0?a:gcd(b,a%b);
}

void init(){
	cnt=0;isprime[0]=isprime[1]=1;
	for(ll i=2;i<maxn;i++){
		if(!isprime[i])	prime[++cnt]=i;
		for(ll j=1;j<=cnt;j++){
			if(i*prime[j]>=maxn)	break;
			isprime[i*prime[j]]=1;
			if(i%prime[j]==0)	break;
		}
	}
}
void solve(){
	init();
	int T;
	scanf("%d",&T);
	while(T--){
		double a,b;
		scanf("%lf %lf",&a,&b);
		if(a == b) printf("2 2\n");
		else{
			ll a0=(ll)(a*100000.0+0.5),b0=(ll)(b*100000.0+0.5);
			ll GCD=gcd(a0,b0);
			a0/=GCD;b0/=GCD;
			if(isprime[a0]||isprime[b0])	printf("impossible\n");
			else	printf("%lld %lld\n",a0,b0);	
		}
	}
}
int main(){
	solve();
	return 0;
} 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值