hdoj 2451 Simple Addition Expression

6 篇文章 0 订阅

类型:数学

题目:http://acm.hdu.edu.cn/showproblem.php?pid=2451

来源:2008 Asia Regional Harbin

思路:设整数n的长度为len, 先求出1 ~ len - 1的所有长度的满足条件的数的和【预处理数组】

然后计算长度为len的数量,假设最高位数位x

x = 1时,没有小于1的数符合条件,再求次高位

x = 2时,累加当前位为1的所有的数【其他低位符合条件】,然后另当前位为2,求次高位

x = 3,累加当前位为1或2的所有的数...

x > 3时,所有的数【其他低位符合条件】均累加,此时即可退出

//hdoj 2451 Simple Addition Expression
#include <iostream>
#include <sstream>
#include <string>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#include <map>
#include <algorithm>
#include <numeric>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <set>
#include <deque>
#include <bitset>
#include <functional>
#include <utility>
#include <iomanip>
#include <cctype>
using namespace std;

#define FOR(i,a,b) for(i = (a); i < (b); ++i)
#define FORE(i,a,b) for(i = (a); i <= (b); ++i)
#define FORD(i,a,b) for(i = (a); i > (b); --i)
#define FORDE(i,a,b) for(i = (a); i >= (b); --i)
#define max(a,b) ((a) > (b)) ? (a) : (b)
#define min(a,b) ((a) < (b)) ? (a) : (b)
#define CLR(a,b) memset(a,b,sizeof(a))
#define PB(x) push_back(x)

const int MAXN = 10000;
const int MAXM = 0;
const int hash_size = 25000002;
const int INF = 0x7f7f7f7f;

__int64 t, n, sum, tmp;
__int64 dp[20] = {0, 3, 3*3, 3*4*3, 3*4*4*3, 3*4*4*4*3, 3*4*4*4*4*3, 3*4*4*4*4*4*3, 3*4*4*4*4*4*4*3, 3*4*4*4*4*4*4*4*3, 3*4*4*4*4*4*4*4*4*3};
__int64 dp1[20] = {1, 3, 4*3, 4*4*3, 4*4*4*3, 4*4*4*4*3, 4*4*4*4*4*3, 4*4*4*4*4*4*3, 4*4*4*4*4*4*4*3, 4*4*4*4*4*4*4*4*3, 4*4*4*4*4*4*4*4*4*3};
string str;

int main()
{
    int i;

    while(scanf("%I64d", &n) != EOF) {
        str = "";
        int len = 0;
        t = n;
        while(t) {
            if(t % 10 > 3)
                str += ('4');
            else
                str += (t % 10 + '0');
            t /= 10;
            ++len;
        }
        sum = 0;
        FORE(i, 1, len - 1)
            sum += dp[i];
        tmp = 0;
        FORDE(i, len - 1, 0) {
            if(i == len - 1) {
                if(len == 1) {
                    if(n < 3)
                        tmp = n;
                    else
                        tmp = 3;
                    break;
                }
                else {
                    if((int)(str[i] - '0') == 1)
                        continue;
                    tmp += ((int)(str[i] - '0') - 1) * dp1[i];
                    if((int)(str[i] - '0') == 4)
                        break;
                }
            }
            else if(i == 0) {
                if((int)(str[i] - '0') == 0)
                    break;
                if((int)(str[i] - '0') == 4)
                    tmp += 3;
                else
                    tmp += (int)(str[i] - '0');
            }
            else {
                if((int)(str[i] - '0') == 0)
                    continue;
                tmp += ((int)(str[i] - '0')) * dp1[i];
                if((int)(str[i] - '0') == 4)
                    break;

            }
        }
        sum += tmp;
        printf("%I64d\n", sum);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值