1010. Radix (25)

简直是哭着做出来的,起码提交了22遍。创纪录了。。。

首先不能像上次那样枚举进制数,那样输入987654321 10 1 10 的话十分钟都算不完,题中只有一个超时很照顾人了。。。

考虑近似计算,利用y=a*x^n+b 算出n,进制数如果有,也不会超过n,因此考虑由n向下寻找。

注意,进制数不能小于数中最大的数字。

如果将两个数字作为字符串输入,注意排除字符串前面的0.

时间限制

400 ms

内存限制

65536 kB

代码长度限制

16000 B

判题程序

Standard

作者

CHEN, Yue

Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 110 be true? The answer is "yes", if 6 is a decimal number and 110 is a binary number.

Now for any pair of positive integers N1 and N2, your task is to find the radix of one number while that of the other is given.

Input Specification:

Each input file contains one test case. Each case occupies a line which contains 4 positive integers:
N1 N2 tag radix
Here N1 and N2 each has no more than 10 digits. A digit is less than its radix and is chosen from the set {0-9, a-z} where 0-9 represent the decimal numbers 0-9, and a-z represent the decimal numbers 10-35. The last number "radix" is the radix of N1 if "tag" is 1, or of N2 if "tag" is 2.

Output Specification:

For each test case, print in one line the radix of the other number so that the equation N1 = N2 is true. If the equation is impossible, print "Impossible". If the solution is not unique, output the smallest possible radix.

Sample Input 1:

6 110 1 10

Sample Output 1:

2

Sample Input 2:

1 ab 1 2

Sample Output 2:

Impossible

来源: <http://www.patest.cn/contests/pat-a-practise/1010>

  1. #include <iostream>
  2. #include <string>
  3. #include <stdio.h>
  4. #include <math.h>
  5. #pragma warning(disable:4996)
  6. using namespace std;
  7. long long int GetNumofStr(string s,int index) {
  8. if (index < 2)
  9. return -1;
  10. long long int n = 0;
  11. for (int i = 0; i < s.length(); i++) {
  12. n *= index;
  13. if (s[i] >= ‘0‘&&s[i] <= ‘9‘)
  14. n += s[i] - ‘0‘;
  15. else if (s[i] >= ‘a‘&&s[i] <= ‘z‘)
  16. n += s[i] - ‘a‘ + 10;
  17. }
  18. return n;
  19. }
  20. int main(void) {
  21. string s1, s2;
  22. int tag, index;
  23. cin >> s1 >> s2 >> tag >> index;
  24. long long int s1num = 0, s2num = 0;
  25. s1num = GetNumofStr(s1, 10);
  26. s2num = GetNumofStr(s2, 10);
  27. if (s1num == 0 && s2num == 0) {
  28. cout << "1";
  29. return 0;
  30. }
  31. else if (s1num*s2num == 0) {
  32. cout << "Impossible";
  33. return 0;
  34. }
  35. if (tag == 2) {
  36. string temp;
  37. temp = s1;
  38. s1 = s2;
  39. s2 = temp;
  40. }
  41. bool zeroHeadRemove = false;
  42. string stemp;
  43. for (int i = 0; i < s2.length(); i++) {
  44. if (s2[i] != ‘0‘)
  45. zeroHeadRemove = true;
  46. if (zeroHeadRemove == true)
  47. stemp += s2[i];
  48. }
  49. s2 = "";
  50. s2 = stemp;
  51. s1num = GetNumofStr(s1, index);
  52. int startIndex = 0;
  53. long long int temp;
  54. if (s2[s2.length() - 1] >= ‘0‘&&s2[s2.length() - 1] <= ‘9‘) {
  55. temp = s1num - s2[s2.length() - 1]+‘0‘;
  56. }
  57. else if (s2[s2.length() - 1] >= ‘a‘&&s2[s2.length() - 1] <= ‘z‘) {
  58. temp = s1num -s2[s2.length() - 1]+‘a‘-10;
  59. }
  60. if (s2[0] >= ‘0‘&&s2[0] <= ‘9‘) {
  61. temp /= (s2[0] - ‘0‘);
  62. }
  63. else if (s2[0] >= ‘a‘&&s2[0] <= ‘z‘) {
  64. temp /= (s2[0] - ‘a‘+10);
  65. }
  66. startIndex = pow(temp, 1.0 / (s2.length() - 1));
  67. char s2max = 0;
  68. for (int i = 0; i < s2.length(); i++) {
  69. if (s2[i] > s2max)
  70. s2max = s2[i];
  71. }
  72. if (s2max >= ‘0‘&&s2max <= ‘9‘) {
  73. if (startIndex < s2max-‘0‘)
  74. startIndex = s2max - ‘0‘+1;
  75. }
  76. if (s2max >= ‘a‘&&s2max <= ‘z‘) {
  77. if (startIndex < s2max - ‘a‘+10)
  78. startIndex = s2max - ‘a‘+11;
  79. }
  80. bool possible = false;
  81. while (true)
  82. {
  83. s2num = GetNumofStr(s2, startIndex);
  84. if (s2num == s1num) {
  85. possible = true;
  86. break;
  87. }
  88. else if (s2num < s1num) {
  89. break;
  90. }
  91. startIndex--;
  92. if (s2max >= ‘0‘&&s2max <= ‘9‘) {
  93. if (startIndex < s2max - ‘0‘)
  94. break;
  95. }
  96. if (s2max >= ‘a‘&&s2max <= ‘z‘) {
  97. if (startIndex < s2max - ‘a‘ + 10)
  98. break;
  99. }
  100. }
  101. if (possible == true) {
  102. cout << startIndex;
  103. }
  104. else
  105. cout << "Impossible";
  106. return 0;
  107. }

