题面
One player came to a casino and found a slot machine where everything depends only on how he plays. The rules follow.
A positive integer aa is initially on the screen. The player can put a coin into the machine and then add 11 to or subtract 11 from any two adjacent digits. All digits must remain from 00 to 99 after this operation, and the leading digit must not equal zero. In other words, it is forbidden to add 11 to 99 , to subtract 11 from 00 and to subtract 11 from the leading 11 . Once the number on the screen becomes equal to bb , the player wins the jackpot. aa and bb have the same number of digits.
Help the player to determine the minimal number of coins he needs to spend in order to win the jackpot and tell how to play.
Input
The first line contains a single integer nn (2≤n≤1052≤n≤105 ) standing for the length of numbers aa and bb .
The next two lines contain numbers aa and bb , each one on a separate line (10n−1≤a,b<10n10n−1≤a,b<10n ).
Output
If it is impossible to win the jackpot, print a single integer −1−1 .
Otherwise, the first line must contain the minimal possible number cc of coins the player has to spend.
min(c,105)min(c,105) lines should follow, ii -th of them containing two integers didi and sisi (1≤di≤n−11≤di≤n−1 , si=±1si=±1 ) denoting that on the ii -th step the player should add sisi to the didi -th and (di+1)(di+1) -st digits from the left (e. g. di=1di=1 means that two leading digits change while di=n−1di=n−1 means that there are two trailing digits which change).
Please notice that the answer may be very big and in case c>105c>105 you should print only the first 105105 moves. Your answer is considered correct if it is possible to finish your printed moves to win the jackpot in the minimal possible number of coins. In particular, if there are multiple ways to do this, you can output any of them.
Examples
Input
3
223
322
Output
2
1 1
2 -1
Input
2
20
42
Output
2
1 1
1 1
Input
2
35
44
Output
-1
Note
In the first example, we can make a +1 operation on the two first digits, transforming number 223223 into 333333 , and then make a -1 operation on the last two digits, transforming 333333 into 322322 .
It's also possible to do these operations in reverse order, which makes another correct answer.
In the last example, one can show that it's impossible to transform 3535 into 4444 .
题目链接
参考链接
Codeforces 1120 简要题解 Author: SC.ldxcaicai
题意
给出a、b两个字符串
对a字符串进行一些修改使得a与b相同,求怎么修改?
修改规则:
①同时对相邻两个数字+1
②同时对相邻两个数字-1
③数字在修改过程中,不能<0或>9
分析
如果要对a1修改b1-a1,则a2在和a1一起修改的基础上,额外还要修改b2-a2-(b1-a1)。
程序
#include<stdio.h>
#include<stdlib.h>
#include<iostream>
using namespace std;
#define maxn 100005
int a[maxn];
int b[maxn];
int add[maxn];
int main()
{
int n;
long long answer=0;
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%1d",&a[i]);
for(int j=1;j<=n;j++)
scanf("%1d",&b[j]);
for(int i=1;i<=n;i++)
{
if(i==1)
add[i]=b[i]-a[i];
else
add[i]=b[i]-a[i]-add[i-1];
}
if(add[n])
{
printf("-1\n");
return 0;
}
for(int i=1;i<n;i++)
answer+=abs(add[i]);
long long actual_answer=min(answer,(long long)1e5);
printf("%lld\n",answer);
int place,now;
place=now=1;
while(actual_answer--)
{
while(add[place]==0)
++place,++now;
while((add[now]<0&&a[now+1]==0)||(add[now]>0&&a[now+1]==9))
now++;
int change=(add[now]>0)? 1:-1;
printf("%d %d\n",now,change);
a[now]+=change;
a[now+1]+=change;
add[now]-=change;
now=max(now-1,place);
}
return 0;
}