B. Little Elephant and Numbers
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard outputThe 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
另外直接循环会超时,可以同时遍历i和i/n,止痒就可以过啦~
代码:
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
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;
}