同余定理简单应用 - poj2769 - hdu 1021 - hdu 2035
同余问题
基本定理:
若a,b,c,d是整数,m是正整数, a = b(mod m), c = d(mod m)
a+c = b+c(mod m)
ac = bc(mod m)
ax+cy = bx+dy(mod m) -同余式可以相加
ac = bd(mod m) -同余式可以相乘
a^n = b^n(mod m)
f(a) = f(b)(mod m)
if a = b(mod m) and d|m then a = b(mod d)
eg: 320 = 20(mod 100) and d = 50 then 320 = 20(mod 50)
and d = 10 then 320 = 20(mod 10)if a = b(mod m) then (a,m) = (b,m)
eg: 17 = 2(mod 5) ==> 1 = 1
if ac = bc(mod m) and (c,m) = d then a = b(mod m/d)
eg: 320 = 20(mod 100) ==> 16 = 1(mod 5)
(a+b)mod m = (a mod m + b mod m)mod m
(a * b)mod m = (a mod m * b mod m) mod m
(a^n)mod m = (a mod m)^n mod m
同余定理应用
pku 2769
需要加一个优化,否则会超时
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
using namespace std;
int people[330];
bool vis[1000010];
bool judge[1000010];
int main()
{
int cas;
scanf("%d",&cas);
int num;
while(cas--)
{
scanf("%d",&num);
memset(judge,0,sizeof(judge));
for(int i = 0 ; i < num ; i++)
scanf("%d",&people[i]);
//剪枝
for(int i = 0 ; i < num ; i++)
for(int j = 0 ; j < num ; j++)
judge[abs(people[i]-people[j])] = 1;
//枚举
int k;
for(k = 1 ;; k++)
{
if(!judge[k])
{
bool isfind = true;
memset(vis,0,sizeof(vis));
for(int i = 0 ; i < num ; i++)
{
if(vis[people[i]%k])
{
isfind = false;
break;
}
vis[people[i]%k] = 1;
}
if(isfind)
{
printf("%d\n",k);
break;
}
}
}
}
return 0;
}
hdu 1021 Fibonacci Again
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
using namespace std;
int F[1000000+10];
void process()
{
memset(F,0,sizeof(F));
F[0] = 7%3, F[1] = 11%3;
for(int i = 2 ; i < 1000000 ; i++)
{
F[i] = (F[i-1]%3+F[i-2]%3)%3;
}
}
int main()
{
process();
int n;
while(cin >> n)
{
if(F[n])
cout << "no" << endl;
else
cout << "yes" << endl;
}
return 0;
}
hdu 2035 人见人爱A^B
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
using namespace std;
int F[1000000+10];
int process(int a, int b)
{
int ans = a;
b--;
while(b--)
{
ans = (ans%1000 * a%1000)%1000;
}
return ans;
}
int main()
{
int a, b;
while(cin >> a >> b && a)
{
cout << process(a,b) << endl;
}
return 0;
}
同余定理简单应用 - poj2769 - hdu 1021 - hdu 2035的更多相关文章
- HDU 1104 Remainder(BFS 同余定理)
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1104 在做这道题目一定要对同余定理有足够的了解,所以对这道题目对同余定理进行总结 首先要明白计算机里的 ...
- hdu 4704 同余定理+普通快速幂
此题往后推几步就可找到规律,从1开始,答案分别是1,2,4,8,16.... 这样就可以知道,题目的目的是求2^n%Mod的结果.....此时想,应该会想到快速幂...然后接着会发现,由于n的值过大, ...
- 题解报告:hdu 1212 Big Number(大数取模+同余定理)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1212 Problem Description As we know, Big Number is al ...
- HDU-1163Eddy's digital Roots,九余定理的另一种写法!
下午做了NYOJ-424Eddy's digital Roots后才正式接触了九余定理,不过这题可不是用的九余定理做的.网上的博客千篇一律,所以本篇就不发篇幅过多介绍九余定理了: 但还是要知道什么是九 ...
- OJ随笔——【1088-N!】——同余定理
题目如下: Description 请求N!(N<=10000),输出结果对10007取余输入每行一个整数n,遇到-1结束.输出每行一个整数,为对应n的运算结果. Sample Input ...
- LightOJ1214 Large Division 基础数论+同余定理
Given two integers, a and b, you should check whether a is divisible by b or not. We know that an in ...
- POJ 2635 The Embarrassed Cryptographer (千进制,素数筛,同余定理)
The Embarrassed Cryptographer Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 15767 A ...
- Light oj 1214-Large Division (同余定理)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1214 题意很好懂,同余定理的运用,要是A数被B数整除,那么A%B等于0.而A很大,那我 ...
- 如何运用同余定理求余数【hdoj 1212 Big Number【大数求余数】】
Big Number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
随机推荐
- kafka Detailed Replication Design V3
参考,https://cwiki.apache.org/confluence/display/KAFKA/kafka+Detailed+Replication+Design+V3 Major chan ...
- 设计模式——抽象工厂模式
Abstract Factory模式,即抽象工厂模式,该模式要求Factory类为抽象的,并且里面的生产出来的产品也是抽象的! 这里需要着重说的一点就是,抽象类和抽象方法的作用--规范了子类的实现,以 ...
- MySQL和时间戳有关的函数
1. MySQL 获得当前时间戳函数:current_timestamp, current_timestamp()mysql> select current_timestamp, current ...
- 剑指Offer——二叉搜索树的后序遍历序列
题目描述: 输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果.如果是则输出Yes,否则输出No.假设输入的数组的任意两个数字都互不相同. 分析: 二叉查找树(Binary Search ...
- 【react读取文件】react发送GET请求读取静态文件
react中,使用发送请求的方式把static文件夹中的前端可访问的静态文件读取成字符串: 1.new request,需要用到getRequestHeaders组件 2.fetch获取respons ...
- golang: multiple http.writeHeader calls
背景: golang的http服务,读取文件,提供给client下载时候. 出现 multiple http.writeHeader calls 错误. func DownloadFile(w htt ...
- 菜鸟也能学cocos2dx3.0 浅析刀塔传奇(下)
首先我们讲点话外的东西,异步载入:众所周知,loading里面一般都是载入数据的,那么是怎么载入的呢? Director::getInstance()->getTextureCache()-&g ...
- SSH三大框架的搭建整合(struts2+spring+hibernate)
本文转载自:https://blog.csdn.net/kyle0349/article/details/51751913
- MySQL学习之——锁(转)
锁,在现实生活中是为我们想要隐藏于外界所使用的一种工具.在计算机中,是协调多个进程或县城并发访问某一资源的一种机制.在数据库当中,除了传统的计算资源(CPU.RAM.I/O等等)的争用之外,数据也是一 ...
- MySQL,sqlalchemy
Mariadb 数据库是一堆表的集合 主键 外键 索引 安装: Centos7 [root@host]# mysqladmin -u root password "new_password& ...