同余问题

基本定理:

若a,b,c,d是整数,m是正整数, a = b(mod m), c = d(mod m)

  1. a+c = b+c(mod m)

  2. ac = bc(mod m)

  3. ax+cy = bx+dy(mod m) -同余式可以相加

  4. ac = bd(mod m) -同余式可以相乘

  5. a^n = b^n(mod m)

  6. f(a) = f(b)(mod m)

  7. 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)

  8. if a = b(mod m) then (a,m) = (b,m)

    eg: 17 = 2(mod 5) ==> 1 = 1

  9. if ac = bc(mod m) and (c,m) = d then a = b(mod m/d)

    eg: 320 = 20(mod 100) ==> 16 = 1(mod 5)

  10. (a+b)mod m = (a mod m + b mod m)mod m

  11. (a * b)mod m = (a mod m * b mod m) mod m

  12. (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的更多相关文章

  1. HDU 1104 Remainder(BFS 同余定理)

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1104 在做这道题目一定要对同余定理有足够的了解,所以对这道题目对同余定理进行总结 首先要明白计算机里的 ...

  2. hdu 4704 同余定理+普通快速幂

    此题往后推几步就可找到规律,从1开始,答案分别是1,2,4,8,16.... 这样就可以知道,题目的目的是求2^n%Mod的结果.....此时想,应该会想到快速幂...然后接着会发现,由于n的值过大, ...

  3. 题解报告:hdu 1212 Big Number(大数取模+同余定理)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1212 Problem Description As we know, Big Number is al ...

  4. HDU-1163Eddy's digital Roots,九余定理的另一种写法!

    下午做了NYOJ-424Eddy's digital Roots后才正式接触了九余定理,不过这题可不是用的九余定理做的.网上的博客千篇一律,所以本篇就不发篇幅过多介绍九余定理了: 但还是要知道什么是九 ...

  5. OJ随笔——【1088-N!】——同余定理

    题目如下: Description 请求N!(N<=10000),输出结果对10007取余输入每行一个整数n,遇到-1结束.输出每行一个整数,为对应n的运算结果.   Sample Input ...

  6. 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 ...

  7. POJ 2635 The Embarrassed Cryptographer (千进制,素数筛,同余定理)

    The Embarrassed Cryptographer Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 15767   A ...

  8. Light oj 1214-Large Division (同余定理)

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1214 题意很好懂,同余定理的运用,要是A数被B数整除,那么A%B等于0.而A很大,那我 ...

  9. 如何运用同余定理求余数【hdoj 1212 Big Number【大数求余数】】

    Big Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

随机推荐

  1. Palindrome---poj3974(最大回文子串manacher)

    题目链接:http://poj.org/problem?id=3974 和hdu上的最长回文题一样,manacher的模板题 #include<stdio.h> #include<s ...

  2. 浅谈virtualenv(虚拟环境)

    简介 virtualenv为应用提供了隔离的Python运行环境,解决了不同应用间多版本的冲突问题. 例如: 如果我们要同时开发多个应用程序,那这些应用程序都会共用一个Python,就是安装在系统的P ...

  3. python的曲线平滑工具,及python画一条线中包含不同粗细不同颜色的画线方法

    from scipy.signal import savgol_filter import matplotlib.pyplot as plt cc = savgol_filter(c, 99, 1) ...

  4. 模块讲解----time与date time(时间模块)

    time和datetime 在python中,通常有一下几种方式来表示时间:1.时间戳:2.格式化时间字符串:3.元祖(struct_time):其中元祖(struct_time分为九个元素) UTC ...

  5. MySQL高可用架构之MHA(转)

    简介: MHA(Master High Availability)目前在MySQL高可用方面是一个相对成熟的解决方案,它由日本DeNA公司youshimaton(现就职于Facebook公司)开发,是 ...

  6. WebStorm keyboard shortcuts

    ctrl + D 向下复制 下面是Webstorm的一些常用快捷键: shift + enter: 另起一行 ctrl + alt + L: 格式化代码 control + E:  光标跳到行尾 it ...

  7. golang在线手册汇总

    1. golang官网 https://golang.org/ 2. golang中国 http://www.golangtc.com/ http://godoc.golangtc.com/pkg/ ...

  8. Delphi APP 開發入門(九)拍照與分享

    Delphi APP 開發入門(九)拍照與分享 分享: Share on facebookShare on twitterShare on google_plusone_share   閲讀次數:30 ...

  9. 在debian9上安装mongodb

    MongoDB是一个免费的开源NoSQL文档数据库,在现代Web应用程序中常用.在本教程中,您将安装MongoDB,管理其服务,并可选择启用远程访问.要遵循这个...... 介绍 MongoDB是一个 ...

  10. 成员函数查找[条款24]---《C++必知必会》

    调用一个成员函数,涉及三个步骤:第一步,编译器查找函数的名字:第二部,从可用候选者中选择最佳匹配函数:第三步,检查是否具有访问该函数的权限. #include<iostream> usin ...