CF Round #422( Div.1/2) D. My pretty girl Noora


D. My pretty girl Noora

time limit per test  1.5seconds

memory limit per test      256megabytes

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

The contest is held in several stages. Suppose that exactlyn girls participate in the competitioninitially. All the participants are divided into equal groups,x participants in each group.Furthermore the numberx is chosen arbitrarily, i. e. on every stage numberx can be different. Within each groupthe jury of the contest compares beauty of the girls in the format "eachwith each". In this way, if group consists of x girls, thencomparisons occur. Then, from each group, the most beautifulparticipant is selected. Selected girls enter the next stage of thecompetition. Thus ifn girls were divided into groups,x participants in each group, then exactlyparticipants will enter the next stage. The contest continuesuntil there is exactly one girl left who will be "Miss PavlopolisUniversity"

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

The organizers of the competition are insane. They give Noorathree integerst,l andr and ask the poor girl to calculate the value of the followingexpression:t0·f(l) + t1·f(l + 1) + ... + tr - l·f(r). However, since thevalue of this expression can be quite large the organizers ask her to calculateit modulo109 + 7. If Noora can calculate the value of this expression theorganizers promise her to help during the beauty contest. But the poor girl isnot strong in mathematics, so she turned for help to Leha and he turned to you.

Input

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

Output

Inthe first line print single integer — the value of the expression modulo109 + 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, inwhich 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 twogroups. In the second stage there will be two girls and there will be one comparison between them. Total2 + 1 = 3 comparisons. You can also leave all girls in same group in thefirst stage. Thencomparisons will occur. Obviously, it's better to split girls into groups in the first way.

Then the value of the expression is19.

 


题意:这题主要问题在于求f(n),f(n)的含义为,有n个女孩,每一轮需要把他们分成k个小组,每一小组人数相同,设为x,(k,x可以自己赋值)然后每个小组两两比较出最漂亮的,显然每组的比较次数为x*(x-1)/2,然后每组最漂亮的共k人继续比较,直到比出所有人中最漂亮的,比较的最少次数即为f(n).


分析:通过打表发现每一轮每个小组的人数都必须是素数,且这个素数是递增的,那么我们每次找到一个数最大的质因子即可(包含自身)。


附上作者的解析:

PS:这题不需要用快速幂计算t^k,这样反而会超时,每次累乘即可。


#include <cstdio>
#include <vector>
#include <iostream>
#include <queue>
#include <cstring>
#include <algorithm>
using namespace std;
#define mst(a,b) memset((a),(b),sizeof(a))
#define f(i,a,b) for(int i=(a);i<=(b);++i)
#define rush() int T;scanf("%d",&T);while(T--)

typedef long long ll;
const int maxn= 5000005;
const ll mod = 1e9+7;
const ll INF = 1e17;
const double eps = 1e-6;

int prime[maxn]= {0};
int num_prime=0;
bool isprime[maxn]= {1,1};
int small[maxn]= {0};
ll num[maxn];


void getprime()            //预处理出每个数最小的质因子(包含自身)
{
    for(int i=2; i<maxn; i++)
    {
        if(!isprime[i])
        {
            prime[num_prime++]=i;
            small[i]=i;
        }
        for(int j=0; j<num_prime&&i*prime[j]<maxn; j++)
        {
            isprime[i*prime[j]]=1;
            if(small[i*prime[j]]==0)
                small[i*prime[j]]=prime[j];
            if(i%prime[j]==0)
                break;
        }
    }
}

void init()
{
    num[2]=1;
    num[3]=3;
    for(int i=4; i<maxn; i++)
    {
        num[i]=num[i/small[i]]+(ll)i*(small[i]-1)/ 2;
        num[i]=num[i]%mod;
    }
}


int main()
{

    ll t,l,r;
    getprime();
    init();
    for(int i=2;i<100;i++)
    {
        printf("i:%d %d\n",i,num[i]);
    }
    while(~scanf("%I64d%I64d%I64d",&t,&l,&r))
    {
        ll ans=0;
        ll temp = 1;
        for(int i=l; i<=r; i++)
        {
            ans+=temp*num[i];
            ans=ans%mod;
            temp=(temp*t)%mod;
        }
        printf("%I64d\n",ans);
    }
    return 0;
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值