Question G of the 16th Program Design Competition of Xidian University

Link: https://www.nowcoder.com/acm/contest/107/G
Source: Niuke.com

Topic description

As we all know, Mr. Ting is the best person in XDUACM laboratory, whether it is studying or playing games. Today he suddenly thought of a fun game. The rules are as follows. In the game, he has to get n small countries. At the beginning, Xiaoguo and Xiaojie each have 1. After a long time of practice, Mr. Ting has learned two kinds of magic, and he can use his wisdom to use magic every time.

The first magic: (Xiaojie becomes a small country) You can copy your own wisdom to the same number of small countries as the current Xiaojie;

The second magic: (Small country explosion) can turn the current Xiaojie into the same number as the small country, and then double the number of small countries!

Because Mr. Ting's intelligence is infinite, he doesn't care about the amount of intelligence spent. But Mr. Ting, who is studious, wants to get n small countries as soon as possible, so that he can have more time to read papers and play games. He wants to ask you how many times you need to use magic at least to get n small countries.

After getting n small countries, Mr. Ting went to study, but more and more gene mutations of the small countries appeared in the computer! They came to organize Teacher Ting to study, and now tell Teacher Ting that I want to get more companions!

Enter description:

For multiple sets of data, a positive integer T (T<=100000) in the first line represents the number of data sets.
The next T lines, each with a positive integer n (n<=10^6).

Output description:

An integer is output for each set of data, indicating how many times the membrane method needs to be used at least to get n small Guoting teachers.
Example 1

enter

2
1
3

output

0
2


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int force[1000010];
long long Ans=0;
inline void dfs(int x,int now,int sum) {
    if(x==1) {
        Ans+=sum*(now-1);
        return;
    }
    if(vis[x]!=now) {
        Ans+=sum*(now-1);
        dfs(x/vis[x],vis[x],1);
    }
    else
        dfs(x/vis[x],now,sum+1);
}
int main() {
    memset(vis,0x3f,sizeof vis);
    vis[1]=1;
    for(int i=2;i<=1e6;++i)
        if(vis[i]==0x3f3f3f3f)
            for(int j=i;j<=1e6;j+=i)
                vis [j] = min (vis [j], i);
    int T,x;
    cin>>T;
    while(T--) {
        cin>>x;
        Years=0;
        dfs(x,0,0);
        cout<<Ans<<endl;
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324643931&siteId=291194637