1009. Product of Polynomials (25)
This time, you are supposed to find A*B where A and B are two polynomials.
Input Specification:
Each input file contains one test case. Each case occupies 2 lines, and each line contains the information of a polynomial: K N1 aN1 N2 aN2 ... NK aNK, where K is the number of nonzero terms in the polynomial, Ni and aNi (i=1, 2, ..., K) are the exponents and coefficients, respectively. It is given that 1 <= K <= 10, 0 <= NK < ... < N2 < N1 <=1000.
Output Specification:
For each test case you should output the product of A and B in one line, with the same format as the input. Notice that there must be NO extra space at the end of each line. Please be accurate up to 1 decimal place.
Sample Input2 1 2.4 0 3.2 2 2 1.5 1 0.5Sample Output
3 3 3.6 2 6.0 1 1.6
#include<stdio.h>
#include<string.h>
double str1[1002], str2[1002];
double res[2004];
int main()
{
freopen("E://input.txt", "r", stdin);
memset(str1, 0, sizeof(str1));
memset(str2, 0, sizeof(str2));
memset(res, 0, sizeof(res));
int n, tmp;
scanf("%d", &n);
for(int i = 0; i < n; i ++)
{
scanf("%d", &tmp);
scanf("%lf", &str1[tmp]);
}
scanf("%d", &n);
for(int i = 0; i < n; i ++)
{
scanf("%d", &tmp);
scanf("%lf", &str2[tmp]);
}
for(int i = 0; i < 1002; i ++)
{
for(int j = 0; j < 1002; j ++)
{
res[i+j] = res[i+j] + str1[i]*str2[j];
}
}
int count = 0;
for(int i = 2004; i >= 0; i --)
{
if(res[i] != 0)
count ++;
}
printf("%d", count);
for(int i = 2004; i >= 0; i --)
{
if(res[i] != 0)
{
count --;
printf(" %d %.1f", i, res[i]);
}
}
printf("\n");
return 0;
}
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 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 10Sample Output 1:
2Sample Input 2:
1 ab 1 2Sample Output 2:
Impossible
#include<iostream>
#include<string>
#include<stdio.h>
using namespace std;
long long to_radix(string num, long long radix)
{
long long res = 0;
for(int i = 0; i < num.length(); i ++)
{
int x;
if(num[i] >= '0' && num[i] <= '9')
x = num[i] - '0';
else
x = num[i] - 'a' + 10;
res = res * radix + x;
}
return res;
}
int main()
{
freopen("E://input.txt", "r", stdin);
string str1, str2, num;
int tag;
long long radix, base;
cin>>str1>>str2>>tag>>radix;
if(tag == 1)
{
num = str2;
base = to_radix(str1, radix);
}
else
{
num = str1;
base = to_radix(str2, radix);
}
//如果num只有一位
if(num.length() == 1)
{
int x;
if(num[0] >= '0' && num[0] <= '9')
x = num[0] - '0';
else
x = num[0] - 'a' + 10;
if(x == base)
cout<<x+1<<endl;
else
cout<<"Impossible"<<endl;
return 0;
}
long long minRadix = 0, maxRadix = base;
//最小的Radinx肯定比num中大,eg. 078最少是9进制,那么最大呢,最大情况就是num就是base-1,这样的话最大maxRadix为base
for(int i = 0; i < num.length(); i ++)
{
int x;
if(num[i] >= '0' && num[i] <= '9')
x = num[i] - '0';
else
x = num[i] - 'a' + 10;
if(x + 1 > minRadix)
minRadix = x + 1;
}
//采用二分查找方法找到基数
while(minRadix <= maxRadix)
{
long long middle = (minRadix + maxRadix) / 2;
long long x = to_radix(num, middle);
if(x == base)
{
cout<<middle<<endl;
return 0;
}
else if(x > base || x < 0)
maxRadix = middle - 1;
else
minRadix = middle + 1;
}
cout<<"Impossible"<<endl;
return 0;
}