类型:数学
题目:http://acm.hdu.edu.cn/showproblem.php?pid=2451
思路:设整数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;
}