减少历遍和用桶排思想

减少开销的思路: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);

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值