UVa Problem Solution: 10168 - Summation of Four Primes

本文探讨了如何通过算法验证每个大于等于8的整数均可表示为四个素数之和的问题,并提供了一段C++代码实现。该算法首先将目标数字减去两个较小的素数(如2和3),然后寻找剩余部分能否被分解成另外两个素数。

According to the Goldbach's Conjecture, every even number that is not less than 4 is the sum of two primes. So we can just reduce the number to an even number by subtracting 4=2+2 or 5=2+3 from it, and then test every possible summation of the remaining two addend to see if they are both primes.

Code:
  1. /***************************************************************************
  2.  *   Copyright (C) 2008 by Liu Kaipeng                                     *
  3.  *   LiuKaipeng at gmail dot com                                           *
  4.  ***************************************************************************/
  5. /* @JUDGE_ID 00000 10168 C++ "Summation of Four Primes" */
  6. #include <algorithm>
  7. #include <cstdio>
  8. #include <cstring>
  9. #include <deque>
  10. #include <fstream>
  11. #include <iostream>
  12. #include <list>
  13. #include <map>
  14. #include <queue>
  15. #include <set>
  16. #include <stack>
  17. #include <string>
  18. #include <vector>
  19. using namespace std;
  20. int const max_primes = 10000001;     
  21. bool primes[max_primes];
  22. void gen_primes()
  23. {
  24.   primes[0] = primes[1] = false;
  25.   for (int i = 2; i < max_primes; ++i)
  26.     primes[i] = true;
  27.   for (int i = 2; i < max_primes; ++i) {
  28.     for (; i < max_primes && !primes[i]; ++i) {}
  29.     for (int j = 2; i*j < max_primes; ++j)
  30.       primes[i*j] = false;
  31.   } 
  32. }
  33. bool sum_primes(int n, int s[])
  34. {
  35.   if (n < 8) return false;
  36.   if (n % 2 == 0) {
  37.     s[0] = s[1] = 2;
  38.     n -= 4;
  39.   } else {
  40.     s[0] = 2;
  41.     s[1] = 3;
  42.     n -= 5;
  43.   }
  44.   for (int i = 2, e = n / 2; i <= e; ++i)
  45.     if (primes[i] && primes[n-i]) {
  46.       s[2] = i;
  47.       s[3] = n - i;
  48.       return true;
  49.     }
  50.   return false;
  51. }
  52.           
  53. int main(int argc, char *argv[])
  54. {
  55. #ifndef ONLINE_JUDGE
  56.   freopen((string(argv[0]) + ".in").c_str(), "r", stdin);
  57.   freopen((string(argv[0]) + ".out").c_str(), "w", stdout);
  58. #endif
  59.   gen_primes();
  60.   char delim[] = "   /n";
  61.   for (int n, s[4]; cin >> n; )
  62.     if (sum_primes(n, s))
  63.       for (int i = 0; i < 4; ++i)
  64.     cout << s[i] << delim[i];
  65.     else
  66.       cout << "Impossible./n";
  67.   return 0;
  68. }

### Amorphous Cell 计算的任务参数与设置 Amorphous Cell 是一种用于构建无定形材料模型的强大工具,在分子动力学模拟中被广泛采用。以下是针对 **Amorphous Cell 2023 版本**在 Windows 系统下运行时的相关任务参数和设置细节。 #### Construction 任务的核心参数配置 Construction 任务的主要目标是创建一个具有指定密度的初始结构,其核心参数包括以下几个方面: - **Density (密度)** 密度是一个关键输入参数,决定了最终生成体系中原子或分子的数量以及分布情况[^1]。通常情况下,用户可以根据实验数据或者理论预测来设定该值。 - **Box Size (盒子尺寸)** 用户可以手动定义仿真盒的大小,也可以通过给定的目标体积自动调整。对于 amorphous cell 的构建而言,合理的 box size 能够有效减少边界效应的影响[^2]。 - **Ewald Summation Method (Ewald 总结方法)** 在处理长程静电相互作用时,Ewald summation 方法是一种高效而精确的选择。它能够很好地平衡精度与计算效率之间的关系[^3]。具体实现上,可以通过调节 real space cutoff 和 reciprocal space parameters 来优化性能表现。 #### van der Waals 项及相关力场设置 van der Waals 力作为短程非键合相互作用的一部分,在描述分子间弱吸引行为方面起着至关重要的作用。当选用 Universal force field 这类通用型力场时,需特别注意以下几点: - **Lennard-Jones Potential Parameters (LJ 参数)** Lennard-Jones potential 常用来表征范德华力的作用形式。其中 ε(epsilon) 表示势阱深度,σ(sigma) 则对应于粒子间的排斥距离尺度[^4]。这些数值会直接影响到系统的稳定性及其物理化学性质的表现。 - **Cutoff Distance (截断半径)** 设置合适的 cutoff distance 对降低计算成本至关重要。过小可能导致重要信息丢失;过大则增加不必要的开销。一般建议基于所选力场所推荐的标准来进行初步尝试后再逐步微调[^5]。 #### 示例代码片段 下面提供了一段 Python 脚本来展示如何利用某些库函数完成上述部分操作: ```python import numpy as np def setup_amorphous_cell(density, box_size, ewald_params, lj_epsilon=1.0, lj_sigma=1.0): """ 配置 Amorphous Cell 构建所需的基础参数 Args: density (float): 目标密度 box_size (tuple): 模拟盒子尺寸 (x,y,z) ewald_params (dict): Ewald 方法相关参数字典 lj_epsilon (float): LJ 势能中的 epsilon 取值,默认为 1.0 kcal/mol lj_sigma (float): LJ 势能中的 sigma 取值,默认单位 Å Returns: dict: 整理后的全部初始化参数集合 """ params = { 'density': density, 'box_dimensions': {'x': box_size[0], 'y': box_size[1], 'z': box_size[2]}, 'ewald_settings': ewald_params, 'lj_parameters': {'epsilon': lj_epsilon, 'sigma': lj_sigma} } return params example_ewald_config = {"real_space_cutoff": 12.0, "reciprocal_k_max": 8} # 单位分别为Å和倒数空间网格数量 configurations = setup_amorphous_cell( density=0.85, box_size=(50., 50., 50.), ewald_params=example_ewald_config, lj_epsilon=0.75, lj_sigma=3.5) print(configurations) ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值