poj之旅——2429,3641,1995
快速幂
3641:费马小定理,快速幂加素数判断。
1995:求几个数的幂的和的模。
2429:给出LCM和GCD,逆推a和b,巨疯狂,数据量极大,模拟过不了,可以借鉴此。
模拟型题。
参考程序:
3641:
#include <iostream>
using namespace std;
typedef long long LL;
LL mod_mult(LL a, LL b, LL m)
{LL res = 0;LL exp = a % m;while (b){if (b & 1){res += exp;if (res > m) res -= m;}exp <<= 1;if (exp > m) exp -= m;b >>= 1;}return res;
}
LL mod_exp(LL a, LL b, LL m)
{LL res = 1;LL exp = a % m;while (b){if (b & 1) res = mod_mult(res, exp, m);exp = mod_mult(exp, exp, m);b >>= 1;}return res;
}
bool is_prime(const int& n)
{for (int i = 2; i * i <= n; ++i){if (n % i == 0){return false;}}return n != 1;
}
int main()
{int p, a;while (cin >> p >> a && p && a){if (!is_prime(p) && (mod_exp(a, p, p) == a)){cout << "yes" << endl;}else{cout << "no" << endl;}}return 0;
}
1995:
#include <iostream>
using namespace std;typedef long long LL;LL mod_mult(LL a, LL b, LL m)
{LL res = 0;LL exp = a % m;while (b){if (b & 1){res += exp;if (res > m) res -= m;}exp <<= 1;if (exp > m) exp -= m;b >>= 1;}return res;
}
LL mod_exp(LL a, LL b, LL m) {LL res = 1;LL exp = a % m;while (b){if (b & 1) res = mod_mult(res, exp, m);exp = mod_mult(exp, exp, m);b >>= 1;}return res;
}
int main(int argc, char *argv[])
{int Z;cin >> Z;while (Z--){int M, H;cin >> M >> H;int ans = 0;while (H--){int A_i, B_i;cin >> A_i >> B_i;ans += mod_exp(A_i, B_i, M);}ans %= M;cout << ans << endl;}return 0;
}
poj之旅——2429,3641,1995相关推荐
- poj之旅——3050
题目描述:在5 * 5的方格里跳房子,起点是任意位置.将跳过的数连起来组成一个5位数(前导零可能),问一共能组成多少个数字? 思路:当前的状态可以定义为当前位置.当前数字长度.当前数字这三个要素,利用 ...
- 国产品牌走国际化还是规模化?
十年来,多少被我们视为骄傲的民族品牌沉沙折载.直面全球化经济和竞争,刻不容缓. 在盘点IT厂商和渠道重要公司的时 候,一个令人吃惊的结果呈现在我们眼前, 那就是,中国本土成长起来的公司所剩无 ...
- GCD and LCM Aizu - 0005(辗转相除)+GCD LCM Inverse POJ - 2429(java或【Miller Rabin素数測试】+【Pollar Rho整数分解】)
题目:GCD and LCM Aizu - 0005 Write a program which computes the greatest common divisor (GCD) and the ...
- POJ 2429 GCD LCM Inverse ★(pollard-ρ DFS枚举)
题目链接:http://poj.org/problem?id=2429 题目大意:给定gcd(a,b)和lcm(a,b)(<2^63),求a和b,如果有多种情况,输出和最小的情况. 首先gc ...
- poj 1995 Raising Modulo Numbers 二分快速幂
题意:给定n对Ai,Bi,求所有Ai的Bi次方之和对M取模的结果: 思路:二分法求快速幂: #include<cstdio> #include<cstring> #includ ...
- Pseudoprime numbers POJ - 3641(快速幂+判素数)
题意: 给你两个数,p和a:满足两个条件: 1.p不是素数: 2.apa^{p}ap %p==a; 满足则输出yes,反之输出no. 题目: Fermat's theorem states that ...
- 【POJ - 1995】Raising Modulo Numbers(裸的快速幂)
题干: People are different. Some secretly read magazines full of interesting girls' pictures, others c ...
- POJ 2429 GCD LCM Inverse
设答案为ans1,ans2 ans1=a1*gcd,ans2=a2*gcd,a1,a2互质 gcd*a1*b1=lcm,gcd*a2*b2=lcm a1*b1=lcm=(ans1*ans2)/gcd= ...
- 求余小技巧 码农场 » POJ 3641 Pseudoprime numbers 题解 《挑战程序设计竞赛》
// return (a * b) % m LL mod_mult(LL a, LL b, LL m) {LL res = 0;LL exp = a % m;while (b){if (b & ...
最新文章
- 调试JDK源码-一步一步看HashMap怎么Hash和扩容
- python_安装PIL/pillow
- mysql char(36)_MySQL中char(36)被认为是GUID导致的BUG及解决方案
- CentOS安装MySQL问题汇总
- cv2.error: OpenCV(4.4.0) C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build-wwma2wne\opencv\module
- HDU 2258 Continuous Same Game
- MATLAB模糊控制算法,驾驶员制动意图识别,Fuzzy模糊控制算法
- MLDN学习笔记 —— Annotation
- tenacity -- Python中一个专门用来retry的库
- 体声波(BAW)射频滤波器行业调研报告 - 市场现状分析与发展前景预测
- MySQL数据库基础学习小终结:连接查询、表结构的修改、约束条件、MySQL与python交互
- 三种治疗新冠肺炎中药颗粒获批上市
- ffplay 播放 USB camera
- 炎龙骑士团-黄金城之迷剧情
- 三年内赶超特斯拉?这家车企的底气是什么?
- 1114: 逆序 C语言
- 数据结构与算法(Python版) | (6) 线性结构---队列、双端队列和列表
- 10 知识问答(下)
- Linux设备驱动程序学习(十)——PCI驱动程序
- SpringCloud整合spring security+ oauth2+Redis实现认证授权