数字游戏2

// 数字游戏2.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>
#include <vector>
#include <cstring>
using namespace std;


/*
https://loj.ac/p/10166


题目描述
由于科协里最近真的很流行数字游戏,某人又命名了一种取模数,这种数字必须满足各位数字之和 mod N 为 0。

现在大家又要玩游戏了,指定一个整数闭区间 [a,b],问这个区间内有多少个取模数。

输入格式
题目有多组测试数据。每组只含三个数字 a, b, N。

输出格式
对于每个测试数据输出一行,表示各位数字和 mod N 为 0 的数的个数。

样例
输入
1 19 9


输出
2

对于全部数据,1<= a,b <=  2^{31}-1,1 <=  N  < 100。
*/

long long dp[10][10][105];
int a, b, n;



void init() {
	memset(dp, 0, sizeof dp);
	for (int i = 0; i <= 9; i++) {
		dp[0][i][i % n] = 1;
	}

	for (int i = 1; i < 10; i++) {
		for (int j = 0; j <= 9; j++) {
			for (int k = 0; k <= 9; k++) {
				for (int t = 0; t <= 100; t++) {
					int idx = (j+ t) % n;
					dp[i][j][idx] += dp[i-1][k][t];
				}
			}
		}
	}

	return;
}

int solve(int t) {
	int res = 0;
	if (t == 0) return 1;
	vector<int> nums;
	while (t) {
		nums.push_back(t % 10);
		t /= 10;
	}

	int sum = 0;
	for (int i = nums.size() - 1; i >= 0; i--) {
		int x = nums[i];
		int find = (n - (sum % n))%n;
		for (int j = x - 1; j >= 0; j--) {
			res += dp[i][j][find];
		}
		sum += x;
	}
	if (sum % n == 0) 
		res += 1;

	return res;
}


int main()
{
	while (cin >> a >> b >> n) {
		init();
		cout << solve(b) - solve(a - 1) << endl;
	}

	return 0;
}

posted on 2024-07-24 19:17  itdef  阅读(28)  评论(0)    收藏  举报

导航