![](https://img-blog.csdnimg.cn/20201014180756926.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
算法笔记
文章平均质量分 52
永夜等天明
这个作者很懒,什么都没留下…
展开
-
计算C(n,m)%p
将组合数CnmC_{n}^{m}Cnm进行质因子分解,假设分解结果为Cnm=p1c1×p2c2…×pkckC_{n}^{m}=p_1^{c_1}×p_2^{c_2}…×p_k^{c_k}Cnm=p1c1×p2c2…×pkck,那么Cnm%pC_{n}^{m}\%pCnm%p就等于p1c1×p2c2…×pkck%pp_1^{c_1}×p_2^{c_2}…×p_k^{c_k}\%pp1c1×p2c2…×pkck%p,通过快速幂来计算每一组的pici%pp_i^{c_i}\%ppic原创 2022-01-03 19:02:27 · 326 阅读 · 0 评论 -
逆元的求解(两种方法)
逆元的求解使用拓展欧几里得算法求解,使用条件是gcd(a,m)=1gcd(a,m)=1gcd(a,m)=1。int inverse(int a, int m) { int x, y; int g = exGcd(a, m, x, y); //求解ax+my=1; return (x % m + m) % m; // a模m的逆元为(x%m+m)%m}若mmm是素数,且aaa不是mmm的倍数,则可以使用费马小定理求解逆元。费马小定理设mmm是素数,aaa是任意整数且a≢0原创 2022-01-03 14:53:41 · 1224 阅读 · 0 评论 -
【PAT A1052】 Linked List Sorting
A linked list consists of a series of structures, which are not necessarily adjacent in memory. We assume that each structure contains an integer key and a Next pointer to the next structure. Now given a linked list, you are supposed to sort the structures原创 2021-04-17 21:47:14 · 71 阅读 · 0 评论 -
【PAT A1032】 Sharing
To store English words, one method is to use linked lists and store a word letter by letter. To save some space, we may let the words share the same sublist if they share the same suffix. For example, loading and being are stored as showed in Figure 1.Fi原创 2021-04-17 21:03:07 · 71 阅读 · 0 评论 -
【PAT A1060】 Are They Equal
If a machine can save only 3 significant digits, the float numbers 12300 and 12358.9 are considered equal since they are both saved as 0.123×10^5 with simple chopping. Now given the number of significant digits on a machine and two float numbers, you are s转载 2021-04-16 20:48:15 · 69 阅读 · 0 评论 -
【PAT A1059】 Prime Factors
Given any positive integer N, you are supposed to find all of its prime factors, and write them in the format N = p1^ k1p2^ k2…*pm^ km.Input Specification:Each input file contains one test case which gives a positive integer N in the range of long int.原创 2021-04-15 21:44:42 · 140 阅读 · 0 评论 -
【PAT B1013】 数素数
令 Pi表示第 i 个素数。现任给两个正整数 M≤N≤10^4,请输出 PM 到 PN的所有素数。输入格式:输入在一行中给出 M 和 N,其间以空格分隔。输出格式:输出从 PM到 PN的所有素数,每 10 个数字占 1 行,其间以空格分隔,但行末不得有多余空格。输入样例:5 27输出样例:11 13 17 19 23 29 31 37 41 4347 53 59 61 67 71 73 79 83 8997 101 103思路一:直接法。用count计数是第几个素数,用c原创 2021-03-25 17:55:25 · 113 阅读 · 0 评论 -
【PAT B1019/A1069】 The Black Hole of Numbers
For any 4-digit integer except the ones with all the digits being the same, if we sort the digits in non-increasing order first, and then in non-decreasing order, a new number can be obtained by taking the second number from the first one. Repeat in this m原创 2021-04-14 22:51:48 · 10565 阅读 · 0 评论 -
输出全排列
请编写程序输出前n个正整数的全排列(n<10),并通过9个测试用例(即n从1到9)观察n逐步增大时程序的运行时间。输入格式:输入给出正整数n(<10)。输出格式:输出1到n的全排列。每种排列占一行,数字间无空格。排列的输出顺序为字典序,…(省略)。输入样例:3输出样例:123132213231312321思路:递归实现。#include <cstdio>const int maxn = 11;int n, P[maxn], hashTable[原创 2021-04-11 22:27:48 · 510 阅读 · 0 评论 -
求第k大数
题目描述给定一个长度为n(1≤n≤1,000,000)的无序正整数序列,以及另一个数k(1≤k≤1,000,000)(关于第k大的数:例如序列{1,2,3,4,5,6}中第3大的数是4。)输入第一行两个正整数m,n。第二行为n个正整数。输出第k大的数。样例输入6 31 2 3 4 5 6样例输出4思路:随机选择算法。注意这个题定义的第k大和书上有所不同,所以在主函数调用Select函数时传入的参数是n-k+1。#include <cstdio>const int原创 2021-04-10 23:28:39 · 370 阅读 · 0 评论 -
【PAT B1045/A1101】 Quick Sort
著名的快速排序算法里有一个经典的划分过程:我们通常采用某种方法取一个元素作为主元,通过交换,把比主元小的元素放到它的左边,比主元大的元素放到它的右边。 给定划分后的 N 个互不相同的正整数的排列,请问有多少个元素可能是划分前选取的主元?例如给定 N=5, 排列是1、3、2、4、5。则:1 的左边没有元素,右边的元素都比它大,所以它可能是主元;尽管 3 的左边元素都比它小,但其右边的 2 比它小,所以它不能是主元;尽管 2 的右边元素都比它大,但其左边的 3 比它大,所以它不能是主元;类似原因,4转载 2021-04-10 22:48:41 · 133 阅读 · 0 评论 -
【PAT B1040/A1093】 Count PAT’s
The string APPAPT contains two PAT’s as substrings. The first one is formed by the 2nd, the 4th, and the 6th characters, and the second one is formed by the 3rd, the 4th, and the 6th characters.Now given any string, you are supposed to tell the number of原创 2021-04-10 20:53:08 · 92 阅读 · 0 评论 -
快速排序qsort
题目描述输入n个整数,用快速排序的方法进行排序Input第一行数字n 代表接下来有n个整数接下来n行,每行一个整数Output升序输出排序结果每行一个数据Sample Input51218141316 Sample Output1213141618 Hintn<=5000每个数据<=5000思路:参考《算法笔记》#include <cstdio>const int maxn = 5010;int A[maxn];//对区转载 2021-04-10 17:35:00 · 554 阅读 · 0 评论 -
基础排序III:归并排序
题目描述归并排序是一个时间复杂度为O(nlogn)的算法,对于大量数据远远优于冒泡排序与插入排序。这是一道排序练习题,数据量较大,请使用归并排序完成。输入第一行一个数字n,代表输入的组数其后每组第一行输入一个数字m,代表待排序数字的个数其后m行每行一个数据,大小在1~100000之间,互不相等,最多有10万个数据。输出升序输出排好序的数据,每行一个数字样例输入11010987654321样例输出12345678910思路:参考《算法笔记》转载 2021-04-10 16:58:55 · 159 阅读 · 0 评论 -
【PAT A1025】 PAT Ranking
Programming Ability Test (PAT) is organized by the College of Computer Science and Technology of Zhejiang University. Each test is supposed to run simultaneously in several places, and the ranklists will be merged immediately after the test. Now it is your原创 2021-03-28 17:21:00 · 51 阅读 · 0 评论 -
日期差值
本文内容来自《算法笔记》和codeup,仅作为学习和记录。题目描述有两个日期,求两个日期之间的天数,如果两个日期是连续的我们规定他们之间的天数为两天。输入有多组数据,每组数据有两行,分别表示两个日期,形式为YYYYMMDD输出每组数据输出一行,即日期差值样例输入2013010120130105样例输出5思路:不妨假设第一个日期早 于第二个日期(否则交换即可)。这种求日期之间相差天数的题目有一个很直接的思路,即令日期不断加1天,直到第一个日期等于第一个日期为止,即可统计出答案。原创 2021-03-26 00:52:12 · 109 阅读 · 0 评论