A. Row GCD (Gcd辗转相除法)

传送门

题意:给你n个数a_1,a_2,a_3,...,a_n,和m个数b_1,b_2,b_3,...,b_n,

让你求每个 j (1<=j<=m)  的GCD(a_1+b_j,a_2+b_j,a_3+b_j,...,a_n+b_j);

思路:从最基础的求最大公约数的原理开始走:

GCD(x,y)=GCD(x,y-x);

那么看整体,GCD(a_1+b_j,a_2+b_j,a_3+b_j,...,a_n+b_j)

=GCD(a_1+b_j,GCD(a_1+b_j,a_2+b_j),GCD(a_1+b_j,a_3+b_j),....,GCD(a_1+b_j,a_n+b_j) )

=GCD(a_1+b_j,a_2-a_1,a_3-a_1,...,a_n-a_1);

那么我们 提前预处理取得GCD(|a_2-a_1|,|a_3-a_1|,...,|a_n-a_1|)(取绝对值是因为取得结果有一部分可能是负数的),然后对每一个a_1+b_j取GCD就可以了。

/**
 *  ┏┓   ┏┓+ +
 * ┏┛┻━━━┛┻┓ + +
 * ┃       ┃
 * ┃   ━   ┃ ++ + + +
 *  ████━████+
 *  ◥██◤ ◥██◤ +
 * ┃   ┻   ┃
 * ┃       ┃ + +
 * ┗━┓   ┏━┛
 *   ┃   ┃ + + + +Code is far away from  
 *   ┃   ┃ + bug with the animal protecting
 *   ┃    ┗━━━┓ 神兽保佑,代码无bug 
 *   ┃  	    ┣┓
 *    ┃        ┏┛
 *     ┗┓┓┏━┳┓┏┛ + + + +
 *    ┃┫┫ ┃┫┫
 *    ┗┻┛ ┗┻┛+ + + +
 */

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <string>
#include <math.h>
#include <vector>
#include <queue>
#include <map>
#define sc_int(x) scanf("%d", &x)
#define sc_ll(x) scanf("%lld", &x)
#define pr_ll(x) printf("%lld", x)
#define pr_ll_n(x) printf("%lld\n", x)
#define pr_int_n(x) printf("%d\n", x)
#define ll long long
using namespace std;

const int N = 1000000 + 100;
int n, m, h;
ll s[N], b[N];

int main()
{
	int t;
	cin >> n >> m;
	for (int i = 1; i <= n; i++)
		sc_ll(s[i]);
	for (int i = 1; i <= m; i++)
		sc_ll(b[i]);

	ll res = 0;
	for(int i =2;i<=n;i++)
	{
		if(i==2)res=abs(s[i]-s[1]);
		else res=__gcd(res,abs(s[i]-s[1]));
	}
	for(int i =1;i<=m;i++)
	cout<<__gcd(res,s[1]+b[i])<<" ";
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
<div v-else-if="per.dict_value === 'file_name'"> <el-tooltip placement="top" effect="dark" v-if="scope.row.download_time || scope.row.repair_download_time"> <div slot="content" > <span v-if="scope.row.repair_download_time">{{ parseTime(scope.row.repair_download_time,'{y}-{m}-{d} {h}:{i}:{s}') }}</span> <span v-else-if="scope.row.download_time">{{ parseTime(scope.row.download_time,'{y}-{m}-{d} {h}:{i}:{s}') }}</span> </div> <el-link v-if="scope.row.repair_file_id" :key="scope.row.repair_file_id" style="display: block;color: #67C23A" icon="el-icon-paperclip" @click="downloadOrderExcel(scope.row.repair_file_id, scope.row.order_no, scope.row.repair_file_name)" >{{ scope.row.repair_file_name }}</el-link> <el-link v-else-if="scope.row.file_id" :key="scope.row.file_id" style="display: block;color: #67C23A" icon="el-icon-paperclip" @click="downloadOrderExcel(scope.row.file_id, scope.row.order_no, scope.row.file_name)" >{{ scope.row[per.dict_value] }}</el-link> </el-tooltip> <div v-else> <el-link v-if="scope.row.repair_file_id" :key="scope.row.repair_file_id" style="display: block;" icon="el-icon-paperclip" @click="downloadOrderExcel(scope.row.repair_file_id, scope.row.order_no, scope.row.repair_file_name)" >{{ scope.row.repair_file_name }}</el-link> <el-link v-else-if="scope.row.file_id" :key="scope.row.file_id" style="display: block;" icon="el-icon-paperclip" @click="downloadOrderExcel(scope.row.file_id, scope.row.order_no, scope.row.file_name)" >{{ scope.row[per.dict_value] }}</el-link> </div> </div>优化这段代码用vue2.0
06-07

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值