[TSOJ Course] 1119 Decision System

Keywords: less

Course 29_14 1119 Decision System

Title:

Title Description:

6 x 9 = 42 is wrong for decimal, but correct for 13. That is, 6 (13) *9 (13) = 42 (13), and 42 (13) = 4 *13 + 2 = 54 (10).
Your task is to write a program that reads in three integers p, q and r, and then determines a binary B (2 <= B <= 16) so that p * q = r. If B has many choices, the output is the smallest.
For example, when p = 11, q = 11, r = 121, there is 11 (3) *11 (3) = 121 (3). Because 11 (3) = 1 * 31 + 1 * 30 = 4 (10) and 121 (3) = 1 * 32 + 2 * 31 + 1 * 30 = 16 (10). For base 10, there is 11 (10) *11 (10) = 121 (10). In this case, output 3. If there is no suitable binary, the output is 0.

Input Description:

The input has T group test samples. T is given in the first line. Each set of test samples takes up one line and contains three integers p, q, r. All bits of p, q, r are numbers, and 1 <= p, q, r <= 1,000,000.

Output description:

Output one line for each test case. This row contains an integer, even if the smallest B that p * q = r holds. If there is no suitable B, output 0.

Sample input:

3
6 9 42
11 11 121
2 2 2

Sample output:

13
3
0

Analysis:

Very routine enumeration questions mainly focus on the principle of binary conversion.

Because the data given in the title is "unknown digits" (that is, not equal to 10 digits), we only need to know how to convert X digits to 10 digits.

Formulas for converting an arbitrary number to a decimal number:

num10 = d0 x D0 + d1 x D1 + d2 x D2 + ...... + dn x Dn

Among them, dn refers to the n+1 bit from right to left, such as d0 is a bit, d1 is ten, etc. D refers to the process of digits. For example, when 5-digit is converted to 10-digit, D is 5.

Supplementary knowledge
The method of converting decimal system to arbitrary system is rolling phase division.
The method is to divide n by D, write the remainder on the left, write the quotient below, and then continue until the quotient is less than D, and then read the remainder from bottom to top.

As shown in the figure, 2018_16 = 126... 2. Write 2 on the left and 126_16 = 7... 14. Write 14 on the left. At this time, 7 is less than 16, stop and read from bottom to top: 7, 14, 2, of which 14 we write E, so 2018 (10) = 7E 2 (16). Brackets denote the decimal number of this number.

Problem solving:

Reference code:

// TSOJ-1119 deterministic system
#include <stdio.h>
#include <math.h>

int to10(int, int);

int main()
{
	int T;
	int p,q,r,tp,tq,tr;
	int i;
	scanf("%d",&T);
	for(;T--;){
		scanf("%d %d %d",&p,&q,&r);
		for(i=2; i<17; i++){
			tp = to10(p,i);
			if(tp==-1)
				continue;
			tq = to10(q,i);
			if(tq==-1)
				continue;
			tr = to10(r,i);
			if(tr==-1)
				continue;
			if(tp*tq==tr){
				break;
			}else{ // tp*tq>tr
				continue;
			}
		}
		if(i>=17){
			printf("0\n");
		}else{
			printf("%d\n",i);
		}
	}
	return 0;
}

int to10(int num, int base)
{
	int ret=0,dig=0,temp;
	while(num>0){
		temp = num%10;
		if(temp>=base){
			return -1; // invalid 
		}
		ret += temp*pow(base,dig++);
		num /= 10;
	}
	return ret;
}

In fact, there is an optimization method, which I calculated by accident. If the relationship is PQ > r, then the decimal system is more than 10, otherwise it is less than 10.

Moreover, if the decimal system is more than 10, we first convert it into tp, tq, tr as the D-ary system. Obviously, if tptq=tr, you will find the answer, but if tptq > tr, you can't find it any more. This certificate is still quite roundabout, if you are interested, you can try to prove it yourself.

The purpose of this optimization method is that it does not need to know the limit of the binary. The traditional method is to enumerate all the binary systems from 2 to 16. If no condition is satisfied, it means No. This method requires us to know the range of the binary system. Now the optimization algorithm does not need to know, it will automatically terminate the search when TP * TQ > tr.

// TSOJ-1119 deterministic system
#include <stdio.h>
#include <math.h>

int to10(int, int);

int main()
{
	int T;
	int p,q,r,tp,tq,tr;
	int i;
	scanf("%d",&T);
	for(;T--;){
		scanf("%d %d %d",&p,&q,&r);
		if(p*q>r){
			// 10+ base
			for(i=11;i<17;i++){
				tp = to10(p,i);
				tq = to10(q,i);
				tr = to10(r,i);
				if(tp*tq==tr){
					break;
				}else if(tp*tq<tr){ // No more.
					tr = -1;
					break;
				}else{ // tp*tq>tr
					continue;
				}
			}
			if(tr==-1 || i>=17){
				// I >= 17 is the requirement of the title. If the title is not in the base range, the value here determines whether tr is equal to -1 or not.
				printf("0\n");
			}else{ // tr!=-1 and i<17
				printf("%d\n",i);
			}
		}else{
			// 2~10 base
			for(i=2;i<=10;i++){
				tp = to10(p,i);
				if(tp==-1)
					continue;
				tq = to10(q,i);
				if(tq==-1)
					continue;
				tr = to10(r,i);
				if(tr==-1)
					continue;
				
				if(tp*tq==tr){
					break;
				}else if(tp*tq<tr){
					tr = -1;
					break;
				}else{ // tp*tq>tr
					continue;
				}				
			}
			if(tr==-1 || i>10){
				// No
				printf("0\n");
			}else{ // tr!=-1 and i<=10
				printf("%d\n",i);
			}
		}
	}
	return 0;
}

int to10(int num, int base)
{
	int ret=0,dig=0,temp;
	while(num>0){
		temp = num%10;
		if(temp>=base){
			return -1; // invalid 
		}
		ret += temp*pow(base,dig++);
		num /= 10;
	}
	return ret;
}

Posted by Dale on Thu, 25 Apr 2019 11:33:35 -0700