脑筋急转弯王国

记录于 2026.01.18 · Melting_Pot

J. 脑筋急转弯王国

题面

对于每个正整数 $i$ ,我们定义它的映射值 $b_i$ 如下:

  1. 如果不存在小于 $i$ 的正整数 $k$ 使得 $b_k = i$ ,那么 $b_i = 5 \times i$ 。
  2. 否则,$b_i$ 等于那个满足 $b_k = i$ 的 $k$ 。

可以证明,这个规则为每个正整数 $i$ 都唯一确定了一个 $b_i$ 。

给定正整数 $N$。请你计算下列表达式的值。

\[S(N) = \sum_{i = 1}^{N} b_i\]

由于答案可能很大,请你输出表达式对 $10^9 + 7$ 取模的结果。

Analyse

不难发现对于单个映射只需考虑其因数中 $5$ 的次数的奇偶性,那么对于多个映射的前缀和只要找规律即可,计算每个次数的贡献,公式也很简单。

设 \(G(x)=\frac{x(x+1)}{2}-5\times \frac{\lfloor\frac{x}{5}\rfloor(\lfloor\frac{x}{5}\rfloor+1)}{2}\)

则有:

\[ans=\sum_{i=0}^{\lfloor\log_{5}n \rfloor} 5^{i\oplus 1} \times G(\lfloor\frac{n}{5^i}\rfloor)\]

难度不高,但是要记得取模防爆,可惜我懒得搞了,__int128 卡过去了。代码丑的要死

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include<bits/stdc++.h>
#define int __int128
using namespace std;
const int p=1e9+7,N=1e5+10;
long long n;
int ans(0LL);
int tem[N],cnt(0LL);
int G(int x){
    return ((x*(x+1)/2)%p)%p-5*(((x/5+1)*(x/5)/2)%p)%p+p;
}
signed main(){
    cin>>n;
    for(int x=n;x;x/=5,cnt++)
    cnt--;
    tem[0]=1LL;
    for(int i=1;i<=20;++i) tem[i]=tem[i-1]*5LL;
    for(int i=0;i<=cnt;++i){
        ans=(ans+tem[i^1]%p*G(n/tem[i])%p)%p;
    }
    cout<<(long long)(ans%p)<<endl;
}