Advertisement
a53

pretty

a53
Dec 5th, 2021
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.86 KB | None | 0 0
  1. #include<iostream>
  2. #define MAXM 10000000
  3. using namespace std;
  4. long long int mod;
  5. int dp[MAXM+1];
  6. int pow (int base, int power) {
  7. if (power==0) return 1;
  8. if (power%2==1) return (base*pow(base,power-1))%mod;
  9. long long int num=pow(base,power/2);
  10. return (num*num)%mod;
  11. }
  12. int main () {
  13. long long int n,i,fact,num,val;
  14. cin >> n >> mod ;
  15. dp[1]=1; dp[2]=1;
  16. fact=2;
  17. if (n==1) {
  18. cout << 0 << endl ;
  19. return 0;
  20. }
  21. for (i=3; i<=min(n,mod); i++) {
  22. dp[i]=((i-1)*dp[i-1]+(i-2)*dp[i-2])%mod;
  23. fact*=i; fact%=mod;
  24. }
  25. if (n<=mod) {
  26. cout << (fact+mod-dp[n])%mod << endl ;
  27. return 0;
  28. }
  29. num=pow((mod-dp[mod-1])%mod,(n-1)/mod);
  30. if (n%mod==0) val=(num*dp[mod])%mod;
  31. else val=(num*dp[n%mod])%mod;
  32. cout << (mod-val)%mod ;
  33. cout << endl ;
  34. return 0;
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement