B-Traveling Salesman Problem[CF-Gym-102134][2016-2017 7th BSUIR Open Programming Contest]
原題傳送門
題面
Traveling Salesman Problem
time limit per test2 secondsmemory limit per test64 megabytesinputstandard inputoutputstandard outputproblem descride
Nowadays British scientists has been investigating the opportunities to increase the performance of traditional computers. Recent research has shown that changing logical zero to logical right shift ( x = x / 2) and logical one to logical negation ( x = 1 - x) will lead to a significant architecture enhancement. Thus, new architecture will be able to solve some NP hard problems. Assume that mentioned above basic operations will be new zero and new one, respectively.
For example, well known traveling salesman problem solved in the proposed architecture will be equivalent to obtaining number (fraction) a2b\frac{a}{2^b}2ba? from a number of one (initial state). Therefore, you are required to write a program (sequence of zeros and ones) to solve traveling salesman problem for given a and b.
Input
Single line contains two integer numbers a and b.
1≤b≤601 ?≤?b?≤? 601?≤?b?≤?60,
1≤a<2b1 ?≤?a?<?2^b1?≤?a?<?2b,
a is odd.
Output
Single line should contain a binary sequence, which is a solution for traveling salesman problem using new architecture. If there are multiple solutions, the minimal length program should be printed.
Sample Input
3 3
Sample Output
0010
題意描述
給定一個a和b,將初始值為1的數字,通過盡可能少的操作0(將數字變為原來的12\frac{1}{2}21?)和操作1(將數字變為1-原來的數字),變成值為a2b\frac{a}{2^b}2ba?
題目分析
一開始以為和目標數字的二進制有關,但題目其實保證了目標數字絕對能通過所給的兩個操作算出來,所以逆向思維。從目標數字出發,若當前數字大于12\frac{1}{2}21?則說明當前數字是由某個數字通過操作1之后得到的,因為經過操作1和操作0的數字必定小于1。如果通過操作0而得到一個大于12\frac{1}{2}21?的數字,那么原數字則大于1。這是顯然錯誤的。若當前數字小于12\frac{1}{2}21?,則說明當前數字是通過操作0得到的。從而逆推出正確的操作順序。
具體代碼
#include <iostream> #include <algorithm> #include <vector> #include <map>using namespace std;const int maxn = 1e6 + 7;long long a, b, cnt; int ans[maxn];long long qpow(long long a, long long x) {long long res = 1;while (x){if(x&1) res *= a;a = a * a;x /= 2;}return res; }int main() {cnt = 0;cin >> a >> b;b = qpow(2, b);while(a != b){if (2 * a > b){a = b - a;ans[++cnt] = 1;}else{b /= 2;ans[++cnt] = 0;}}for (int i = cnt; i >= 1; i--) cout << ans[i];return 0; }總結
以上是生活随笔為你收集整理的B-Traveling Salesman Problem[CF-Gym-102134][2016-2017 7th BSUIR Open Programming Contest]的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 如何用html制作博客页面,HTML个人
- 下一篇: “莫兰迪色系” 高级灰