Question Description:
Ragu has given a range [L, R] to Smith. Smith wants to require to find the number of integers 'X' in the range such that GCD(X, F(X)) > 1 where F(x) is equal to the sum of digits of 'X' in its hexadecimal (or base 16) representation.
Example : F(27) = 1+B=1+11=12
(27 in hexadecimal is written as 1B)
Constraints:
1 <= T <= 50
1 <= L
R <= 10^5
Input Format:
The first line contains a positive integer 'T' denoting the number of questions that you are asked.
Each of the next 'T' lines contain two integers 'L' and 'R' denoting the range of questions.
#include<stdio.h>
int g(int a,int b){return b?g(b,a%b):a;}
int f(int x){int s=0;while(x){s+=x%16;x/=16;}return s;}
int search(int a, int b){int c=0,i;for(i=a;i<=b;i++)c+=(g(i,f(i))>1);return c;}
int main(){int t,l,r;scanf("%d",&t);while(t--){scanf("%d %d",&l,&r);printf("%d\n",search(l,r));}return 0;}
0 Comments