Dr. Orooji, a number theoretician on the side, has written an amazing program that prime fectorizes numbers. He is so ecstatic that his program works, that he has forgotten to properly format (clean up) the output of his program. Luckily, Dr. O has hired you as a student assistant, to do his grunt work, formatting the output of his ingenius program. Your goal is to take the output of Dr. O’s program, and nicely format it for everyone else.The Problem:
Dr. O’s output consists of several cases (data sets) which are theinput to your program. Each input case (to your program) contains several pairs of numbers, each pair being a base and an exponent. Although each of the bases are prime numbers already, Dr. O hasn’t bothered to list them in numerical order. Furthermore, sometimes he’s listed the same base several times - thus a 2^3 might be listed as well as a 2^5. Clearly these should be coalesced into one term, 2^8. Finally, Dr. O’s output does not put exponents in a “superscript” position. Your job will be to fix all three of these issues with each input case given.
The Input:
Input starts with a positive integer, n, on the first line by itself indicating the total number of test cases in the input file (i.e., the number of data sets to be processed). The next n lines contain the test cases, one per line. Each test case will contain several positive integers separated by spaces on a single line. The first positive integer on each line, k(0< k< 20), will represent the number of terms in the prime factorization for that case. This will be followed by k pairs of positive integers, all separated by spaces. The first integer of each pair will be a positive prime number less than 10,000 (representing a base). The second integer of each pair will be a positive integer less than 100,000, representing the exponent to which the corresponding prime number is raised in the prime factorization given.
The Output:
For each input case, first output thefollowing header line:Prime Factorization #m:where m(l ≤m≤n) represents the input case number (starting with 1).The rest of the output for each case will be on the following two lines. In particular, each base will be on the second line and each exponent will be on the first line. The bases will be in ascending numerical order. The corresponding exponents will start in the following column (after its base ends) on the first row. Each following base will start in the next column after the previous exponent ends.Leave a blank line after the output for each data set. Follow the format illustrated in Sample Output.
样例输入
3 2 5 101 17 3 13
4 5 2 5 19 3 5 11 1
5 2 10 3 11 2 12 3 13 2 14
样例输出
Prime Factorization #1:
5 13 17
2 3 101
Prime Factorization #2:
5 21 1
3 5 11
Prime Factorization #3:
36 24
2 3
#include<bits/stdc++.h>
using namespace std;
#define ll long long
int num[100010],shu[30],sh[30],bo[100010];
int main()
{
ios::sync_with_stdio(false);
int n,k;
cin>>n;
for(int tip=1;tip<=n;tip++)
{
cin>>k;
k*=2;
for(int i=1;i<=k;i++)
{
cin>>shu[i];
num[shu[i]]=0;bo[shu[i]]=0;
}
for(int i=2;i<=k;i+=2)
{
num[shu[i-1]]+=shu[i];
}
int top=0;
for(int i=1;i<=k;i+=2)
{
if(bo[shu[i]]==0)
{
sh[top]=shu[i];top++;bo[shu[i]]=1;
}
}
sort(sh,sh+top);
cout<<"Prime Factorization #"<<tip<<":"<<endl;
for(int i=0;i<top;i++)
{
int tmp=sh[i];
while(tmp)
{
cout<<" ";tmp/=10;
}
cout<<num[sh[i]];
}
cout<<endl;
for(int i=0;i<top;i++)
{
int tmp=num[sh[i]];
cout<<sh[i];
while(tmp)
{
cout<<" ";tmp/=10;
}
}
cout<<endl<<endl;
}
return 0;
}