首页 > 其他分享 >B. Sum of Two Numbers

B. Sum of Two Numbers

时间:2023-02-11 13:23:28浏览次数:51  
标签:digits 10 int Sum Two leq Numbers test sum

B. Sum of Two Numbers

The sum of digits of a non-negative integer $a$ is the result of summing up its digits together when written in the decimal system. For example, the sum of digits of $123$ is $6$ and the sum of digits of $10$ is $1$. In a formal way, the sum of digits of $\displaystyle a=\sum_{i=0}^{\infty} a_i \cdot 10^i$, where $0 \leq a_i \leq 9$, is defined as $\displaystyle\sum_{i=0}^{\infty}{a_i}$.

Given an integer $n$, find two non-negative integers $x$ and $y$ which satisfy the following conditions.

  • $x+y=n$, and
  • the sum of digits of $x$ and the sum of digits of $y$ differ by at most $1$.

It can be shown that such $x$ and $y$ always exist.

Input

Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10\,000$).

Each test case consists of a single integer $n$ ($1 \leq n \leq 10^9$)

Output

For each test case, print two integers $x$ and $y$.

If there are multiple answers, print any.

Example

input

5
1
161
67
1206
19

output

1 0
67 94
60 7
1138 68
14 5

Note

In the second test case, the sum of digits of $67$ and the sum of digits of $94$ are both $13$.

In the third test case, the sum of digits of $60$ is $6$, and the sum of digits of $7$ is $7$.

 

解题思路

  经典思维题想不出来。比赛的时候想了很久,最后还是写暴搜过的,先贴个代码。

#include <bits/stdc++.h>
using namespace std;

typedef long long LL;

int n;
vector<int> num;
int pow10[10];
int ans[2];

int get(int x) {
    int ret = 0;
    while (x) {
        ret += x % 10;
        x /= 10;
    }
    return ret;
}

bool dfs(int a, int b, int u, int r) {
    if (u == num.size() && a + b == n) {
        ans[0] = a, ans[1] = b;
        return true;
    }
    for (int i = 0; i <= 9; i++) {
        int j = ((num[u] - i - r) % 10 + 10) % 10;
        int t = abs(get(i * pow10[u] + a) - get(j * pow10[u] + b));
        if (t <= 1 && dfs(i * pow10[u] + a, j * pow10[u] + b, u + 1, (i + j) / 10)) return true;
    }
    return false;
}

void solve() {
    scanf("%d", &n);
    num.clear();
    for (int i = n; i; i /= 10) {
        num.push_back(i % 10);
    }
    pow10[0] = 1;
    for (int i = 1; i < num.size(); i++) {
        pow10[i] = pow10[i - 1] * 10;
    }
    dfs(0, 0, 0, 0);
    printf("%d %d\n", ans[0], ans[1]);
}

int main() {
    int t;
    scanf("%d", &t);
    while (t--) {
        solve();
    }
    
    return 0;
}
View Code

  思路就是从低位开始往高位搜,假设$n$的第$i$位是$a_i$,$y$的第$i$位是$b_i$,$x$的第$i$位是$c_i$,然后从$0 \sim 9$枚举$b_i$,那么$c_i = (a_i - b_i - r) \bmod 10$,其中$r$是来自低位的进位,同时还要满足$b_i + c_i \leq s$,$s$是指允许两数相差的最大值。实际上加上这些约束条件的剪枝后有效状态是非常少的(大概十多个,其中只要看哪个位两数相差为$1$就好了),因此可以过。

  实际上这是道思维题来的。每一位都可以看作是独立的,因此可以没有进位。如果$n$是偶数的话那么直接$x = y = \frac{n}{2}$就好了。否则$n$是奇数,枚举$n$的每一位,如果$a_i$是偶数,那么$b_i = c_i = \frac{a_i}{2}$。如果$a_i$是奇数,那么$b_i$和$c_i$一个取$\left\lfloor \frac{a_i}{2} \right\rfloor$,一个取$\left\lceil \frac{a_i}{2} \right\rceil$,为了保证最后所有位数之和相差不超过$1$,可以交替地给$b_i$和$c_i$分配较大的数和较小的数。

  AC代码如下:

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 
 4 void solve() {
 5     int n;
 6     scanf("%d", &n);
 7     int a = 0, b = 0, s = 0;
 8     for (auto &c : to_string(n)) {
 9         int x = c - '0';
10         if (x & 1) {
11             a = a * 10 + (x + s) / 2;   // s是奇数,那么把较大的数分配给a
12             b = b * 10 + (x + !s) / 2;  // s是偶数,那么把较大的数分配给b
13             s ^= 1;
14         }
15         else {
16             a = a * 10 + x / 2;
17             b = b * 10 + x / 2;
18         }
19     }
20     printf("%d %d\n", a, b);
21 }
22 
23 int main() {
24     int t;
25     scanf("%d", &t);
26     while (t--) {
27         solve();
28     }
29     
30     return 0;
31 }

 

