等差数列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 ...
随机推荐
- Android 开发调试最优选项
1 开发环境 VS2019 16.4.5 2 开发调试选项 Android 选项
- 本地文件包含(LFI)漏洞
PHP file://封装 PHP php://filter PHP ZIP封装LFI 通过/proc/self/environ执行LFI 空字节技术 截断LFI绕过 通过邮件给目标机器发送一个反弹s ...
- JAVA是否最适合企业应用开发?
· JAVA是否最适合企业应用开发? 当我刚入行做程序员的时候,那是在01-02年,铺天盖地的都是java,j2ee,公司也使用java作为开发语言,我也就随大流加入javaer阵营. 从那时起,各种 ...
- ovs安装教程
原文链接:https://www.cnblogs.com/goldsunshine/p/10331606.html Open vSwitch系列之二 安装指定版本ovs Open vSwitch系 ...
- 设置完代理IP,手机连接WiFi时连不上,一直在转
fiddler抓包,设置代理后,会出现手机连不上网的情况,针对这个情况处理方式为: 首先如果已经连接上WiFi了,但是提示“不可上网”,那就把WiFi断开重新连接下. 不行的话就试试下面的办法 一.配 ...
- Linux一些基本命令、inode定义、软硬链接
1.创建普通文件命令:touch 命令 2.创建目录文件命令:mkdir 命令 3.删除普通文件命令:rm 命令 4.删除目录文件命令:rmdir 命令 5.给普通文件写东西命令:vim 命令 6.查 ...
- qsort 与sort 对结构体排序实例
qsort 与sort 对结构体排序实例 #include<bits/stdc++.h> using namespace std; typedef struct { string book ...
- 多线程的sleep、yield、join用法及sleep与wait的区别
Thread类的方法列表:sleep.yield.join用于线程的协作,围绕线程的调度 1.join()等待线程结束:调用join方法的线程,执行结束后才会释放锁.主线程main中调用启动线程(调用 ...
- 查看Spark与Hadoop等其他组件的兼容版本
安装与Spark相关的其他组件的时候,例如JDK,Hadoop,Yarn,Hive,Kafka等,要考虑到这些组件和Spark的版本兼容关系.这个对应关系可以在Spark源代码的pom.xml文件中查 ...
- 5G将至,4G降速:是谣言还是真相?
畅用移动智能终端设备,早已成为大众日常生活中的一部分.卫报专栏作家伯克曼提到,"据估计,70%的人会抱着手机或iPad刷资讯入睡."当移动智能终端变得如此重要时,与之息息相关的网络 ...