2019年华南理工大学程序设计竞赛(春季赛)H Parco_Love_GCD

传送门.

题解:

首先要想到显而易见的结论:
当右端点r固定时,gcd[l…r]不同的值只有 l o g 值 域 log值域 log个。

假设l一开始在r上,不断向左移,当gcd变化时,gcd至少/2,所以最多除以log次。

那么不难得到一个暴力的做法:
右端点向右扫
维护gcd发生变化的左端点

右端点右移时,变化点的gcd对a[r]取gcd,再加上r为变化点,unique一下即可。

复杂度: O ( n l o g 2 n ) O(nlog^2n) O(nlog2n),两个log都是跑不满的,所以飞快。

也可以优化一下,就是合并的右端点一定是一段后缀,但是极限复杂度不变。

Code:

#include<ctime>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define fo(i, x, y) for(int i = x, B = y; i <= B; i ++)
#define ff(i, x, y) for(int i = x, B = y; i <  B; i ++)
#define fd(i, x, y) for(int i = x, B = y; i >= B; i --)
#define ll long long
#define pp printf
using namespace std;

const int N = 5e5 + 5;

int n, a[N], v[N], d[N], d0;

int gcd(int x, int y) {
	return !y ? x : gcd(y, x % y);
}

int cmp(int x, int y) {
	return v[x] == v[y];
}

const int mo = 1e9 + 7;

ll ans;

