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.

Example
Input
2 2 4
Output
19
Note

Consider the sample.

It is necessary to find the value of .

f(2) = 1. From two girls you can form only one group of two people, in which there will be one comparison.

f(3) = 3. From three girls you can form only one group of three people, in which there will be three comparisons.

f(4) = 3. From four girls you can form two groups of two girls each. Then at the first stage there will be two comparisons, one in each of the two groups. In the second stage there will be two girls and there will be one comparison between them. Total 2 + 1 = 3 comparisons. You can also leave all girls in same group in the first stage. Then comparisons will occur. Obviously, it's better to split girls into groups in the first way.

Then the value of the expression is .


  题目大意 某个学校要开展神奇的选美大赛,然后首先将n个女孩分成x组(x自定,且为n的约数),然后每一组中两两进行比较选出最漂亮的一位参加下一轮选拔。最终剩下的1人是冠军。定义f(n)表示在n个女孩中,选出最漂亮的一位最少需要的比较次数。求

  看到l和r的范围是5e6,大概可以猜到快速预处理出f(i)然后暴力求和。而且看到AC这道题的人数有1000多人,也就是说不太可能是莫比乌斯反演之类的神数论方法。

  关键在于如何快速预处理出f(i)。

  首先写出f(n)的定义式(用数学语言去代替文字语言,某种程度上简化了问题)(d不等于1)。

  再看看数据范围,是不是觉得该用线性筛啊?好,那一定和最小的质因子脱不了干系。然后再举几个数据。

  于是我们再根据数学的直觉和人生的哲理得到当n是合数(素数的时候只有一种转移,可以直接算),每组人数是n的最小质因子的时候最优。(说白了,我就是猜的。但是说实话,比赛的时候确实需要一些直觉。。对于像我这种蒟蒻,如果比赛时花半天耗在证明上,万一证出来没时间写,多亏,还不如骗骗分,得一分算一分,WA再猜,再推。比赛结束了慢慢证明)

  由于想了半天,没有想出来,就打上Lazy标记吧,回去问问学长们。
  为了简化计算,所以证明一下

  然后来证明一下,在转移的时候,合数没有素数优秀。

  设,假设,那么现在需要找多一组合法的转移使得转以后的函数值比这个小。但是很不辛的是,我们可以证明当

  会比它优秀一些。由于f(x)都一样,现在来证明前一个的后面那一坨比后一个后面那一坨大。

  (这一段感谢codeforecs题解,如果没看懂,可以自己去看原文)

  现在我们希望证明,当d1和d2都是素数的时候,且d1 < d2,那么当f(x)先通过d2再通过d1转移到f(n)比先通过d1再通过d2转移更优秀(意思说如果d不是n的最小质因子,那么我就可以构造出一组转移把它更优秀)。这次我们使用作差法。

  

  因为,所以最终的式子的正负性只和有关,因为d1,d2都大于1,所以有。所以

  因此若n的最小质因子为d,则f(n)通过f(n / d)比其它任何质因子p,f(n)通过f(n / p)都要优秀。

  (这一段是我自己证的,有问题请一定指出来,由于我跳了比较多的步骤,所以如果没有看懂,请在评论区提出疑问。)

Code

 /**
* Codeforces
* Problem#882D
* Accepted
* Time: 202ms
* Memory: 29300k
*/
#include <bits/stdc++.h>
using namespace std;
typedef bool boolean; const int moder = 1e9 + ; int t, l, r; inline void init() {
scanf("%d%d%d", &t, &l, &r);
} int num = ;
int prime[];
boolean vis[];
int *f;
inline int C2(int n) { return (n * 1LL * (n - ) / ) % moder; }
inline void Euler() {
f = new int[(r + )];
memset(vis, , sizeof(boolean) * (r + ));
f[] = ;
for(int i = ; i <= r; i++) {
if(!vis[i]) prime[num++] = i, f[i] = C2(i);
for(int j = ; j < num && i * prime[j] <= r; j++) {
int c = i * prime[j];
vis[c] = true;
f[c] = (f[i] + (f[prime[j]] * 1LL * i) % moder) % moder;
if((i % prime[j]) == ) break;
}
}
// for(int i = 1; i <= r; i++)
// cout << f[i] << " ";
// cout << endl;
} int res = ;
inline void solve() {
int t0 = ;
for(int i = l; i <= r; i++, t0 = (t0 * 1LL * t) % moder) {
res = (res + t0 * 1LL * f[i]) % moder;
}
printf("%d\n", res);
} int main() {
init();
Euler();
solve();
return ;
}

更新日志

  • 2017-8-2 21:30 补上证明

