ZOJ 3470 Magic Squares

七彩虹C.B75K主板的安装详细介绍 七彩虹B75主板的安装介绍,包括CPU,内存条和跳线(含详细彩图) 立即下载
Magic Squares

Time Limit: 2 Seconds      Memory Limit: 65536 KB


One day, the astronauts from China arrived in a planet that they have never been before. First, they sent a robot called AAA to explore the environment as usual.

However, AAA became confused as soon as it departed from the aerostat: it found that the surface of the planet was divided into many blocks we call magic squares, each block is marked by an unique integer in a special order(as shown in the left graph). Fortunately, AAA is so clever that it soon found the relationship between these blocks and the Descartes coordinate system (as shown in the right graph).

Before each movement, AAA would send its current block number to the monitor, then move to the right position the monitor order it to. Assuming that the monitor would only order AAA to move to the block neighbored to the current block. We say two blocks are neighbor if they share one same edge.

To complete the task in time, the monitor need to respond to the request of the robot as soon as possible. But the computing progress may be too complicated for beings. So they now turn to you, an excellent programmer, help them list the blocks the AAA can reach at the time in order to sent command to the robot in a short time.

Input

The first line of input is the number of test cases T, then T lines follow, each is a test case with only one positive integers n no larger than 2,000,000,000.

Output

The output consists of exactly T lines, one line for each case.
Line i (1 <= i <= T) should contain an increasing sequence of integers separated by single spaces -- the numbers of the blocks that the robot could reach (with its current block numbered n).

Sample Input

1
5

Sample Output

4 6 16 18



先判断数字所处的层数,然后分8种情况(在四个拐角处或是在四条边上)来推往里或往外一层的相邻的两个数字(因为四个数字中一定有两个是比其小1和大1的数字其他两数字是不同层的,需要分情况讨论),最后可以将8种情况整理成4种情况进行编写 ~

代码如下:

#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<cmath>

using namespace std;

int save[5];

void s_save(int x, int y, int m, int n)
{
    save[0] = x;
    save[1] = y;
    save[2] = m;
    save[3] = n;
}

int dif(int num, int i, int dig, int d)
{
    if(num < dig)
    {
        s_save(num - d + 8 - 2 * i,num - 1,num + 1,num + d + 2 * i);
        return 1;
    }
    else if(num == dig)
    {
        s_save(num - 1, num + 1, num + d + 2 * i, num + d + 2 * (i + 1));
        return 1;
    }
    return 0;
}

void solve(int num, int cov, int dig, int d)
{
    int flag = 0;
    dig += cov;
    flag = dif(num, 0, dig, d); //从上 边开始推
    if(!flag)  //判断是否在左 边上
    {
        dig += cov * 2;
        flag = dif(num, 1, dig, d);
    }
    if(!flag)  //判断是否在下 边上
    {
        dig += cov * 2 + 1;
        flag = dif(num, 2, dig, d);
    }
    if(!flag)  //判断是否在右 边上
    {
        dig += cov * 2 + 1;
        flag = dif(num, 3, dig, d);
    }
    if(!flag)  //判断是否在转完一圈的 上边上
    {
        s_save(num - d,num - 1,num + 1,num + d + 8);
    }
}

void find_cov(int num) //确定元素所在层数
{
    int d = 3, cov = 0, dig = 1;
    while(1)
    {
        if(cov)
            d += 8;
        dig += d;
        if(num <= dig)
            break;
        ++cov;
    }
    if(num == 1)
        s_save(2,4,6,8);
    else
        solve(num, cov, dig - d, d);
}

int main()
{
#ifdef test
    freopen("in.txt", "r", stdin);
#endif
    int t, num;
    scanf("%d", &t);
    while(t--)
    {
        scanf("%d", &num);
        find_cov(num);
        printf("%d %d %d %d\n",save[0],save[1],save[2],save[3]);
    }
    return 0;
}



科技中介服务机构如何提升客户在技术评估与需求匹配环节的服务效率与专业度.docx 科技中介服务机构如何提升客户在技术评估与需求匹配环节的服务效率与专业度 立即下载

相关推荐

51单片机的温湿度检测报警系统仿真资料

51单片机的温湿度检测报警proteus仿真系统

ZOJ 2835 Magic Square(set容器使用)

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2835 #include #include #include using namespace std; int main() { int matrix[9][9]; int line[9],column[9];//每行每列的和

Learn as if you were to live forever 903

数控弯管机xyz转换ybc的算法_花了一年时间开发的三维弯管机交互式转档软件(三维管子模型UG,SOLIDWORK,PRO/E文件转成YBC)...

在弯管机加工中,由管子模型生成可直接进行弯管加工的YBC数据可以大大提高弯管编程过程.传统的做法是先用dxf数据文件(用autocad绘制管子的轴心线数据)转出XYZ数据,然后由XYZ数据转成YBC数据,而且大部分厂家对文件版本以及软件做管方式都有不少限制,这大大花费了用户的时间跟精力.随着企业的信息化管理以及不断的技术升级,传统用autocad线条模式来描述管子的方式正在被用户用PRO/E,UG...

