CodeForces

来源:互联网 发布:mac iphone 蓝牙 网络 编辑:程序博客网 时间:2024/06/16 09:45
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

题意:一个数ni可以平均分成相等的几份bj ,每个bj可以继续分,直到不分或不能分之后,f(ai)=sum(bj*(bj-1)/2 )+sum(ci*(ci-1)/2)......

例如 4  可以分成 2* 2   ,2*(2-1)/2=1, 然后因为分成了2份,还要加上2*(2-1)/2=1,一共比较3次,或者不分 ,4*(4-1)/2=6,分成2份显然效果更优

类似  12,  分成 6*2  ,一共比较 6次,然后剩下6个,再分成2*3,比较3次,剩下3个,比较3次,一共12次

或者分成3*4,比较24+3次


现在给出t跟 l,r区间,

计算tt0·f(l) + t1·f(l + 1) + ... + tr - l·f(r).

题意理解了半天,大概思想就是求f[x]的时候,尽量把x往小了分,因为次数跟x是成指数趋势增长的,多分几份的增长次数远小于分成大块的增长次数

这样的话可以预处理素数直接筛一遍,得到每个数最小的质因子,记录质因子的答案就可以了

#include <iostream>using namespace std;#include <string.h>#include <string>#define inf 1000000007#define ll long longll value[5000005],pre[5000005],prin[5000005];ll l,r,t;void init(){memset(value,0,sizeof(value));memset(prin,0,sizeof(prin));ll i,j,k;for(i=2;i<5000001;i++){if(!value[i]){for(j=2;i*j<5000001;j++){k=i*j;if(!value[k]){value[k]=i;pre[k]=j;}}pre[i]=i*(i-1)/2;pre[i]=pre[i]%inf;}}for(i=2;i<5000001;i++){j=i;if(value[i]){while(value[j]){prin[i]+=pre[value[j]]*pre[j];prin[i]=prin[i]%inf;j=pre[j];}}prin[i]+=pre[j];prin[i]=prin[i]%inf;}/*cout<<"prin : ";for(i=2;i<=6;i++)cout<<prin[i]<<' ';cout<<endl;cout<<"pre : ";for(i=2;i<=6;i++)cout<<pre[i]<<' ';cout<<endl;cout<<"value : ";for(i=2;i<=6;i++)cout<<value[i]<<' ';cout<<endl;*/}ll solve(){ll i,m=1,ans=0;for(i=l;i<=r;i++){ans+=prin[i]*m;ans=ans%inf;m=(m*t)%inf;}return ans;}int main(){init();cin>>t>>l>>r;cout<<solve()<<endl;return 0;}




原创粉丝点击