Codeforces 490D Chocolate Number Theory
Portal: cf 490D
There are two rectangles. Now you can perform two operations on the rectangle.
(1) Remove the rectangle by half (if one side is half of the original one, this side is required to be divisible by two)
(2) Remove the rectangle by 1/3 (one side is changed to the original 2/3, which requires that the side be divisible by 3)
How many operations can be performed to make the two rectangles have the same area and output the side length of the two rectangles after the operation?
We can find that the two operations are equivalent to removing a prime factor 2, or converting a prime factor 3 into a prime factor 2, which will not affect other prime factors, therefore, we first remove the prime factor 2 and 3 to determine whether the product of the remainder child element set is equal. if the product is not equal, the product cannot be equal through the operation.
Then make the records make the prime factor 3 and 2 equal
/*************************************** * *************** File Name: d. cpp * Author: kojimai * Create Time: ******************************** * *********************/# include
# Include
# Include
# Include
Using namespace std; int cnt2 [5]; int cnt3 [5]; int main () {long x1, y1, x2, y2; cin >>> x1 >> x2; cin> y1> y2; long t1 = x1, t2 = x2, k1 = y1, k2 = y2; memset (cnt2, 0, sizeof (cnt2 )); memset (cnt3, 0, sizeof (cnt3); while (t1% 2 = 0) {t1/= 2; cnt2 [0] ++ ;} while (t1% 3 = 0) {t1/= 3; cnt3 [0] ++;} while (t2% 2 = 0) {t2/= 2; cnt2 [1] ++;} while (t2% 3 = 0) {t2/= 3; cnt3 [1] ++;} while (k1% 2 = 0) {k1/= 2; cnt2 [2] ++;} while (k1% 3 = 0) {k1/= 3; cnt3 [2] ++;} while (k2% 2 = 0) {k2/= 2; cnt2 [3] ++ ;} while (k2% 3 = 0) {k2/= 3; cnt3 [3] ++ ;} // records the number of prime factor 2 and 3 in the four sides, if (t1 * t2! = K1 * k2) // the product of the remaining prime factor is not equal to cout <-1 <
Y3) {xd3 = x3-y3; x4 + = xd3; // each time you remove a prime factor 3, you need to add a prime factor 2ans + = xd3 ;} else if (x3 <y3) {yd3 = y3-x3; y4 + = yd3; ans + = yd3 ;} // if (x4> y4) {xd4 = x4-y4; ans + = xd4;} else if (x4 <y4) {yd4 = y4-x4; ans + = yd4;} // The number of two equals to printf ("% d \ n", ans) by operating on prime factor 2 ); if (xd3) // perform the 1/3 operation on the first rectangle {while (x1% 3 = 0 & xd3) {x1 = x1/3*2; xd3 --;} while (x2% 3 = 0 & xd3) {x2 = x2/3*2; xd3 -- ;}} if (xd4) // perform the 1/2 operation on the first rectangle {while (x1% 2 = 0 & xd4) {x1 = x1/2; xd4 --;} while (x2% 2 = 0 & xd4) {x2/= 2; xd4 -- ;}} if (yd3) // perform the 1/3 operation on the second rectangle {while (y1% 3 = 0 & yd3) {y1 = y1/3*2; yd3 --;} while (y2% 3 = 0 & yd3) {y2 = y2/3*2; yd3 -- ;}} if (yd4) // perform the 1/2 operation on the second rectangle {while (y1% 2 = 0 & yd4) {y1 = y1/2; yd4 --;} while (y2% 2 = 0 & yd4) {y2/= 2; yd4 -- ;}} cout <
And modify the edge length.