HDU 4278 水~ 数位DP

本文解析了HDU4278题目,采用数位DP的方法解决了一个数范围内,每位上不含3或8的数字计数问题。通过源代码示例展示了如何利用快速幂计算有效值。

HDU 4278
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=4278
题意:
问一个数范围内,每个位(个十百千万)上没有3或者8的数字有多少个。
思路:
简单八进制,用数位DP的思想一下就能理解。

比赛的时候按组合数学来排除不合法的答案,然后就各种复杂了……
源码:

**#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <string>
#include <iostream>
using namespace std;
int ppow(int a,int x)
{
    int ans = 1;
    while(x){
        if(x & 1)   ans *= a;
        a = a * a;
        x >>= 1;
    }
    return ans;
}
int main()
{
    string str;
    while(cin >> str){
        if(str[0] == '0')   break;
        int len = str.length();
        int ans = 0;
        for(int i = 0 ; i < len ; i++){
            ans += (str[i] - '0') * ppow(8, len - i - 1);
            if(str[i] - '0' > 3)  ans -= ppow(8, len - i - 1);
            if(str[i] - '0' > 8)  ans -= ppow(8, len - i - 1);
        }
        cout << str << ": " <<ans<<endl;
    }
    return 0;
}**
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值