weixin_29065959的博客 1963

[模拟+二分]zoj 3470:Magic Squares

大致题意:     如题目中给出的图片 对于这样的一个无线扩展出去的图,输入一个数n,求出数字上下左右的4个数字,按造升序输出。   大致思路:     突破点在,对于每一圈右下角的数字都是(a*2-1)*(a*2-1),a为当前在第a圈。如此,通过二分枚举判定出这个点在第几个圈内。然后在推导这个点和上下左右点的关系。   #include&lt;iostream&gt; #...

手上有剑,心中有爱 124

小红书x-mini签名逆向实战:Frida动态Hook与算法还原

在移动App协议分析中,请求签名机制是保障接口安全的核心技术,其本质是客户端对请求参数、时间戳、设备指纹等要素进行加密认证的过程。理解签名原理需掌握Native层加密流程、JNI调用机制及密钥动态派生逻辑。Frida作为主流动态插桩工具,能绕过符号剥离、字符串加密和控制流混淆等反分析手段,在函数执行瞬间捕获寄存器状态、内存数据与调用上下文,实现签名算法的完整还原。该技术广泛应用于合规内容监测、API自动化调用与竞品协议研究等工程场景。本文聚焦小红书x-mini签名这一典型样本,详解从JNI_OnLoad定位

weixin_31264565的博客 457

别再手动写JSON了!用Node-RED OPC UA节点5分钟搞定楼宇温湿度数据采集

本文介绍了如何利用Node-RED和OPC UA节点快速实现楼宇温湿度数据的自动化采集与JSON格式转换。通过可视化工具Node-RED,开发者无需编写复杂代码,5分钟内即可完成从传感器到数据输出的全流程配置,大幅提升智慧楼宇和工业物联网项目的实施效率。

weixin_30368405的博客 432

UVa 10791 - Minimum Sum LCM

分解质因子的一个题,将最小公倍数分解质因子,最小的和sum便为各个质因子的相应次方数之和。 此题难点在于几个特殊的情况的处理: (1)当N = 1时,应输出2(1*1=1,sum=1+1=2); (2)当N是素数的时候,输出N+1(N*1=N,sum=N+1); (3)当只有单质因子时,sum=质因子相应次方+1; (4)当N=2147483647时,它是一个素数,此时输出2

GooMaple 2505

采用超级电容设计的UPS不间断电源,用于掉电数据保存,可续航10-60秒

关键词:树莓派 CM4 UPS 不间断电源 掉电保存 超级电容 SuperCap 充电 升压 续航 10F 法拉电容 Mcuzone 野芯科技 概述:我们经常会遇到系统意外掉电的情况,对于一些可靠性要求很高的工业现场设备或者运行过程中有重要数据需要保存的平台就需要UPS电源了。常规的UPS采用后备锂电池设计,具备无缝切换、续航时间长、稳定可靠等特点,但同时也有价格过高、体积过大的缺点。对于不少仅需在检测到系统掉电后对数据进行保存即可的应用而言,这些标准的UPS电源就不太合适,为此我...

6605

HDU 1717 - 小数化分数2

整数化小数,不循环的小数容易化。对于循环小数化分数原理如下: ⑴ 把0.4747……和0.33……化成分数。 例1: 0.4747……×100=47.4747…… 0.4747……×100-0.4747……=47.4747……-0.4747…… (100-1)×0.4747……=47 即99×0.4747…… =47 那么 0.4747……=47/99 例2

GooMaple 2113

盘点那些好用的SAP FIORI App(二) -上传凭证Upload Journal Entry

SAP FICO运维问题及解决方案分享

lijinbao171022的博客 1542

从零构建2.4G无线通信系统:MSPM0G3507与NRF24L01的深度协同设计

本文详细介绍了基于MSPM0G3507微控制器与NRF24L01射频模块构建2.4G无线通信系统的深度协同设计方案。内容涵盖系统架构、SPI通信优化、功耗管理、抗干扰机制及中断/DMA高效利用,为工业物联网应用提供高可靠、低功耗的无线传输解决方案。

rice5的博客 821

UVa 10177 - (2/3/4)-D Sqr/Rects/Cubes/Boxes?

曾经放了好久的题,今天A掉了,其实就是推公式了,唯一觉得被坑了就是在windows下long long占位符是%I64d,而UVa用的貌似是linux,为此WR了一次,查了 一下,改成%lld后AC了。 题目解法: 对于正方形正方块来说,比较好找找规律,比如边长是4的二维下的正方形就是:1+4+9+25=39.即:前面所有边长的平方和,三维四维的相应的就是三次方和四次方。 对于边不同长的长

GooMaple 1662

《我的Classic Platform AUTOSAR之路》 BSWM 模式管理(二)ESH

