-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from pedroulissespu/master
lol
- Loading branch information
Showing
1 changed file
with
45 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#include "bits/stdc++.h" | ||
using namespace std; | ||
|
||
int dp[20][200][2]; | ||
|
||
int digitDP(int idx, int sum, int can, vector<int> &digit) | ||
{// idx eh o indice atual, sum a soma dos digitos ate idx, | ||
//e can uma flag para indicar se pode colocar | ||
//qualquer valor a partir daqui | ||
if(idx == (int)digit.size()) | ||
return sum; | ||
if(dp[idx][sum][can] != -1) | ||
return dp[idx][sum][can]; | ||
int ans = 0; | ||
for(int i = 0; i < 10; i++) | ||
if(can or i <= digit[idx]) | ||
ans += digitDP(idx + 1, sum + i, | ||
can or i < digit[idx], digit); | ||
return dp[idx][sum][can] = ans; | ||
} | ||
|
||
int query(int x) // responde a consulta de 0 ate x | ||
{ | ||
memset(dp, -1, sizeof(dp)); | ||
vector<int> digit; | ||
while(x) | ||
{ | ||
digit.push_back(x%10); | ||
x /= 10; | ||
} | ||
reverse(digit.begin(), digit.end()); | ||
return digitDP(0, 0, 0, digit); | ||
} | ||
|
||
int main() | ||
{ | ||
int q, a, b; | ||
cin >> q; | ||
while(q--) | ||
{ | ||
cin >> a >> b; | ||
cout << query(b) - query(a - 1) << '\n'; | ||
} | ||
return 0; | ||
} |