模拟
- 输入系数可能为0,这种情况直接排除
- 次数一致的项合并,合并后可能产生系数为0的项,这种情况也要排除
- 感觉这个用map略麻烦,题解用hash数组。以后想到用map时,可以优先考虑hash数组。
#include <cstdio>
#include <algorithm>
#include <map>
using namespace std;
int main() {
int n;
map<int, int, greater<int>> func;
scanf("%d", &n);
int a, b;
for (int i = 0; i < n; i++) {
scanf("%d %d", &a, &b);
if (b >= 5 && a != 0) {
for (int k = 0; k < 5; k++) {
a *= b;
b --;
}
if (!func.count(b)) {
func[b] = a;
} else {
func[b] += a;
}
}
}
int size = func.size();
if (size == 0) {
printf("0 0");
return 0;
}
int i = 0;
for (map<int, int>::iterator it = func.begin(); it != func.end(); it++) {
if (it -> second != 0) {
printf("%d %d", it -> second, it -> first);
if (i != size - 1) printf("\n");
}
i ++;
}
}
数学
进制转换
- 负三进制转换,需要了解的编程除余知识:
n-被除数,r-除数
当r<0,n>0时,商和余数都是非负数
当r<0,n<0时,商是非负数,而余数在[r+1, 0]之间
这道题要求余数不能为负数,所以对于负的余数+r使其变化到非负值,相对的,商加一。
#include <cstdio>
#include <vector>
using namespace std;
int main() {
int n;
vector<int> v;
scanf("%d", &n);
if (n == 0) {
printf("0");
return 0;
}
int mod = 0;
while (n != 0) {
mod = n % (-3);
n = (n - mod) / (-3);
if (mod < 0) {
mod += 3;
n ++;
}
v.push_back(mod);
// printf("%d %d\n", mod, n);
}
for (int i = v.size() - 1; i >= 0; i--) {
printf("%d", v[i]);
}
}
二分法
- 寻找第一个元素x的下标,没有就返回-1
- 递增序列,则寻找从左到右,第一个满足x的元素下标
#include <cstdio>
#include <algorithm>
#define maxn 100000
using namespace std;
int n, x, a[maxn];
// 实现的是lower_bound:找出第一个大于等于x的元素下标
int binarySearch() {
int l = 0, r = n; // 左闭右闭,搜查范围是[0, n],n为找不到的情况
int mid = (l + r) / 2;
while (l < r) { // 夹出一个元素,l=r
if (a[mid] < x) { // 当mid所指元素小于x,说明第一个大于等于x的元素应该在mid的右边
l = mid + 1;
} else { // 反之,第一个大于等于x的元素在mid的左边或者为mid
r = mid;
}
mid = (l + r) / 2;
}
return l;
}
int main() {
scanf("%d %d", &n, &x);
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
int idx = binarySearch();
if (idx < n && a[idx] == x) printf("%d", idx);
else printf("-1");
}
- 寻找最后一个元素x的下标,没有就返回-1
#include <cstdio>
#define maxn 100000
using namespace std;
int n, x;
int a[maxn];
// 实现upper_bound
int BinarySearch () {
int l = 0, r = n;
int mid = (l + r) / 2;
while (l < r) {
if (a[mid] <= x) { // 第一个大于x的元素在mid的右边
l = mid + 1;
} else { // 在mid的左边
r = mid;
}
mid = (l + r) / 2;
}
return l;
}
int main() {
scanf("%d %d", &n, &x);
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
int idx = BinarySearch();
if (idx > 0 && a[idx-1] == x) printf("%d", idx-1);
else printf("-1");
return 0;
}
旋转数组🥲
int l = 0, r = n - 1, mid;
while (l < r) {
mid = (l + r) / 2;
if (a[mid] > a[r]) {
l = mid + 1;
} else {
r = mid;
}
}
求方程根
木棒切割
总结
如果l = m, r = m -1, mid = (r+m+1)/ 2 不然会死循环,干
如果l = m + 1, r = m, mid = (r+m)/ 2
双指针
归并排序
#include <cstdio>
#define maxn 1000
using namespace std;
void merge(int a[], int l1, int r1, int l2, int r2) {
int idx1 = l1, idx2 = l2, idx = 0;
int temp[maxn];
while (idx1 <= r1 && idx2 <= r2) {
if (a[idx1] < a[idx2]) {
temp[idx++] = a[idx1++];
} else {
temp[idx++] = a[idx2++];
}
}
while (idx1 <= r1) {
temp[idx++] = a[idx1++];
}
while(idx2 <= r2) {
temp[idx++] = a[idx2++];
}
for (int i = 0; i < idx; i++) {
a[l1 + i] = temp[i];
}
}
void mergesort(int a[], int left, int right) { // 用来划分的- -
if (left < right) { // 当只剩一个元素时返回
int mid = (left + right) / 2;
mergesort(a, left, mid);
mergesort(a, mid+1, right);
merge(a, left, mid, mid + 1, right);
}
}
int main() {
int n, a[maxn];
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
mergesort(a, 0, n-1);
for (int i = 0; i < n; i++) {
printf("%d", a[i]);
if (i != n-1) printf(" ");
}
}
前缀和
平衡数组
- 累加累乘这些可能会溢出,前缀和尤为注意
- 注意到a[i]的取值只有1,2,3,利用这一点简化
#include <cstdio>
#include <algorithm>
#define maxn 1000
using namespace std;
int main() {
int n, a[maxn], lefttwo[maxn], leftthree[maxn], cnt2 = 0, cnt3 = 0;
scanf("%d", &n);
fill(lefttwo, lefttwo + n, 0);
fill(leftthree, leftthree + n, 0);
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
if (a[i] == 2) {
cnt2 ++;
} else if (a[i] == 3){
cnt3 ++;
}
lefttwo[i] = cnt2;
leftthree[i] = cnt3;
}
if (lefttwo[n-1] % 2 == 1 || leftthree[n-1] % 2 == 1) {
printf("-1");
return 0;
}
for (int i = 0; i < n; i++) {
if (lefttwo[i]*2 == lefttwo[n-1] && leftthree[i]*2 == leftthree[n-1]) {
printf("%d", i);
break;
}
}
}
快速排序
#include <cstdio>
#define maxn 1000
using namespace std;
int Partition(int a[], int l, int r) {
int temp = a[l]; // 主元
while (l < r) {
while (l < r && temp < a[r]) r--; // 右边的比主元大,反之扔左边
a[l] = a[r];
while (l < r && a[l] <= temp) l++; // 左边小于等于主元,反之扔右边
a[r] = a[l];
}
a[l] = temp; // l==r,主元放置到了最终的位置上
return l;
}
void quicksort(int a[], int l, int r) {
if (l < r) { // 只有一个元素就不用排啦
int pos = Partition(a, l, r); // 得到一个元素的位置
quicksort(a, l, pos-1);
quicksort(a, pos+1, r);
}
}
int main() {
int n, a[maxn];
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
quicksort(a, 0, n-1);
for (int i = 0; i < n; i++) {
printf("%d", a[i]);
if (i != n-1) printf(" ");
}
}
数学
最大公约数
int gcd(int a, int b) {
if (b == 0) return a;
else return gcd(b, a%b);
}
最小公倍数
- a,b的最小公倍数:ab/d,d是a,b的最大公约数
- 为了防止溢出可以写成:a/d * b
素数
质因子分解
#include <cstdio>
#include <vector>
#include <utility>
using namespace std;
typedef pair<int, int> factor;
int main() {
int n;
vector<factor> v;
scanf("%d", &n);
for (int i = 2; i*i <= n; i++) { // 筛选[2, sqrt(n)]的质数
int cnt = 0;
while (n % i == 0) { // 包含了筛法,所以不用使用素数表
cnt ++;
n /= i;
}
if (cnt != 0) v.push_back(factor(i, cnt));
}
if (n != 1) { // n为质数的情形
v.push_back(factor(n, 1));
}
for (int i = 0; i < v.size(); i++) {
printf("%d %d", v[i].first, v[i].second);
if (i != (int)v.size() - 1) printf("\n");
}
}
- 求n!的质因子:n!中有(n/p + n/p ^ 2 + n/p ^ 3 + …)个质因子p
- n!的末尾有多少个零:末尾0的个数等于n!中因子10的个数,而10的个数等于n!中质因子5的个数。
组合数
#include <cstdio>
#include <cstring>
#define maxn 51
typedef long long LL;
using namespace std;
// c[i][j] = c[i-1][j] + c[i-1][j-1]:c[j/i] = c[j/i-1] + c[j-1/i-1]
// c[i][j] = c[i][n-j]:c[j/i] = c[i-j/i]
int main() {
LL n, m;
LL c[maxn][maxn];
scanf("%lld %lld", &n, &m);
// 初始化
// c[0/i] = 1, c[i/i] = 1
memset(c, 0, sizeof(c));
for (LL i = 0; i <= n; i++) {
c[i][0] = 1;
c[i][i] = 1;
}
for (LL i = 2; i <= n; i++) {
for (LL j = 0; j <= i/2; j++) {
c[i][j] = c[i-1][j] + c[i-1][j-1];
c[i][i-j] = c[i][j];
}
}
printf("%lld", c[n][m]);
}
- 组合数取模:c[m/n] % p
#include <cstdio>
#include <cstring>
#define maxn 501
typedef long long LL;
using namespace std;
// c[i][j] = c[i-1][j] + c[i-1][j-1]:c[j/i] = c[j/i-1] + c[j-1/i-1]
// c[i][j] = c[i][n-j]:c[j/i] = c[i-j/i]
int main() {
LL n, m, p;
LL c[maxn][maxn];
scanf("%lld %lld %lld", &n, &m, &p);
// 初始化
// c[0/i] = 1, c[i/i] = 1
memset(c, 0, sizeof(c));
for (LL i = 0; i <= n; i++) {
c[i][0] = 1;
c[i][i] = 1;
}
for (LL i = 2; i <= n; i++) {
for (LL j = 0; j <= i/2; j++) {
c[i][j] = (c[i-1][j] + c[i-1][j-1]) % p; // 这里取模就好
c[i][i-j] = c[i][j];
}
}
printf("%lld", c[n][m]);
}