CodeForces - 1167B
传送门https://codeforces.com/contest/1167/problem/B
题意
交互题:现在你有6个数4, 8, 15, 16, 23, 42组成的某种组合,你可以询问系统四个问题,每次询问的格式为?i j
然后系统会回复你ai*aj的值,问你这个组合是哪种,题目肯定有解且解是唯一的。
思路
我们可以发现这6个数的乘积两两都不相等,如果我们询问a[1]和a[2] 和 a[1]和a[3] 那么我们就可以确定 a[1],a[2],a[3] ,
如果我们询问a[4]和a[5] 和 a[4]和a[6] 那么我们就可以确定 a[4],a[5],a[6] 。
也就是说实际上, 只要我知道询问了"a[1]*a[2], a[1]*a[3], a[4]*a[5], a[4]*a[6]."就能唯一确定一种排列, 那么我们就可以枚举所有的排列, 合适的那个就是答案了...(next_permutation)
代码
1 #include <iostream> 2 #include <algorithm> 3 #include <cstdio> 4 #include <cmath> 5 #include <string> 6 #include <string.h> 7 #include <map> 8 //#include <unordered_map> 9 #include <set> 10 #include <vector> 11 #include <stack> 12 #include <queue> 13 #include <stack> 14 #include <list> 15 #define Fbo friend bool operator < (node a, node b) 16 #define mem(a, b) memset(a, b, sizeof(a)) 17 #define FOR(a, b, c) for(int a = b; a <= c; a++) 18 #define RFOR(a,b, c) for(int a = b; a >= c; a--) 19 #define off ios::sync_with_stdio(0) 20 bool check1(int a) { return (a & (a - 1)) == 0 ? true : false; } 21 22 using namespace std; 23 typedef pair<int, int> pii; 24 typedef long long ll; 25 const int INF = 0x3f3f3f3f; 26 const int mod = 1e9 + 7; 27 const int Maxn = 2e5 + 5; 28 const double pi = acos(-1.0); 29 const double eps = 1e-8; 30 31 int a[6] = { 4,8,15,16,23,42 }; 32 int b[4]; 33 34 int main() { 35 off; 36 cout << "? 1 2" << endl; 37 cin >> b[0]; 38 cout << "? 2 3" << endl; 39 cin >> b[1]; 40 cout << "? 4 5" << endl; 41 cin >> b[2]; 42 cout << "? 5 6" << endl; 43 cin >> b[3]; 44 do { 45 if (a[0] * a[1] != b[0]) continue; 46 if (a[1] * a[2] != b[1]) continue; 47 if (a[3] * a[4] != b[2]) continue; 48 if (a[4] * a[5] != b[3]) continue; 49 break; 50 } while (next_permutation(a, a + 6)); 51 cout << "!"; 52 for (auto x : a) cout << ' ' << x; 53 return 0; 54 }

浙公网安备 33010602011771号