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
題意描述
給定一個(gè)a和b,將初始值為1的數(shù)字,通過盡可能少的操作0(將數(shù)字變?yōu)樵瓉淼?span id="ozvdkddzhkzd" class="katex--inline">12\frac{1}{2}21?)和操作1(將數(shù)字變?yōu)?-原來的數(shù)字),變成值為a2b\frac{a}{2^b}2ba?
題目分析
一開始以為和目標(biāo)數(shù)字的二進(jìn)制有關(guān),但題目其實(shí)保證了目標(biāo)數(shù)字絕對(duì)能通過所給的兩個(gè)操作算出來,所以逆向思維。從目標(biāo)數(shù)字出發(fā),若當(dāng)前數(shù)字大于12\frac{1}{2}21?則說明當(dāng)前數(shù)字是由某個(gè)數(shù)字通過操作1之后得到的,因?yàn)榻?jīng)過操作1和操作0的數(shù)字必定小于1。如果通過操作0而得到一個(gè)大于12\frac{1}{2}21?的數(shù)字,那么原數(shù)字則大于1。這是顯然錯(cuò)誤的。若當(dāng)前數(shù)字小于12\frac{1}{2}21?,則說明當(dāng)前數(shù)字是通過操作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; }總結(jié)
以上是生活随笔為你收集整理的B-Traveling Salesman Problem[CF-Gym-102134][2016-2017 7th BSUIR Open Programming Contest]的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 如何用html制作博客页面,HTML个人
- 下一篇: “莫兰迪色系” 高级灰