Codeforces Amr and Chemistry (Mathematics + chaos)
Question: give n numbers. Each number can be multiplied by two or divided by two (the downward integer is equivalent to the left or right shift). Ask how many operations can make the n numbers disguised.
Idea: first consider the possible values of each number, and express a number in the form of s * 2 ^ k. s is an odd number.
Then all values of this number may be s '* 2 ^ x, (s' = s/2, (s/2)/2 ,.....) and s '* 2 ^ x <= 100000
Because the data range of this question is small and each value may have a few hundred or more values, the number of times each value may be obtained from 1 to 100000 and the total number of operations are recorded, finally, traverse from 1 to 100000 and obtain the smallest ans.
Ps: the individual competition has done countless code sprees in the afternoon ...... the code is optimized at night ..........
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Define eps 1e-6 # define LL long using namespace std; // const int maxn = 100 + 5; const int INF = 100000000; int n, abc; int vol [100010], ans [100010]; // vol indicates the number of times each capacity may be obtained. ans indicates the total number of operations required for obtaining the capacity int main () {// freopen(input.txt, r, stdin ); while (scanf (% d, & n) = 1) {memset (vol, 0, sizeof (vol); memset (ans, 0, sizeof (ans )); for (int I = 0; I <n; I ++) {cin> abc; int t = abc; int cnt = 1, cnt1 = 0; abc <= 1; w Hile (abc <= 100000) {vol [abc] ++; ans [abc] + = cnt; cnt ++; abc <= 1 ;}cnt = 0; while (t % 2 = 0) {vol [t] ++; ans [t] + = cnt; cnt ++; cnt1 ++; t >>=1 ;} vol [t] ++; ans [t] + = cnt; t> = 1; cnt1 ++; while (t! = 0) {int u = t; int cnt2 = cnt1; while (u <= 100000) {vol [u] ++; ans [u] + = cnt2; cnt2 ++; u <= 1 ;}if (t % 2 = 0) {while (t % 2 = 0) {t >>=1; cnt1 ++; vol [t] ++; ans [t] + = cnt1 ;}}t >>= 1; cnt1 ++ ;}} int pans = INF; for (int I = 0; I <= 100000; I ++) if (vol [I] = n) pans = min (pans, ans [I]); // cout <vol [2] <endl <vol [1] <endl <vol [8] <endl; cout <pans <endl ;} return 0 ;}
??