等差数列Arithmetic Progressions题解(USACO1.4)
Arithmetic Progressions USACO1.4
An arithmetic progression is a sequence of the form a, a+b, a+2b, ..., a+nb where n=0,1,2,3,... . For this problem, a is a non-negative integer and b is a positive integer.
Write a program that finds all arithmetic progressions of length n in the set S of bisquares. The set of bisquares is defined as the set of all integers of the form p2 + q2 (where p and q are non-negative integers).
INPUT:
N (3 <= N <= 25), the length of progressions for which to search
M (1 <= M <= 250), an upper bound to limit the search to the bisquares with 0 <= p,q <= M.
OUTPUT:
If no sequence is found, a single line reading `NONE'. Otherwise, output one or more lines, each with two integers: the first element in a found sequence and the difference between consecutive elements in the same sequence. The lines should be ordered with smallest-difference sequences first and smallest starting number within those sequences first.
There will be no more than 10,000 sequences.
此题目需要一些小的剪枝,详见注释。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define rint register int
const int MAXN=100000+5;
int a[MAXN];
int aa[10005],bb[10005];
bool tab[MAXN];
int n,m,cnt,tot,mx;
int main()
{
freopen("ariprog.in","r",stdin);
freopen("ariprog.out","w",stdout);
scanf("%d%d",&n,&m);
for(rint i=0;i<=m;++i)
for(rint j=0;j<=m;++j)
a[++cnt]=i*i+j*j,tab[a[cnt]]=1;//预处理双平方数表,快速查表
sort(a+1,a+cnt);
cnt=unique(a+1,a+cnt+1)-a-1;
mx=m*m<<1;
int r=mx/(n-1);//公差上界,最大的数除以要求的长度
for(rint i=1;i<=r;++i)
{
for(rint j=1;j<=cnt;++j)
{
rint c=0;
for(rint k=n-1;k>0&&a[j]+i*k<=mx;--k)//若超过max退出循环
//从大到小枚举,不符合情况易退出
if(!tab[a[j]+i*k]) //若有一个不符合条件即break
break;
else ++c;
if(c==n-1)
{
aa[++tot]=a[j];
bb[tot]=i;
}
}
}
if(tot==0)
puts("NONE");
else
for(int i=1;i<=tot;++i)
printf("%d %d\n",aa[i],bb[i]);
return 0;
}
等差数列Arithmetic Progressions题解(USACO1.4)的更多相关文章
- 洛谷P1214 [USACO1.4]等差数列 Arithmetic Progressions
P1214 [USACO1.4]等差数列 Arithmetic Progressions• o 156通过o 463提交• 题目提供者该用户不存在• 标签USACO• 难度普及+/提高 提交 讨论 题 ...
- [USACO1.4]等差数列 Arithmetic Progressions
题目描述 一个等差数列是一个能表示成a, a+b, a+2b,..., a+nb (n=0,1,2,3,...)的数列. 在这个问题中a是一个非负的整数,b是正整数.写一个程序来找出在双平方数集合(双 ...
- luogu P1214 [USACO1.4]等差数列 Arithmetic Progressions
题目描述 一个等差数列是一个能表示成a, a+b, a+2b,..., a+nb (n=0,1,2,3,...)的数列. 在这个问题中a是一个非负的整数,b是正整数.写一个程序来找出在双平方数集合(双 ...
- 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 (不互质中国剩余定理)
Two Arithmetic Progressions 题目链接: http://codeforces.com/contest/710/problem/D Description You are gi ...
- E - Two Arithmetic Progressions(CodeForces - 710D)(拓展中国剩余定理)
You are given two arithmetic progressions: a1k + b1 and a2l + b2. Find the number of integers x such ...
- [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 ...
随机推荐
- 剑指offer-面试题19-正则表达式匹配-字符串
/* 题目: 实现一个函数用来匹配包含'.'和'*'的正则表达式. '.'表示比配任意字符,‘*’表示匹配0个或多个字符串. */ /* 思路: 采用递归的方法. 基础条件:当字符串和模式串存在空的情 ...
- gitlab 备份和恢复
前言 gitlab这个代码托管工具真是强大,很多东西都是做好了直接用的. 这里就包括备份和恢复功能. 正文 备份 我们可以直接运行此命令,来进行备份. sudo gitlab-rake gitlab: ...
- Java定时任务之Timer
Timer是Java中实现定时任务的方式之一,下面是一个简单的例子: import java.util.Timer; import java.util.TimerTask; public class ...
- VSCode配置之open-with-Live-Server 无法打开浏览器【解决方法】
如果你的vscode编辑器打开浏览器时默认打开的是iE,想要把它改为chrome,怎么办呢? 我遇到如下原因: 这是按照网上的setting.json配置 这是运行了 open-with-live-s ...
- SSRF服务器端请求伪造
SSRF漏洞原理 SSRF(Server-Side Request Forgery:服务器端请求伪造)是一种由恶意访问者构造形成由服务端发起请求的一个安全漏洞一般情况下,SSRF访问的目标是从外网无法 ...
- JavaDay2(中)
Java循环与分支练习 习题1: 输出1~100内前5个可以被3整除的数. public class Day2_Test1 { //输出1~100内前5个可以被3整除的数. public static ...
- day03_2spring3
SSH整合(续) 一.spring整合hibernate:有hibernate.cfg.xml 前提:导入jar包,在前面已经介绍了jar包的整合,我们只需要将整合的所有jar包导进去即可. 1.创建 ...
- ubuntu set up 3 - cuda
https://www.pugetsystems.com/labs/hpc/How-to-install-CUDA-9-2-on-Ubuntu-18-04-1184/ How to install C ...
- go语言 base58编码解码
package main import ( "bytes" "encoding/hex" "fmt" "math/big" ...
- 51Nod 1068 Bash游戏 V3 (这规律不好找)
有一堆石子共有N个.A B两个人轮流拿,A先拿.每次拿的数量只能是2的正整数次幂,比如(1,2,4,8,16....),拿到最后1颗石子的人获胜.假设A B都非常聪明,拿石子的过程中不会出现失误.给出 ...