sgu 330——Numbers

Description



Young Andrew is playing yet another numbers game. Initially, he writes down an integer A. Then, he chooses some divisor d 1 of A, 1 <     d 1 <    A, erases A and writes A 1=A+     d 1 instead. Then, he chooses some divisor d 2 of A 1, 1 <     d 2 <    A 1, erases A 1 and writes A 2=A 1+     d 2 instead.    

I.e., at any step he chooses some positive integer divisor of the current number, but not 1 and not the whole number, and increases the current number by it.    

Is it possible for him to write number B if he started with number A?

Input

The only line of input contains two integers A and B, 2 ≤     A <    B ≤ 10 12.

Output

If there's no solution, output "    
Impossible
" (without quotes) to the only line of output. If there's one, output the sequence of numbers written starting with A and ending with B, one per line. You're not asked to find the shortest possible sequence, however, you should find a sequence with no more than 500 numbers. It is guaranteed that if there exists some sequence for the given A and B, then there exists a sequence with no more than 500 numbers in it.

Sample Input

sample input
sample output
12 57
12
16
24
27
30
40
50
52
54
57

sample input
sample output
3 6
Impossible

 

如果A是偶数,B是偶数,则一直加A最大的偶约数。否则,把AB加上或减去最小的奇约数,如果不能把他们变成偶数,则不存在。

剩下的数字不会超过500个。

#include<iostream>
#include<cstdio>
using namespace std;
#define LL __int64
LL prime[1000006];
bool p[1100006];
LL cnt;
void init()
{
	LL i,j;
	cnt=0;
	for(i=2;i<=1000000;i++)
		if(!p[i])
		{
			prime[++cnt]=i;
			for(j=i*i;j<=1000000;j+=i)
				p[j]=1;
		}
}
int main()
{
	LL X,Y;
	LL x,y;
	init();
	cin>>X>>Y;
	if(x==2)
	{
		cout<<"Impossible"<<endl;
		return 0;
	}
	x=X;
	y=Y;
	if(x&1)
	{
		for(LL i=1;i<=cnt;i++)
			if(x%prime[i]==0&&prime[i]!=x)
			{
				x+=prime[i];
				break;
			}
	}
	if(y&1)
	{
		for(LL i=1;i<=cnt;i++)
			if(y%prime[i]==0&&prime[i]!=y)
			{
				y-=prime[i];
				break;
			}
	}
	if((x&1)||(y&1)||x>y)
	{
		cout<<"Impossible"<<endl;
		return 0;
	}
	LL c=0;
	LL ans[1600];
	
	ans[++c]=X;
	if(x!=X)
		ans[++c]=x;
	while(x<y)
	{
		LL temp=x;
		while(temp%2==0)
			temp/=2;
		temp=x/temp;
		if(temp==x)
			temp/=2;
		while(x+temp>y)
			temp/=2;
		x+=temp;
		ans[++c]=x;
	}
	if(y!=Y)
		ans[++c]=Y;
	for(LL i=1;i<=c;i++)
		printf("%I64d\n",ans[i]);
	return 0;
}


 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值