Advertisement
a53

fact

a53
Oct 27th, 2017
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.79 KB | None | 0 0
  1. #include<cstdio>
  2. #define T 5
  3. using namespace std;
  4. long n,b,d[10][3],nr;
  5. FILE *fin=fopen("fact.in","r");
  6. FILE *fout=fopen("fact.out","w");
  7.  
  8. void desc(long b){
  9. nr=0;
  10. for(long i=2;b>1;i++)
  11. if(!(b%i)){
  12. d[nr][0]=i;
  13. d[nr][1]=0;
  14. while(!(b%i)){
  15. d[nr][1]++;
  16. b/=i;
  17. }
  18. nr++;
  19. }
  20. }
  21.  
  22. long cifra()
  23. {
  24. long i,j,p=1,aux;
  25. for(j=0;j<nr;j++)
  26. d[j][2]=0;
  27. for(i=1;i<=n;i++){
  28. aux=i;
  29. for(j=0;j<nr;j++){
  30. while(!(aux%d[j][0])){
  31. d[j][2]++;
  32. aux/=d[j][0];
  33. }
  34. }
  35. p=(p*(aux%b))%b;
  36. }
  37. return p;
  38. }
  39.  
  40. long minim()
  41. {
  42. long min=d[0][2]/d[0][1];
  43. for(long i=1;i<nr;i++)
  44. if(d[i][2]/d[i][1]<min)
  45. min=d[i][2]/d[i][1];
  46. return min;
  47. }
  48.  
  49. long calcul()
  50. {
  51. long i,j,p=cifra();
  52. long min=minim();
  53. for(i=0;i<nr;i++)
  54. for(j=0;j<d[i][2]-d[i][1]*min;j++)
  55. p=(p*d[i][0])%b;
  56. return p;
  57. }
  58.  
  59. void scrie()
  60. {
  61. long x=calcul();
  62. if(x<10)
  63. fprintf(fout,"%ld\n",x);
  64. else
  65. fprintf(fout,"%c\n",'A'+(x-10));
  66. }
  67.  
  68. int main()
  69. {
  70. for(int i=0;i<T;i++)
  71. {
  72. fscanf(fin,"%ld%ld",&n,&b);
  73. desc(b);
  74. scrie();
  75. }
  76. fclose(fin);
  77. fclose(fout);
  78. return 0;
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement