【学校个人赛】two operations 想法题 基础数学

先上题目:
C. Two operations
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given an integer nn, and you have initially two integers aa and xx, a=1a=1, x=0x=0.

You need to find the minimum number of operations in order to make the value of xx equal to the value of nn.

In one operation you can do one of these operations:

1- Increase the value of xx by aa, x=x+ax=x+a.

2- Change the value of aa into xx, then increase the value of xx by aa, a=xa=x, x=x+ax=x+a.

Can you find the answer?

Input

The first line of input contains one integer t(1t50)(1≤t≤50)

For each of the next tt lines, the input will contain one integer n(1n109)(1≤n≤109).

Output

For each test case print it's answer on a line.

Example
input
Copy
4
1
2
3
4
output
Copy
1
2
3
3

 题目大意:最开始给出a=1,x=0,然后给出两种操作,1)x=x+a,2)a=x,x=x+a 

刚开始想着bfs,然后理所当然的就是TLE,百思不得其解。

赛后看大佬代码才知道,给出的两个操作暗藏玄机。

#include<bits/stdc++.h>
using namespace std;
int main()
{
    std::ios::sync_with_stdio(false);
    cin.tie(0);
    int t,n,ans;
    cin>>t;
    while(t--)
    {
        cin>>n;
        ans=1;
        m=sqrt(n);
        for(int i=2;i<=m;++i)
        {
            while(!(n%i))
            {
                n/=i;
                ans+=i-1;
            }
        }
        if(n>1) ans+=n-1;
        cout<<ans<<endl;
    }
}

其中,std::ios::sync_with_stdio(false);cin.tie(0);这句是用来加速cin,cout的也是看了大佬代码才知道。

for中while是用来筛掉n的所有质因子的。

一开始看甚是疑惑,后来动笔尝试了一下。

首先必有一部操作,就是对a=1,x=0进行一次操作1,是的a=1,x=1,这是必要的。所以ans=1;

要使x乘上某个质数 i ,(就需要进行 i-1 次操作1),或者 (进行 1 次 操作2,然后进行 i-2 次操作1)
即每乘上某个数,都要进行i-1次操作,所以分解质因子。
 
【蒟蒻在努力】--2020.03.11

 

猜你喜欢

转载自www.cnblogs.com/AlexCCC/p/12464711.html