Professor Amit Gupta has given you a mathematical programming assignment. It goes as follows:
= sum of numbers that divide both x and y, i.e., sum of the common divisors of x and y.
Given the value of N, you are required to calculate the value of S.
.
As the value of S can be large, find it modulo .
Example:
Input: n = 5
Output: 33
Approach
C++
#include <bits/stdc++.h>using namespace std;#define N 1000000007#define LL long long#define mod 1000000007#define invertMod6 166666668long long numberSummation(long long m, long long n){long long a, b;a = (n / m) + m;b = (n / m) + 1 - m;a = (a % mod) * (b % mod);a %= mod;a *= m;a %= mod;return a;}long long numberSummation2(long long n){long long ans;ans = (((n % mod) *((n + 1) % mod)) %mod) *(((2 * n) % mod + 1) % mod);ans %= mod;ans *= invertMod6;ans %= mod;return ans;}int main(){long long n = 5;long long ans = 0, i;for (i = 1, ans = 0; i * i <= n; i++){ans += numberSummation(i, n);ans %= mod;}ans -= numberSummation2(i - 1);if (ans < 0){ans += mod;}cout << ans << "\n";return 0;}
No comments:
Post a Comment