我是靠谱客的博主 可靠彩虹,这篇文章主要介绍C - Skip AtCoder - 4255,现在分享给大家,希望可以做个参考。

题目很简单,就是求所有与初始距离差的绝对的最大公约数。

欧几里得二进制,解法。

原理:

先把公因子的2全部提出来,

又因为:gcd(x,y)==gcd(x-y,y){x>y},利用这个性质不断减小x;

该算法效率很高,比普通gcd的效率高60%。(每一次循环结束时,x,y必然是奇数)

AC code:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include<bits/stdc++.h> #define model l,mid,num<<1 #define moder mid+1,r,num<<1|1 #define bug(k) cout<<"text :"<<t<<endl #define lowbit(k) k&-k using namespace std; typedef long long LL; const LL mod=1e9+7; const int M=1e5+5; map <string,int> ma; int n,x; int arr[M]; int bits_gcd(int x,int y) { if(y==0)return x; if(x==0)return y; int i,j; for(i=0;0==(x&1);i++)x>>=1; for(j=0;0==(y&1);j++)y>>=1; if(i>j)i=j; while(1) { if(x<y)x^=y,y^=x,x^=y; if(0==(x-=y))return y<<i; while(0==(x&1))x>>=1; } } int main() { int ans; int t; while(cin>>n>>x) { ans=INT_MAX; for(int i=1;i<=n;++i) { scanf("%d",&t); if(t>x)t^=x,x^=t,t^=x; arr[i]=x-t; } int ans=arr[1]; for(int i=2;i<=n;++i) { // cout<<ans<<" "<<arr[i]<<endl; // cout<<bits_gcd(ans,arr[i])<<endl; ans=bits_gcd(ans,arr[i]); } cout<<ans<<endl; } }

 

最后

以上就是可靠彩虹最近收集整理的关于C - Skip AtCoder - 4255的全部内容,更多相关C内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(65)

评论列表共有 0 条评论

立即
投稿
返回
顶部