//Accepted    212 KB    16 ms
 //筛素数+bfs
 #include <cstdio>
 #include <cstring>
 #include <iostream>
 #include <queue>
 using namespace std;
 ;
 ;
 bool pri[imax_n];
 bool vis[imax_n];
 void prime()
 {
     ;i<imax_n;i++)
     {
         for (int j=i*i;j<imax_n;j+=i)
         {
             pri[j]=;
         }
     }
     pri[]=pri[]=;
 }
 queue<int > q,step;
 int ans;
 void bfs(int s,int e)
 {
     while (!q.empty()) q.pop();
     while (!step.empty()) step.pop();
     memset(vis,false,sizeof(vis));
     ans=inf;
     q.push(s);
     vis[s]=true;
     step.push();
     while (!q.empty())
     {
         int x=q.front();
         q.pop();
         int temp_step=step.front();
         step.pop();
         if (x==e)
         {
             ans=temp_step;
             return ;
         }
         ;
         ;i<=;i++)
         {
             ;
              && !vis[nx])
             {
                 q.push(nx);
                 step.push(temp_step+);
                 vis[nx]=true;
             }
         }
         t=(x%)%+x/*;
         ;i<=;i++)
         {
             ;
              && !vis[nx])
             {
                 q.push(nx);
                 step.push(temp_step+);
                 vis[nx]=true;
             }
         }
         t=x/*+x%%;
         ;i<=;i++)
         {
             ;
              && !vis[nx])
             {
                 q.push(nx);
                 step.push(temp_step+);
                 vis[nx]=true;
             }
         }
         t=x/*;
         ;i<=;i+=)
         {
             int nx=t+i;
              && !vis[nx])
             {
                 q.push(nx);
                 step.push(temp_step+);
                 vis[nx]=true;
             }
         }
     }
 }
 int main()
 {
     prime();
     int T;
     scanf("%d",&T);
     while (T--)
     {
         int a,b;
         scanf("%d%d",&a,&b);
         bfs(a,b);
         if (ans==inf)
         printf("Impossible\n");
         else
         printf("%d\n",ans);
     }
     ;
 }

poj3126 筛素数+bfs的更多相关文章

  1. Prime Path素数筛与BFS动态规划

    埃拉托斯特尼筛法(sieve of Eratosthenes ) 是古希腊数学家埃拉托斯特尼发明的计算素数的方法.对于求解不大于n的所有素数,我们先找出sqrt(n)内的所有素数p1到pk,其中k = ...

  2. Prime Path(POJ - 3126)【BFS+筛素数】

    Prime Path(POJ - 3126) 题目链接 算法 BFS+筛素数打表 1.题目主要就是给定你两个四位数的质数a,b,让你计算从a变到b共最小需要多少步.要求每次只能变1位,并且变1位后仍然 ...

  3. 【板子】gcd、exgcd、乘法逆元、快速幂、快速乘、筛素数、快速求逆元、组合数

    1.gcd int gcd(int a,int b){ return b?gcd(b,a%b):a; } 2.扩展gcd )extend great common divisor ll exgcd(l ...

  4. CF449C Jzzhu and Apples (筛素数 数论?

    Codeforces Round #257 (Div. 1) C Codeforces Round #257 (Div. 1) E CF450E C. Jzzhu and Apples time li ...

  5. 洛谷P3383 【模板】线性筛素数

    P3383 [模板]线性筛素数 256通过 579提交 题目提供者HansBug 标签 难度普及- 提交  讨论  题解 最新讨论 Too many or Too few lines 样例解释有问题 ...

  6. 洛谷 P3383 【模板】线性筛素数

    P3383 [模板]线性筛素数 题目描述 如题,给定一个范围N,你需要处理M个某数字是否为质数的询问(每个数字均在范围1-N内) 输入输出格式 输入格式: 第一行包含两个正整数N.M,分别表示查询的范 ...

  7. POJ2689-Prime Distance-区间筛素数

    最近改自己的错误代码改到要上天,心累. 这是迄今为止写的最心累的博客. Prime Distance Time Limit: 1000MS   Memory Limit: 65536K Total S ...

  8. 线性筛素数和理解 洛谷P3383

    题目链接:https://www.luogu.org/problemnew/show/P3383 线性筛法筛素数的特点是每一个数字只被遍历一次,即时间复杂度为O(n),所以说他是线性的,并且所有的非素 ...

  9. ACM-ICPC 2018 南京赛区网络预赛 J题Sum(线性筛素数)

    题目链接:https://nanti.jisuanke.com/t/30999 参考自博客:https://kuangbin.github.io/2018/09/01/2018-ACM-ICPC-Na ...

随机推荐

  1. jsp页面 直接从地址栏中 获取 参数的方法

    function GetQueryString(name) {      var reg = new RegExp("(^|&)"+ name +"=([^&am ...

  2. MySQL初级培训

    按照一个MySQL DBA在工作中接触到部分的先后顺序,编排培训目录如下. 字段选取 int , decimal, char , varchar , blob ,timestamp SQL优化 exp ...

  3. setInterval小问题

    先看下面代码: for (var i = 0; i < 3; i++) { setTimeout(function () { console.log(i) }, 1000); } 运行效果是 输 ...

  4. Ubuntu 安装 Redis

    1. 下载并安装 redis 2.6.16版 sudo mkdir /usr/local/src/Redis cd /usr/local/src/Redis sudo wget http://down ...

  5. 词法分析器--DFA(c++实现)

    语言名为TINY 实例程序: begin var x,y:interger; x:=; read(x); then x:=x-y; x:=x+y; write(x); end TINY语言扫描程序的D ...

  6. PowerDesigner生成SQL脚本时,对象带有双引号的问题解决

    在pdm查看脚本时,发现表名和字段名带有双引号: 1.create table"cfg_user_card_account"  ( 2.  "user_card_acco ...

  7. Codeforces Round #308 (Div. 2)----C. Vanya and Scales

    C. Vanya and Scales time limit per test 1 second memory limit per test 256 megabytes input standard ...

  8. matlab(数组、矩阵)

  9. 377. Combination Sum IV——DP本质:针对结果的迭代,dp[ans] <= dp[ans-i] & dp[i] 找三者关系 思考问题的维度+1,除了数据集迭代还有考虑结果

    Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...

  10. 编程思考 PetShop读后感

    标准,插拔式的设计思想建立一致的标准是通向“复用”的通道.分层,使其得到的充分的独立.一个东西如果独立了[不是孤立],这个事物就具有很强大的力量,这个和一个人的成长是相同的道理.所以呢,在写程序的过程 ...