0
点赞
收藏
分享

微信扫一扫

[USACO1.4]等差数列 Arithmetic Progressions

笙烛 2022-05-27 阅读 58

题目描述

一个等差数列是一个能表示成a, a+b, a+2b,..., a+nb (n=0,1,2,3,...)的数列。

在这个问题中a是一个非负的整数,b是正整数。写一个程序来找出在双平方数集合(双平方数集合是所有能表示成p的平方 + q的平方的数的集合,其中p和q为非负整数)S中长度为n的等差数列。

输入输出格式

输入格式:

第一行: N(3<= N<=25),要找的等差数列的长度。

第二行: M(1<= M<=250),搜索双平方数的上界0 <= p,q <= M。

输出格式:

如果没有找到数列,输出`NONE'。

如果找到了,输出一行或多行, 每行由二个整数组成:a,b。

这些行应该先按b排序再按a排序。

所求的等差数列将不会多于10,000个。

输入输出样例

输入样例#1:

复制

5
7

输出样例#1: 复制

1 4
37 4
2 8
29 8
1 12
5 12
13 12
17 12
5 20
2 24

说明

题目翻译来自NOCOW。

USACO Training Section 1.4

枚举前两项,看是否满足;

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
//#include
//#pragma GCC optimize(2)
using namespace std;
#define maxn 200005
#define inf 0x7fffffff
//#define INF 1e18
#define rdint(x) scanf("%d",&x)
#define rdllt(x) scanf("%lld",&x)
#define rdult(x) scanf("%lu",&x)
#define rdlf(x) scanf("%lf",&x)
#define rdstr(x) scanf("%s",x)
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int U;
#define ms(x) memset((x),0,sizeof(x))
const long long int mod = 1e9 + 7;
#define Mod 1000000000
#define sq(x) (x)*(x)
#define eps 1e-3
typedef pair pii;
#define pi acos(-1.0)
//const int N = 1005;
#define REP(i,n) for(int i=0;i<(n);i++)
typedef pair pii;
inline ll rd() {
ll x = 0;
char c = getchar();
bool f = false;
while (!isdigit(c)) {
if (c == '-') f = true;
c = getchar();
}
while (isdigit(c)) {
x = (x << 1) + (x << 3) + (c ^ 48);
c = getchar();
}
return f ? -x : x;
}

ll gcd(ll a, ll b) {
return b == 0 ? a : gcd(b, a%b);
}
int sqr(int x) { return x * x; }


/*ll ans;
ll exgcd(ll a, ll b, ll &x, ll &y) {
if (!b) {
x = 1; y = 0; return a;
}
ans = exgcd(b, a%b, x, y);
ll t = x; x = y; y = t - a / b * y;
return ans;
}
*/

int n, m;
struct node {
int a, b;
}nd[maxn];
bool cmp(node x, node y) {
if (x.b < y.b)return true;
if (x.b == y.b&&x.a < y.a)return true;
return false;
}
int ans;
bool fg[maxn];


int main() {
//ios::sync_with_stdio(0);
rdint(n); rdint(m);
for (int i = 0; i <= m; i++)
for (int j = 0; j <= m; j++)fg[i*i + j * j] = true;
int maxx = m * m * 2;
for (int i = 0; i <= maxx; i++) {
if (fg[i]) {
for (int j = i + 1; j <= maxx; j++) {
if (fg[j]) {
int dt = j - i;
int Max = i + dt * (n - 1);
if (Max > maxx)break;
bool f = true;
for (int k = i + dt; k <= Max; k += dt) {
if (!fg[k]) {
f = false; break;
}
}
if (f) {
nd[++ans].a = i;
nd[ans].b = dt;
}
}
}
}
}
if (ans == 0) {
cout << "NONE" << endl;
}
else {
sort(nd + 1, nd + 1 + ans, cmp);
for (int i = 1; i <= ans; i++) {
cout << nd[i].a << ' ' << nd[i].b << endl;
}
}
return 0;
}

 

EPFL - Fighting

举报

相关推荐

等差数列划分

算法-等差数列

等差数列求和

24 求等差数列

蓝桥杯——等差数列

1246. 等差数列

0 条评论