int main() {
	scanf("%d", &n);
	fo(i, 1, n) scanf("%d", &a[i]);
	fo(i, 1, n) {
		fo(j, 1, d0) v[d[j]] = gcd(v[d[j]], a[i]);
		d[++ d0] = i; v[i] = a[i];
		d0 = unique(d + 1, d + d0 + 1, cmp) - (d + 1);
		fo(j, 1, d0 - 1) ans = (ans + (ll) v[d[j]] * (d[j + 1] - d[j])) % mo;
		ans = (ans + (ll) v[d[d0]] * (i - d[d0] + 1)) % mo;
	}
	pp("%lld", ans);
}
units real atom_style full boundary p p s timestep 1 neighbor 2.0 bin neigh_modify every 1 delay 0 check yes read_data NiCr-H2O-O2.data mass 3 58.69 # Ni mass 4 51.96 # Cr mass 1 16.00 # O mass 2 1.008 # H group Ni type 3 group Cr type 4 group O type 1 group H type 2 group alloy union Ni Cr group steam union O H group all union alloy steam pair_style reaxff NULL pair_coeff * * ffield.reax.CHOFeAlNiCuSCr O H Ni Cr velocity all create 300.0 98797 fix 1 all qeq/reax 1 0.0 10.0 1.0e-6 reaxff minimize 1.0e-4 1.0e-6 1000 10000 fix 2 all nvt temp 300 300 100.0 dump 2 all custom 100 relax.dump id type x y z dump_modify 2 element O H Ni Cr thermo 100 thermo_style custom step temp pe ke etotal press run 50000 1 by 1 by 1 MPI processor grid reading bonds ... 300 bonds reading angles ... 100 angles Finding 1-2 1-3 1-4 neighbors ... special bond factors lj: 0 0 0 special bond factors coul: 0 0 0 2 = max # of 1-2 neighbors 1 = max # of 1-3 neighbors 1 = max # of 1-4 neighbors 2 = max # of special neighbors special bonds CPU = 0.001 seconds read_data CPU = 0.023 seconds 2042 atoms in group Ni 874 atoms in group Cr 300 atoms in group O 200 atoms in group H 2916 atoms in group alloy 500 atoms in group steam 3416 atoms in group all WARNING: Changed valency_val to valency_boc for X (src/REAXFF/reaxff_ffield.cpp:300) CITE-CITE-CITE-CITE-CITE-CITE-CITE-CITE-CITE-CITE-CITE-CITE-CITE Your simulation uses code contributions which should be cited: - pair reaxff command: doi:10.1016/j.parco.2011.08.005 - fix qeq/reaxff command: doi:10.1016/j.parco.2011.08.005 The log file lists these citations in BibTeX format. CITE-CITE-CITE-CITE-CITE-CITE-CITE-CITE-CITE-CITE-CITE-CITE-CITE WARNING: Using a manybody potential with bonds/angles/dihedrals and special_bond exclusions (src/pair.cpp:244) WARNING: Bonds are defined but no bond style is set (src/force.cpp:197) WARNING: Likewise 1-2 special neighbor interactions != 1.0 (src/force.cpp:199) WARNING: Angles are defined but no angle style is set (src/force.cpp:202) WARNING: Likewise 1-3 special neighbor interactions != 1.0 (src/force.cpp:204) No /omp style for force computation currently active Neighbor list info ... update: every = 1 steps, delay = 0 steps, check = yes max neighbors/atom: 2000, page size: 100000 master list distance cutoff = 12 ghost atom cutoff = 12 binsize = 6, bins = 5 5 12 2 neighbor lists, perpetual/occasional/extra = 2 0 0 (1) pair reaxff, perpetual attributes: half, newton off, ghost, omp pair build: half/bin/newtoff/ghost/omp stencil: full/ghost/bin/3d bin: standard (2) fix qeq/reax, perpetual, copy from (1) attributes: half, newton off pair build: copy stencil: none bin: none Setting up cg style minimization ... Unit style : real Current step : 0 WARNING: Fix qeq/reaxff CG convergence failed after 200 iterations at step 0 (src/REAXFF/fix_qeq_reaxff.cpp:798) WARNING: Fix qeq/reaxff CG convergence failed after 200 iterations at step 0 (src/REAXFF/fix_qeq_reaxff.cpp:798) Per MPI rank memory allocation (min/avg/max) = 518.3 | 518.3 | 518.3 Mbytes Step Temp E_pair E_mol TotEng Press Volume 0 300 279680.55 0 282734.39 491600.17 63408.327 WARNING: Fix qeq/reaxff CG convergence failed after 200 iterations at step 1 (src/REAXFF/fix_qeq_reaxff.cpp:798) WARNING: Fix qeq/reaxff CG convergence failed after 200 iterations at step 1 (src/REAXFF/fix_qeq_reaxff.cpp:798) WARNING: Fix qeq/reaxff CG convergence failed after 200 iterations at step 2 (src/REAXFF/fix_qeq_reaxff.cpp:798) WARNING: Fix qeq/reaxff CG convergence failed after 200 iterations at step 2 (src/REAXFF/fix_qeq_reaxff.cpp:798) WARNING: Fix qeq/reaxff CG convergence failed after 200 iterations at step 2 (src/REAXFF/fix_qeq_reaxff.cpp:798) WARNING: Fix qeq/reaxff CG convergence failed after 200 iterations at step 2 (src/REAXFF/fix_qeq_reaxff.cpp:798) WARNING: Fix qeq/reaxff CG convergence failed after 200 iterations at step 2 (src/REAXFF/fix_qeq_reaxff.cpp:798) WARNING: Fix qeq/reaxff CG convergence failed after 200 iterations at step 2 (src/REAXFF/fix_qeq_reaxff.cpp:798) WARNING: Fix qeq/reaxff CG convergence failed after 200 iterations at step 3 (src/REAXFF/fix_qeq_reaxff.cpp:798) WARNING: Fix qeq/reaxff CG convergence failed after 200 iterations at step 3 (src/REAXFF/fix_qeq_reaxff.cpp:798) WARNING: Fix qeq/reaxff CG convergence failed after 200 iterations at step 3 (src/REAXFF/fix_qeq_reaxff.cpp:798) WARNING: Fix qeq/reaxff CG convergence failed after 200 iterations at step 3 (src/REAXFF/fix_qeq_reaxff.cpp:798) WARNING: Fix qeq/reaxff CG convergence failed after 200 iterations at step 4 (src/REAXFF/fix_qeq_reaxff.cpp:798) WARNING: Fix qeq/reaxff CG convergence failed after 200 iterations at step 4 (src/REAXFF/fix_qeq_reaxff.cpp:798) WARNING: Fix qeq/reaxff CG convergence failed after 200 iterations at step 4 (src/REAXFF/fix_qeq_reaxff.cpp:798) WARNING: Fix qeq/reaxff CG convergence failed after 200 iterations at step 4 (src/REAXFF/fix_qeq_reaxff.cpp:798) WARNING: Fix qeq/reaxff CG convergence failed after 200 iterations at step 4 (src/REAXFF/fix_qeq_reaxff.cpp:798) WARNING: Fix qeq/reaxff CG convergence failed after 200 iterations at step 4 (src/REAXFF/fix_qeq_reaxff.cpp:798) WARNING: Fix qeq/reaxff CG convergence failed after 200 iterations at step 5 (src/REAXFF/fix_qeq_reaxff.cpp:798) WARNING: Fix qeq/reaxff CG convergence failed after 200 iterations at step 5 (src/REAXFF/fix_qeq_reaxff.cpp:798) WARNING: Fix qeq/reaxff CG convergence failed after 200 iterations at step 5 (src/REAXFF/fix_qeq_reaxff.cpp:798) WARNING: Fix qeq/reaxff CG convergence failed after 200 iterations at step 5 (src/REAXFF/fix_qeq_reaxff.cpp:798) write_data relax_final.data上述程序出现错误
最新发布
12-02
### 'Changed valency_val to valency_boc for X' 此警告通常表明在原子类型 X 的参数设置里,价态相关的值从 `valency_val` 转变为 `valency_boc`。这或许是因所用势函数版本或者参数文件的更新所致。解决办法是检查势函数文件与输入脚本,保证价态参数设置无误。要确保使用的参数文件和势函数版本是兼容的,也可以查阅 LAMMPS 文档,了解该原子类型对应的正确价态参数设置。 ### 'Using a manybody potential with bonds/angles/dihedrals and special_bond exclusions' 当使用多体势函数,同时又定义了键、角、二面角以及特殊键排除规则时,就会出现这个警告。虽然多数情况下这不会对模拟造成影响,但可能会让计算结果产生一些难以预料的状况。解决办法是重新评估是否真的需要同时使用多体势函数和这些拓扑结构定义。若确实需要,可尝试调整特殊键排除规则,确保其与势函数相匹配。例如,查看特殊键排除规则是否会排除掉多体势函数需要的相互作用。 ### 'Bonds are defined but no bond style is set' 该警告意味着在输入文件中定义了键,但未设置相应的键势函数。没有键势函数,LAMMPS 就无法计算键的相互作用。解决办法是在输入脚本里添加合适的键势函数设置。例如,如果是简单的谐振键势,可以使用如下代码: ```lammps bond_style harmonic bond_coeff 1 100.0 1.0 ``` 这里 `bond_style harmonic` 设定了键势函数为谐振势,`bond_coeff 1 100.0 1.0` 为键类型 1 设置了力常数 100.0 和平衡键长 1.0。 ### 'Fix qeq/reaxff CG convergence failed after 200 iterations' 此警告表明在使用 `fix qeq/reaxff` 进行电荷平衡计算时,共轭梯度(CG)算法在 200 次迭代后未能收敛。这可能是由于体系的初始电荷分布不合理、步长过大或者体系过于复杂等原因造成的。解决办法有以下几种: - 增大最大迭代次数:可以通过设置 `fix_modify` 命令来增加最大迭代次数。例如: ```lammps fix 1 all qeq/reaxff 1 0.001 2000 fix_modify 1 maxiter 5000 ``` 这里将最大迭代次数从默认的 200 增加到了 5000。 - 调整初始电荷分布:尝试为体系提供更合理的初始电荷分布。可以在初始结构中手动设置部分原子的电荷,或者使用其他方法预平衡电荷。 - 减小步长:在 `fix qeq/reaxff` 命令中减小步长参数,例如将步长从 0.001 减小到 0.0001。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值