B-number
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2143 Accepted Submission(s): 1152
Problem Description
A wqb-number, or B-number for short, is a non-negative integer whose decimal form contains the sub- string "13" and can be divided by 13. For example, 130 and 2613 are wqb-numbers, but 143 and 2639 are not. Your task is to calculate how many wqb-numbers from 1 to n for a given integer n.
Input
Process till EOF. In each line, there is one positive integer n(1 <= n <= 1000000000).
Output
Print each answer in a single line.
Sample Input
Sample Output
Author
wqb0039
Source
2010 Asia Regional Chengdu Site —— Online Contest
Recommend
lcy
#include
#include
#include
#define LL long long
using namespace std;
const int MAXN = 15;
const int MOD = 13;
int dp[MAXN][MAXN][MAXN][2],digit[MAXN],cnt;
int solve(int pos, int pre, int mod, bool flag, bool limit){
if (!pos) return !mod && flag;
if (!limit && dp[pos][pre][mod][flag]!=-1)
return dp[pos][pre][mod][flag];
int val_sz = limit ? digit[pos] : 9;
int sum = 0;
for (int i = 0; i <= val_sz; ++i){
int lim = limit && (i == digit[pos]);
if (pre == 1 && i == 3)
sum += solve(pos - 1, i, (mod * 10 + i) % MOD, 1, lim);
else
sum += solve(pos - 1, i, (mod * 10 + i) % MOD, flag, lim);
}
if (!limit) dp[pos][pre][mod][flag] = sum;
return sum;
}
void init_digits(int x){
memset(digit, 0, sizeof(digit));
cnt = 0;
while (x){
digit[++cnt] = x % 10;
x /= 10;
}
}
void check(int x){
int sum = 0;
for (int i = 1; i <= x; ++i){
if (i % MOD) continue;
init_digits(i);
int ok = 0;
for (int j = cnt; j; --j)
if (j < cnt && digit[j] == 3 && digit[j+1] == 1){
ok = 1; break;
}
sum += ok;
}
printf("%d\n", sum);
}
int n;
int main(){
while (~scanf("%d",&n)){
memset(dp, -1, sizeof(dp));
init_digits(n);
printf("%d\n", solve(cnt, 0, 0, 0, 1));
//check(n);
}
return 0;
}
Statistic | Submit | Discuss | Note