C. Digital Root
题目连接:
http://www.codeforces.com/contest/10/problem/C
Description
Not long ago Billy came across such a problem, where there were given three natural numbers A, B and C from the range [1, N], and it was asked to check whether the equation AB = C is correct. Recently Billy studied the concept of a digital root of a number. We should remind you that a digital root d(x) of the number x is the sum s(x) of all the digits of this number, if s(x) ≤ 9, otherwise it is d(s(x)). For example, a digital root of the number 6543 is calculated as follows: d(6543) = d(6 + 5 + 4 + 3) = d(18) = 9. Billy has counted that the digital root of a product of numbers is equal to the digital root of the product of the factors' digital roots, i.e. d(xy) = d(d(x)d(y)). And the following solution to the problem came to his mind: to calculate the digital roots and check if this condition is met. However, Billy has doubts that this condition is sufficient. That's why he asks you to find out the amount of test examples for the given problem such that the algorithm proposed by Billy makes mistakes.
Input
The first line contains the only number N (1 ≤ N ≤ 106).
Output
Output one number — the amount of required A, B and C from the range [1, N].
Sample Input
4
Sample Output
2
Hint
题意
问你[1,n]中有多少 AB!=C,但是D(A)D(B)=D(C)的
D(A)是数根的意思,翻译过来就是这个数%9
题解:
容斥做,首先把所有的D(A)D(B)=D(C)的计算过来
然后减去AB==C且D(A)D(B)=D(C)的,由于显然AB=C,那么D(A)D(B)=D(C)
所以我们只需要减去AB=C的就好了,我们暴力枚举A,看B的个数有n/A个
然后莽一波……
代码
#include<bits/stdc++.h>
using namespace std;
int a[10];
int main()
{
int n;long long ans = 0;
scanf("%d",&n);
for(int i=1;i<=n;i++)a[i%9]++,ans-=n/i;
for(int i=0;i<9;i++)for(int j=0;j<9;j++)ans+=1ll*a[i]*a[j]*a[i*j%9];
cout<<ans<<endl;
}