Educational Codeforces Round 90 (Rated for Div. 2) E - Sum of Digits 无脑打表
#include<map> #include<queue> #include<time.h> #include<limits.h> #include<cmath> #include<ostream> #include<iterator> #include<set> #include<stack> #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; #define rep(i,m,n) for(int i=m;i<=n;i++) #define mem(st) memset(st,0,sizeof st) int read() { int res=0,ch,flag=0; if((ch=getchar())=='-') //判断正负 flag=1; else if(ch>='0'&&ch<='9') //得到完整的数 res=ch-'0'; while((ch=getchar())>='0'&&ch<='9') res=res*10+ch-'0'; return flag?-res:res; } typedef long long ll; typedef pair<int,int> pii; typedef unsigned long long ull; typedef pair<double,double> pdd; const int inf = 0x3f3f3f3f; const int maxn=1e6+10; int mp[200][10]= { { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0}, { 2, 0, 0, 0, 0, 0, 0, 0, 0, 0}, { 3, 1, 0, 0, 0, 0, 0, 0, 0, 0}, { 4, 0, 0, 0, 0, 0, 0, 0, 0, 0}, { 5, 2, 0, 0, 0, 0, 0, 0, 0, 0}, { 6, 0, 1, 0, 0, 0, 0, 0, 0, 0}, { 7, 3, 0, 0, 0, 0, 0, 0, 0, 0}, { 8, 0, 0, 0, 0, 0, 0, 0, 0, 0}, { 9, 4, 2, 0, 0, 0, 0, 0, 0, 0}, { 19, 9, 0, 1, 0, 0, 0, 0, 0, 0}, { 29, 5, 0, 0, 0, 0, 0, 0, 0, 0}, { 39, 19, 3, 0, 0, 0, 0, 0, 0, 0}, { 49, 6, 0, 0, 0, 0, 0, 0, 0, 0}, { 59, 29, 0, 2, 0, 0, 0, 0, 0, 0}, { 69, 7, 4, 9, 1, 0, 0, 0, 0, 0}, { 79, 39, 0, 0, 0, 0, 0, 0, 0, 0}, { 89, 8, 0, 0, 0, 0, 0, 0, 0, 0}, { 99, 49, 5, 3, 0, 0, 0, 0, 0, 0}, { 199, 18, 0, 19, 9, 0, 0, 0, 0, 0}, { 299, 59, 0, 8, 2, 0, 0, 0, 0, 0}, { 399, 28, 6, 0, 0, 1, 0, 0, 0, 0}, { 499, 69, 0, 4, 0, 0, 0, 0, 0, 0}, { 599, 38, 0, 29, 8, 0, 0, 0, 0, 0}, { 699, 79, 7, 18, 19, 9, 0, 0, 0, 0}, { 799, 48, 0, 7, 3, 0, 0, 0, 0, 0}, { 899, 89, 0, 5, 0, 0, 0, 0, 0, 0}, { 999, 58, 17, 39, 7, 2, 0, 0, 0, 0}, { 1999, 189, 0, 28, 18, 0, 1, 0, 0, 0}, { 2999, 68, 0, 17, 29, 0, 0, 0, 0, 0}, { 3999, 289, 27, 6, 4, 7, 9, 0, 0, 0}, { 4999, 78, 0, 49, 6, 0, 0, 0, 0, 0}, { 5999, 389, 0, 38, 17, 0, 8, 0, 0, 0}, { 6999, 88, 37, 27, 28, 3, 0, 0, 0, 0}, { 7999, 489, 0, 16, 39, 0, 7, 0, 0, 0}, { 8999, 98, 0, 59, 5, 0, 2, 0, 0, 0}, { 9999, 589, 47, 48, 16, 5, 6, 1, 0, 0}, { 19999, 198, 0, 37, 27, 0, 19, 9, 0, 0}, { 29999, 689, 0, 26, 38, 0, 5, 8, 0, 0}, { 39999, 298, 57, 69, 49, 4, 18, 7, 0, 0}, { 49999, 789, 0, 58, 15, 0, 4, 6, 0, 0}, { 59999, 398, 0, 47, 26, 0, 17, 5, 0, 0}, { 69999, 889, 67, 36, 37, 15, 3, 4, 0, 0}, { 79999, 498, 0, 79, 48, 0, 16, 3, 0, 0}, { 89999, 989, 0, 68, 59, 0, 29, 2, 0, 0}, { 99999, 598, 77, 57, 25, 14, 15, 19, 1, 0}, { 199999, 1989, 0, 46, 36, 0, 28, 18, 0, 1}, { 299999, 698, 0, 89, 47, 0, 14, 17, 0, 2}, { 399999, 2989, 87, 78, 58, 25, 27, 16, 0, 3}, { 499999, 798, 0, 67, 69, 0, 13, 15, 0, 4}, { 599999, 3989, 0, 56, 35, 0, 26, 14, 0, 5}, { 699999, 898, 97, 189, 46, 24, 39, 13, 0, 6}, { 799999, 4989, 0, 88, 57, 0, 25, 12, 0, 7}, { 899999, 998, 0, 77, 68, 0, 38, 29, 0, 8}, { 999999, 5989, 197, 66, 79, 35, 24, 28, 11, 9}, {1999999, 1998, 0, 289, 45, 0, 37, 27, 0, 10}, {2999999, 6989, 0, 188, 56, 0, 23, 26, 0, 11}, {3999999, 2998, 297, 87, 67, 34, 36, 25, 0, 12}, {4999999, 7989, 0, 76, 78, 0, 49, 24, 0, 13}, {5999999, 3998, 0, 389, 89, 0, 35, 23, 0, 14}, {6999999, 8989, 397, 288, 55, 45, 48, 22, 0, 15}, {7999999, 4998, 0, 187, 66, 0, 34, 39, 0, 16}, {8999999, 9989, 0, 86, 77, 0, 47, 38, 0, 17}, {9999999, 5998, 497, 489, 88, 44, 33, 37, 21, 18}, { 0, 19989, 0, 388, 189, 0, 46, 36, 0, 19}, { 0, 6998, 0, 287, 65, 0, 59, 35, 0, 20}, { 0, 29989, 597, 96, 76, 55, 45, 34, 0, 21}, { 0, 7998, 0, 589, 87, 0, 58, 33, 0, 22}, { 0, 39989, 0, 488, 188, 0, 44, 32, 0, 23}, { 0, 8998, 697, 387, 289, 54, 57, 49, 0, 24}, { 0, 49989, 0, 196, 75, 0, 43, 48, 0, 25}, { 0, 9998, 0, 689, 86, 0, 56, 47, 0, 26}, { 0, 59989, 797, 588, 187, 65, 69, 46, 31, 27}, { 0, 19998, 0, 487, 288, 0, 55, 45, 0, 28}, { 0, 69989, 0, 296, 389, 0, 68, 44, 0, 29}, { 0, 29998, 897, 789, 85, 64, 54, 43, 0, 30}, { 0, 79989, 0, 688, 186, 0, 67, 42, 0, 31}, { 0, 39998, 0, 587, 287, 0, 53, 59, 0, 32}, { 0, 89989, 997, 396, 388, 75, 66, 58, 0, 33}, { 0, 49998, 0, 889, 489, 0, 79, 57, 0, 34}, { 0, 99989, 0, 788, 95, 0, 65, 56, 0, 35}, { 0, 59998, 1997, 687, 286, 74, 78, 55, 41, 36}, { 0, 199989, 0, 496, 387, 0, 64, 54, 0, 37}, { 0, 69998, 0, 989, 488, 0, 77, 53, 0, 38}, { 0, 299989, 2997, 888, 589, 85, 63, 52, 0, 39}, { 0, 79998, 0, 787, 195, 0, 76, 69, 0, 40}, { 0, 399989, 0, 596, 386, 0, 89, 68, 0, 41}, { 0, 89998, 3997, 1989, 487, 84, 75, 67, 0, 42}, { 0, 499989, 0, 988, 588, 0, 88, 66, 0, 43}, { 0, 99998, 0, 887, 689, 0, 74, 65, 0, 44}, { 0, 599989, 4997, 696, 295, 185, 87, 64, 51, 45}, { 0, 199998, 0, 2989, 486, 0, 73, 63, 0, 46}, { 0, 699989, 0, 1988, 587, 0, 86, 62, 0, 47}, { 0, 299998, 5997, 987, 688, 94, 189, 79, 0, 48}, { 0, 799989, 0, 796, 789, 0, 85, 78, 0, 49}, { 0, 399998, 0, 3989, 395, 0, 188, 77, 0, 50}, { 0, 899989, 6997, 2988, 586, 285, 84, 76, 0, 51}, { 0, 499998, 0, 1987, 687, 0, 187, 75, 0, 52}, { 0, 999989, 0, 896, 788, 0, 83, 74, 0, 53}, { 0, 599998, 7997, 4989, 889, 194, 186, 73, 61, 54}, { 0,1999989, 0, 3988, 495, 0, 289, 72, 0, 55}, { 0, 699998, 0, 2987, 686, 0, 185, 89, 0, 56}, { 0,2999989, 8997, 996, 787, 385, 288, 88, 0, 57}, { 0, 799998, 0, 5989, 888, 0, 184, 87, 0, 58}, { 0,3999989, 0, 4988, 989, 0, 287, 86, 0, 59}, { 0, 899998, 9997, 3987, 595, 294, 93, 85, 0, 60}, { 0,4999989, 0, 1996, 786, 0, 286, 84, 0, 61}, { 0, 999998, 0, 6989, 887, 0, 389, 83, 0, 62}, { 0,5999989, 19997, 5988, 988, 485, 285, 82, 71, 63}, { 0,1999998, 0, 4987, 1989, 0, 388, 189, 0, 64}, { 0,6999989, 0, 2996, 695, 0, 284, 188, 0, 65}, { 0,2999998, 29997, 7989, 886, 394, 387, 187, 0, 66}, { 0,7999989, 0, 6988, 987, 0, 193, 186, 0, 67}, { 0,3999998, 0, 5987, 1988, 0, 386, 185, 0, 68}, { 0,8999989, 39997, 3996, 2989, 585, 489, 184, 0, 69}, { 0,4999998, 0, 8989, 795, 0, 385, 183, 0, 70}, { 0,9999989, 0, 7988, 986, 0, 488, 92, 0, 71}, { 0,5999998, 49997, 6987, 1987, 494, 384, 289, 81, 72}, { 0,19999989, 0, 4996, 2988, 0, 487, 288, 0, 73}, { 0,6999998, 0, 9989, 3989, 0, 293, 287, 0, 74}, { 0,29999989, 59997, 8988, 895, 685, 486, 286, 0, 75}, { 0,7999998, 0, 7987, 1986, 0, 589, 285, 0, 76}, { 0,39999989, 0, 5996, 2987, 0, 485, 284, 0, 77}, { 0,8999998, 69997, 19989, 3988, 594, 588, 283, 0, 78}, { 0,49999989, 0, 9988, 4989, 0, 484, 192, 0, 79}, { 0,9999998, 0, 8987, 995, 0, 587, 389, 0, 80}, { 0,59999989, 79997, 6996, 2986, 785, 393, 388, 91, 81}, { 0,19999998, 0, 29989, 3987, 0, 586, 387, 0, 82}, { 0,69999989, 0, 19988, 4988, 0, 689, 386, 0, 83}, { 0,29999998, 89997, 9987, 5989, 694, 585, 385, 0, 84}, { 0,79999989, 0, 7996, 1995, 0, 688, 384, 0, 85}, { 0,39999998, 0, 39989, 3986, 0, 584, 383, 0, 86}, { 0,89999989, 99997, 29988, 4987, 885, 687, 292, 0, 87}, { 0,49999998, 0, 19987, 5988, 0, 493, 489, 0, 88}, { 0,99999989, 0, 8996, 6989, 0, 686, 488, 0, 89}, { 0,59999998, 199997, 49989, 2995, 794, 789, 487, 191, 90}, { 0,199999989, 0, 39988, 4986, 0, 685, 486, 0, 181}, { 0,69999998, 0, 29987, 5987, 0, 788, 485, 0, 182}, { 0,299999989, 299997, 9996, 6988, 985, 684, 484, 0, 183}, { 0,79999998, 0, 59989, 7989, 0, 787, 483, 0, 184}, { 0,399999989, 0, 49988, 3995, 0, 593, 392, 0, 185}, { 0,89999998, 399997, 39987, 5986, 894, 786, 589, 0, 186}, { 0,499999989, 0, 19996, 6987, 0, 889, 588, 0, 187}, { 0,99999998, 0, 69989, 7988, 0, 785, 587, 0, 188}, { 0,599999989, 499997, 59988, 8989, 1985, 888, 586, 291, 189}, { 0,199999998, 0, 49987, 4995, 0, 784, 585, 0, 190}, { 0,699999989, 0, 29996, 6986, 0, 887, 584, 0, 281}, { 0,299999998, 599997, 79989, 7987, 994, 693, 583, 0, 282}, { 0,799999989, 0, 69988, 8988, 0, 886, 492, 0, 283}, { 0,399999998, 0, 59987, 9989, 0, 989, 689, 0, 284}, { 0,899999989, 699997, 39996, 5995, 2985, 885, 688, 0, 285}, }; void solve() { int n,k; cin>>n>>k; if(k==0) { if(n%9) cout<<n%9; for(int i=1;i<=n/9;i++) cout<<9; cout<<endl; return ; } if(mp[n-1][k]!=0) { cout<<mp[n-1][k]<<endl; return ; } if(mp[n-1][k]==0) { int ans=0; for(int i=0; i<=k; i++) { int t=i; while(t) { ans+=t%10; t/=10; } } if(ans==n) { cout<<"0"<<endl; return ; } else { cout<<"-1"<<endl; return; } } else cout<<mp[n-1][k]<<endl; } signed main() { int t; cin>>t; while(t--) solve(); return 0; }