Tuesday, 8 October 2013

CodeChef October Challenge 2013 Editorial [Maxim and Dividers]

Problem: Maxim and Dividers
for a number N, by remove any digit from it, if it became a lucky* number, N is overlucky number.
you will be given a number, you have to calculate how many of its divisors is overlucky.

Observation: any number having at least one 4 or 7 is overlucky number.

first, make a function isOverlucky() to check weather a number overlucky or not.
now, loop for 1 to square root to find all its divisors and check them.

code:
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. bool isOverlucky(int n)
  5. {
  6. while(n)
  7. {
  8. if(n%10==4 || n%10==7)
  9. return true;
  10. n/=10;
  11. }
  12. return false;
  13. }
  14.  
  15. int main()
  16. {
  17. int tc,kk=1, n;
  18. cin>>tc;
  19. while(tc--)
  20. {
  21. cin>>n;
  22. int sq=sqrt(n), ans=0;
  23. for(int i=1;i<=sq;i++)
  24. {
  25. if(n%i==0)
  26. {
  27. if(overlucky(i))
  28. ans++;
  29. if(overlucky(n/i))
  30. ans++;
  31. }
  32. }
  33. if(sq*sq==n) if(isOverlucky(sq)) ans--;
  34. cout<<ans<<endl;
  35. }
  36. return 0;
  37. }

 Note: if N is square number, then it is calculating twice, don't forget to check that.

*lucky number: any number having the digit only 4 and 7 is lucky number.
example: 4, 444, 74744, 777 etc.

No comments:

Post a Comment