CF221B Little Elephant and Numbers 题解
思路
按题意模拟即可。
在 \(1\) 至 \(\sqrt{n}\) 之间寻找 \(n\) 的因数,如果 \(i \) 是 \(n\) 的因数,那么 \(n÷i\) 一定也是 \(n\) 的因数,再判断 \(i\) 和 \(n÷i\) 是否与 \(x\) 含有共同数字。如果有,则方案数加一。
注意:如果 \(i\) 和 \(n÷i\) 是相同的数字,则要去重。
#include <bits/stdc++.h>
using namespace std;
int a[10], b[10];
int x, ans, t;
bool check(int n)
{
memset(b, 0, sizeof b);
for(; n;) b[n % 10] = 1, n /= 10;
for(int i = 0; i < 10; ++ i)
if(a[i] && b[i])
return 1;
return 0;
}
main()
{
cin >> x;
t = x;
for(; t;) b[t % 10] = 1, t /= 10;
for(int i = 1; i <= x / i; ++ i)
if(x % i == 0)
{
ans += check(i);
if(x / i != i)
ans += check(x / i);
}
cout << ans;
return 0;
}