Stay Hungry,Stay Foolish!

D - Div Game -- ATCODER

D - Div Game

https://atcoder.jp/contests/abc169/tasks/abc169_d

参考: https://blog.csdn.net/justidle/article/details/106474626

 

思路

计算n中所有质数的幂,

  Note: 此处不需要使用筛法先求出所有的质数,然后计算其幂

i在[2, sqrt(n)]范围, 但是在质数判定完成后, 可以使用 n/=质数, 来动态减少[2, sqrt(n)]范围

此外质数的出现个数,使用map<ll, ll> counter数据结构来存储,

每个质数判定后,counter++;

对于幂,使用1,2,3,...x = (1+x)*x/2来计算最大的x,即x个满足要求的因子

 

Code

https://atcoder.jp/contests/abc169/submissions/35981643

 

long long n;
map<long long, long long> counter;

int main()
{
    cin >> n;
    
    for(long long i=2; i*i<=n; i++){
        while(n % i == 0){
            counter[i]++;
            n /= i;
        }
    }
    
    if (n > 1){
        counter[n]++;
    }

    long long sum = 0;

    map<long long, long long>::iterator it;
    for(it=counter.begin(); it!=counter.end(); it++){
        long long total = it->second;
        
        long long i;
        for(i=1; i*(i+1)/2<=total; i++);
        i--;
        
        sum += i;
    }

    cout << sum << endl;

    return 0;
}

 

posted @ 2022-10-26 20:42  lightsong  阅读(31)  评论(0编辑  收藏  举报
Life Is Short, We Need Ship To Travel