bhupixb

Sum of Geometric Progression

Jul 3rd, 2019
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.51 KB | None | 0 0
  1. // Source : //https://discuss.codechef.com/t/sum-of-gp-mod-m/20998/2
  2. #define int long long
  3. const int mod = 1e9 + 7;
  4. int mul(int x, int y) {
  5. return (x * y) % mod;
  6. }
  7.  
  8. int add(int x, int y) {
  9. return (x + y) % mod;
  10. }
  11. // a is common ratio
  12. // series always starts from 1
  13. int GeometricProg(int a, int n) {
  14. if (!n) {
  15. return 1;
  16. }
  17. if (n & 1) {
  18. int x = gp(mul(a, a), n / 2);
  19. return mul((1 + a), x);
  20. }
  21. else {
  22. int x = gp(mul(a, a), n / 2 - 1);
  23. return add(1, mul(a, mul((a+1), x)));
  24. }
  25. }
Advertisement
Add Comment
Please, Sign In to add comment