#include<bits/stdc++.h>
using namespace std;
unsigned long long int x[52][52];
void binomial()
{
int n, k;
x[0][0] = 1;
for(n=1; n<=51; n++)
{
x[n][0] = x[n][n] = 1;
for(k=1; k<n; k++)
x[n][k] = x[n-1][k-1] + x[n-1][k];
}
}
int main()
{
binomial();
int w, o;
while(cin >> w)
{
for(o=1; o<=w; o++)
{
string s, a = "", b = "";
cin >> s;
int i, j, p = 0;
i = 1;
while(s[i] != '+')
{
a.push_back(s[i]);
i++;
}
i = i+1;
while(s[i] != ')')
{
b.push_back(s[i]);
i++;
}
i = i + 2;
while(i<s.size())
{
p = p * 10 + (s[i] - 48);
i++;
}
cout << "Case " << o << ": ";
if(p == 1)
{
cout << a << '+' << b << endl;
continue;
}
int c = p, d = 0, f = 0;
i=p, j=0;
while(c >= 0 && d <=p)
{
if(j != 0)
cout << '+';
if(x[i][j] > 1)
cout << x[i][j] << '*';
if(c == 1)
{
f = 1;
cout << a;
}
else if(c > 1)
{
f = 1;
cout << a << '^' <<c;
}
if(d == 1)
{
if(f == 1)
cout << '*';
cout << b;
}
else if(d > 1)
{
if(f == 1)
cout << '*';
cout << b << '^' << d;
}
j++, c--, d++, f = 0;
}
cout << endl;
}
}
return 0;
}
using namespace std;
unsigned long long int x[52][52];
void binomial()
{
int n, k;
x[0][0] = 1;
for(n=1; n<=51; n++)
{
x[n][0] = x[n][n] = 1;
for(k=1; k<n; k++)
x[n][k] = x[n-1][k-1] + x[n-1][k];
}
}
int main()
{
binomial();
int w, o;
while(cin >> w)
{
for(o=1; o<=w; o++)
{
string s, a = "", b = "";
cin >> s;
int i, j, p = 0;
i = 1;
while(s[i] != '+')
{
a.push_back(s[i]);
i++;
}
i = i+1;
while(s[i] != ')')
{
b.push_back(s[i]);
i++;
}
i = i + 2;
while(i<s.size())
{
p = p * 10 + (s[i] - 48);
i++;
}
cout << "Case " << o << ": ";
if(p == 1)
{
cout << a << '+' << b << endl;
continue;
}
int c = p, d = 0, f = 0;
i=p, j=0;
while(c >= 0 && d <=p)
{
if(j != 0)
cout << '+';
if(x[i][j] > 1)
cout << x[i][j] << '*';
if(c == 1)
{
f = 1;
cout << a;
}
else if(c > 1)
{
f = 1;
cout << a << '^' <<c;
}
if(d == 1)
{
if(f == 1)
cout << '*';
cout << b;
}
else if(d > 1)
{
if(f == 1)
cout << '*';
cout << b << '^' << d;
}
j++, c--, d++, f = 0;
}
cout << endl;
}
}
return 0;
}
Comments
Post a Comment