Codeforces 735D. Taxes

Mr. Funt now lives in a country with a very specific tax laws. The total income of mr. Funt during this year is equal to n (n?≥?2) burles and the amount of tax he has to pay is calculated as the maximum divisor of n (not equal to n, of course). For example, if n?=?6 then Funt has to pay 3 burles, while for n?=?25 he needs to pay 5 and if n?=?2 he pays only 1 burle.

As mr. Funt is a very opportunistic person he wants to cheat a bit. In particular, he wants to split the initial n in several parts n1?+?n2?+?...?+?nk?=?n (here k is arbitrary, even k?=?1 is allowed) and pay the taxes for each part separately. He can‘t make some part equal to 1 because it will reveal him. So, the condition ni?≥?2 should hold for all i from 1 to k.

Ostap Bender wonders, how many money Funt has to pay (i.e. minimal) if he chooses and optimal way to split n in parts.

Input

The first line of the input contains a single integer n (2?≤?n?≤?2·109) — the total year income of mr. Funt.

Output

Print one integer — minimum possible number of burles that mr. Funt has to pay as a tax.

读题易想到所有素数都只算1的税,所以联想到分解成素数(本身素数那就是1)。然后根据哥德巴赫猜想,所有偶数可以分解为两个素数的和,所以除了2以外的所有偶数答案都是2,而对于非素数奇数就是看他能不能分解2+素数的形式,否则就是3

#include <iostream>
#include <iomanip>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
typedef long long LL;
LL n;
LL res = 0;
bool isprime(LL n){
  if (n == 2){
    return true;
  }
  for (int i=2;i<=sqrt(n);i++){
    if (n % i == 0){
      return false;
    }
  }
  return true;
}
int main(){
  // freopen("test.in","r",stdin);
  cin >> n;
  if (isprime(n)){
    cout << 1; return 0;
  }
  if (n % 2 == 0){
    cout << 2;
  }
  else {
    if (isprime(n-2)){
      cout << 2;
    }
    else
      cout << 3;
  }
  return 0;
}

时间: 2024-10-10 11:40:32

Codeforces 735D. Taxes的相关文章

codeforces 735D Taxes(数论)

Maximal GCD 题目链接:http://codeforces.com/problemset/problem/735/D --每天在线,欢迎留言谈论. 题目大意: 给你一个n(2≤n≤2e9) 代表一个人的收入. 他需要交税,规则:交税金额为n的最大公约数(本身不算) 他想通过把钱分成几份,然后分别交税,达到交税最少. 知识点: 哥德巴赫猜想:①如果一个数为偶数,那么可以拆成两个质数相加 ②如果一个奇数 (n-2)为质数那么他也可以拆成两个质数相加(2+(n-2)) ③其他的奇数 可以拆成

Codeforces 735D:Taxes(哥德巴赫猜想)

http://codeforces.com/problemset/problem/735/D 题意:给出一个n,这个n可以分解成 n = n1 + n2 + -- + nk,其中k可以取任意数.要使得分解以后所有的n的最大因子(不包括自己本身)的和最小,问最小的和是多少. 思路:比赛的时候想到全部拆成素数是最好的,但是不知道怎么拆,看别人跑的特别快,就知道是数论题,绝望之下试了两发暴力,都是TLE了,GG.早上起来才知道有"哥德巴赫猜想"这个东西. 内容大概是如下两点: 1.所有大于2

codeforces736b Taxes (Codeforces Round #382 (Div. 1))

题意:纳税额为金额的最大因数(除了本身).为了逃税将金额n分为n1+n2+.......问怎样分纳税最少. 哥德巴赫猜想: 任一大于2的偶数都可写成两个质数之和. 质数情况: 任何大于5的奇数都是三个素数之和 因此,如果n是偶数,结果就是2. 如果n是奇数.奇数只能拆成一奇一偶.分为2种情况.一偶是2时,那么如果一奇是质数,结果为2.如果一奇不是质数,那么结果至少为3,由于哥德巴赫猜想,3一定可行的,因此结果就是3. 一偶不是2时,一偶至少要交税2,一奇至少交税1,同样由猜想,结果为3. //#

D. Taxes【分拆素数和】

D. Taxes[分拆素数和] 题意 给出一个整数n,n可以由很多其他数的和组成,但不能出现1,当然也可以不拆分,然后问你:给你个n然后让你求n的最大因子为多少(不包括n本身),然后如果n被拆分的话,就是求拆分出来的这些数的因子和,规矩同上. 思路 哥德巴赫猜想:任何一个大于二的偶数都可以分解为两个素数和. 然后假如是奇数的话,直接特判下就好了. 代码实现 //链接:https://codeforces.com/problemset/problem/735/D #include<bits/std

【codeforces 718E】E. Matvey&#39;s Birthday

题目大意&链接: http://codeforces.com/problemset/problem/718/E 给一个长为n(n<=100 000)的只包含‘a’~‘h’8个字符的字符串s.两个位置i,j(i!=j)存在一条边,当且仅当|i-j|==1或s[i]==s[j].求这个无向图的直径,以及直径数量. 题解:  命题1:任意位置之间距离不会大于15. 证明:对于任意两个位置i,j之间,其所经过每种字符不会超过2个(因为相同字符会连边),所以i,j经过节点至多为16,也就意味着边数至多

Codeforces 124A - The number of positions

题目链接:http://codeforces.com/problemset/problem/124/A Petr stands in line of n people, but he doesn't know exactly which position he occupies. He can say that there are no less than a people standing in front of him and no more than b people standing b

Codeforces 841D Leha and another game about graph - 差分

Leha plays a computer game, where is on each level is given a connected graph with n vertices and m edges. Graph can contain multiple edges, but can not contain self loops. Each vertex has an integer di, which can be equal to 0, 1 or  - 1. To pass th

Codeforces Round #286 (Div. 1) A. Mr. Kitayuta, the Treasure Hunter DP

链接: http://codeforces.com/problemset/problem/506/A 题意: 给出30000个岛,有n个宝石分布在上面,第一步到d位置,每次走的距离与上一步的差距不大于1,问走完一路最多捡到多少块宝石. 题解: 容易想到DP,dp[i][j]表示到达 i 处,现在步长为 j 时最多收集到的财富,转移也不难,cnt[i]表示 i 处的财富. dp[i+step-1] = max(dp[i+step-1],dp[i][j]+cnt[i+step+1]) dp[i+st

Codeforces 772A Voltage Keepsake - 二分答案

You have n devices that you want to use simultaneously. The i-th device uses ai units of power per second. This usage is continuous. That is, in λ seconds, the device will use λ·ai units of power. The i-th device currently has bi units of power store