fork(5) download
  1. #include<iostream>
  2. #include<cstring>
  3. #include<cstdio>
  4. #include<sstream>
  5. using namespace std;
  6. int del[]={0,9,90,738,5274,32490,168570,712890,2345850,5611770,8877690};
  7. int f[]={1,1,2,6,24,120,720,5040,40320,362880,3628800};
  8. long long int fordel(long long int k,long long int n)
  9. {
  10. if(k>n)
  11. return f[k]/f[k-n];
  12. else
  13. return f[k]/f[n-k];
  14. }
  15.  
  16. string itoa(int i)
  17. {
  18. string s;
  19. ostringstream out;
  20. out << i;
  21. s = out.str();
  22. return s;
  23. }
  24. int diglen(long long n)
  25. {
  26.  
  27. int cnt=0;
  28. while(n)
  29. {n/=10;
  30. cnt++;
  31. }
  32. return cnt;
  33. }
  34.  
  35. long long int delicious(long long int n)
  36. {
  37. if(n<=10)
  38. return n;
  39. if(diglen(n)>10)
  40. return del[10];
  41. string s=itoa(n);
  42. int k=s.size();
  43. long long int res=del[k-1];
  44. int b[10];
  45. for(int i=0;i<10;i++)
  46. b[i]=0;
  47.  
  48. for(int i=0;i<k;i++)
  49. {
  50. int d=s[i]-'0';
  51. for(int j=0;j<=9;j++)
  52. if(i==0 && j==0)
  53. continue;
  54. else if(b[j]==0 && j<d)
  55. {res+=fordel(9-i,k-i-1);
  56. // cout<<fordel(9-i,k-i-1)<<" for"<<d<<endl;
  57. }
  58. else if(b[j]==1 && j==d)
  59. return res;
  60. b[d]=1;
  61. }
  62. return res+1;
  63. }
  64.  
  65. string itoa(long long int i)
  66. {
  67. string s;
  68. ostringstream out;
  69. out << i;
  70. s = out.str();
  71. return s;
  72. }
  73.  
  74. int isdel(string s)
  75. { int c[10];
  76. for(int i=0;i<10;i++)
  77. c[i]=0;
  78. for(int i=0;i<s.size();i++)
  79. if(c[s[i]-'0']==1)
  80. return 0;
  81. else
  82. c[s[i]-'0']++;
  83. return 1;
  84. }
  85.  
  86. int main()
  87. { long long int t,l,r;
  88. cin>>t;
  89. while(t--)
  90. {
  91. scanf("%lld %lld",&l,&r);
  92. if(isdel(itoa(l))==0)
  93. {
  94. // cout<<delicious(l)<<" "<<delicious(r)<<endl;
  95. printf("%lld\n",delicious(r)-delicious(l));
  96. }
  97. else
  98. {
  99. //cout<<delicious(l)<<" "<<delicious(r)<<endl;
  100. printf("%lld\n",delicious(r)-delicious(l)+1);
  101. }
  102. }
  103. return 0;
  104. }
Success #stdin #stdout 0.01s 2824KB
stdin
1
1234567890 1234567980
stdout
3