2015
05-23

An educational software company, All Computer Math (ACM), has a section on multiplication of integers. They want to display the calculations in the traditional grade school format, like the following computation of 432 × 5678:

    432   5678-------   3456  3024 25922160-------2452896

Note well that the final product is printed without any leading spaces, but that leading spaces are necessary on some of the other lines to maintain proper alignment. However, as per our regional rules, there should never be any lines with trailing white space. Note that the lines of dashes have length matching the final product.

As a special case, when one of the digits of the second operand is a zero, it generates a single 0 in the partial answers, and the next partial result should be on the same line rather than the next line down. For example, consider the following product of 200001 × 90040:

     200001      90040-----------    8000040180000900-----------18008090040

The rightmost digit of the second operand is a 0, causing a 0 to be placed in the rightmost column of the first partial product. However, rather than continue to a new line, the partial product of 4 × 200001 is placed on the same line as that 0. The third and fourth least-significant digits of the second operand are zeros, each resulting in a 0 in the second partial product on the same line as the result of 9 × 200001.

As a final special case, if there is only one line in the partial answer, it constitutes a full answer, and so there is no need for computing a sum. For example, a computation of 246 × 70 would be formatted as

  246   70-----17220

Your job is to generate the solution displays.

The input contains one or more data sets. Each data set consists of two positive integers on a line, designating the operands in the desired order. Neither number will have more than 6 digits, and neither will have leading zeros. After the last data set is a line containing only 0 0.

The input contains one or more data sets. Each data set consists of two positive integers on a line, designating the operands in the desired order. Neither number will have more than 6 digits, and neither will have leading zeros. After the last data set is a line containing only 0 0.

432 5678
200001 90040
246 70
0 0

Problem 1
432
5678
-------
3456
3024
2592
2160
-------
2452896
Problem 2
200001
90040
-----------
8000040
180000900
-----------
18008090040
Problem 3
246
70
-----
17220

MCPC 2011Hdu4207-4214（未完全）题解

http://acm.hdu.edu.cn/showproblem.php?pid=4207

432

5678

——-

3456

3024

2592

2160

——-

2452896

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <cmath>
#include <queue>
using namespace std;
template <class T> void checkmin(T &t,T x) {if(x < t) t = x;}
template <class T> void checkmax(T &t,T x) {if(x > t) t = x;}
template <class T> void _checkmin(T &t,T x) {if(t==-1) t = x; if(x < t) t = x;}
template <class T> void _checkmax(T &t,T x) {if(t==-1) t = x; if(x > t) t = x;}
typedef pair <int,int> PII;
typedef pair <double,double> PDD;
typedef long long ll;
#define foreach(it,v) for(__typeof((v).begin()) it = (v).begin(); it != (v).end ; it ++)
ll a , b;
int cas = 1;
int wei(ll a) {
int ans = 0;
while(a) {
a /= 10;
ans ++;
}
checkmax(ans , 1);
return ans;
}
int oowei(ll a) {
int ans = 0;
while(a) {
if(a % 10 != 0) ans ++;
a /= 10;
}
checkmax(ans , 1);
return ans;
}
int main() {
while(cin >> a >> b) {
if(a + b == 0) break;
printf("Problem %d\n",cas++);
ll c = a * b;
int Wa = wei(a) , Wb = wei(b) , Wc = wei(c);
if(c == 0) {
int dd = max(Wa , Wb);
if(a != 0) cout << a << endl;
else for(int i=1;i<dd;i++) putchar(' ');
cout << a << endl;
if(b != 0) cout << b << endl;
else for(int i=1;i<dd;i++) putchar(' ');
cout << b << endl;
for(int i=0;i<dd;i++) putchar('-');
puts("");
for(int i=1;i<dd;i++) putchar(' ');
cout << c << endl;
continue;
}
int oWb = oowei(b);
for(int i=0;i<Wc-Wa;i++) putchar(' ');
cout << a << endl;
for(int i=0;i<Wc-Wb;i++) putchar(' ');
cout << b << endl;
for(int i=0;i<Wc;i++) putchar('-');
puts("");
int cc = 0;
while(b) {
ll d = b%10;
b /= 10;
if(d == 0) { ad +=1; cc++; continue; }
d *= a;
ll Wd = wei(d);
for(int i=0;i<Wc-Wd-cc;i++) putchar(' ');
cout << d ;
cout << endl;
cc ++;
}
if(oWb > 1) {
for(int i=0;i<Wc;i++) putchar('-');
puts("");
cout << c << endl;
}
}
return 0;
}

