初等数论
ComBoat
俺好菜呀o(╥﹏╥)o
展开
-
HDU 6114 Chess
Problem Description 車是中国象棋中的一种棋子,它能攻击同一行或同一列中没有其他棋子阻隔的棋子。一天,小度在棋盘上摆起了许多車……他想知道,在一共N×M个点的矩形棋盘中摆最多个数的車使其互不攻击的方案数。他经过思考,得出了答案。但他仍不满足,想增加一个条件:对于任何一个車A,如果有其他一个車B在它的上方(車B行号小于車A),那么車A必须在車B的右边(車A列号大于車B)。 现在要问问你,满足要求的方案数是多少。 Input 第一行一个正整数T,表示数据组数。 对于每组数据:一行,两个正整数N原创 2021-02-28 20:15:16 · 79 阅读 · 0 评论 -
组合数取余
组合数C(n,m)%k的计算 方法一 递推公式 C(n,m)=C(n-1,m)+C(n-1,m-1) 一边计算一边取余 时间复杂度O(n2) 适用范围m,n<1000,k为自然数 #include<bits/stdc++.h> using namespace std; int c[1001][1001]; int main() { int n,m,k; scanf("%d%d%d",&n,&m,&k); for(int i=1;i<=n;i++原创 2021-02-28 20:07:11 · 442 阅读 · 0 评论 -
快速幂
#include<iostream> using namespace std; //(a^b)%c long long fastpower(long long a,long long b,long long c) { long long ans=1; a%=c; while(b) { if(b&1) ans=(ans*a)%c; a=(a*a)%c; b>>=1; } return ans; } int main() { long原创 2021-02-23 19:05:49 · 51 阅读 · 0 评论 -
HDU 1576 A/B(逆元)
Problem Description 要求(A/B)%9973,但由于A很大,我们只给出n(n=A%9973)(我们给定的A必能被B整除,且gcd(B,9973) = 1)。 Input 数据的第一行是一个T,表示有T组数据。 每组数据有两个数n(0 <= n < 9973)和B(1 <= B <= 10^9)。 Output 对应每组数据输出(A/B)%9973。 Sample Input 2 1000 53 87 123456789 Sample Output 7922 606原创 2021-02-21 11:41:14 · 65 阅读 · 0 评论 -
乘法逆元
乘法逆元:若a*x ≡1(mod p),且gcd(a,p)=1,则称a关于模p的乘法逆元为x。 求解方法 1.拓展欧几里得 exgcd用以求解ax+by=gcd(a,b)式的问题,当gcd(a,b)=1时, 令p=b,原式化为ax+py=1,即a*x ≡1(mod p) #include<iostream> #include<cstring> using namespace std; void exgcd(int a,int b,int &x,int &y) {原创 2021-02-21 11:35:49 · 111 阅读 · 2 评论 -
HDU 4920 Matrix multiplication
Problem Description Given two matrices A and B of size n×n, find the product of them. bobo hates big integers. So you are only asked to find the result modulo 3. Input The input consists of several tests. For each tests: The first line contains n (1≤n≤800)原创 2021-02-18 22:29:11 · 116 阅读 · 0 评论 -
质因子分解
原理 ∀N>1,若N不为素数,则有 其中p1<p2<…<pn均为素数,an均为正整数。 方法 枚举1~sqrt(n)范围内所有素数p,判断p是否为n的因子。是因子时,将该因子个数标记递增,并使n=n/p,直至不是因子为止。当上述步骤结束后,若n仍大于1,则n有且仅有一个大于sqrt(n)的因子,记录并加入数组即可。 枚举素数时首先要找出所有的素数,这里使用的是欧拉筛 点击查看素数筛 代码实现 #include<bits/stdc++.h> using names原创 2021-02-05 22:39:05 · 112 阅读 · 0 评论 -
素数筛(埃氏筛 欧拉筛)
埃氏筛 #include<bits/stdc++.h> using namespace std; int check[1000001]; int pri[1000001]; int main() { int cnt=0; int n; scanf("%d",&n); for(int i=2;i<=n;i++) { if(!check[i]) pri[++cnt]=i; for(int j=2;j*i<=n;j++) check[j*i]=1;原创 2021-02-05 22:26:12 · 91 阅读 · 0 评论 -
拓展欧几里得算法
先说一下欧几里得算法 也叫辗转相除发,用来求两个数的最大公约数 代码实现 #include<bits/stdc++.h> using namespace std; int gcd(int a,int b) { if(b==0) return a; else return gcd(b,a%b); } int main() { int a,b; cin>>a>>b; cout<<gcd(a,b); return 0; } 拓展欧几里得算法 求原创 2021-02-05 22:19:42 · 74 阅读 · 0 评论