// 出自ICPC 2018网络赛C - Dream & D - Find Integer

// 对大佬来讲的水题,本菜鸡尽量学会的防爆零题。。。

// 今晚翻看vjudge昨日任务上的C题,发现好多人一发过了,我想它应该不难吧,但自己始终没想明白,到底该如何构造一种符合题意封闭的加法和乘法运算。

// 参考了这里的说明,才完全弄懂了,写点东西记录学习成果。

C - Dream (HDU 6440)

题目要求重新定义加法和乘法,对任意非负整数都满足:(m+n)p=mp+np,其中p为素数。 之前我在数论部分的博客已经提到过费马小定理,比赛时候面对这题时竟然完全没有联想到,反倒还被题目的各种概念给绕晕。这里再次说明费马小定理

假如p是质数,且gcd(a,p)=1,那么 ap-1≡1(mod p)

这样就找到了符合题意的运算,即模p加法与模p乘法。明白这一点,代码就很简单了:

#include <cstdio>.
#include <iostream>
#define sci(i) scanf("%d", &i)
#define rep(i, a, b) for(int i=a;i<b;i++)
using namespace std; int main()
{
int T; sci(T);
while(T--)
{
int p; sci(p);
rep(i, , p)
{
rep(j, , p)
printf("%d ", (i+j)%p);
printf("\n");
} rep(i, , p)
{
rep(j, , p)
printf("%d ", i*j%p);
printf("\n");
}
}
return ;
}

// 第一次尝试用以上宏定义#define的新姿势,代码更显简短清爽~~~

 

D - Find Interger (HDU 6441)

题意很明朗: 给定a和n,求满足条件 an+bn=cn 的正整数b和c。

// 上一行会吞字,原因不明。。。

// 避免吞字,截图插入。。。

// 左边原文,右边显示

这与费马大定理有关:

当整数时,关于的方程没有正整数解

那么题目就简单分四种情况:

  1. n>2,由费马大定理可知,不存在解
  2. n=0时,等式恒不成立,无解输出 -1 -1
  3. n=1时,可取b=1, c=a+1
  4. 对于n=2的解(b, c),详细见代码部分的注释
#include <iostream>
#include <cstdio>
using namespace std; int n, a;
int main()
{
int T;
cin>>T;
while(T--)
{
scanf("%d %d", &n, &a);
if(n>||n==) {printf("-1 -1\n"); continue; }
if(n==) {printf("1 %d\n", a+); continue; }
else
{ //a*a = c*c - b*b = (c-b)*(c+b)
if(a&) // c-b=1, c+b=a*a
printf("%d %d\n", (a*a-)/, (a*a+)/);
else // c-b=2, c+b=a*a/2
printf("%d %d\n", (a*a/-)/, (a*a/+)/);
}
}
return ;
}

End.

水题两篇 Dream & Find Integer (HDU 6440/6441)的更多相关文章

  1. 寒假第一发(CF水题两个)

    地址http://codeforces.com/contest/799 A. Carrot Cakes In some game by Playrix it takes t minutes for a ...

  2. 每日一刷(2018多校水题+2016icpc水题)

    11.9 线段树 http://acm.hdu.edu.cn/showproblem.php?pid=6315 求逆序对个数 http://acm.hdu.edu.cn/showproblem.php ...

  3. HDU 5832 A water problem(某水题)

    p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...

  4. hdu 2393:Higher Math(计算几何,水题)

    Higher Math Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  5. HDU 5578 Friendship of Frog 水题

    Friendship of Frog Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.ph ...

  6. HDU 5832 A water problem (带坑水题)

    A water problem 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5832 Description Two planets named H ...

  7. HDU 5538 L - House Building 水题

    L - House Building Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.ph ...

  8. 动态规划之HDU水题

    做水题的感觉真好系列 HDU 2084 数塔 1: 12: 1 23: 1 2 34: 1 2 3 45: 1 2 3 4 5 dp[i][j]第i行第j个数取得的最大值dp[i][j] = max( ...

  9. <hdu - 3999> The order of a Tree 水题 之 二叉搜索的数的先序输出

    这里是杭电hdu上的链接:http://acm.hdu.edu.cn/showproblem.php?pid=3999  Problem Description: As we know,the sha ...

随机推荐

  1. shell 命令 用户管理

     1. 查看保存用户相关信息的文件 [ cat /etc/passwd ]  [linux    :    x    :   1000  :   1000   :   linux,,,   :    ...

  2. Mysql优化-概述

    摘抄并用于自查笔记 为什么要优化 一个应用吞吐量瓶颈往往出现在数据库的处理速度上. 随着应用程序的使用,数据库数据逐渐增多,数据库处理压力逐渐增大. 关系型数据库的数据是存放在磁盘上,读写速度慢(与内 ...

  3. 数据库MySQL--连接查询

    例子文件1:https://files.cnblogs.com/files/Vera-y/myemployees.zip 例子文件2:https://files-cdn.cnblogs.com/fil ...

  4. [190308]Ubuntu 安装完之后,安装的软件小记

    install software vim sudo apt-get install -y vim Typora command copy from Typora website # or run: # ...

  5. CF773E Blog Post Rating

    题意:有一篇博客.一共有n个人,心中有他们期望该博客得到的赞数a[i].当某个时刻该博客的获赞数<a[i],则该人会使得赞数+1,当赞数>a[i],该人会使得赞数-1,当赞数=a[i],不 ...

  6. js获取当前网址Url

    js获取当前路径并截取 var str = window.location.href;// str = 'https://localhost:8080/mark' var index = str .l ...

  7. VBS脚本完美实现开机延时启动

    目录 概述 vbs内容示例: vbs示例语句分析 自定义vbs脚本 一些问题和解决方法   概述 系统开机时,顺带自动启动了不少驱动程序,使得电脑开机后鼠标要呆滞许久.为了加快windows的开机速度 ...

  8. 推荐一个Java设计模式写的很好的博客

    博客地址:https://quanke.gitbooks.io/design-pattern-java/%E9%9D%A2%E5%90%91%E5%AF%B9%E8%B1%A1%E8%AE%BE%E8 ...

  9. C++——类的继承(派生)

    类的继承就是子类可以拥有父类的成员变量和成员函数 //public 修饰的成员变量 方法 在类的内部 类的外部都能使用//protected: 修饰的成员变量方法,在类的内部使用 ,在继承的子类中可用 ...

  10. DNS的解析过程

    1.什么是DNS 在互联网上,唯一标识一台计算机的是IP地址,但是IP地址不方便记忆,通过一个域名对应一个IP地址,来达到找到IP地址的目的,那么DNS就是将域名转换成IP地址的过程. 2.DNS查询 ...