Codeforce Supplement-Mathematical Questions for Simple Thinking

Insert picture description here

Title description

Ridbit starts with an integer n.

In a move, he can perform one of the following operations.

Divide n by one of its positive factors, or...
if n is greater than 1, subtract 1 from n.
The positive divisor is the divisor of a number, not including itself. For example, 1, 2, 4, 5, and 10 are positive divisors of 20, but 20 itself is not.

How many moves must Ridbit at least have to reduce n to 1?

enter

The first line contains an integer t (1≤t≤1000)-the number of test cases.

The only line of each test case contains an integer n (1≤n≤10^9).

Output

For each test case, output the minimum number of steps required to reduce n to 1.

Sample

input
6
1
2
3
4
6
9
output
0
1
2
2
2
3

Ideas

Just consider odd and even numbers. First, you need to judge 1, 2, 3. 1 is 0 steps, 2 is one step, and 3 is two steps. The remaining even numbers only need two steps, because they can all be divisible by 2. Perform a one-step instruction to become 2 and then subtract 1 to become 1. For odd numbers, first subtract 1 and then become even numbers, as above, and then perform two-step instructions, so three steps are required for odd numbers.

AC code

#include<bits/stdc++.h>
using namespace std;
int main()
{
    
    
	int t;
	scanf("%d",&t);
	while(t--)
	{
    
    
		int ans=0;
		int x;
		scanf("%d",&x);
		if(x==1||x==2||x==3) ans=x-1;
		else if(x&1) ans=3;
		else ans=2;
		cout<<ans<<endl; 
	}
	return 0;
} 

Guess you like

Origin blog.csdn.net/qq_45327808/article/details/109920677
Recommended