Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2020
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.35 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define fastio() ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
  4. #define pb push_back
  5. #define ll long long
  6. #define mp make_pair
  7. #define ff first
  8. #define ss second
  9. #define pii pair<int,int>
  10. #define sq(x) ((x)*(x))
  11. #define all(v) v.begin(),v.end()
  12. const int MOD = 1000*1000*1000 + 7;
  13. const double PI = 3.14159265;
  14. bool check(int sum1, int n) // checks if it is possible to make any number with sum s1 and n digits
  15. {
  16. // sum1 is sum of digits // check basically checks if u can have a n digits number with sum = s1
  17. if((sum1>(9*n)||(sum1<0))) // understood ?
  18. return false;
  19. else
  20. return true;
  21. }
  22. int main()
  23. {
  24. fastio();
  25. int m,s;
  26. cin>>m>>s;
  27. int sum=s,nod=m,num,flag; // nod = m
  28. if (m==1&&s==0)
  29. cout<<"0 0";
  30. else if((s==0)||(s>(9*m))) // not possible
  31. {
  32. cout<<"-1 -1";
  33. return 0;
  34. }
  35. else // minimum number
  36. {
  37. for (int i = 0; i < nod; ++i)
  38. {
  39. if(i==0)
  40. {
  41. num=1,flag=1; // num = 1; (first digit can't be 0)
  42. while(flag)
  43. {
  44. if(check((sum-num),(nod-i-1)))
  45. {
  46. cout<<num; // smallest possible digit
  47. flag--;
  48. sum-=num; // subtracting from num
  49. }
  50. else
  51. num++;
  52. }
  53.  
  54. }
  55. else
  56. {
  57. num=0,flag=1; // num = 0;
  58. while(flag)
  59. {
  60. if(check((sum-num),(nod-i-1)))
  61. {
  62. cout<<num;
  63. flag--;
  64. sum-=num;
  65. }
  66. else
  67. num++;
  68. }
  69. }
  70. }
  71. cout<<" ";
  72. sum=s;
  73. nod=m;
  74. while((sum>9)&&(nod>0)) // printing 9s
  75. {
  76. cout<<9;
  77. nod--;
  78. sum-=9;
  79. }
  80. if(nod!=0)
  81. {
  82. cout<<sum;
  83. nod--;
  84. }
  85. while(nod!=0)
  86. {
  87. cout<<0;
  88. nod--;
  89. }
  90. }
  91.  
  92. return 0;
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement