题目链接:
思路:
重点是这个d(x)怎么求,肯定不能直接模拟,实际上它等效于
int d(int x){ return (x%9) ? x%9 : 9; }
然后,用一个cnt数组记录,cnt[i]表示1~n中有cnt[i]个数的d值等于i。
然后在1~9范围内枚举a和b,算出对应的c。
最后,减去a*b = c的部分。
代码:
#include <bits/stdc++.h>
using namespace std;
#define int long long
int n, ans = 0;
int cnt[10];
int d(int x){ return (x%9) ? x%9 : 9; }
signed main(){
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> n;
for(int i=1; i<=n; i++)
cnt[d(i)]++;
for(int i=1; i<=9; i++){
for(int j=1; j<=9; j++){
ans += cnt[i]*cnt[j]*cnt[d(i*j)];
}
}
//整除分块的模板(这部分是抄的,还没学懂)
for(int i=1, r=0; i<=n; i=r+1){
r = n/(n/i);
ans -= (r-i+1) * (n/i);
}
cout << ans;
}