Codeforces Round #685 (Div. 2) A. Subtract or Divide 构造

Ridbit starts with an integer n.

In one move, he can perform one of the following operations:

divide n by one of its proper divisors, or
subtract 1 from n if n is greater than 1.
A proper divisor is a divisor of a number, excluding itself. For example, 1, 2, 4, 5, and 10 are proper divisors of 20, but 20 itself is not.

What is the minimum number of moves Ridbit is required to make to reduce n to 1?

Input
The first line contains a single integer t (1≤t≤1000) — the number of test cases.

The only line of each test case contains a single integer n (1≤n≤109).

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

Example
inputCopy
6
1
2
3
4
6
9
outputCopy
0
1
2
2
2
3
Note
For the test cases in the example, n may be reduced to 1 using the following operations in sequence

1
2→1
3→2→1
4→2→1
6→2→1
9→3→2→1
前几个特判一下,然后偶数–2--1,奇数先减1到偶数再—2----1

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<map>
#include<stack>
#include<set>
#include<queue>
#include<vector>
#include<stdlib.h>
 
#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<cstdio>
#include<cstdlib>
using namespace std;
typedef long long ll;
const int maxn=1e6+5;
const int inf=0x3f3f3f3f;

ll m,n,t,ans,res,tmp,a[maxn]; 
int main()
{
	ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
	cin>>t;
	while(t--)
	{
		cin>>n;
		if(n==1)
			cout<<"0"<<endl;
		else if(n==2)
			cout<<"1"<<endl;
		else if(n==3)
			cout<<"2"<<endl;
		else if(n%2==0)
			cout<<"2"<<endl;
		else
			cout<<"3"<<endl;
	}
	return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值