• [找规律]Number Game 2022牛客多校第6场 J


    题目描述

    There are three integers A,BA, BA,B and CCC written on the blackboard.

    You can perform the following two operations as many times as you like:

    1. Change BBB to A−BA-BA−B.

    2. Change CCC to B−CB-CB−C.

    Please note that each time you don't need to perform all two operations. You can choose one type of operation to perform.

    You are given an integer xxx. Answer whether you can change CCC into xxx using these operations.

    You need to answer TTT queries independently.

    输入描述:

    The first line contains a positive integer T(1≤T≤105)T(1\leq T\leq 10 ^ 5)T(1≤T≤105).

    Each of the next TTT lines contains four integers A,B,C,x(−108≤A,B,C,x≤108)A, B, C, x(-10 ^ 8 \leq A, B, C, x \leq 10 ^ 8)A,B,C,x(−108≤A,B,C,x≤108).

    输出描述:

    For each test case, output "Yes" if CCC can become xxx, and "No" otherwise (without quotes).

    示例1

    输入

    3 
    2 4 3 1 
    2 4 3 2 
    4 2 2 0

    输出

    Yes
    No
    Yes

    说明

    Please note that A,B,C,xA, B, C, xA,B,C,x could be negative.

    备注:

    Please note that A,B,C,xA, B, C, xA,B,C,x could be negative.

    题意: 给出四个整数a,b,c和x,每次操作可以让b = a-b或者c = b-c,问在若干次操作后能否让c等于x。

    分析: b的值只能是b或者是a-b,而c的值可以多写几个找找规律,第一轮c的值可以为b-c或a-b-c,第二轮c的值可以为a-2*b+c或-a+2*b+c,第三轮c的值可以为-a+3*b-c或2*a-3*b-c,第四轮c的值可以为2*a-4*b+c或-2*a+4*b+c,挨着看可能不好找规律,但是隔一个再看就能找到规律了,其实就是b-c+k*(2*b-a)或a-2*b+c+k*(a-2*b)或a-b-c+k*(a-2*b)或-a+2*b+c+k*(2*b-a),其中k为任意自然数,所以就是让x和这四个数做差,看看差值是否为相应的倍数即可。

    具体代码如下:

    1. #include
    2. #include
    3. #include
    4. #include
    5. #include
    6. #include
    7. #define int long long
    8. using namespace std;
    9. signed main()
    10. {
    11. int T;
    12. cin >> T;
    13. while(T--){
    14. int a, b, c, x;
    15. scanf("%lld%lld%lld%lld", &a, &b, &c, &x);
    16. int n1 = b-c, n2 = -a+2*b+c, n3 = a-b-c, n4 = a-2*b+c;
    17. if(2*b-a == 0){
    18. if(x-n1 == 0 || x-n2 == 0 || x-n3 == 0 || x-n4 == 0) puts("Yes");
    19. else puts("No");
    20. continue;
    21. }
    22. if((x-n1) % (2*b-a) == 0){
    23. puts("Yes");
    24. continue;
    25. }
    26. if((x-n2) % (2*b-a) == 0){
    27. puts("Yes");
    28. continue;
    29. }
    30. if((x-n3) % (a-2*b) == 0){
    31. puts("Yes");
    32. continue;
    33. }
    34. if((x-n4) % (a-2*b) == 0){
    35. puts("Yes");
    36. continue;
    37. }
    38. puts("No");
    39. }
    40. return 0;
    41. }

     

  • 相关阅读:
    「C#」WPF学习笔记-基础类及继承关系
    如何对待工作中的失误
    C语言自动生成代码注释:koroFileHeader插件
    PMP考试通关宝典-敏捷专题
    AI问诊逐渐取代医生是不是伪命题?实测国内外医疗专用大模型
    【文件系统】Linux文件系统的基本存储机制
    C#设置Textbox控件不可编辑
    正则系列之正则表达式可选参数
    PyQt5 信号(Signal)与槽(Slot)
    如何定义版本号?
  • 原文地址:https://blog.csdn.net/m0_55982600/article/details/126201169