Skip to content
Programming101
Programming101

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programming101
Programming101

Learn everything about programming

HackerEarth Plot the Curve problem solution

YASH PAL, 31 July 2024
In this HackerEarth Plot, the Curve problem solution You are given with integers a,b,c,d,m. These represent the modular equation of a curve y*y mod m = (ax^3 + bx^2 + cx + d) mod m
Also, you are provided with an array A of size N. Now, your task is to find the number of pairs in the array that satisfy the given modular equation.
If (Ai,Aj) is a pair then Aj^2 mod m = (aAi^3 + bAi^2 + cAi + d) mod m.
Since the answer could be a very large output it modulo 10^9 + 7.
HackerEarth Plot the Curve problem solution

HackerEarth Plot the Curve problem solution.

#include <bits/stdc++.h>
#define M 1000000007
using namespace std;

int main() {

ios_base::sync_with_stdio(false);
cin.tie(NULL);
int T;
cin>>T;
while(T--)
{
long long a,b,c,d,m;
cin>>a>>b>>c>>d>>m;
int N;
cin>>N;
int arr[N];
unordered_map<int,long long> X,Y;
for(int i=0;i<N;i++)
{
cin>>arr[i];
long long x=((((((a*arr[i])%m)*arr[i])%m)*arr[i])%m+(((b*arr[i])%m)*arr[i])%m+(c*arr[i])%m+d%m)%m;
long long y=(arr[i]*arr[i])%m;
if(x<0)
x=m+x;
if(y<0)
y=m+y;
X[x]++;
Y[y]++;
}

long long ans=0;
for(auto it=X.begin();it!=X.end();it++)
ans=(ans + (it->second * Y[it->first])%M)%M;
cout<<ans<<endl;

}
}

Second solution

#include <bits/stdc++.h>

using namespace std;

const int md = 1E9 + 7;
const int N = 1E5 + 5;
long long ar[N];
map<long long, int> mp;

int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin >> t;
while(t --) {
long long a, b, c, d, m;
cin >> a >> b >> c >> d >> m;
int n;
cin >> n;
for(int i = 0; i < n; i ++) {
cin >> ar[i];
mp[(((ar[i] * ar[i]) % m) + m) % m] ++;
}
long long ans = 0;
for(int i = 0; i < n; i ++) {
long long x = (((((((a * ar[i]) % m) * ((ar[i] * ar[i]) % m)) % m) + (b * ((ar[i] * ar[i]) % m) % m) + ((c * ar[i]) % m) + d) % m) + m) % m;
if(mp.find(x) != mp.end())
ans += mp[x];
}
cout << (ans % md) << 'n';
mp.clear();
}
return 0;
}
coding problems solutions

Post navigation

Previous post
Next post
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
  • Hackerrank Day 6 Lets Review 30 days of code solution
How to download udemy paid courses for free

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
©2025 Programming101 | WordPress Theme by SuperbThemes