Codeforces Round #136 (Div. 2) B. Little Elephant and Numbers


B. Little Elephant and Numbers
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

The Little Elephant loves numbers.

He has a positive integer x. The Little Elephant wants to find the number of positive integers d, such that d is the divisor of x, and xand d have at least one common (the same) digit in their decimal representations.

Help the Little Elephant to find the described number.

Input

A single line contains a single integer x (1 ≤ x ≤ 109).

Output

In a single line print an integer — the answer to the problem.

Sample test(s)
input
1
output
1
input
10
output
2
题意:给你一个数n,求n一共有多少个其中至少包含一个他本身的数字的因子(一共有三行英文,我愣是看了半个小时还没看懂,惭愧啊!!!)。

另外直接循环会超时,可以同时遍历i和i/n,止痒就可以过啦~

代码:

#include
#include
#include
#include
using namespace std;
int s[10];
int f(int x)
  {
      while(x)
      {
         if(s[x%10]==1)
            return 1;
          x=x/10;
        }
   return 0;
  }
int main()
{
  int n,i,ans;
  while(~scanf("%d",&n))
   {
       int x=n;
     memset(s,0,sizeof(s));
     ans=0;
     while(x)
      {
          s[x%10]=1;
          x=x/10;

      }
     for(i=1;i*i<=n;i++)
       if(n%i==0)
        {
            ans+=f(i);
         if(n/i!=i)
          ans+=f(n/i);
        }
    printf("%d\n",ans);
   }
	return 0;
}






你可能感兴趣的:(CF,C/C++)