水题两篇 Dream & Find Integer (HDU 6440/6441)
// 出自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。


// 上一行会吞字,原因不明。。。
// 避免吞字,截图插入。。。
// 左边原文,右边显示
这与费马大定理有关:
当整数
时,关于
的方程
没有正整数解
那么题目就简单分四种情况:
- n>2,由费马大定理可知,不存在解
- n=0时,等式恒不成立,无解输出 -1 -1
- n=1时,可取b=1, c=a+1
- 对于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)的更多相关文章
- 寒假第一发(CF水题两个)
地址http://codeforces.com/contest/799 A. Carrot Cakes In some game by Playrix it takes t minutes for a ...
- 每日一刷(2018多校水题+2016icpc水题)
11.9 线段树 http://acm.hdu.edu.cn/showproblem.php?pid=6315 求逆序对个数 http://acm.hdu.edu.cn/showproblem.php ...
- HDU 5832 A water problem(某水题)
p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...
- hdu 2393:Higher Math(计算几何,水题)
Higher Math Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- HDU 5578 Friendship of Frog 水题
Friendship of Frog Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.ph ...
- HDU 5832 A water problem (带坑水题)
A water problem 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5832 Description Two planets named H ...
- HDU 5538 L - House Building 水题
L - House Building Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.ph ...
- 动态规划之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( ...
- <hdu - 3999> The order of a Tree 水题 之 二叉搜索的数的先序输出
这里是杭电hdu上的链接:http://acm.hdu.edu.cn/showproblem.php?pid=3999 Problem Description: As we know,the sha ...
随机推荐
- Deep Dive into Neo4j 3.5 Full Text Search
In this blog we will go over the Full Text Search capabilities available in the latest major release ...
- 火狐浏览器缓存导致JS已经改变的ID没改变
问题主要就是火狐浏览器缓存. 比如,自己写一个JS,如下: $(document).ready(function () { $("#bigRoom").live("cli ...
- grep每次读取多大的文本
Most of the tools do not actually read a single line in from a file at a time, rather they use a buf ...
- C++ SOCKET 基础编程
{ http://c.biancheng.net/socket/ }
- 尚学python课程---14、python中级语法
尚学python课程---14.python中级语法 一.总结 一句话总结: var[1:5] 访问模式:比如字符串,比如列表元祖,字典等 del 删除模式:比如列表.元祖.字典 1.Python的N ...
- 线程池_ThreadPool
using System; using System.Collections.Generic; using System.Drawing; using System.Drawing.Imaging; ...
- 获取一个数组(vector)与查找值(value)的差最小绝对值的成员索引的算法
代码如下: 函数作用:传递进来一个数组(vector),和一个需要查找的值(value),返回与value的差值绝对值最小的vector成员索引,若value不在vector范围中,则返回-1: in ...
- <数据可视化>样例+数据+画图
1 样例 1.1样例1 子图系列 from pylab import * def f(x): return np.exp(-x) * np.cos(2*np.pi*x) x1 = np.arange( ...
- <scrapy爬虫>scrapy命令行操作
1.mysql数据库 2.mongoDB数据库 3.redis数据库 1.创建项目 scrapy startproject myproject cd myproject 2.创建爬虫 scrapy g ...
- java关于lombok(包括父类参数)
java关于lombok对bean对象进行自动设置 使用说明 使用方式 注释类型 @NonNull @Data(常用) @NoArgsConstructor(常用)/@RequiredArgsCons ...
时,关于
的方程
没有正整数解