Advertisement
a53

SimonaH

a53
Nov 30th, 2019
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.91 KB | None | 0 0
  1. #include <fstream>
  2. #define Ull unsigned long long int
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. Ull n;
  8. int p;
  9. ifstream f("simonah.in");
  10. f>>n>>p;
  11. f.close();
  12. int c[10]={0},nrc=0,sc=0;
  13. while(n)
  14. ++c[n%10],sc+=n%10,++nrc,n/=10;
  15. if(nrc==1)
  16. for(int i=1;i<10;++i)
  17. if(c[i])
  18. {
  19. ofstream g("simonah.out");
  20. g<<i%p;
  21. g.close();
  22. return 0;
  23. }
  24. Ull pr=1,fct[19]={1,1,2,6,24,120,720,5040,40320,362880,3628800,39916800,479001600,6227020800,87178291200,1307674368000,20922789888000,355687428096000,6402373705728000};
  25. for(int i=1;i<10;++i)
  26. if(c[i])
  27. pr*=fct[c[i]];
  28. Ull sol=0;
  29. switch(p)
  30. {
  31. case 2:
  32. for(int i=1;i<10;i+=2)
  33. if(c[i])
  34. sol+=fct[nrc-1]*c[i]/pr;
  35. break;
  36. case 3:
  37. sol=sc%3*(fct[nrc]/pr);
  38. break;
  39. case 4:
  40. int r4;
  41. for(int i=1;i<10;++i)
  42. if(c[i])
  43. {
  44. if(c[i]>1)
  45. {
  46. r4=(i*10+i)%4;
  47. sol+=r4*fct[nrc-2]*c[i]*(c[i]-1)/pr;
  48. }
  49. for(int j=i+1;j<10;++j)
  50. if(c[j])
  51. {
  52. r4=(i*10+j)%4;
  53. sol+=r4*fct[nrc-2]*c[i]*c[j]/pr;
  54. r4=(j*10+i)%4;
  55. sol+=r4*fct[nrc-2]*c[i]*c[j]/pr;
  56. }
  57. }
  58. break;
  59. case 5:
  60. for(int i=1;i<10;++i)
  61. if(i!=5)
  62. if(c[i])
  63. sol+=((i%5)*fct[nrc-1]*c[i])/pr;
  64. break;
  65. }
  66. ofstream g("simonah.out");
  67. g<<sol;
  68. g.close();
  69. return 0;
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement