fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define ll long long
  5.  
  6. ll dp[5001][496];
  7.  
  8. ll solve(ll k , ll f , ll x){
  9. if(k == 0){
  10. if(x == 0) return 0;
  11. return 1e9;
  12. }
  13.  
  14. if(dp[k][x] != -1){
  15. return dp[k][x];
  16. }
  17.  
  18. ll ans = 1e12;
  19.  
  20. for(int i=1;i<10;i++){
  21. if(k >= i){
  22. ans = min(ans , solve(k-i , f , ((10 * x) + i) % f) + 1);
  23. }
  24. }
  25.  
  26. return dp[k][x] = ans;
  27. }
  28.  
  29. int main(){
  30. ll f , k;
  31. cin>>k>>f;
  32.  
  33. memset(dp,-1,sizeof(dp));
  34.  
  35. ll ans = solve(k,f,0);
  36.  
  37. cout<<ans<<endl;
  38. return 0;
  39. }
  40.  
Runtime error #stdin #stdout 0.01s 22728KB
stdin
Standard input is empty
stdout
Standard output is empty