[USACO1.4]等差数列 Arithmetic Progressions
题目描述
一个等差数列是一个能表示成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个。
输入输出样例
说明
题目翻译来自NOCOW。
USACO Training Section 1.4
枚举前两项,看是否满足;
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<bitset>
#include<ctime>
#include<deque>
#include<stack>
#include<functional>
#include<sstream>
//#include<cctype>
//#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<int, int> pii;
#define pi acos(-1.0)
//const int N = 1005;
#define REP(i,n) for(int i=0;i<(n);i++)
typedef pair<int, int> 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;
}
[USACO1.4]等差数列 Arithmetic Progressions的更多相关文章
- 洛谷P1214 [USACO1.4]等差数列 Arithmetic Progressions
P1214 [USACO1.4]等差数列 Arithmetic Progressions• o 156通过o 463提交• 题目提供者该用户不存在• 标签USACO• 难度普及+/提高 提交 讨论 题 ...
- luogu P1214 [USACO1.4]等差数列 Arithmetic Progressions
题目描述 一个等差数列是一个能表示成a, a+b, a+2b,..., a+nb (n=0,1,2,3,...)的数列. 在这个问题中a是一个非负的整数,b是正整数.写一个程序来找出在双平方数集合(双 ...
- 等差数列Arithmetic Progressions题解(USACO1.4)
Arithmetic Progressions USACO1.4 An arithmetic progression is a sequence of the form a, a+b, a+2b, . ...
- poj 3006 Dirichlet's Theorem on Arithmetic Progressions【素数问题】
题目地址:http://poj.org/problem?id=3006 刷了好多水题,来找回状态...... Dirichlet's Theorem on Arithmetic Progression ...
- USACO 1.4 Arithmetic Progressions
Arithmetic Progressions An arithmetic progression is a sequence of the form a, a+b, a+2b, ..., a+nb ...
- [Educational Codeforces Round 16]D. Two Arithmetic Progressions
[Educational Codeforces Round 16]D. Two Arithmetic Progressions 试题描述 You are given two arithmetic pr ...
- Dirichlet's Theorem on Arithmetic Progressions 分类: POJ 2015-06-12 21:07 7人阅读 评论(0) 收藏
Dirichlet's Theorem on Arithmetic Progressions Time Limit: 1000MS Memory Limit: 65536K Total Submi ...
- POJ 3006 Dirichlet's Theorem on Arithmetic Progressions (素数)
Dirichlet's Theorem on Arithmetic Progressions Time Limit: 1000MS Memory Limit: 65536K Total Submi ...
- (素数求解)I - Dirichlet's Theorem on Arithmetic Progressions(1.5.5)
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit cid=1006#sta ...
随机推荐
- 3.1 第一个场景 HelloWorldScene
HelloWorldScene.h #ifndef __HELLOWORLD_SCENE_H__ #define __HELLOWORLD_SCENE_H__ #include "cocos ...
- cocos2d-x CSV文件读取 (Excel生成csv文件)
实现类 CCSVParse.h #ifndef __C_CSV_PARSE__ #define __C_CSV_PARSE__ #include "cocos2d.h" #incl ...
- bzoj 2553: [BeiJing2011]禁忌 AC自动机+矩阵乘法
题目大意: http://www.lydsy.com/JudgeOnline/problem.php?id=2553 题解: 利用AC自动机的dp求出所有的转移 然后将所有的转移储存到矩阵中,进行矩阵 ...
- Ubuntu 14.04开发环境
安装ssh服务:sudo apt-get install openssh-server 安装vim:sudo apt-get install vim-gtk 安装gparted:sudo apt-ge ...
- [转]关于新一轮QQ Tencent://Message 在线联系
关于在线QQ代码. 以前的QQ代码都需要添加好友. 现在的 首先是到http://wp.qq.com/生成你的QQ在线代码 很长的一段代码,并且每个QQ生成的sigT字符串都是不一样的.. 闲来无事, ...
- Impala的JDBC无法连接
这是因为客户端连接的JDBC是Impala的master机器,而不是DataNode:因为JDBC的服务宿主是Impalad,而Impalad只是部署在DataNode
- Python:struct模块的pack、unpack
mport struct pack.unpack.pack_into.unpack_from 1 # ref: http://blog.csdn<a href="http://lib. ...
- unittest 执行测试脚本输出测试报告
import unittestimport HTMLTestRunnertest as HTMLTestRunner#获取路径path = './'#创建测试套件,读取测试脚本suite = unit ...
- js产生不同的随机数
前言:前几天写到一个程序,用到要使用不同随机数的方法,结果愣是整了半天没整出来,说来也是惭愧啊(亏我还是软件工程的学生,其实这个问题以前遇到过,只是我逃避了,哎,自己刨的坑终究会把自己陷进去,╮(╯▽ ...
- 使用root直接安装python3.5.2
操作系统:Centos7.4 不使用pyenv管理器直接进行编译安装3.5.2版本. 下载tgz的包,如果没有安装wget,请yum -y install wget 解压python包: 将解压后的包 ...