Advertisement
Saleh127

UVA 568

Oct 21st, 2020
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.11 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define ll long long
  4. #define test int t; cin>>t; for(int cs=1;cs<=t;cs++)
  5.  
  6. ll fact[500000];
  7.  
  8. ll gunn(ll x,ll fact[],ll sizee)
  9. {
  10. ll c=0,r;
  11.  
  12. for(ll i=0;i<sizee;i++)
  13. {
  14. r=fact[i]*x+c;
  15. fact[i]=r%10;
  16. c=r/10;
  17. }
  18. while(c)
  19. {
  20. fact[sizee]=c%10;
  21. c/=10;
  22. sizee++;
  23. }
  24. return sizee;
  25. }
  26.  
  27. void factorial(ll n)
  28. {
  29. fact[0]=1;
  30. ll sizee=1;
  31. for(ll i=2;i<=n;i++)
  32. {
  33. sizee=gunn(i,fact,sizee);
  34. }
  35.  
  36. for(ll i=0;i<sizee;i++)
  37. {
  38. if(fact[i]!=0)
  39. {
  40. printf("%5lld -> %lld\n",n,fact[i]);
  41. break;
  42. }
  43. }
  44. }
  45.  
  46.  
  47. /*
  48. another process
  49.  
  50. fact[0]=fact[1]=1;
  51. i=2;
  52. while(i<=10000)
  53. {
  54. ll ans;
  55. ans=i*fact[i-1];
  56. while(ans%10==0)
  57. {
  58. ans/=10;
  59. }
  60. ans=ans%100000;
  61. fact[i]=ans;
  62. i++;
  63.  
  64. cout<<fact[input]%10<<endl;
  65. }
  66. */
  67.  
  68. int main()
  69. {
  70. ios_base::sync_with_stdio(0);
  71. cin.tie(0);cout.tie(0);
  72.  
  73. ll a;
  74. while(cin>>a)
  75. {
  76. factorial(a);
  77. }
  78. return 0;
  79. }
  80.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement