同余定理简单应用 - 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 ...
随机推荐
- 剑指Offer——字符串的排列
题目描述: 输入一个字符串,按字典序打印出该字符串中字符的所有排列.例如输入字符串abc,则打印出由字符a,b,c所能排列出来的所有字符串abc,acb,bac,bca,cab和cba. 输入描述: ...
- Oracle 学习笔记 12 -- 序列、索引、同义词
版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/Topyuluo/article/details/24232449 数据库的对象包含:表.视图.序列. ...
- nanomsg(ZeroMQ with C)
1.应用手册 https://github.com/nanomsg/nanomsg % mkdir build % cd build % cmake .. % cmake --build . % ct ...
- nginx映射文件服务器文件夹
nginx映射文件服务器文件夹 普通用户A安装的nginx,yum源搭建文件服务器,新建普通用户B,其主目录是文件服务器需要访问的目录 普通用户A启动nginx无法访问B用户的文件服务器目录,提示40 ...
- strtoul函数的使用
函数原型: unsigned long strtoul(const char *nptr,char **endptr,int base ) 参数1:字符串起始地址参数2:返回字符串有效数字的结束地址, ...
- JAVA集合详解(Collection和Map接口)
原文地址http://blog.csdn.net/lioncode/article/details/8673391 在JAVA的util包中有两个所有集合的父接口Collection和Map,它们的父 ...
- 我与前端之间不得不说的三天两夜之jQuery
前端基础之jquery 一 jQuery是什么? [1] jQuery由美国人John Resig创建,至今已吸引了来自世界各地的众多 javascript高手加入其team. [2] jQuery是 ...
- idea 取消控制台的行数限制
有时候我们要输出大量的信息放到控制台显示,但是多了之后就出现最上面的信息被覆盖删除, 因此就需要设置控制台的显示行数,但在idea7之后的版本中,取消了对控制台行数设置选项, 只能通过更改配置文件进行 ...
- Codeforces Round #425 (Div. 2) B - Petya and Exam
地址:http://codeforces.com/contest/832/problem/B 题目: B. Petya and Exam time limit per test 2 seconds m ...
- Ubuntu 16.04 安装Postman
Ubuntu 16.04 安装Postman: 1.官网下载地址:https://www.getpostman.com/根据机器类型选择64位下载. 2.进入下载目录,解压该文件sudo tar -x ...