【CodeChef】Origin(数论)

题目大意:

\(f(x)=\begin{cases} x,1\le x\le9\\ f(x的各数位之和),x>9\\ \end{cases}\)

\(\sum_{i=1}^{n}f(i)\)


根据打表找规律,我们会发现\(f(x)=(x-1)\bmod 9+1\)

所以\(\sum_{i=1}^{n}f(i)\)
\(=\sum_{i=1}^{\lfloor\frac{n}{9}\rfloor\cdot 9}f(x)+\sum_{i=\lfloor\frac{n}{9}\rfloor\cdot 9+1}^{n}f(x)\)
\(=\lfloor\frac{n}{9}\rfloor\cdot\sum_{i=1}^{9}f(i)+\sum_{i=1}^{n\bmod 9}f(i)\)
\(=\lfloor\frac{n}{9}\rfloor\cdot\sum_{i=1}^{9}i+\sum_{i=1}^{n\bmod 9}i\)
\(=\lfloor\frac{n}{9}\rfloor\cdot\frac{(1+9)\times9}{2}+\frac{(1+n\bmod 9)*(n\bmod 9)}{2}\)
\(=\lfloor\frac{n}{9}\rfloor\cdot 45+\frac{(1+n\bmod 9)*(n\bmod 9)}{2}\)

#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long ll;
const ll N=1e6+10,inf=1e18+10,mod=1e9+7;
int main(){
	int T;
	cin >> T;
	while(T--){
		ll n;
		cin >> n;
		cout << n/9*45+(1+n%9)*(n%9)/2 << endl;
	}
	return 0;
}
posted @ 2024-05-16 14:48  Alric  阅读(7)  评论(0编辑  收藏  举报