Codeforces Round 880 (Div. 2)题解

C. k-th equality

Consider all equalities of form a+b=c, where a has A digits, b has B digits, and c has C digits. All the numbers are positive integers and are written without leading zeroes. Find the k-th lexicographically smallest equality when written as a string like above or determine that it does not exist.

For example, the first three equalities satisfying A=1, B=1, C=2 are

  • 1+9=10
  • 2+8=10
  • 2+9=11

An equality s is lexicographically smaller than an equality t with the same lengths of the numbers if and only if the following holds:

  • in the first position where s and t differ, the equality s has a smaller digit than the corresponding digit in t

Input

Each test contains multiple test cases. The first line of input contains a single integer t (1≤t≤103) — the number of test cases. The description of test cases follows.

The first line of each test case contains integers A, B, C, k (1≤A,B,C≤6, 1≤k≤1012).

Each input file has at most 55 test cases which do not satisfy A,B,C≤3.

Output

For each test case, if there are strictly less than k valid equalities, output −1.

Otherwise, output the k-th equality as a string of form a+b=c.

Example

input

7

1 1 1 9

2 2 3 1

2 2 1 1

1 5 6 42

1 6 6 10000000

5 5 6 3031568815

6 6 6 1000000000000

output

2 + 1 = 3
10 + 90 = 100
-1
9 + 99996 = 100005
-1
78506 + 28543 = 107049
-1

题意:

给四个数 a b c k,找一个等式A+B=C,满足A是a位数,B是b位数,C是c位数,求满足条件字典序树第k小的等式  

思路:

枚举A,每次找到可以到达的c的最小最大值,ans统计当前可以组成的等式个数

如果ans大于等于k,则目标状态一定是当前的a,然后通过 ans 和 k 差值,通过c的最大值减去差值 计算出 c 即可

代码:

#include
#define endl '\n'
#define debug(x) cout<<#x<<'='< pii;
typedef long long ll;
const int INF=0x3f3f3f3f;

void solve(){
    int mn[10]={0,1,10,100,1000,10000,100000,10000000};
    int a,b,c,k;
    cin>>a>>b>>c>>k;
    int ans=0;
    for(int i=mn[a];i<10*mn[a];i++){    //枚举a
        int l=i+mn[b],r=i+10*mn[b]-1;   //计算出可到达的c
        l=max(l,mn[c]);     //和c的范围取交集
        r=min(r,mn[c]*10-1);
        ans+=max(0ll,r-l+1);
        if(ans>=k){
            int tmp=ans-k;      //计算比目标状态大多少,用r减去
            cout<

你可能感兴趣的:(c++,算法)