Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Source : //https://discuss.codechef.com/t/sum-of-gp-mod-m/20998/2
- #define int long long
- const int mod = 1e9 + 7;
- int mul(int x, int y) {
- return (x * y) % mod;
- }
- int add(int x, int y) {
- return (x + y) % mod;
- }
- // a is common ratio
- // series always starts from 1
- int GeometricProg(int a, int n) {
- if (!n) {
- return 1;
- }
- if (n & 1) {
- int x = gp(mul(a, a), n / 2);
- return mul((1 + a), x);
- }
- else {
- int x = gp(mul(a, a), n / 2 - 1);
- return add(1, mul(a, mul((a+1), x)));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment