CodeForces 1121E Once in a casino(思维+模拟)

本文介绍了一个基于老虎机的策略游戏,玩家需要通过精确的操作将屏幕上的数字从初始状态a调整到目标状态b,以赢得大奖。游戏规则允许玩家投入硬币,并选择对屏幕上任意两个相邻数字加1或减1,但数字范围必须保持在0到9之间,且首位数字不能为零。文章提供了游戏的详细规则、输入输出格式和示例,以及如何确定最少硬币花费和操作步骤的算法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题面

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 1121E

参考链接

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;
}

 

### Codeforces 887E Problem Solution and Discussion The problem **887E - The Great Game** on Codeforces involves a strategic game between two players who take turns to perform operations under specific rules. To tackle this challenge effectively, understanding both dynamic programming (DP) techniques and bitwise manipulation is crucial. #### Dynamic Programming Approach One effective method to approach this problem utilizes DP with memoization. By defining `dp[i][j]` as the optimal result when starting from state `(i,j)` where `i` represents current position and `j` indicates some status flag related to previous moves: ```cpp #include <bits/stdc++.h> using namespace std; const int MAXN = ...; // Define based on constraints int dp[MAXN][2]; // Function to calculate minimum steps using top-down DP int minSteps(int pos, bool prevMoveType) { if (pos >= N) return 0; if (dp[pos][prevMoveType] != -1) return dp[pos][prevMoveType]; int res = INT_MAX; // Try all possible next positions and update 'res' for (...) { /* Logic here */ } dp[pos][prevMoveType] = res; return res; } ``` This code snippet outlines how one might structure a solution involving recursive calls combined with caching results through an array named `dp`. #### Bitwise Operations Insight Another critical aspect lies within efficiently handling large integers via bitwise operators instead of arithmetic ones whenever applicable. This optimization can significantly reduce computation time especially given tight limits often found in competitive coding challenges like those hosted by platforms such as Codeforces[^1]. For detailed discussions about similar problems or more insights into solving strategies specifically tailored towards contest preparation, visiting forums dedicated to algorithmic contests would be beneficial. Websites associated directly with Codeforces offer rich resources including editorials written after each round which provide comprehensive explanations alongside alternative approaches taken by successful contestants during live events. --related questions-- 1. What are common pitfalls encountered while implementing dynamic programming solutions? 2. How does bit manipulation improve performance in algorithms dealing with integer values? 3. Can you recommend any online communities focused on discussing competitive programming tactics? 4. Are there particular patterns that frequently appear across different levels of difficulty within Codeforces contests?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值