Friday 7 February 2014

Special Digit - Code Chef Problem

You have a number N and you want to calculate how many divisors of N are special.
A number is said to be special if it is possible to remove some digits from it to get a number having 3, 5 or 6 only.For exemple number 38597 is special since it is posible to remove digits 8, 9, 7 to get 35. You can remove some digits but not all digits. You can remove digits from left, right or middle.

Solution:
#include<iostream> #include<cmath> using namespace std; int check(int n) { if( n < 1 ) { return 0; } if( ((n % 10) == 3) || ((n % 10) == 5) || ((n % 10) == 6) ) { return 1; } else { n = n / 10 ; return (check(n)); } } int main() { int T = 0; long long N = 0; cin>>T; long long temp = 0 ; long long count = 0; for(int i = 0 ; i < T ; i++) { cin>>N; count = 0 ; long long j = 1; for(; j <sqrt(N); j++) { if( (N % j) == 0) { count = count + check(j) + check(N/j); } } if(j*j == N) count = count + check(j); cout<<count<<endl; } return 0; }

No comments:

Post a Comment