来自为知笔记(Wiz)

时间: 2024-10-22 19:05:25

1010. Radix (25)的相关文章

PAT 1010. Radix (25)

1010. Radix (25) Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 110 be true? The answer is "yes", if 6 is a decimal number and 110 is a binary number. Now for any pair of positive integers N1 and N2, your task i

1010. Radix (25)——PAT (Advanced Level) Practise

题目信息: 1010. Radix (25) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 110 be true? The answer is "yes", if 6 is a decimal number and 110 is a b

1010. Radix (25)(进制 + 二分 + 模拟)

Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 110 be true? The answer is "yes", if 6 is a decimal number and 110 is a binary number. Now for any pair of positive integers N1 and N2, your task is to find the rad

1010 Radix(25 分)

1010 Radix(25 分) Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 110 be true? The answer is yes, if 6 is a decimal number and 110 is a binary number. Now for any pair of positive integers N?1?? and N?2??, your task is

1010 Radix

1010 Radix 注意点 如111 1 1 10类似情况下,若n为个位数,如果本身比另一个数小,则多少的进制都是没有用的(可能会造成空循环而超时),不过好像没有这么一个测试用例 进制应该比最少数据中的最大的数要大一,如8最少是9进制的数,z最少是36进制的数 注意算法中的超时问题如9999999999 11 1 10很容易超时,注意匹配的算法 如果用c等强类型语言写,注意溢出问题,int类型肯定是不够的 python3代码 def getNum(num, radix): sum = 0 co

A1010. Radix (25)

Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 110 be true? The answer is "yes", if 6 is a decimal number and 110 is a binary number. Now for any pair of positive integers N1 and N2, your task is to find the rad

PAT 1010 Radix

过了一天感觉没干什么,想刷一发题弥补一下,正在考虑去做哪道,室友说去试试PAT 1010,果然当年自己直接跳过了这题,一看这通过率(0.07)有点夸张了.题目是已知一个数,求另外一个数的基数,使得这两个数数值上相等.很自然的考虑到使用二分搜索来确定这个基数,数字表示使用[0-9a-z],这tmd的让人很容易的想到基数的范围就在1~36之间了,艹,基数是可以超过这个范围的,如果没有考虑到这一点,可以得到的一个典型分值就是19分.不过基数在[1, 36]之间的话这个搜索范围太小了,直接暴力遍历也可以

pat Radix(25分)

Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 110 be true? The answer is yes, if 6 is a decimal number and 110 is a binary number. Now for any pair of positive integers N?1?? and N?2??, your task is to find the radi

pat1010. Radix (25) BUG!!!

#include<stdio.h> #include<stdlib.h> #include<string.h> int c2int(char c){ if('0'<=c&&c<='9') return c-'0'; else return c-'a'+10; } long s2decimal(char str[], long radix){ long digit, sum=0; // 必须判断每一位是否小于radix int i=0; for