数论专题
数论知识点
sherry味
Keep Thinking, Keep Coding, Keep Learning!
Always Challenge Miracle.
展开
-
快速幂(递归和非递归算法)
快速幂(Exponentiation by squaring,平方求幂):简单而高效地计算方法,算法的时间复杂度是O(Log n)。例题:3的5次方如何计算呢?(a的n次方%p)方法一:3*333*3=243(进行四次连乘,复杂度为O(n-1),适用于a,n都很小的情况。方法二(递归算法):(33)(33)3=243。a的n/2次方a的n/2次方a。即a的n-1次方*a。模板:#include <bits/stdc++.h>//递归模板求快速幂;复杂度O(log n)using原创 2020-06-07 18:17:11 · 988 阅读 · 0 评论 -
组合数公式整理
转载 2020-06-07 12:13:52 · 630 阅读 · 0 评论 -
C. Kuroni and Impossible Calculation(抽屉原理)
题目题意:输入n,m;给出n个数,求任意两个数差值的绝对值modm的值。思路:直接o(n*n)会超时;若n>m,利用抽屉原理,至少有两个数同余,结果为0;否则,直接直接暴力判断。AC代码#include <bits/stdc++.h>using namespace std;#define ll long longconst int maxn=2e5+10;l...原创 2020-03-04 22:59:11 · 272 阅读 · 0 评论 -
I Older Brother(质因子分解)
题目题意:q=p^k(p为素数,k>=1)问是否能找到这样的p素数;思路:1.基本方法:数据是1e9;时间是1s,1s最多跑3e8;优化一下若是素数直接输出yes;否则利用素数筛把所有的素数存放起来,暴力试探是否q=p^k;2.质因子分解定理(算数基本定理):任何一个大于 1 的整数都能唯一分解为有限个质数的乘积。AC代码1#include <bits/stdc++....原创 2020-01-12 09:20:47 · 181 阅读 · 0 评论 -
辗转相除法gcd
1举例:比如说56和24的最大公约数,我们用辗转相除法可以这么做:56÷24=2······824÷8=3······0所以56和24的最大公约数是8。2简单的来说辗转相除法的原理就是:先比较两个数使第一个数为最大数a,第二个数为最小数b使最大数%最小数得到余数a%b=temp后将余数赋值给最小数a=temp再去除最大数b即b%a一直往复直到余数不为03代码: #inc...原创 2019-08-02 21:32:50 · 308 阅读 · 0 评论 -
D - Jumps( gcd)
A frog lives in a one-dimensional world in the point with the coordinate 0. He needs to get to the point with the coordinate x. For some reason he cannot make jumps of arbitrary length, and can jump o...原创 2019-09-27 19:40:04 · 200 阅读 · 0 评论 -
C - Fear Factoring(除法分块+等差数列求和)
AC代码#include <bits/stdc++.h>using namespace std;typedef unsigned long long ll;//必须是unsignedll f(ll n)//除法分块+等差数列求{ ll left,right; ll sum=0; for(left=1; left<=n; left=right...原创 2019-11-02 20:11:51 · 242 阅读 · 0 评论 -
E - Coprime Integers(莫比乌斯函数筛)
题目AC代码#include<iostream>//算法#include<cstdio>#include<climits>#include<algorithm>#include<cstring>typedef long long ll;using namespace std;const int maxn=1000000...原创 2019-11-02 11:39:19 · 216 阅读 · 0 评论