寒假的第一天,终于有空再写题目了,专心备战了。本想拿usaco上的题目练手热身,结果被磕住了T T。
其实这是一道穷举题,一开始我在穷举a,b,但是怎么优化就是过不了Test 8,后来参照NOCOW上的解题弄了se和list两个数组,也算是终于通过了,这时候已经是Submission #7了。(啊,有两次是因为没开文件,一次是因为没打‘NONE’)
总之,祝寒假的OI学习顺利吧~All or Nothing, Now or Never!

program ariprog2;
var se:array[..*] of boolean;
list:array[..] of longint;
a0,b0:array[..] of longint;
m,n,i,j,a,b,count,ans_count,temp:longint;
flag,code:boolean;
procedure start;
var i,j:longint;
begin
count:=;
for i:= to m do
for j:= to m do
begin
if se[i*i+j*j]=false then
begin
se[i*i+j*j]:=true;
inc(count);
list[count]:=i*i+j*j;
end;
end;
end;
procedure pd(a,b:longint);
var i:integer;
flag:boolean;
begin
if a+(n-)*b>m*m* then exit;
flag:=true;
for i:= to n- do
if se[a+i*b]=false then
begin
flag:=false;
break;
end;
if flag then
begin
inc(ans_count);
a0[ans_count]:=a;
b0[ans_count]:=b;
end;
end; begin
assign(input,'ariprog.in');reset(input);
assign(output,'ariprog.out');rewrite(output);
readln(n);
readln(m);
start;
for i:= to count- do
for j:=i+ to count do
begin
a:=list[i];
if list[j]<a then a:=list[j];
b:=abs(list[j]-list[i]);
pd(a,b);
end;
if ans_count<> then
begin
for i:= to ans_count- do
for j:=i+ to ans_count do
begin
if (b0[i]>b0[j]) or ((b0[i]=b0[j])and(a0[i]>a0[j])) then
begin
temp:=b0[i];b0[i]:=b0[j];b0[j]:=temp;
temp:=a0[i];a0[i]:=a0[j];a0[j]:=temp;
end;
end;
for i:= to ans_count do
writeln(a0[i],' ',b0[i]);
end
else
writeln('NONE');
close(input);close(output);
end.

ariprog2

Executing...
Test 1: TEST OK [0.000 secs, 720 KB]
Test 2: TEST OK [0.000 secs, 720 KB]
Test 3: TEST OK [0.000 secs, 720 KB]
Test 4: TEST OK [0.000 secs, 720 KB]
Test 5: TEST OK [0.022 secs, 720 KB]
Test 6: TEST OK [0.151 secs, 720 KB]
Test 7: TEST OK [1.717 secs, 720 KB]
Test 8: TEST OK [3.823 secs, 720 KB]
Test 9: TEST OK [3.834 secs, 720 KB]

All tests OK.

话说,过了的那个程序可以再优化的。
题目讲输出范围小于10000我就直接上O(N^2)排序了,用快排可以再省一点时间吧。

顺便把那个怎么也过不掉Test 8的程序也发上来好了,不能让自己白打这么久!!

program ariprog;
var se,cha:array[..*] of boolean;
m,n,i,j,k,l,a,b,max,num_se:longint;
flag,code:boolean; procedure start;
var i,j:integer;
begin
fillchar(se,sizeof(se),false);
for i:= to m do
for j:= to m do
se[i*i+j*j]:=true;
fillchar(cha,sizeof(cha),false);
for i:= to m do
for j:= to m do
for k:= to i do
for l:= to j do
cha[i*i+j*j-k*k-l*l]:=true;
end; begin
assign(input,'ariprog.in');reset(input);
assign(output,'ariprog.out');rewrite(output);
readln(n);
readln(m);
max:=*m*m;
code:=false;
start;
for b:= to trunc(max/(n-)) do
if cha[b]=true then
begin
for a:= to *m*m do
begin
if a+(n-)*b>max then break;
if se[a]=true then
begin
i:=;flag:=true;
while i<=n- do
begin
if se[a+i*b]=false then
begin
flag:=false;
break;
end;
inc(i);
end;
if flag then
begin
code:=true;
writeln(a,' ',b);
end;
end;
end;
end;
if not code then writeln('NONE');
close(input);close(output);
end.

