C. Really Big Numbers
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Ivan likes to learn different things about numbers, but he is especially interested in really big numbers. Ivan thinks that a positive integer number x is really big if the difference between x and the sum of its digits (in decimal representation) is not less than s. To prove that these numbers may have different special properties, he wants to know how rare (or not rare) they are — in fact, he needs to calculate the quantity of really big numbers that are not greater than n.
Ivan tried to do the calculations himself, but soon realized that it’s too difficult for him. So he asked you to help him in calculations.
Input
The first (and the only) line contains two integers n and s (1 ≤ n, s ≤ 1018).
Output
Print one integer — the quantity of really big numbers that are not greater than n.
Examples
input
12 1
output
3
input
25 20
output
0
input
10 9
output
1
Note
In the first example numbers 10, 11 and 12 are really big.
In the second example there are no really big numbers that are not greater than 25 (in fact, the first really big number is 30: 30 - 3 ≥ 20).
In the third example 10 is the only really big number (10 - 1 ≥ 9).
记录下每行h[i],每列l[j],中最小的数,也即是本行(列)最多能加的数,优先加那些必须要行或者列,即若 a[i][j] > h[i](l[j]),者执行列加(行加),~执行过程中若出现 a[i][j] > h[i] + l[j] 的情况者无法满足·,若最后剩下全相同且不为 0 的情况,便根据行列,执行即可
AC代码:
#include<cstdio>
#include<cmath>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int MAX = 110;
const int INF = 510;
typedef long long LL;
int a[MAX][MAX],h[MAX],l[MAX],n,m,num,o,p;
struct node{
string s;
int o;
}st[MAX * MAX * INF];
void hh(int x,int y){
if(x > h[y]){
p = 0;
return ;
}
for(int i = 1; i <= x; i++)
st[++num].s = "row",st[num].o = y;
for(int i = 1; i <= m; i++)
a[y][i] -= x;
}
void ll(int x,int y){
if(x > l[y]){
p = 0;
return ;
}
for(int i = 1; i <= x; i++)
st[++num].s = "col",st[num].o = y;
for(int i = 1; i <= n; i++)
a[i][y] -= x;
}
void in(){
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++)
h[i] = min(h[i],a[i][j]),l[j] = min(l[j],a[i][j]);
}
void slo(){
int ok = 1;
for(int i = 1; i <= n && ok; i++)
for(int j = 1; j <= m && ok; j++){
if(a[i][j] > h[i]){
ll(a[i][j] - h[i],j);
ok = 0,o = 1;
}
else if(a[i][j] > l[j]){
hh(a[i][j] - l[j],i);
ok = 0,o = 1;
}
}
}
void sh(){
for(int i = 1; i <= n; i++){
for(int j = 1; j <= a[1][1]; j++)
st[++num].s = "row",st[num].o = i;
}
}
void sl(){
for(int i = 1; i <= m; i++){
for(int j = 1; j <= a[1][1]; j++)
st[++num].s = "col",st[num].o = i;
}
}
int main()
{
scanf("%d %d",&n,&m);
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++)
scanf("%d",&a[i][j]);
fill(h,h + MAX,INF);
fill(l,l + MAX,INF);
num = 0;
o = p = 1;
while(o && p){
o = 0;
in();
slo();
}
if(!p){
puts("-1");
return 0;
}
in();
if(a[1][1]){
if(n > m)
sl();
else
sh();
}
printf("%d\n",num);
for(int i = 1; i <= num; i++)
cout << st[i].s << " " << st[i].o <<endl;
return 0;
}