Codeforces 822 D. My pretty girl Noora

In Pavlopolis University where Noora studies it was decided to hold beauty contest "Miss Pavlopolis University". Let's describe the process of choosing the most beautiful girl in the university in more detail.

The contest is held in several stages. Suppose that exactly n girls participate in the competition initially. All the participants are divided into equal groups, x participants in each group. Furthermore the number x is chosen arbitrarily, i. e. on every stage number x can be different. Within each group the jury of the contest compares beauty of the girls in the format "each with each". In this way, if group consists of x girls, then comparisons occur. Then, from each group, the most beautiful participant is selected. Selected girls enter the next stage of the competition. Thus if n girls were divided into groups, x participants in each group, then exactly participants will enter the next stage. The contest continues until there is exactly one girl left who will be "Miss Pavlopolis University"

But for the jury this contest is a very tedious task. They would like to divide the girls into groups in each stage so that the total number of pairwise comparisons of the girls is as few as possible. Let f(n) be the minimal total number of comparisons that should be made to select the most beautiful participant, if we admit n girls to the first stage.

The organizers of the competition are insane. They give Noora three integers t, l and r and ask the poor girl to calculate the value of the following expression: t0·f(l) + t1·f(l + 1) + ... + tr - l·f(r). However, since the value of this expression can be quite large the organizers ask her to calculate it modulo 109 + 7. If Noora can calculate the value of this expression the organizers promise her to help during the beauty contest. But the poor girl is not strong in mathematics, so she turned for help to Leha and he turned to you.

Input

The first and single line contains three integers t, l and r (1 ≤ t < 109 + 7, 2 ≤ l ≤ r ≤ 5·106).

Output

In the first line print single integer — the value of the expression modulo 109 + 7.

设f(i)为i个人进行若干轮小组赛,每一轮分若干个人数相同的的组,每一组比较出最优者的代价是x(x-1)/2,最后比较出第一名的最小比较次数,求一个关于fl-r的多项式的值

喜闻乐见的结论题,首先可以容易得出设f(i) = f(i / j) + (i / j) * j * (j - 1) / 2,观察发现,显然选一个数的倍数为j是肯定不如直接选这个数优的,所以j必然是i的质因子,考虑选取质因子的本质,选2为组数相当于用了n / 2 * 2 * ( 2- 1) / 2的代价淘汰了1/2的人,这个式子化简后是n(2-1)/2,选3化简后就是用n(3-1)/2的代价淘汰了2/3的人,相当于用了比选2多1/2的代价多淘汰了1/6的人,由此可以猜想当质因子中选最小的最优,打表验证后确实如此,于是就线性筛扫一遍统计答案

/*program by mangoyang*/
#include<bits/stdc++.h>
#define inf (0x7f7f7f7f)
#define Max(a, b) ((a) > (b) ? (a) : (b))
#define Min(a, b) ((a) < (b) ? (a) : (b))
typedef long long ll;
using namespace std;
template <class T>
inline void read(T &x){
    int f = 0, ch = 0; x = 0;
    for(; !isdigit(ch); ch = getchar()) if(ch == '-') f = 1;
    for(; isdigit(ch); ch = getchar()) x = x * 10 + ch - '0';
    if(f) x = -x;
}
#define int ll
#define N (5000005)
#define P (1000000007)
ll f[N], prime[N], g[N], tot;
main(){
    ll t, l, r; ll ans = 0;
    read(t), read(l), read(r);
    for(int i = 2; i < N; i++){
        if(!g[i]) g[i] = i, prime[++tot] = i;
        for(int j = 1; i * prime[j] < N && j <= tot; j++){
            g[i*prime[j]] = prime[j];
            if(i % prime[j] == 0) break;
        }
        f[i] = (i * (g[i] - 1) / 2 + f[i/g[i]]) % P; 
    }
    for(int i = l, now = 1; i <= r; i++, (now *= t) %= P)
        (ans += now * f[i] % P) %= P;
    cout << ans << endl;
    return 0;
}
萌萌哒的代码

猜你喜欢

转载自www.cnblogs.com/mangoyang/p/9216605.html