减少开销的思路:1、关于因子问题,从1还是一直历遍到n开销较大,历遍到sqrt(n)即可,因为知道一个因子x,用n/x即可知道另一个因子
2、已知所有的可能出现的情况(有限个),可以用数组标记每种情况。
3、一个内容承载多重信息:如果涉及数字,可将数组下标与数字本身联系起来。
B - Little Elephant and Numbers
Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u
Description
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 x andd 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 Input
Input
1
Output
1
Input
10
Output
2
#include<iostream>//此题容易超时
#include<cmath>
#include<cstdio>
using namespace std;
int main()
{
int n,number;
number=0;
scanf("%d",&n);
int a[11];
for(int i=0;i<10;i++) a[i]=0;
int t0=n;
while(t0>0)
{
a[t0%10]=1;
t0=t0/10;
}
for(int i=1;i*i<=n;i++)//思路1
{
if(n%i==0)
{ int t=i;
while(t>0)
{
int k;
k=t%10;
if(a[k]==1)//思路2
{
number++;
break;
}
t=t/10;
}
if(i*i!=n)//避免重复计算i
{
int h=n/i;
while(h>0)
{
int k;
k=h%10;
if(a[k]==1)
{
number++;
break;
}
h=h/10;
}
}
}
}
printf("%d\n",number);
}