Advertisement
ismaeil

C - Chocolate

Aug 7th, 2020
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.23 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define ll long long
  3. using namespace std;
  4.  
  5. typedef pair<int, int> ii;
  6. #define S second
  7. #define F first
  8.  
  9. map<int ,int> Cnt[4];
  10. int L ,R ,Ans;
  11. ii a ,b;
  12.  
  13. void FactOfNum(int n ,int idx) {
  14.     for(int i = 2 ; i <= 4 ; i++)
  15.         if( n % i == 0 )
  16.             while( n % i == 0 )
  17.                 n /= i ,Cnt[idx][i]++;
  18.     if( n != 1 ) Cnt[idx][n]++;
  19. }
  20.  
  21. int main(){
  22.     scanf("%d%d%d%d" ,&a.F ,&a.S ,&b.F ,&b.S);
  23.  
  24.     FactOfNum(a.F ,0);
  25.     FactOfNum(a.S ,1);
  26.     FactOfNum(b.F ,2);
  27.     FactOfNum(b.S ,3);
  28.  
  29.     int _2A = Cnt[0][2] + Cnt[1][2];
  30.     int _3A = Cnt[0][3] + Cnt[1][3];
  31.     int _2B = Cnt[2][2] + Cnt[3][2];
  32.     int _3B = Cnt[2][3] + Cnt[3][3];
  33.  
  34.     ll MulA = 1ll * a.F * a.S;
  35.     ll MulB = 1ll * b.F * b.S;
  36.  
  37.     while( _3A > _3B ){
  38.         if( Cnt[0][3] ){
  39.             Cnt[0][3] --;
  40.             Cnt[0][2] ++;
  41.             a.F /= 3;
  42.             a.F *= 2;
  43.         } else if( Cnt[1][3] ) {
  44.             Cnt[1][3] --;
  45.             Cnt[1][2] ++;
  46.             a.S /= 3;
  47.             a.S *= 2;
  48.         }
  49.         MulA /= 3;
  50.         MulA *= 2;
  51.         _3A --;
  52.         _2A ++;
  53.         Ans ++;
  54.     }
  55.  
  56.     while( _3B > _3A ){
  57.         if( Cnt[2][3] ){
  58.             Cnt[2][3] --;
  59.             Cnt[2][2] ++;
  60.             b.F /= 3;
  61.             b.F *= 2;
  62.         } else if( Cnt[3][3] ) {
  63.             Cnt[3][3] --;
  64.             Cnt[3][2] ++;
  65.             b.S /= 3;
  66.             b.S *= 2;
  67.         }
  68.         MulB /= 3;
  69.         MulB *= 2;
  70.         _3B --;
  71.         _2B ++;
  72.         Ans ++;
  73.     }
  74.  
  75.     while( _2A > _2B ){
  76.         if( Cnt[0][2] ){
  77.             Cnt[0][2] --;
  78.             a.F /= 2;
  79.         } else if( Cnt[1][2] ) {
  80.             Cnt[1][2] --;
  81.             a.S /= 2;
  82.         }
  83.         MulA /= 2;
  84.         _2A --;
  85.         Ans ++;
  86.     }
  87.  
  88.     while( _2B > _2A ){
  89.         if( Cnt[2][2] ){
  90.             Cnt[2][2] --;
  91.             b.F /= 2;
  92.         } else if( Cnt[3][2] ) {
  93.             Cnt[3][2] --;
  94.             b.S /= 2;
  95.         }
  96.         MulB /= 2;
  97.         _2B --;
  98.         Ans ++;
  99.     }
  100.  
  101.     bool flag = (MulA == MulB);
  102.     if( flag ) {
  103.         printf("%d\n" ,Ans);
  104.         printf("%d %d\n" ,a.F ,a.S);
  105.         printf("%d %d\n" ,b.F ,b.S);
  106.     } else puts("-1");
  107.     return 0;
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement