等差数列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 ...
随机推荐
- spark-sklearn TypeError: 'JavaPackage' object is not callable
from sklearn import svm, datasets from spark_sklearn import GridSearchCV from pyspark import SparkCo ...
- vue.js事件传值之子组件传向父组件以及$emit的使用
在项目开发中,有时候会遇到一种需求比如是:在子组件中,通过一个事件,比如点击事件,去改变父组件中的某个值,下面来看看是怎么个流程 还是先截图目录结构 父组件为app.vue,components中的文 ...
- float浮动造成高度塌陷的解决办法
Float是我们在页面布局中常用的,也是非常重要的一个属性,可以让页面布局变得更加灵活. 但是在继续学习之后,尤其是掌握了宽高自适应之后,我们常常会发现一个奇怪的现象:如果父元素没有设置高度,而子元素 ...
- 群晖DSM修改ssh权限实现免密码登陆
问题 使用ssh-id-copy正确上传公钥后依然无法免密码登陆 原因 群晖DSM中.ssh文件夹权限不当 解决 赋予正确权限 admin@DiskStation:/var/services/home ...
- XSS漏洞原理
注入型漏洞的本质都是服务端分不清用户输入的内容是数据还是指令代码,从而造成用户输入恶意代码传到服务端执行. 00x01js执行 Js是浏览器执行的前端语言,用户在存在xss漏洞的站点url后者能输入数 ...
- linux执行计划任务at命令
https://www.jianshu.com/p/17f5aa01853f 在linux计划任务中, cron 是周期性运行某任务,假如我只想在未来的某时间点只是要求任务运行一次,这就用到at命令了 ...
- loj6278 数列分块入门题2
题意:支持区间加,询问区间中元素排名 维护两个域.一个域维护原序列,一个域维护快内排序序列. 每次修改后更新快内排序序列. 修改时O(sqrt(n)log(sqrt(n))) 询问时O(sqrt(n) ...
- ActiveMQ的p2p模式与发布订阅模式
1.消息中间件:采用异步通讯防止,支持点对点以及发布订阅模式,可以解决高并发问题 传统调用接口,可能发生阻塞,重复提交,超时等等问题,可以利用消息中间件发送异步通讯请求 ...
- Spring Data JPA介绍与简单案例
一.Spring Data JPA介绍 可以理解为JPA规范的再次封装抽象,底层还是使用了Hibernate的JPA技术实现,引用JPQL(Java Persistence Query Languag ...
- wso2 linux上部署说明
1.启动wso2