Ankush08's blog

By Ankush08, history, 3 years ago, In English

I was attempting this problem : https://codeforces.com/contest/628/problem/D and came up with a solution which basically look like this :

int d, m;

ll int magicNumberDP(string n, int idx,ll int curNum, bool tight) {
	if (idx >= n.length()) {
		return !curNum ;
	}

	int uprBnd = (tight) ? n[idx] - '0' : 9 ;

	ll int ans = 0 ;

	for (int dig = 0 ; dig <= uprBnd ; dig++) {
		if ((idx + 1) % 2 == 0 and d != dig) {
			continue ;
		}

		if ((idx + 1) % 2 == 1 and d == dig) {
			continue ;
		}

		ans = ans + magicNumberDP(n, idx + 1, (curNum * 10 + dig) % m, tight & (dig == uprBnd)) ;
	}

	return ans;
}

void magicNumber() {
	cin >> m >> d ;
	ll int r, l;
	cin >> l >> r ;

	l--;

	string R = to_string(r);
	string L = to_string(l);

	int ansR = magicNumberDP(R, 0, 0ll, 1);

	int ansL = magicNumberDP(L, 0, 0ll, 1);

	cout << ansL << " " << ansR << endl ;
	cout << ansR - ansL << endl;
}

But this is giving wrong answer can someone tell me why I am wrong and how can I correct it?

  • Vote: I like it
  • 0
  • Vote: I do not like it

| Write comment?
»
3 years ago, # |
  Vote: I like it 0 Vote: I do not like it

You haven't applied modular operation. See my solution : https://codeforces.com/contest/628/submission/119295777

»
3 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Didn't you read the constraints?

It is guaranteed that a ≤ b, the number of digits in a and b are the same and don't exceed 2000.

Variable $$$l$$$ and $$$r$$$ should have a string data type, taking input in integer and converting it into string won't work here.