C - Carrying Conundrum

  1. Problem

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 + 29762039+2976 would be as shown:

However, Alice evaluates it as shown:

In particular, this is what she does:

  • add 99 and 66 to make 1515, and carry the 11 to the column two columns to the left, i. e. to the column "00 99";
  • add 33 and 77 to make 1010 and carry the 11 to the column two columns to the left, i. e. to the column "22 22";
  • add 11, 00, and 99 to make 1010 and carry the 11 to the column two columns to the left, i. e. to the column above the plus sign;
  • add 11, 22 and 22 to make 55;
  • add 11 to make 11.

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

Alice comes up to Bob and says that she has added two numbers to get a result of nn. 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 nn. Note that pairs (a, b)(a,b) and (b, a)(b,a) are considered different if a \ne ba=b.

Input

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

The only line of each test case contains an integer nn (2 \leq n \leq 10^92≤n≤109) — 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 nn.

Example

input

5
100
12
8
2021
10000

output

9
4
7
44
99

Note

In the first test case, when Alice evaluates any of the sums 1 + 91+9, 2 + 82+8, 3 + 73+7, 4 + 64+6, 5 + 55+5, 6 + 46+4, 7 + 37+3, 8 + 28+2, or 9 + 19+1, she will get a result of 100100. The picture below shows how Alice evaluates 6 + 46+4:

2. Analysis

题目大意

定义新的加法运算:计算时所有的进位都要左移一位。

给出一个数 n ,问有多少对 (a,b) 在新定义下满足 a+b=n ( (a,b)(a,b) 和 (b,a)(b,a) 算作不同方案)?

解题思路

将 n 的奇偶位分开,变成两个数 oddNum , evenNum ,因为奇偶位之间的进位并不影响。答案就是( oddNum + 1 ) ∗ ( evenNum + 1 ) − 2 

3. Accepted Code

#include <stdio.h>
#include <stdlib.h>
int power( int x, int i); //乘方操作
int main()
{
    int t;
    scanf("%d", &t);
    int n;
    while(t--)
    {
        scanf("%d", &n);
        int oddArr[100000], evenArr[100000];
        int oddCnt = 0, evenCnt = 0;
        int flag=1;
        while(n)
        {
            if(flag%2==1)
            {
                //奇数位置
                oddArr[oddCnt++] = n%10;
            }
            else if(flag%2==0)
            {
                //偶数位置
                evenArr[evenCnt++] = n%10;
            }
            n = n/10;
            flag++;
        }
        int oddNum=0, evenNum=0;
        int i;
        for(i=oddCnt-1; i>=0; i--)
            oddNum += (oddArr[i] * power(10, i));
        for(i=evenCnt-1; i>=0; i--)
            evenNum += (evenArr[i] * power(10, i));
        printf("%d\n", (oddNum+1)*(evenNum+1)-2);

    }
    return 0;
}
int power( int x, int i){
//由于pow(10, i)当i=2时的结果是99,故需自定义乘方运算
    int j;
    int mul=1;
    for(j=0; j<i; j++){
        mul = mul*x;
    }
    return mul;
}

关于 pow(10, i) 当i=2时的结果是99的原因 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值