http://acm.hdu.edu.cn/showproblem.php?pid=4208

（题解：暂缺）

http://acm.hdu.edu.cn/showproblem.php?pid=4209

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <cmath>
#include <queue>
using namespace std;
template <class T> void checkmin(T &t,T x) {if(x < t) t = x;}
template <class T> void checkmax(T &t,T x) {if(x > t) t = x;}
template <class T> void _checkmin(T &t,T x) {if(t==-1) t = x; if(x < t) t = x;}
template <class T> void _checkmax(T &t,T x) {if(t==-1) t = x; if(x > t) t = x;}
typedef pair <int,int> PII;
typedef pair <double,double> PDD;
typedef long long ll;
#define foreach(it,v) for(__typeof((v).begin()) it = (v).begin(); it != (v).end ; it ++)
int n , a , b , ans , cas = 1;
double tmp;
int main() {
while(~scanf("%d",&n) && n) {
tmp = -1.0;
for(int i=0;i<n;i++) {
scanf("%d%d",&a,&b);
double tp = ((double)(a*a)) / ((double)b);
if(tp > tmp) {ans = a; tmp = tp;}
}
}
return 0;
}

http://acm.hdu.edu.cn/showproblem.php?pid=4210

Each row must contain each of the digits 1 through 9.
Each column must contain each of the digits 1 through 9.
Each of the indicated three-by-three squares must contain each of the digits 1 through 9.
For a su-domino-ku, nine arbitrary cells are initialized with the numbers 1 to 9. This leaves 72 remaining cells. Those must be filled by making use of the following set of 36 domino tiles. The tile set includes one domino for each possible pair of unique numbers from 1 to 9 (e.g., 1+2, 1+3, 1+4, 1+5, 1+6, 1+7, 1+8, 1+9, 2+3, 2+4, 2+5, …). Note well that there are not separate 1+2 and 2+1 tiles in the set; the single such domino can be rotated to provide either orientation. Also, note that dominos may cross the boundary of the three-by-three squares (as does the 2+9 domino in our coming example).

DLX解决精确覆盖问题。

http://acm.hdu.edu.cn/showproblem.php?pid=4211

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <cmath>
#include <queue>
using namespace std;
template <class T> void checkmin(T &t,T x) {if(x < t) t = x;}
template <class T> void checkmax(T &t,T x) {if(x > t) t = x;}
template <class T> void _checkmin(T &t,T x) {if(t==-1) t = x; if(x < t) t = x;}
template <class T> void _checkmax(T &t,T x) {if(t==-1) t = x; if(x > t) t = x;}
typedef pair <int,int> PII;
typedef pair <double,double> PDD;
typedef long long ll;
#define foreach(it,v) for(__typeof((v).begin()) it = (v).begin(); it != (v).end ; it ++)
int main() {
char ch[100100];
bool vis[26];
while(gets(ch)) {
if(strcmp(ch,"END") == 0) break;
int ok = 1;
memset(vis,0,sizeof(vis));
for(int i=0;ch[i];i++) {
if(ch[i] == ' ') continue;
int t = ch[i] - 'A';
if(vis[t]) { ok=0;break; }
vis[t] = 1;
}
if(ok) puts(ch);
}
return 0;
}

http://acm.hdu.edu.cn/showproblem.php?pid=4212

http://acm.hdu.edu.cn/showproblem.php?pid=4213

http://acm.hdu.edu.cn/showproblem.php?pid=4214

（暂wa