列表

详情


NC24165. [USACO 2015 Jan B]It's All About the Base

描述

Bessie the cow has been taking computing classes at her local college (or "cow-ledge", in her case), and she has been very excited to recently learn about writing numbers in different bases. Recall that a number written in base B has digit places representing 1, B, B^2, B^3, and so on from right to left. For example, in our familiar base 10 numbering system, we have digits representing 1's, 10's, 100's, 1000's and so on. The sequence of digits 1234, interpreted in base 10, really means 1(1000) + 2(100) + 3(10) + 4(1). The same sequence of digits 1234, interpreted in base 5, would mean 1(125) + 2(25) + 3(5) + 4(1), which adds up to the number 194 in base 10. Bessie notices that if the base increases, so does the number represented by a sequence of digits -- for example, 1234 in base 7 represents a larger number than 1234 in base 6. 
When writing numbers in base B, each digit can range from 0 through B-1, so for example in base 10 each digit is in the range 0..9, and in base 5 each digit is in the range 0..4. It is entirely possible to consider bases larger than 10. 
 Computer scientists often use base 16 ("hexadecimal"), where the letters A..F represent digits of values 10..15. For example, BEEF in hexadecimal corresponds to 11(4096) + 14(256) + 14(16) + 15, which adds up to the number 48879 in base 10. Bessie is intrigued by the concept of using bases much larger than 10. She takes a number N and writes it down in two different bases X and Y, where both X and Y are in the range 10..15,000. Interestingly, in both cases, she gets a sequence of 3 digits, each of which happens to be only in the range 1..9. Unfortunately, due to Bessie's poor memory, she has now forgotten N, X, and Y! Given just the two 3-digit sequences she wrote down, please help her figure the two bases X and Y that she used. 
Note that due to the potential size of X and Y, a program that searches exhaustively over every possible value of X and Y (nearly 15,000^2 possibilities!) will not run within the time limit, so it would not receive full credit.

输入描述

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 is
792.

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C++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;
}

上一题