CF1567C

· · 个人记录

Carrying Conundrum

思维题。

实质上是做了一个奇偶数位分开的加法运算,那么把就数位组成的数单独抽成两个数 ab,那么显然根据乘法原理,拆分的总方案数是 (a+1)(b+1),那么答案就是 (a+1)(b+1)-2(第一个加数为零或第二个加数为零)。

时间复杂度 O(T\log n)

代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#define ll long long
using namespace std;

ll T,ans,n,o,e;

ll buf[22];ll len=-1;

inline ll read() {
    ll ret=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9') {if(ch=='-') f=-f;ch=getchar();}
    while(ch>='0'&&ch<='9') {ret=(ret<<3)+(ret<<1)+ch-'0';ch=getchar();}
    return ret*f;
}

void write(ll x) {
    static char buf[22];static ll len=-1;
    if(x>=0) {
        do{buf[++len]=x%10+48;x/=10;}while(x);
    }
    else {
        putchar('-');
        do{buf[++len]=-(x%10)+48;x/=10;}while(x);
    }
    while(len>=0) putchar(buf[len--]);
}

void writeln(ll x) {
    write(x);putchar('\n');
}

int main() {

    T=read();

    while(T--) {
        n=read();e=0;o=0;
        memset(buf,0,sizeof(buf));
        if(n>=0) {
            do{buf[++len]=n%10;n/=10;}while(n);
        }
        while(len>=0) {
            if(len&1) {
                o*=10;o+=buf[len];len--;
            }
            else {
                e*=10;e+=buf[len];len--;
            }
        }
    //  printf("o=%lld e=%lld\n",o,e);
        ans=(o+1)*(e+1)-2;
        writeln(ans);
    }

    return 0;
}