【Codeforces Round #422 (Div. 2) D】My pretty girl Noora

【题目链接】:http://codeforces.com/contest/822/problem/D

【题意】

有n个人参加选美比赛;
要求把这n个人分成若干个相同大小的组;
每个组内的人数是相同的;
然后每个组内的人,两两比较;
每个组得出最美的人;
然后每个组中最美的人再重复上述步骤;
直到只剩一个人;
问你如何选定每个阶段的分组;
使得比较的次数最少;

【题解】

只考虑一轮的情况;
设x是分组后每个组的人数;
然后一共有n个人;
则这一轮比较的次数就为
nxx(x1)2
->n(x1)2
这样看来x最小的时候效果最好;
则每个数字都取最小的因子就好;
这样设数字i的最小因子为p[i]
则设dp[i]表示i个人的时候最少需要多少次比较
dp[n]=dp[np[i]]+p[i]p[i1]2np[i]
用筛法求出每个数的最小因子就好;
(最小因子都是质数吧.)

【Number Of WA

0

【反思】

这种题做得比较少吧;
每一轮是独立的!
递推+贪心的方法;

【完整代码】

#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ms(x,y) memset(x,y,sizeof x)
#define Open() freopen("F:\rush.txt","r",stdin)
#define Close() ios::sync_with_stdio(0)

typedef pair<int,int> pii;
typedef pair<LL,LL> pll;

const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
const int INF = 5e6+10;
const LL MOD = 1e9+7;

LL t,l,r;
LL p[INF],f[INF];

int main(){
    //Open();
    Close();
    cin >> t >> l >> r;
    rep1(i,1,(int) 5e6)
        p[i] = i;
    rep1(i,2,sqrt((int) 5e6)){
        if (p[i]==i){
            for (int k = i;k <= (int) 5e6;k+=i)
                if (p[k]==k)
                    p[k] = i;
        }
    }
    rep1(i,1,(int) 5e6 ){
        f[i] = ((p[i]*(p[i]-1)/2)%MOD*(i/p[i])%MOD + f[i/p[i]])%MOD;
    }
    LL now = 1;
    LL ans = 0;
    rep1(i,(int)l,(int)r){
        ans = (ans + now*f[i])%MOD;
        now = (now*t)%MOD;
    }
    cout << ans << endl;
    return 0;
}
原文地址:https://www.cnblogs.com/AWCXV/p/7626220.html