Codeforces 822D My pretty girl Noora - 线性筛 - 动态规划的更多相关文章

  1. Codeforces 893E Counting Arrays:dp + 线性筛 + 分解质因数 + 组合数结论

    题目链接:http://codeforces.com/problemset/problem/893/E 题意: 共q组数据(q <= 10^5),每组数据给定x,y(x,y <= 10^6 ...

  2. Codeforces 822D My pretty girl Noora(最小素因子的性质)

    题目大意:一场选美比赛有N个人,可以分成N/x,每组x人.每组的比较次数为x(x-1)/2,f[N]为最后决出冠军所需的比较次数,可以通过改变x的值使f[N]改变.题目给出t,l,r(1 ≤ t &l ...

  3. Codeforces Round #304 (Div. 2)(CF546D) Soldier and Number Game(线性筛)

    题意 给你a,b(1<=b<=a<=5000000)表示a!/b!表示的数,你每次可以对这个数除以x(x>1且x为这个数的因子)使他变成a!/b!/x, 问你最多可以操作多少次 ...

  4. Educational Codeforces Round 37-F.SUM and REPLACE (线段树,线性筛,收敛函数)

    F. SUM and REPLACE time limit per test2 seconds memory limit per test256 megabytes inputstandard inp ...

  5. 【Educational Codeforces Round 37】F. SUM and REPLACE 线段树+线性筛

    题意 给定序列$a_n$,每次将$[L,R]$区间内的数$a_i$替换为$d(a_i)$,或者询问区间和 这题和区间开方有相同的操作 对于$a_i \in (1,10^6)$,$10$次$d(a_i) ...

  6. Codeforces 1047C (线性筛+因数分解)

    题面 传送门 分析 1.暴力做法 首先先把每个数除以gcd(a1,a2-,an)gcd(a_1,a_2 \dots,a_n )gcd(a1​,a2​-,an​) 可以O(namax)O(n\sqrt ...

  7. Educational Codeforces Round 89 (Rated for Div. 2)D. Two Divisors 线性筛质因子

    题目链接:D:Two Divisors 题意: 给你n个数,对于每一个数vi,你需要找出来它的两个因子d1,d2.这两个因子要保证gcd(d1+d2,vi)==1.输出的时候输出两行,第一行输出每一个 ...

  8. bzoj2693--莫比乌斯反演+积性函数线性筛

    推导: 设d=gcd(i,j) 利用莫比乌斯函数的性质 令sum(x,y)=(x*(x+1)/2)*(y*(y+1)/2) 令T=d*t 设f(T)= T可以分块.又由于μ是积性函数,积性函数的约束和 ...

  9. BZOJ 2693: jzptab [莫比乌斯反演 线性筛]

    2693: jzptab Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 1194  Solved: 455[Submit][Status][Discu ...

随机推荐

  1. 总结我在huawei matebook D 2018版中安装archlinux的过程

    1.首先当然是准备一个启动U盘.按理说UEFI启动方式,只要将ISO镜像中的文件copy到U盘根目录即可,可以实际用的时候虽然能启动,但是进入live的时候会有些问题,所以老老实实用UltraISO ...

  2. anacoda 安装默认源中没有的包

    pip install -i https://pypi.tuna.tsinghua.edu.cn/simple 1 安装失败 conda install pygame 2 搜索 anaconda se ...

  3. Oracle10g 连接 sqlserver hsodbc dblink 方式 非透明网关

    Oracle10g 连接 sqlserver hsodbc dblink 方式 非透明网关 那个上传图片太麻烦了,发布到百度文库了 http://wenku.baidu.com/view/b38ae8 ...

  4. 在统一软件开发过程中使用UML

    如何在统一软件开发过程中使用UML? 起始阶段常用UML图 在起始阶段,通常有用例图.类图.活动图.顺序图等UML图的参与. 获取用户需求之后首先要将这些需求转化为系统的顶层用例图. 在确定了用例之后 ...

  5. B树,B+树,B*树以及R树的介绍

    https://blog.csdn.net/peterchan88/article/details/52248714 作者:July.weedge.Frankie.编程艺术室出品. 说明:本文从B树开 ...

  6. 【转】通过Excel生成批量SQL语句,处理大量数据

    经常会遇到这样的要求:用户给发过来一些数据,要我们直接给存放到数据库里面,有的是Insert,有的是Update等等,少量的数据我们可以采取最原始的办法,也就是在SQL里面用Insert into来实 ...

  7. kalinux实现自适用全屏、与物理主机共享文件方法

    1.执行虚拟机>安装VMware Tools菜单命令,自动挂载光驱(一般是自动挂载的,如果没有自动挂载请自行百度linux如何手动挂载光驱) 2.打开vm光驱的vmtools复制此文件到桌面: ...

  8. HIBERNATE与 MYBATIS的对比

    我是一名java开发人员,hibernate以及mybatis都有过学习,在java面试中也被提及问道过,在项目实践中也应用过,现在对hibernate和mybatis做一下对比,便于大家更好的理解和 ...

  9. 51Nod 1433 0和5

    小K手中有n张牌,每张牌上有一个一位数的数,这个字数不是0就是5.小K从这些牌在抽出任意张(不能抽0张),排成一行这样就组成了一个数.使得这个数尽可能大,而且可以被90整除. 注意: 1.这个数没有前 ...

  10. Numpy 矩阵

    矩阵定义在NumPy中,矩阵是ndarray的子类,可以由专用的字符串格式来创建 1. 创建矩阵mat函数创建矩阵(mat函数创建矩阵时,若输入已为matrix或ndarray对象,则不会为它们创建副 ...