参考资料

  Codeforces Round #851 (Div. 2) Editorial:https://codeforces.com/blog/entry/112584

标签:digits,10,int,Sum,Two,leq,Numbers,test,sum
From: https://www.cnblogs.com/onlyblues/p/17111272.html

相关文章

  • B. Sum of Two Numbers
    B.SumofTwoNumbershttps://codeforces.com/problemset/problem/1788/B 思路计算原数的所有位置上的digit和得到digit和的一半 对于原数从左到右切分,前半部分d......
  • A. One and Two
    A.OneandTwohttps://codeforces.com/problemset/problem/1788/A思路统计2的个数,如果是奇数个,不满足如果是偶数个,2队列中,前半部分最后一个2则为目标位置如果个数......
  • E. Sum Over Zero
    E.SumOverZeroYouaregivenanarray$a_1,a_2,\ldots,a_n$of$n$integers.Consider$S$asasetofsegmentssatisfyingthefollowingconditions.Eache......
  • Session-based Recommendation with Graph Neural Networks
    目录概符号说明基本框架gatedGRU技术细节代码WuS.,TangY.,ZhuY.,WangL.,XieX.andTanT.Session-basedrecommendationwithgraphneuralnetworks.InAAA......
  • 合理安排kafka的broker、partition、consumer数量
    broker的数量最好大于等于partition数量一个partition最好对应一个硬盘,这样能最大限度发挥顺序写的优势。一个broker如果对应多个partition,需要随机分发,顺序IO会退化成随......
  • 联邦学习论文阅读笔记03 Incentive Design for Efficient Federated Learning in Mobi
    精翻:https://blog.csdn.net/weixin_43978453/article/details/104947600 挑战:客户端自身存在计算与通信成本,不给足够的好处不愿意参与训练。方法:基于合约理论设计了一......
  • pytest---多重断言(pytest-assume)
    前言在编写自动化测试用例的时候,可能一条用例存在这多条断言,那么在自动化中如何编写多条断言且断言失败后还能继续往下执行?这里引入新的插件pytest-assumepytest-assume......
  • 124. Binary Tree Maximum Path Sum[Hard]
    124.BinaryTreeMaximumPathSumApathinabinarytreeisasequenceofnodeswhereeachpairofadjacentnodesinthesequencehasanedgeconnectingthem.......
  • 什么是Azure Network Watcher
    现如今,越来越多的企业开始在云中部署他们的数据资产。一般情况下,当企业在云中部署某个应用时,基本上都是首先构建一个虚拟网络,然后进行一些基本的安全和连接设置。然后使其通......
  • IOS开发者自带弱网测试工具界面说明NETWORK LINK CONDITIONER
    IOS手机的开发者自带了弱网模拟工具,以下是界面说明,便于大家使用时自行配置使用。测试工具NETWORKLINKCONDITIONER。1、准备环境,设置中调出:开发者选项(如果没有,需要真机联......