C - Number Transformation

本篇介绍了一道算法题目,通过广度优先搜索(BFS)寻找从整数s通过加上其素因数转换到另一整数t所需的最小步骤数。文章提供了完整的C++实现代码,包括素数表的生成以及广搜过程。

In this problem, you are given an integer number s. You can transform any integer number A to another integer number B by adding x to A. This x is an integer number which is a prime factor of A (please note that 1 and A are not being considered as a factor of A). Now, your task is to find the minimum number of transformations required to transform s to another integer number 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 number of transformations needed. If it's impossible, then print -1.

Sample Input

2

6 12

6 13

Sample Output

Case 1: 2

Case 2: -1

题意:献给一个T,表示有T组数据,然后又给两个数s,t;s经过加素因数变成c(素因数是指1~c内的因子(这些必须是素数)),问最少需要加几次。不能加到t时输出-1.

题解:这是一道简单的广搜题,但是首先需要素数打表(否则会时间超限)。

 

#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
int n,m,flag,ss[1100],book[1100];
void sushu()
{
    memset(ss,0,sizeof(ss));
    for(int i=2;i<1100;i++)
    {
        if(ss[i]==0)
        {
            for(int j=i*2;j<1100;j+=i)
                ss[j]=1;
        }
    }
}
struct node
{
    int x,f;
};
void bfs(int x1)
{
    queue<node>q;
    node a,b;
    a.x=x1;
    a.f=0;
    book[x1]=1;
    while(!q.empty())
        q.pop();
    q.push(a);
    while(!q.empty())
    {
        a=q.front();
        q.pop();
        for(int i=2;i<a.x;i++)
        {
            if(a.x%i==0&&ss[i]==0)
            {
                b.x=a.x+i;
                if(book[b.x]==1||b.x>m)
                    continue;
                book[b.x]=1;
                b.f=a.f+1;
                if(b.x==m)
                {
                    flag=b.f;
                    return ;
                }
                q.push(b);
            }
        }
    }
    return ;
}
int main()
{
    int T,k=1;
    scanf("%d",&T);
    sushu();
    while(T--)
    {
        flag=0;
        memset(book,0,sizeof(book));
        scanf("%d%d",&n,&m);
        if(n==m)
            printf("Case %d: 0\n",k++);
        else
        {
            bfs(n);
            if(flag==0)
                printf("Case %d: -1\n",k++);
            else
                printf("Case %d: %d\n",k++,flag);
        }
    }
    return 0;
}

 

【顶级EI完整复现】【DRCC】考虑N-1准则的分布鲁棒机会约束低碳经济调度(Matlab代码实现)内容概要:本文介绍了名为《【顶级EI完整复现】【DRCC】考虑N-1准则的分布鲁棒机会约束低碳经济调度(Matlab代码实现)》的技术资源,聚焦于电力系统中低碳经济调度问题,结合N-1安全准则与分布鲁棒机会约束(DRCC)方法,提升调度模型在不确定性环境下的鲁棒性和可行性。该资源提供了完整的Matlab代码实现,涵盖建模、优化求解及仿真分析全过程,适用于复杂电力系统调度场景的科研复现与算法验证。文中还列举了大量相关领域的研究主题与代码资源,涉及智能优化算法、机器学习、电力系统管理、路径规划等多个方向,展示了广泛的科研应用支持能力。; 适合人群:具备一定电力系统、优化理论和Matlab编程基础的研究生、科研人员及从事能源调度、智能电网相关工作的工程师。; 使用场景及目标:①复现高水平期刊(如EI/SCI)关于低碳经济调度的研究成果;②深入理解N-1安全约束与分布鲁棒优化在电力调度中的建模方法;③开展含新能源接入的电力系统不确定性优化研究;④为科研项目、论文撰写或工程应用提供可运行的算法原型和技术支撑。; 阅读建议:建议读者结合文档提供的网盘资源,下载完整代码与案例数据,按照目录顺序逐步学习,并重点理解DRCC建模思想与Matlab/YALMIP/CPLEX等工具的集成使用方式,同时可参考文中列出的同类研究方向拓展研究思路。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值