codeforces 1567 C. Carrying Conundrum

本场比赛其他题目的题解

A. Domino Disaster
B. MEXor Mixup
C. Carrying Conundrum
D. Expression Evaluation Error
E. Non-Decreasing Dilemma

C. Carrying Conundrum

开启传送门

题目描述

Alice has just learned addition. However, she hasn’t learned the concept of “carrying” fully — instead of carrying to the next column, she carries to the column two columns to the left.

For example, the regular way to evaluate the sum 2039 + 2976 2039 + 2976 2039+2976 would be as shown:

However, Alice evaluates it as shown:

In particular, this is what she does:

  • add 9 9 9 and 6 6 6 to make 15 15 15, and carry the 1 1 1 to the column two columns to the left, i. e. to the column " 0 0 0 9 9 9";
  • add 3 3 3 and 7 7 7 to make 10 10 10 and carry the 1 1 1 to the column two columns to the left, i. e. to the column " 2 2 2 2 2 2";
  • add 1 1 1, 0 0 0, and 9 9 9 to make 10 10 10 and carry the 1 1 1 to the column two columns to the left, i. e. to the column above the plus sign;
  • add 1 1 1, 2 2 2 and 2 2 2 to make 5 5 5;
  • add 1 1 1 to make 1 1 1.

Thus, she ends up with the incorrect result of 15005 15005 15005.

Alice comes up to Bob and says that she has added two numbers to get a result of n n n. However, Bob knows that Alice adds in her own way. Help Bob find the number of ordered pairs of positive integers such that when Alice adds them, she will get a result of n n n. Note that pairs ( a , b ) (a, b) (a,b) and ( b , a ) (b, a) (b,a) are considered different if a ≠ b a \ne b a=b.

Input

The input consists of multiple test cases. The first line contains an integer t   ( 1 ≤ t ≤ 1000 ) t\ (1\le t\le 1000) t (1t1000) — the number of test cases. The description of the test cases follows.

The only line of each test case contains an integer n   ( 2 ≤ n ≤ 1 0 9 ) n\ (2\le n\le 10^9) n (2n109) — the number Alice shows Bob.

Output

For each test case, output one integer — the number of ordered pairs of positive integers such that when Alice adds them, she will get a result of n n n.

Example

Input1
5
100
12
8
2021
10000
Output1
9
4
7
44
99

Note

In the first test case, when Alice evaluates any of the sums 1 + 9 , 2 + 8 , 3 + 7 , 4 + 6 , 5 + 5 , 6 + 4 , 7 + 3 , 8 + 2 , o r 9 + 1 1+9, 2+8, 3+7, 4+6, 5+5, 6+4, 7+3, 8+2, or 9+1 1+9,2+8,3+7,4+6,5+5,6+4,7+3,8+2,or9+1, she will get a result of 100 100 100. The picture below shows how Alice evaluates 6 + 4 6+4 6+4:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-5cx3hQfu-1631530468282)(https://espresso.codeforces.com/704366959d40aa707742d72e1e660c9027ff73cc.png)]

题意

多组样例,每次给你一个正整数 n n n。表示两个多位数相加的和。但是这个加法比较特殊,每次进位都是进到前两位上。(逢十进十?)

问你有多少加数被加数对,满足用上述加法得到的和为 n n n

分析

我们先考虑正常加法,逢十进一的情况,如果一个和为 n n n,那么有多少种两个非负整数相加得到 n n n 呢?

很显然有的方案数为 0 + n , 1 + ( n − 1 ) , … , n + 0 0+n, 1+(n-1), \dots, n+0 0+n,1+(n1),,n+0 一共 n + 1 n+1 n+1 种。

基于此, 很自然的想法是,既然都是隔一位进一位,那么显然我可以按照奇数位,偶数位进行拆分,拆成两个和。

剩下的问题不就转换成了两个子问题,每个子问题都是 如果一个和为 n n n,呢么有多少种两个非负整数相加得到 n n n 呢?

最后一个点是,由于题目要求不能有一个加数为 0 0 0 所以需要排除 0 + n , n + 0 0+n, n+0 0+n,n+0 这两种case。

#include<bits/stdc++.h>
using namespace std;
void solve() {
	string a;
	cin>>a;
	long long num[2] = {0,0};
	for(int i = 0;i<a.length();i++) num[i&1] = num[i&1]*10 + a[i] - '0';
	cout<<(num[0]+1)*(num[1]+1) - 2<<endl;
}
int main() {
	int t;
	cin>>t;
	while(t--) solve();
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zuhiul

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值