codeforces 177B2 B2. Rectangular Game(数论)

来源:互联网 发布:tw域名查询 编辑:程序博客网 时间:2024/04/29 07:53

题目链接:

codeforces 177B2


题目大意:

给出n个点,每次排成a*b的矩形,每次留下b个,重新排,每次操作后剩下的点数的和最大是多少。


题目分析:

每次只需要将当前数的最小的质因数作为行数,这样能够保证剩下的最多,导致最终的结果最大。


AC代码:

#include <iostream>#include <cstring>#include <cstdio>#include <algorithm>#include <vector>#define MAX 100007using namespace std;typedef long long LL;int mark[MAX],n;vector<int> p;void init ( ){    p.push_back ( 2 );    memset ( mark , -1 , sizeof ( mark ));    for ( int i = 3 ; i < MAX ; i++ )    {        if (~mark[i]) continue;        p.push_back ( i );        for ( int j = 2*i  ; j < MAX ; j += i )            mark[j] = i;    }}int main ( ){    init ();    int m = p.size();    while (~scanf ( "%d" , &n ))    {        LL ans = 1;        while ( n != 1 )        {            int pp = -1;            for ( int i = 0; i < m && p[i]*p[i] <= n; i++ )            {                if ( n%p[i] ) continue;                pp = p[i];                break;            }            if ( pp == -1 ) pp = n;            ans += n;            n /= pp;        }        printf ("%d\n" , ans );    }}
0 0
原创粉丝点击