只有 PostRun 和 wakeup 才能切换到 RUN,其他状态需要先切换到 wakeup 模式。进入 RUN 模式需要打开通信 allow,退出 Run 需要 disable 通信 allow。Prepareshut 与 dem_shutdown 绑定,如果没有 dem 配置,不会生成。除了 Init 模式下,其他模式下都要检查 ECU 的唤醒源和通信 pending 需求。与 ECUM 相关,整个 ECU 状态管理的状态机制。postrun 可以不配置,需要根据自己的需求使能。

libuping2023的博客 964

UVa 10387 - Billiard

可以发现走过的总的水平距离为与垂直边碰撞的次数乘以水平边的长度,即:(a * m),同理总的垂直距离为(b * n)。 所以,其弧度为 (a*m)/(b*n) 的反正切值,再转换为角度,总长度用勾股定理算出来再除以时间,就是速度。 代码如下: #include #include #include #include #include using namespace std; int

GooMaple 1311

【人工智能】Spring AI项目在定义 `response_format` 的 Invalid schema for response_format ‘custom_schema‘

确保。

奶爸的编程之路,也就一周冷个三天 562

怀旧版电脑GBC模拟器

再现原版GBC掌机的界面,电脑运行连游戏界面都一样大小,很怀旧,很真实,(英文版)强烈推荐,很小的文件,很难才找到的。

UVa 138 - Street Numbers

题目实际就是给你两个数m、n,使得m、n之间的数之和与1~m之间的数之和相等(不包括m)。 即:1+2+...+m-1 == (m+1)+...+n; 因此我们可以套用求和公式,两边移项化简得:2*pow(m,2) = n*(n+1)。 因此我们可以得到:m = sqrt(n*(n+1)/2)。枚举n的值,从而判断所得到的m值是否为整数,若为整数则说明两边的值实际相等。得到输出满足条件的最小的

GooMaple 1231

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

UVa 10023 - Square root

大数开平方,本来用从网上查的手动开平方的方法写的代码,但中间过程需要用到double型的量存取,结果TLE,调试了一下,原来是double型的量存取长度不够,换成long double型的量,可以存到10的100次方了,但交上以后依然WA,又调试了半天才发现,当double型量存取数字到18位左右以后,虽然依然可以存取数字,但数字的精度已经会有改变,也就是说位数还是那个位数,但实际数字已经不对了。

GooMaple 1177

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

ZOJ 1577 - GCD & LCM

GCD & LCM Time Limit: 2 Seconds      Memory Limit: 65536 KB Given x and y (2 1) p and q are positive integers; 2) GCD(p, q) = x; 3) LCM(p, q) = y. Input x and y, one line for each test.

GooMaple 1064

UVa 10006 - Carmichael Numbers

打素数表+快速幂取模。 题目较简单,只要数字满足其不是素数且利用“Fermat test”方法证明所有情况下均满足,则其为Carmichael Numbers。注意数字范围,分治求幂模时要用long long。 代码如下: #include #include #include bool vis[65002]; void Is_Prime(int n)//打素数表 { m

GooMaple 867

HDU 3641 Treasure Hunting

Treasure Hunting Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1321    Accepted Submission(s): 403 Problem Description Zstu_yhr i

GooMaple 836

UVa 106 - Fermat vs. Pythagoras

对于a2+b2=c2,求出所有满足条件的(a,b,c)三元组,输出a,b,c三个互质的组别的数量和从1到N中所有 的三元组中都没有出现过的数字的个数。 要求互质毕达哥拉斯三元组,可直接得到: a = m^2-n^2    b = 2mn    c = m^2+n^2 因为三个变量之间两两互质,所以a,b中必为一奇一偶。(详细证明见:http://www.cnblogs.com/de

GooMaple 824

UVa 11121 - Base -2

将一个十进制转化为-2进制的数。同样求余数,当n%-2 代码如下: #include #include #include int main() { #ifdef test freopen("sample.txt", "r", stdin); #endif int t, n, bi, cct=0; char binary[1000]; scanf("%d

GooMaple 823

UVa 10183 - How Many Fibs?

求区间[a,b]内斐波那契数的个数,因为最大到了10的100次方,初步递推一下,10的100次方左右,可达到斐波那契数列的第480项左右,因此用暴力枚举+大数加法即可。 代码如下: #include #include #include char fibo[480][105]; void Fibonacci(char *a, char *b, char *c)//斐波那契数列打表 {

GooMaple 789

UVa 10079 - Pizza Cutting

简单推公式的题。 代码如下: #include #include #include #include #include #include using namespace std; int main() { #ifdef test freopen("sample.txt", "r", stdin); #endif long long n; while(sc

GooMaple 783

UVa 993 - Product of digits

分解因子,从9开始分解,一直分解到2。0与1需要特殊处理。 代码如下: #include #include #include #include #include #include using namespace std; int main() { #ifdef test freopen("in.txt", "r", stdin); #endif int t, n, a[

GooMaple 776
上一篇: ZOJ 3501 Roman Order
下一篇: ZOJ 2859 Matrix Searching
GooMaple
博客等级 码龄14年 79粉丝 197原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值