Light OJ:1141 Number Transformation(BFS+素因子)

      1141 - Number Transformation
Time Limit: 2 second(s)Memory Limit: 32 MB

In this problem, you are given an integer number s.You can transform any integer number A to another integer number Bby adding x to A. This x is an integer number which is aprime factor of A (please note that 1 and A are not beingconsidered as a factor of A). Now, your task is to find the minimumnumber of transformations required to transform s to another integernumber t.

Input

Input starts with an integer T (≤ 500),denoting the number of test cases.

Each case contains two integers: s (1 ≤ s ≤100) and t (1 ≤ t ≤ 1000).

Output

For each case, print the case number and the minimum numberof transformations needed. If it's impossible, then print -1.

Sample Input

Output for Sample Input

2

6 12

6 13

Case 1: 2

Case 2: -1

 


题目大意:给你两个数s和t,s加上s的一个素因子得到新的s,问至少这样几步可以使得s变成t。
解题思路:素数打表,然后把1000以内所有的数的素因子放到vector里面,然后bfs搜索,一旦搜到符合s变成t,那么肯定是最少步数,且用vis记录到达过的状态,避免重复搜索(因为没加vis比赛时超内存了,因为比如你输入6 999,那么中间过程可能是
6+2=8
6+3=9
8+2=10
9+3=12
10+2=12
10+5=15
12+2=14
12+3=15,其中12,15此时被最少搜索两次,后面还有可能再次搜到它们,要达到999很难,所以把这些状态要标记,避免重复搜索)。
代码如下:
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
using namespace std; 
int a,b,ans;
int su[1010];
int vis[1010];
vector<int> v[1010];
struct node
{
	int num,step;
};
void dabiao()
{
	su[0]=1;
	su[1]=1;
	for(int i=2;i<1010;i++)
	{
		if(su[i]==1)
		{
			continue;
		}
		for(int j=i*2;j<1010;j=j+i)
		{
			su[j]=1;
		}
	}
	for(int i=0;i<1010;i++)
	{
		v[i].clear();
	}
	for(int i=0;i<1010;i++)
	{
		for(int j=2;j<i;j++)
		{
			if(su[j]==0&&i%j==0)
			{
				v[i].push_back(j);
			}
		}
	}
}
void bfs()
{
	memset(vis,0,sizeof(vis));
	node x,y;
	x.num=a;
	x.step=0;
	queue<node> q;
	while(!q.empty())
	{
		q.pop();
	}
	q.push(x);
	vis[a]=1;
	while(!q.empty())
	{
		x=q.front();
		q.pop();
		if(x.num==b)
		{
			ans=x.step;
			break;
		}
		for(int i=0;i<v[x.num].size();i++)
		{
			y.num=x.num+v[x.num][i];
			if(y.num<=b&&vis[y.num]==0)
			{
				vis[y.num]=1;
				y.step=x.step+1;
				q.push(y);
			}
		}
	}
}
int main()
{
	dabiao();//素数打表+素因子打表 
	int t;
	scanf("%d",&t);
	int cnt=1;
	while(t--)
	{
		scanf("%d%d",&a,&b);
		ans=-1;//初始化结果 
		bfs();
		printf("Case %d: %d\n",cnt++,ans);
	}
	return 0;
} 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
增加到了多少? 回答: 根据引用\[1\]和引用\[3\]的描述,给出一个数x,每分钟x会增加它的最小质因子。根据引用\[3\]中的算法,我们可以通过循环判断x是否能被质数整除,如果能被整除,则将该质数作为最小质因子,并将x除以该质数,同时统计该质因子的个数。然后继续循环判断x是否能被下一个质数整除,直到x不能被任何质数整除为止。根据引用\[1\]中的算法,如果到最后x的值不是1,而是别的数,那么将这个数作为最后一个质因子,并将个数设置为1。最后,将所有的质因子和对应的个数记录下来。根据引用\[2\]中的描述,如果x的倍数已经赋过值并且是最小的质因子,那么会依次向后顺序进行,直到遇到没有赋质因子的值。因此,我们可以通过一个数组biao\[\]来判断是否之前给x赋过值,通过while (!biao\[i\])来选择没有赋质因子的值。根据以上算法,我们可以计算出多少分钟过后x增加到了多少。 #### 引用[.reference_title] - *1* *3* [《算法笔记》第4章 入门篇(2)---算法初步 5.5 质因子分解](https://blog.csdn.net/wsfhdhjs/article/details/107073090)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [最小质因子问题](https://blog.csdn.net/qq_56762247/article/details/119418862)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值