[AHOI2009] similar distribution digit dp

Title Description

Give two numbers a,b, and find out the number of numbers whose sum of the numbers in [a,b] can divide the original number by integral.

I / O format

Input format:

 

One line, two integers a and b

 

Output format:

 

An integer indicating the answer

 

Example of input and output

Input example ා 1: copy

10 19

Output example: copy

3

Significant digit dp;

What we need is each number and the number that can be divided by the original number;

But the scope reaches, array range cannot be opened;

What should I do? Considering modulus;

Let's enumerate modules. If the sum of modules can be divided by integers and the current module is the sum of all the digits, then the answer will be + 1;

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<bitset>
#include<ctime>
#include<deque>
#include<stack>
#include<functional>
#include<sstream>
//#include<cctype>
//#pragma GCC optimize("O3")
using namespace std;
#define maxn 100005
#define inf 0x3f3f3f3f
#define INF 999999999999999
#define rdint(x) scanf("%d",&x)
#define rdllt(x) scanf("%lld",&x)
#define rdult(x) scanf("%lu",&x)
#define rdlf(x) scanf("%lf",&x)
#define rdstr(x) scanf("%s",x)
typedef long long  ll;
typedef unsigned long long ull;
typedef unsigned int U;
#define ms(x) memset((x),0,sizeof(x))
const long long int mod = 1e9 + 7;
#define Mod 100003
#define sq(x) (x)*(x)
#define eps 1e-3
typedef pair<int, int> pii;
#define pi acos(-1.0)
const int N = 1005;
#define REP(i,n) for(int i=0;i<(n);i++)
inline ll rd() {
	ll x = 0;
	char c = getchar();
	bool f = false;
	while (!isdigit(c)) {
		if (c == '-') f = true;
		c = getchar();
	}
	while (isdigit(c)) {
		x = (x << 1) + (x << 3) + (c ^ 48);
		c = getchar();
	}
	return f ? -x : x;
}

ll gcd(ll a, ll b) {
	return b == 0 ? a : gcd(b, a%b);
}
ll sqr(ll x) { return x * x; }

/*ll ans;
ll exgcd(ll a, ll b, ll &x, ll &y) {
	if (!b) {
		x = 1; y = 0; return a;
	}
	ans = exgcd(b, a%b, x, y);
	ll t = x; x = y; y = t - a / b * y;
	return ans;
}
*/

ll dp[30][200][200];
int a[30];
ll sum;
ll MOD;

ll dfs(int pos, int lead, int limit, ll sum, ll mod) {
	ll ans = 0;
	if (pos == 0 && lead) {
		if (sum == MOD && mod == 0)return 1;
		return 0;
	}
	if (limit == 0 && lead&&dp[pos][sum][mod] != -1)return dp[pos][sum][mod];
	int up = limit ? a[pos] : 9;
	for (int i = 0; i <= up; i++) {
		ans += dfs(pos - 1, lead || i, limit && (i == up), sum + i, (mod * 10 + i) % MOD);
	}
	if (lead&&limit == 0)dp[pos][sum][mod] = ans;
	return ans;
}

ll sol(ll x) {
	int pos = 0;
	while (x) {
		a[++pos] = x % 10; x /= 10;
	}
	ll ans = 0;
	for (MOD = 1; MOD <= 9 * pos; MOD++) {
		memset(dp, -1, sizeof(dp));
		ans += dfs(pos, 1, 1, 0, 0);
	}
	return ans;
}

int main()
{
	//ios::sync_with_stdio(false);
	ll a, b; rdllt(a); rdllt(b);
	cout << sol(b) - sol(a - 1) << endl;
    return 0;
}

 

 

Keywords: Programming iOS

Added by phpbrat on Tue, 10 Dec 2019 22:17:49 +0200