//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. WebDriver 在使用 CSS Selector 与 XPath 在查找元素时如何取舍

    开发在做Web系统时,用的是css div划分层,使用jQuery 选取元素.

  2. Unity的HTC VIVE SDK研究(手柄按键功能的研究,比较详细)

    http://blog.csdn.net/ystistheking/article/details/51553237 想交流的朋友我们可以微博互粉,我的微博黑石铸造厂厂长 ,缺粉丝啊 .....求粉求 ...

  3. mydbtest文档

    mydbtest是楼方鑫编写的一个数据库测试工具,有需要的话,请自取 http://pan.baidu.com/s/1mgJpukg#path=%252FOneSQL%252FDocument 找到& ...

  4. python 练习 16

    #!/usr/bin/python # -*- coding: UTF-8 -*- import time myD = {1: 'a', 2: 'b'} for key, value in dict. ...

  5. [HBuilder] 简介

    官网首页: http://www.dcloud.io/runtime.html 特点: 编码比其他工具快5倍 代码输入法:按下数字快速选择候选项 可编程代码块:一个代码块,少敲50个按键 内置emme ...

  6. mybatis 简单配置

    一.com/book/map包下有两个配置文件: 1.MyBatisConfig.xml <?xml version="1.0" encoding="UTF-8&q ...

  7. cookie、 sessionStorage 、localStorage之间的区别和使用

    1.cookie:存储在用户本地终端上的数据.有时也用cookies,指某些网站为了辨别用户身份,进行session跟踪而存储在本地终端上的数据,通常经过加密.一般应用最典型的案列就是判断注册用户是否 ...

  8. 137. Single Number II——问题是查找,本质是hash查找,只是记录的是32 bit中各个位出现次数而已

    Given an array of integers, every element appears three times except for one. Find that single one. ...

  9. JBOSS常用端口说明

    1.jboss 的端口修改位置总结 Jboss通常占用的端口是1098,1099,4444,4445,8080,8009,8083,8093这几个, 默认端口是8080 在windows系统中: 10 ...

  10. 学习shell之前你不得不了解的小知识

    1.!!命令                   # 表示上一条输出history |grep 23         #表示历史记录中的第23条!vim                     #上一 ...