bhupixb

Big Mod

Aug 5th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.34 KB | None | 0 0
  1. #include <cmath>
  2. #include <cstdio>
  3. #include <vector>
  4. #include <iostream>
  5. #include <algorithm>
  6. using namespace std;
  7. typedef unsigned long long int LL;
  8. const LL mod = 1e10;
  9. LL bigMod(LL a, LL b, LL c) {
  10. if (a == 0 || b == 0) {
  11. return 0;
  12. }
  13. if (a == 1) {
  14. return b;
  15. }
  16. if (b == 1) {
  17. return a;
  18. }
  19.  
  20. // Returns: (a * b/2) mod c
  21. LL a2 = bigMod(a, b / 2, c);
  22.  
  23. // Even factor
  24. if ((b & 1) == 0) {
  25. // [((a * b/2) mod c) + ((a * b/2) mod c)] mod c
  26. return (a2 + a2) % c;
  27. } else {
  28. // Odd exponent
  29. // [(a mod c) + ((a * b/2) mod c) + ((a * b/2) mod c)] mod c
  30. return ((a % c) + (a2 + a2)) % c;
  31. }
  32. }
  33. LL po(LL x, LL n){
  34. LL res = 1LL;
  35. while(n){
  36. if(n & 1){
  37. if(res > 1e9 || x > 1e9)
  38. res = bigMod(res, x, mod) % mod;
  39. else
  40. res = (res * x) % mod;
  41. }
  42. n = n / 2;
  43. if(x > 1e9)
  44. x = bigMod(x, x, mod);
  45. else{
  46. x = (x * x) % mod;
  47. }
  48. }
  49. return res;
  50. }
  51.  
  52. int main() {
  53. ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);
  54. int n, i;LL ans = 0;
  55. cin >> n;
  56. for(i = 1; i <= n; i++){
  57. ans = (ans % mod + po(i,i)) % mod;
  58. }
  59. cout << ans;
  60. //cout << po(2, 5);
  61. return 0;
  62. }
Add Comment
Please, Sign In to add comment