ariprog

Section 1.4 Arithmetic Progressions的更多相关文章

  1. USACO Section 1.4 Arithmetic Progressions 解题报告

    题目 题目描述 现在给你一个数集,里面的数字都是由p^2+q^2这种形式构成的0 <= p,q <= M,我现在需要你在其中找出一个长为N的等差数列,数列中的第一个数字为a,公差为b,当你 ...

  2. 洛谷P1214 [USACO1.4]等差数列 Arithmetic Progressions

    P1214 [USACO1.4]等差数列 Arithmetic Progressions• o 156通过o 463提交• 题目提供者该用户不存在• 标签USACO• 难度普及+/提高 提交 讨论 题 ...

  3. [Educational Codeforces Round 16]D. Two Arithmetic Progressions

    [Educational Codeforces Round 16]D. Two Arithmetic Progressions 试题描述 You are given two arithmetic pr ...

  4. 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 ...

  5. POJ 3006 Dirichlet's Theorem on Arithmetic Progressions (素数)

    Dirichlet's Theorem on Arithmetic Progressions Time Limit: 1000MS   Memory Limit: 65536K Total Submi ...

  6. poj 3006 Dirichlet's Theorem on Arithmetic Progressions【素数问题】

    题目地址:http://poj.org/problem?id=3006 刷了好多水题,来找回状态...... Dirichlet's Theorem on Arithmetic Progression ...

  7. (素数求解)I - Dirichlet&#39;s Theorem on Arithmetic Progressions(1.5.5)

    Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit cid=1006#sta ...

  8. USACO 1.4 Arithmetic Progressions

    Arithmetic Progressions An arithmetic progression is a sequence of the form a, a+b, a+2b, ..., a+nb ...

  9. Educational Codeforces Round 16 D. Two Arithmetic Progressions (不互质中国剩余定理)

    Two Arithmetic Progressions 题目链接: http://codeforces.com/contest/710/problem/D Description You are gi ...

随机推荐

  1. NowCoder猜想(素数筛法+位压缩)

    在期末被各科的大作业碾压快要窒息之际,百忙之中抽空上牛客网逛了逛,无意中发现一道好题,NowCoder猜想,题意很明显,就是个简单的素数筛法,但竟然超内存了,我晕(+﹏+)~  明明有 3 万多 k ...

  2. linux特殊字符

    linux特殊字符: * 匹配文件名中的任何字符串,包括空字符串. ? 匹配文件名中的任何单个字符. [...] 匹配[ ]中所包含的任何字符. [!...] 匹配[ ]中非感叹号!之后的字符. 当s ...

  3. 在linux(CentOS-6.7_x86_64)上安装mysql成功记录

    查看linux服务器的yum源设置: [root@hadoop03 yum.repos.d]# cd /etc/yum.repos.d [root@hadoop03 yum.repos.d]# ll ...

  4. SSH命令行上传/下载文件

    上传:scp /path/file(这部分为本地的路径) user(远端目标用户名)@host(远端目标IP):/pathorfile(文件存储路径) 下载:scp user(远端用户名)@host( ...

  5. NumPy(Numeric Python)使用方法

    NumPy官网 参考:<Python数据分析基础教程:NumPy学习指南> 用Python做科学计算(好东西) NumPy是python的核心库,是python机器学习编程的最底层的库,不 ...

  6. canvas调节视频颜色

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. nginx配置 php 单入口

    location / {            root   html;            index  index.html index.htm index.php; if (!-e $requ ...

  8. android入门之: SharedPreferences

    读取数据: 保存数据: +++++++++++++++++++方法详解++++++++++++++++++++++++++++++ SharedPreferences综述: 使用getSharedPr ...

  9. hadoop分布式的环境搭建

    版本: 使用hadoop1.1.2    JDK为java7 1.下载hadoop 2.配置hadoop文件 3测试 1.下载hadoop: 1.1 在https://archive.apache.o ...

  10. Reorder List [LeetCode]

    Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You must do thi ...