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;
}**

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

被折叠的 条评论
为什么被折叠?



