• C. Fishingprince Plays With Array--Codeforces Global Round 21


    Problem - C - Codeforces

    C. Fishingprince Plays With Array

    time limit per test

    2 seconds

    memory limit per test

    512 megabytes

    input

    standard input

    output

    standard output

    Fishingprince is playing with an array [a1,a2,…,an][a1,a2,…,an]. He also has a magic number mm.

    He can do the following two operations on it:

    • Select 1≤i≤n1≤i≤n such that aiai is divisible by mm (that is, there exists an integer tt such that m⋅t=aim⋅t=ai). Replace aiai with mm copies of aimaim. The order of the other elements doesn't change. For example, when m=2m=2 and a=[2,3]a=[2,3] and i=1i=1, aa changes into [1,1,3][1,1,3].
    • Select 1≤i≤n−m+11≤i≤n−m+1 such that ai=ai+1=⋯=ai+m−1ai=ai+1=⋯=ai+m−1. Replace these mm elements with a single m⋅aim⋅ai. The order of the other elements doesn't change. For example, when m=2m=2 and a=[3,2,2,3]a=[3,2,2,3] and i=2i=2, aa changes into [3,4,3][3,4,3].

    Note that the array length might change during the process. The value of nn above is defined as the current length of the array (might differ from the nn in the input).

    Fishingprince has another array [b1,b2,…,bk][b1,b2,…,bk]. Please determine if he can turn aa into bb using any number (possibly zero) of operations.

    Input

    Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤1041≤t≤104). Description of the test cases follows.

    The first line of each test case contains two integers nn and mm (1≤n≤5⋅1041≤n≤5⋅104, 2≤m≤1092≤m≤109).

    The second line of each test case contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤1091≤ai≤109).

    The third line of each test case contains one integer kk (1≤k≤5⋅1041≤k≤5⋅104).

    The fourth line of each test case contains kk integers b1,b2,…,bkb1,b2,…,bk (1≤bi≤1091≤bi≤109).

    It is guaranteed that the sum of n+kn+k over all test cases does not exceed 2⋅1052⋅105.

    Output

    For each testcase, print Yes if it is possible to turn aa into bb, and No otherwise. You can print each letter in any case (upper or lower).

    Example

    input

    Copy

    5
    5 2
    1 2 2 4 2
    4
    1 4 4 2
    6 2
    1 2 2 8 2 2
    2
    1 16
    8 3
    3 3 3 3 3 3 3 3
    4
    6 6 6 6
    8 3
    3 9 6 3 12 12 36 12
    16
    9 3 2 2 2 3 4 12 4 12 4 12 4 12 4 4
    8 3
    3 9 6 3 12 12 36 12
    7
    12 2 4 3 4 12 56
    

    output

    Copy

    Yes
    Yes
    No
    Yes
    No
    

    Note

    In the first test case of the sample, we can do the second operation with i=2i=2: [1,2,2,4,2]→[1,4,4,2][1,2,2,4,2]→[1,4,4,2].

    In the second testcase of the sample, we can:

    • do the second operation with i=2i=2: [1,2,2,8,2,2]→[1,4,8,2,2][1,2,2,8,2,2]→[1,4,8,2,2].
    • do the second operation with i=4i=4: [1,4,8,2,2]→[1,4,8,4][1,4,8,2,2]→[1,4,8,4].
    • do the first operation with i=3i=3: [1,4,8,4]→[1,4,4,4,4][1,4,8,4]→[1,4,4,4,4].
    • do the second operation with i=2i=2: [1,4,4,4,4]→[1,8,4,4][1,4,4,4,4]→[1,8,4,4].
    • do the second operation with i=3i=3: [1,8,4,4]→[1,8,8][1,8,4,4]→[1,8,8].
    • do the second operation with i=2i=2: [1,8,8]→[1,16][1,8,8]→[1,16].
    • ======================================================================
    • 全都展开,注意展开很可能会爆掉,所以放进结构体进行判断,注意个数竟然会超int,开ll包过。
      1. #include
      2. typedef long long int ll;
      3. using namespace std;
      4. typedef struct
      5. {
      6. ll id,num;
      7. } xinxi;
      8. xinxi s[10000000],t[10000000];
      9. int main()
      10. {
      11. int tt;
      12. cin>>tt;
      13. while(tt--)
      14. {
      15. int n,m;
      16. scanf("%d%d",&n,&m);
      17. int len=0,len1=0;
      18. for(int i=1; i<=n; i++)
      19. {
      20. int x;
      21. scanf("%d",&x);
      22. if(x%m)
      23. {
      24. if(s[len].id==x)
      25. {
      26. s[len].num++;
      27. }
      28. else
      29. {
      30. len++;
      31. s[len].id=x;
      32. s[len].num=1;
      33. }
      34. }
      35. else
      36. {
      37. ll cnt=1;
      38. while(x%m==0)
      39. {
      40. cnt*=(m);
      41. x/=m;
      42. }
      43. if(s[len].id==x)
      44. {
      45. s[len].num+=cnt;
      46. }
      47. else
      48. {
      49. len++;
      50. s[len].id=x;
      51. s[len].num=cnt;
      52. }
      53. }
      54. }
      55. len1=len;
      56. len=0;
      57. scanf("%d",&n);
      58. int flag=0;
      59. for(int i=1; i<=n; i++)
      60. {
      61. int x;
      62. scanf("%d",&x);
      63. if(x%m)
      64. {
      65. if(t[len].id==x)
      66. {
      67. t[len].num++;
      68. }
      69. else
      70. {
      71. len++;
      72. t[len].id=x;
      73. t[len].num=1;
      74. }
      75. }
      76. else
      77. {
      78. ll cnt=1;
      79. while(x%m==0)
      80. {
      81. cnt*=(m);
      82. x/=m;
      83. }
      84. if(t[len].id==x)
      85. {
      86. t[len].num+=cnt;
      87. }
      88. else
      89. {
      90. len++;
      91. t[len].id=x;
      92. t[len].num=cnt;
      93. }
      94. }
      95. }
      96. if(len1!=len)
      97. {
      98. cout<<"No"<
      99. }
      100. else
      101. {
      102. flag=0;
      103. for(int i=0; i<=len; i++)
      104. {
      105. if(t[i].id!=s[i].id||t[i].num!=s[i].num)
      106. {
      107. flag=1;
      108. break;
      109. }
      110. }
      111. if(flag)
      112. {
      113. cout<<"No"<
      114. }
      115. else
      116. {
      117. cout<<"YES"<
      118. }
      119. }
      120. for(int i=0;i<max(len1,len);i++)
      121. {
      122. s[i].id=0;
      123. s[i].num=0;
      124. t[i].id=0;
      125. t[i].num=0;
      126. }
      127. }
      128. return 0;
      129. }

  • 相关阅读:
    AI&Cloud 分论坛 07-AI原生数据库与RAG【文档管理】
    自动驾驶行业观察之2023上海车展-----车企发展趋势(3)
    【Python】爬虫练习-爬取豆瓣网电影评论用户的观影习惯数据
    Vue3 实现页面简单的CRUD
    【brpc学习案例实践一】rpc服务构造基本流程
    R语言ggpubr包优雅绘制带统计数据的箱线图
    Python之json的dump和dumps方法
    在阿里做前端程序员,我是这样规划的
    第七十天 c++perimer70~115string类型总结
    java计算机毕业设计家政服务公司管理信息源码+系统+数据库+lw文档
  • 原文地址:https://blog.csdn.net/jisuanji2606414/article/details/126141300