• Multiply and Rotate (BFS/队列)


    Multiply and Rotate

    时间限制: 1.000 Sec  内存限制: 128 MB

    题目描述

    We have a positive integer a. Additionally, there is a blackboard with a number written in base 10.
    Let x be the number on the blackboard. Takahashi can do the operations below to change this number.

    Erase x and write x multiplied by a, in base 10.
    See x as a string and move the rightmost digit to the beginning.
    This operation can only be done when x≥10 and x is not divisible by 10.
    For example, when a=2,x=123, Takahashi can do one of the following.


    Erase x and write x×a=123×2=246.
    See x as a string and move the rightmost digit 3 of 123 to the beginning, changing the number from 123 to 312.
    The number on the blackboard is initially 1. What is the minimum number of operations needed to change the number on the blackboard to N? If there is no way to change the number to N, print −1.

    Constraints
    2≤a<106
    2≤N<106
     
    All values in input are integers.

    输入

    输出

    Print the answer.

    样例输入 

    【样例1】
    3 72
    【样例2】
    2 5
    【样例3】
    2 611
    【样例4】
    2 767090

    样例输出 

    【样例1】
    4
    【样例2】
    -1
    【样例3】
    12
    【样例4】
    111

    提示

    样例1解释
    We can change the number on the blackboard from 1 to 72 in four operations, as follows.
    Do the operation of the first type: 1→3.
    Do the operation of the first type: 3→9.
    Do the operation of the first type: 9→27.
    Do the operation of the second type: 27→72.
    It is impossible to reach 72 in three or fewer operations, so the answer is 4.
    样例2解释
    It is impossible to change the number on the blackboard to 5.
    样例3解释
    There is a way to change the number on the blackboard to 611 in 12 operations: 1→2→4→8→16→32→64→46→92→29→58→116→611, which is the minimum possible.

    题目大意:给定两个数a,n,最初有一个数字1,问每次执行如下操作之一,最少的变为n的操作次数是多少?

    1.乘以a

    2.如果这个数>=0并且不是10的倍数,那么把他的最后一位移到第一位


    两个字符与十进制相互转换的函数:

    1.to_string(x) 将整数x转化为字符串s

    2.stoi(s) 将字符串s转化为整数x 

    1. #include
    2. #include
    3. #include
    4. using namespace std;
    5. const int N=1e6+10;
    6. int b[N];
    7. queue<int> q;
    8. int main()
    9. {
    10. int a,n;cin>>a>>n;
    11. q.push(n);
    12. memset(b,-1,sizeof b);
    13. b[n]=0;
    14. while(q.size())
    15. {
    16. int x=q.front();q.pop();
    17. if(x<2) break;
    18. if(x%a==0&&b[x/a]==-1) b[x/a]=b[x]+1,q.push(x/a);
    19. if(x>=10)
    20. {
    21. string s=to_string(x);
    22. int y=stoi(s.substr(1)+s[0] );
    23. if(s[1]!='0'&&b[y]==-1) b[y]=b[x]+1,q.push(y);
    24. }
    25. }
    26. cout<1];
    27. return 0;
    28. }

     

  • 相关阅读:
    ELK技术介绍:背景、功能及应用场景全面解析
    浅谈芯片验证中的仿真运行之 compilation unit 技术(实践篇)
    【附源码】Python计算机毕业设计美食城网站设计
    【SwiftUI模块】0012、SwiftUI-搭建一个类似微博、网易云、抖音个人页面的头部下拉放大图片效果
    JavaScript基础之十JavaScript的DOM操作
    "MixFormer: Co-Scaling Up Dense and Sequence in Industrial Recommenders" 论文笔记
    LeetCode-6104. 统计星号_Python
    【自然语言处理(NLP)】基于FNN网络的电影评论情感分析
    map 和 set 的介绍
    自学WEB后端05-Node.js后端服务链接数据库redis
  • 原文地址:https://blog.csdn.net/m0_63363129/article/details/126078331