You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
p/lqb-19-b-cplus-arithmetic-series/
问题描述 数学老师给小明出了一道等差数列求和的题目。但是粗心的小明忘记了一 部分的数列,只记得其中N 个整数。 现在给出这N 个整数,小明想知道包含这N 个整数的最短的等差数列有几项?\n输入格式 输入的第一行包含一个整数N。 第二行包含N 个整数A1; A2; …… ; AN。(注意A1 ~AN 并不一定是按等差数 列中的顺序给出)\n输出格式 输出一个整数表示答案。\n样例输入 1 2 5 2 6 4 10 20 样例输出 1 10 样例说明 包含2、6、4、10、20 的最短的等差数列是2、4、6、8、10、12、14、16、 18、20。\n评测用例规模与约定 对于所有评测用例,2 <= N <= 100000,0 <= Ai <= 109。\n1 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 #include #include #include #include #include #include #include #include #include #include using namespace std; #define N 100005 int num[N]={0},d[N]={0}; int gcd(int a,int b) { return (b>0)?gcd(b,a%b):a; } int main() { int n; scanf("%d",&n); for(int i=0;i<n;i++) scanf("%d",&num[i]); sort(num,num+n); bool zero=false; for(int i=0;i<n-1;i++) { d[i]=num[i+1]-num[i]; if(d[i]==0) { zero=true; break; } } if(zero)//常数数列 printf("%d\n",n); else { int mind=gcd(d[0],d[1]); for(int i=2;i<n-1;i++) mind=gcd(mind,d[i]); printf("%d\n",(num[n-1]-num[0])/mind+1); } return 0; }
https://blog.debuginn.com/p/lqb-19-b-cplus-arithmetic-series/
Beta Was this translation helpful? Give feedback.
All reactions