2 solutions

  • 1
    @ 2025-10-29 14:15:59

    T2

    30 分

    暴力搜索即可。

    60 分

    显然变换过程中数一定变小,记忆化搜索即可。

    正解

    显然如果 nn 中存在除了 2,3,52,3,5 的其他质因子,必定无解。

    考虑三种变换的实质。

    操作 11:去掉当前数中的 11 个质因子 22

    操作 22 :将当前数中的质因子 22 增加 11 个,去掉当前数中的 11 个质因子 33

    操作 33 :将当前数中的质因子 22 增加 22 个,去掉当前数中的 11 个质因子 55

    那么做法就出来了,一个质因子 22 贡献为 11,质因子 33 贡献为 22,质因子 55 贡献为 33

    复杂度 O(Tlogn)\mathcal{O}(T\log n)

    • 0
      @ 2026-4-15 16:55:27
      #include<iostream>
      using namespace std;
      int main(){
          int T;
          cin>>T;
          while(T--){
              long long n;
              cin>>n;
              int cnt2=0,cnt3=0,cnt5=0;
              while(n%2==0){
                  cnt2++;
                  n/=2;
              }
              while(n%3==0){
                  cnt3++;
                  n/=3;
              }
              while(n%5==0){
                  cnt5++;
                  n/=5;
              }
              if(n!=1){
                  cout<<-1<<endl;
                  continue;
              }
              int steps=0;
              steps+=cnt5;
              cnt2+=2*cnt5;
              steps+=cnt3;
              cnt2+=cnt3;
              steps+=cnt2;
              cout<<steps<<endl;
          }
          return 0;
      }
      • 1

      Information

      ID
      1610
      Time
      1000ms
      Memory
      256MiB
      Difficulty
      4
      Tags
      (None)
      # Submissions
      85
      Accepted
      37
      Uploaded By