-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathtp.cpp
150 lines (136 loc) · 2.76 KB
/
tp.cpp
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include <iostream>
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <bits/stdc++.h>
#include <set>
#include <stddef.h>
#include <stdlib.h>
#include <unordered_set>
#include <unordered_map>
#include <map>
#include <string>
#include <ctype.h>
#include <queue>
// #pragma GCC optimize "trapv"
typedef long long int ll;
using namespace std;
#define f(abcd, n) for (int abcd = 0; abcd < n; abcd++)
#define in(n) cin >> n;
#define print(n) cout << n;
#define out(n) cout << n << "\n";
bool sortbysec(const pair<ll, ll> &a,
const pair<ll, ll> &b)
{
return (a.second < b.second);
}
ll power(long long x, ll y, ll p)
{
ll res = 1; // Initialize result
x = x % p; // Update x if it is more than or
// equal to p
if (x == 0)
return 0; // In case x is divisible by p;
while (y > 0)
{
// If y is odd, multiply x with result
if (y & 1)
res = (res * x) % p;
// y must be even now
y = y >> 1; // y = y/2
x = (x * x) % p;
}
return res;
}
string decToBinary(int n, int numBit)
{
// array to store binary number
int binaryNum[32];
// counter for binary array
int i = 0;
while (n > 0)
{
// storing remainder in binary array
binaryNum[i] = n % 2;
n = n / 2;
i++;
}
// printing binary array in reverse order
string ans = "";
for (int j = i - 1; j >= 0; j--)
{
char c = binaryNum[j] + '0';
ans = ans + (c);
}
if (ans.size() < numBit)
{
int tp = numBit - ans.size();
for (int i = 0; i < tp; i++)
{
ans = '0' + ans;
}
}
return ans;
}
#define MAX 1000
int multiply(int x, int res[], int res_size);
long long int factorial(long long int n)
{
if (n == 0)
return 1;
return ((n % 1000000007) * factorial(n - 1) % 1000000007) % 1000000007;
}
ll printDivisors(ll n)
{
vector<ll> v;
ll cnt = 0;
// Note that this loop runs till square root
for (ll i = 1; i <= sqrt(n); i++)
{
if (n % i == 0)
{
// If divisors are equal, print only one
if (n / i == i)
{
// v.push_back(i);
cnt++;
// cout << " " << i;
}
else // Otherwise print both{
{
// v.push_back(i);
// v.push_back(n/i);
// cout << " " << i << " " << n / i;
cnt += 2;
}
}
}
return cnt;
}
int main()
{
// ios_base::sync_with_stdio(false);
// cin.tie(NULL);
int t;
in(t)
while (t--)
{
ll n;
in(n)
ll mod = 10e9 + 7;
in(mod)
ll factors = printDivisors(n);
// (2^factors-1)%mod;
// out( % mod)
if ((power(2, factors, mod) - 1) % mod < 0)
{
out((power(2, factors, mod) - 1 + mod) % mod)
}
else
{
out(power(2, factors, mod) - 1)
}
// out((power(2, factors, mod) - 1 ) % mod);
}
return 0;
}