MATH PROBLEM

closed account (3U5X216C)
Prakhar is fond of solving the mathematical equations,one day his girlfriend asked him a new equation to solve.Equation is as follows:
K*(K+1) = 4*A*B + 2*max(A,B)
She also gives him an array(A) of pair(A,B) of size N,and asks Q queries,in each query she gives the value of K and asks for the number of pairs which is present in the array(A) and satisfies the given equation for given K.
Prakhar got stucked in solving the given equation but he don't want to dissapoint his girlfriend,so he is asking for your help,can you help him out?

INPUT:
First line contains two space seperated integer N [size of the array(A)] and Q (number of query to ask).
Next N lines contains two space separated integers each lines denotes [information of pair(A,B)] of array(A).
Next line contains Q space sepereted integers (value of K).

OUTPUT:
For each Q, print a single integer in new line denoting inverse modulo of the number of pairs which satisfies the given equation and also present in the given array(A) with 10^9+9 or -1 if there is no such pair exist.

CONSTRAINTS:
1<=N<=10^5
1<=A[i],B[i]<=10^9
1<=Q<=10^5
1<=K<=2*10^9

Hi i have solved this but it gives me time limit exceeded can anyone suggest anything new to me?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include<bits/stdc++.h>
using namespace std;

#define ull unsigned long long int
#define mod 1000000009

long long int inverse(long long int z, long long int m) {
    int d = m;
    int y = 0, x = 1;
    if (m == 1) {
        return 0;
    }
    while (z > 1) {
        int query = z / m;
        int b = m;
        m = z % m, z = b;
        b = y;
        y = x - query * y;
        x = b;
    }
    if (x < 0) {
        x += d;
    }
    return x;
}

int main() {
    map<pair<ull,ull>,int> m;
    vector<pair<int,int>> v;
    ull n;
    ull q;
    ull a;
    ull b;
    ull k;
    cin >> n >> q;
    ull arr[n];
    for (int i = 0; i < n; ++i) {
        cin >> a >> b;
        v.push_back(make_pair(a,b));
        arr[i] = (4 * a * b) + (2 * max(a,b));
        if (m[{a,b}] == 0) {
            m[{a,b}] = 1;
        } else {
            m[{a,b}] += 1; 
        }
    }
    ull exp = 0;
    for (int i = 0; i < q; ++i) {
        cin >> k;
        exp = k * (k + 1);
        ull c = 0;
        ull ans = 0;
        for (auto j = m.begin(); j != m.end(); ++j, ++c) {
            if (arr[c] == exp) {
                int x = v[c].first;
                int y = v[c].second;
                ans = ans + m[{x,y}];
            }
        }
        if (ans < 1) {
            cout << -1 << endl;
        } else {
            cout << inverse(ans,mod) << endl;
        }
    }
    return EXIT_SUCCESS;
}
closed account (4z86RXSz)
just make a unordered map and store value of 4ab+2max(a,b)
for each a,b then when you enter k check wheather k(k+1) exists in map or not if it doesnt cout<<-1 else
cout<<invmod(k(k+1),1000000009);
closed account (3U5X216C)
@iamdad3 thanks your trick worked
Topic archived. No new replies allowed.