NC24165. [USACO 2015 Jan B]It's All About the Base
描述
输入描述
The input file starts with an integer K, then it contains K lines each
specifying a separate test case. Each test case consists of two
3-digit numbers. The first is a number N written in base X, and the
second is N written in base Y (N, X, and Y are possibly different for
each test case).
输出描述
Your output should contain K lines, one for each test case. On each
line, output the two numbers X and Y for the relevant test case,
separated by a space. A unique solution for each case is guaranteed
to exist.
示例1
输入:
1 419 792
输出:
47 35
说明:
The number 8892, written in base 47, is 419. Written in base 35, it isC++14(g++5.4) 解法, 执行用时: 14ms, 内存消耗: 504K, 提交时间: 2020-09-12 11:03:38
#include<iostream> #include<cstdio> #include<algorithm> #include<cstring> #include<queue> #include<bitset> #include<time.h> #include<string> #include<cmath> #include<stack> #include<map> #include<set> #define ui unsigned int //ios::sync_with_stdio(false); #define max(a,b) ((a)>(b)?(a):(b)) #define min(a,b) ((a)<(b)?(a):(b)) #define ull unsigned long long #define endd puts(""); #define re register #define endl "\n" #define int long long #define double long double #define il inline using namespace std; #define PI 3.1415926535898 const double eqs = 1e-8; const long long max_ = 1e5 + 7; const int mod = 1000000007; const int inf = 1e9 + 7; const long long INF = 2e18 + 7; inline int read() { int s = 0, f = 1; char ch = getchar(); while (ch<'0' || ch>'9') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0'&&ch <= '9') { s = s * 10 + ch - '0'; ch = getchar(); } return s * f; } inline void write(int x) { if (x < 0) { putchar('-'); x = -x; } if (x > 9) write(x / 10); putchar(x % 10 + '0'); } int N, M, C,node[max_]; bool check(int standard) { int num = 1,cap = 0; for (re int i = 1; i <= N; i++) { if (cap + 1 <= C) { if (node[i] - node[i - cap] <= standard) { cap++; } else { cap = 1; num++; } } else { cap = 1; num++; } }if (num <= M)return 1; else return 0; } il pair<int, int>cal(int a, int b, int c) { return make_pair((-b + (int)sqrt(b * b - 4 * a * c)) / (2 * a), (-b - (int)sqrt(b * b - 4 * a * c)) / (2 * a)); } int A[5], B[5]; signed main() { int T = read(),temp,a,b,c,ans; pair<int, int> pa; while (T--){ temp = read(); A[1] = temp / 100; A[2] = (temp / 10)%10; A[3] = temp % 10; temp = read(); B[1] = temp / 100; B[2] = (temp / 10) % 10; B[3] = temp % 10; for (int i = 10; i <= 15000; i++) { a = B[1]; b = B[2]; c = B[3] - A[3] - A[1] * i *i - A[2] * i; pa = cal(a, b, c); if (pa.first >= 10 && pa.first <= 15000 && A[3] + A[1] * i *i + A[2] * i == B[3] + B[1] * pa.first *pa.first + B[2] * pa.first) { cout << i << " " << pa.first << endl; break; } if (pa.second >= 10 && pa.second <= 15000 && A[3] + A[1] * i *i + A[2] * i == B[3] + B[1] * pa.second *pa.second + B[2] * pa.second) { cout << i << " " << pa.second << endl; break; } } } return 0; }
Python3(3.5.2) 解法, 执行用时: 183ms, 内存消耗: 3388K, 提交时间: 2020-09-15 16:11:14
def helper(): nums = list(map(int, input().split())) x = [nums[0] % 10, (nums[0] // 10) % 10, nums[0] // 100] y = [nums[1] % 10, (nums[1] // 10) % 10, nums[1] // 100] t1, t2 = nums[0], nums[1] bs1, bs2 = 10, 10 while t1 != t2: if t1 < t2: bs1 += 1 t1 = x[0] + x[1] * bs1 + x[2] * bs1 * bs1 else: bs2 += 1 t2 = y[0] + y[1] * bs2 + y[2] * bs2 * bs2 print("{} {}".format(bs1, bs2)) n = int(input()) for i in range(n): helper()
C++11(clang++ 3.9) 解法, 执行用时: 4ms, 内存消耗: 484K, 提交时间: 2020-09-14 21:55:04
#include<bits/stdc++.h> #define int long long using namespace std; int b[3],c[3]; void A() { int x,y,tx,ty; scanf("%lld%lld",&x,&y); b[0]=x%10; b[1]=x/10%10; b[2]=x/100; c[0]=y%10; c[1]=y/10%10; c[2]=y/100; tx=x; ty=y; x=y=10; while(tx!=ty) { if(tx<ty) { x++; tx=b[0]+b[1]*x+b[2]*x*x; } else { y++; ty=c[0]+c[1]*y+c[2]*y*y; } } printf("%lld %lld\n",x,y); } signed main() { int t; scanf("%lld",&t); while(t--) A(); return 0; }