【Codeforces Round 324 (Div 2)D】【miller-rabin素数检验 猜想】Dima and Lisa 奇数拆分成三素数

本文探讨了一种将奇数表示为最多三个质数之和的方法,并通过哥德巴赫猜想和自定义猜想来解决该问题。利用Miller-Rabin素性检验算法确保所选数字为质数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

D. Dima and Lisa
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Dima loves representing an odd number as the sum of multiple primes, and Lisa loves it when there are at most three primes. Help them to represent the given number as the sum of at most than three primes.

More formally, you are given an odd numer n. Find a set of numbers pi (1 ≤ i ≤ k), such that

  1. 1 ≤ k ≤ 3
  2. pi is a prime

The numbers pi do not necessarily have to be distinct. It is guaranteed that at least one possible solution exists.

Input

The single line contains an odd number n (3 ≤ n < 109).

Output

In the first line print k (1 ≤ k ≤ 3), showing how many numbers are in the representation you found.

In the second line print numbers pi in any order. If there are multiple possible solutions, you can print any of them.

Sample test(s)
input
27
output
3
5 11 11
Note

A prime is an integer strictly larger than one that is divisible only by one and by itself.

#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<math.h>
#include<iostream>
#include<string>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<bitset>
#include<algorithm>
#include<time.h>
using namespace std;
void fre(){freopen("c://test//input.in","r",stdin);freopen("c://test//output.out","w",stdout);}
#define MS(x,y) memset(x,y,sizeof(x))
#define MC(x,y) memcpy(x,y,sizeof(x))
#define MP(x,y) make_pair(x,y)
#define ls o<<1
#define rs o<<1|1
typedef long long LL;
typedef unsigned long long UL;
typedef unsigned int UI;
template <class T1,class T2>inline void gmax(T1 &a,T2 b){if(b>a)a=b;}
template <class T1,class T2>inline void gmin(T1 &a,T2 b){if(b<a)a=b;}
const int N=0,M=0,Z=1e9+7,ms63=1061109567;
int casenum,casei;
LL MUL(LL x,int p,int Z)
{
    LL y=1;
    while(p)
    {
        if(p&1)y=y*x%Z;
        x=x*x%Z;
        p>>=1;
    }
    return y;
}
bool miller_rabin(int n)
{
    if(n<=1)return 0;
    if(n==2)return 1;
    if(n%2==0)return 0;
    int p=n-1;
    srand(time(NULL));
    int TIMES=9;
    for(int i=1;i<=TIMES;i++)
    {
        int x=rand()%(n-1)+1;
        if(MUL(x,p,n)!=1)return 0;
    }
    return 1;
}
int main()
{
	int n;
	while(~scanf("%d",&n))
	{
		if(miller_rabin(n))printf("1\n%d\n",n);
		else
		{
			int top=n/2-1;
			if(top%2==0)--top;
			for(int i=top;i>=3;i-=2)
			{
				if(miller_rabin(i)&&miller_rabin(n-i*2))
				{
					printf("3\n%d %d %d\n",i,i,n-i*2);
					break;
				}
			}
		}
	}
	return 0;
}
/*
【题意】
让你把一个奇数n([3,1e9]),拆分成1~3个奇数之和的形式

【类型】
猜想 暴力 miller_rabin素数检验

【分析】
方法1,哥德巴赫猜想——任意一个大于2的偶数都可以写成2个质数之和。
于是我们对于>=9的数,先拆出一个3,剩下的暴力枚举=x+y。

方法2,wkc猜想>_<——我感觉任意一个>=9的奇数都可以写成素数x+素数x+素数y
于是就这么写,然后就过了QwQ

然而如何判定拆分的数是否为素数呢?素数检验!

【时间复杂度&&优化】
复杂度为玄学!

*/


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值