So there is [L, R] range given and we have to count the number of integers in the range such that
GCD(i, H(i)) H(i) represents sum of digits in hexadecimal representation. H(27) = 1 + B = 1 + 11 = 12 , (27 is written as 1B in hexadecimal representation.)
I have solved this question without making a prefix table, but when I am trying to make a prefix table it gives some unusual output.
I am attaching my code here.
Input format
The first line contains a positive integer denoting the number of questions that you are asked. Each of the next lines contain two integers and denoting the range of questions.
Output Format
For each test case, you are required to print exactly numbers as the output.
Input: 3 1 3 5 8 7 12
Correct Output: 2 4 6
My Output(Incorrect): 1 2 4
#include<bits/stdc++.h>
using namespace std;
#define fastIO ios_base::sync_with_stdio(false);cin.tie(NULL); cout.tie(NULL);
#define ll long long
const ll MOD = 1000000007;
int pre[100001];
int sumdigit(int n)
{
char hexadigit[100];
int i = 0;
while (n != 0)
{
int temp = n % 16;
if (temp < 10)
{
hexadigit[i] = temp + 48;
i++;
}
else
{
hexadigit[i] = temp + 55;
i++;
}
n = n/16;
}
int sum = 0;
for (int j = 0; j < i; j++)
{
if (isdigit(hexadigit[j]))
sum = sum + hexadigit[j] - '0';
else
sum = sum + hexadigit[j] - 'A' + 10;
}
return sum;
}
void solve()
{
int L, R;
cin>>L>>R;
cout << pre[R] - pre[L + 1]<<"\n";
}
int main()
{
// #ifndef ONLINE_JUDGE
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
// #endif
pre[0] = 1;
int count = 0;
for (int i = 1; i < 100001; i++)
{
if(__gcd(i, sumdigit(i) ) > 1)
count++;
else
pre[i] = count + pre[i - 1];
}
int t;
cin>>t;
while(t--)
{
solve();
}
}