sgu154:非常好的數(shù)論+二分題目 You task is to find minimal natural number N, so that N! contains exactly Q zeroes on the trail in decimal notation. As you know N! = 1*2*...*N. For example, 5! = 120, 120 contains one zero on the trail.
Input One number Q written in the input (0<=Q<=10^8).
Output Write "No solution", if there is no such number N, and N otherwise.
Sample test(s) Input 2 Output 10
首先要明白一件事x!末尾的0的個數(shù)至于2和5的個數(shù)有關(guān),又因為2的個數(shù)已經(jīng)多余5,所以階乘末尾 0的個數(shù)完全等價于所有數(shù)中5的個數(shù) 所以階乘末尾0的個數(shù)可以用如下函數(shù)計算 int count(int x) //count the num of 0s in x! { int res = 0; while(x > 0) { res += x / 5; x /= 5; } return res; } 然后題目要求末尾個數(shù)有n個0的x!中,x為多少 因為哦count函數(shù)具有單調(diào)增加的性質(zhì),所以完全可以二分尋找符合條件的x trick 1.n == 0 ,時答案是1 trick 2.二分出來的結(jié)果有可能應(yīng)該輸出No Solution !(具體原因自己考慮一下)
sgu175:經(jīng)典 Let phi(W) is the result of encoding for algorithm: 1. If the length of W is 1 then phi(W) is W; 2. Let coded word is W = w1w2...wN and K = N / 2 (rounded down); 3. phi(W) = phi(wNwN-1...wK+1) + phi(wKwK-1...w1). For example, phi('Ok') = 'kO', phi('abcd') = 'cdab'. Your task is to find position of letter wq in encoded word phi(W).
Input Given integers N, q (1 <= N <= 10^9; 1<= q <= N), where N is the length of word W.
Output Write position of letter wq in encoded word phi(W).
google 了以下發(fā)現(xiàn)了一個很好的想法,以下是我跟據(jù)那個想法寫的遞歸版本 LL n, q; int bin(LL n, LL q) { if(n <= 1) return 1; LL k = n / 2; if (q > k) { return bin(n - k, n - q + 1); } else { return n - k + bin(k, k - q + 1); } }