PROBLEM LINK: Sequence Sum
Author: Rishabh Rathi
Tester: Rishabh Rathi
DIFFICULTY:
CAKEWALK
PREREQUISITES:
Maths
PROBLEM:
You are given a sequence - 11, 17, 23, 29, ……
You need to find the total number of sweets needed to distribute among the first N children (sum of first N elements).
NOTE : Since answer can be large, print answer modulo 10^9+7
EXPLANATION:
The sequence given is an Arithmetic Progression. Here, first term (a) is 11 and common difference (d) is 6.
Sum of N terms of arithmetic progression, \sum_{i = 1}^{N} a_i
= \frac{N [2a + (n – 1)d]}{2}
=\frac{N [2*11 + (N – 1)*6]}{2}
=\frac{N [22 + 6N - 6]}{2}
=\frac{N [16 + 6N]}{2}
=N [8 + 3N]
SOLUTIONS:
Setter's Solution (Python)
import sys
def input():
return sys.stdin.readline().strip()
mod = 10**9 + 7
t = int(input())
for _ in range(t):
n = int(input())
ans = (n*(8 + 3*n))%mod
print(ans)
Tester's Solution (CPP)
#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
long long int n, ans;
int t; cin>>t;
while(t--) {
cin>>n;
ans = ((n%mod) * (8 + 3*n)%mod)%mod;
cout<<ans<<"\n";
}
return 0;
}
Feel free to share your approach. In case of any doubt or anything is unclear, please ask it in the comments section. Any suggestions are welcome