一.求模算法
资料来源:运算规则
模运算与四则运算有点相似,except除法
(a+b)%p=(a%p+b%p)%p;
(a-b)%p=(a%p-b%p)%p;
(a·b)%p=(a%p·b%p)%p;
a ^ b % p = ((a % p)^b) % p
例题链接:Fibonacci Again
AC code
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
//(a+b)%p=(a%p+b%p)%p;
int p[1000003];
int main() {
int n;
memset(p, 0, sizeof(p));
p[0] = 7 % 3;
p[1] = 11 % 3;
int prev=0;
while (scanf("%d", &n)!= EOF) {
if (n > prev) {
for (int i = 2; i <= n; i++) {
p[i] = (p[i - 1] % 3 + p[i - 2] % 3) % 3;
}
}
if (p[n] == 0)cout << "yes\n";
else cout << "no\n";
}
return 0;
}
二.STL中好用的一些函数
-
nth_element(p,p+k,p+n)+p[k]
求出数组长度为n的数组中第k小的元素 -
lower_bound(iterator it1,iterator it2,val)
返回在有序序列中第一个大于val的迭代器
若不存在,则返回it2的地址
若要取其索引,index=lower_bound(p,p+n,val)-p;即减去起始迭代器 -
upper_bound(iterator it1,iterator it2,val)
返回在有序序列中第一个大于等于val的迭代器
若不存在,则返回it2的地址
若要取其索引,方法同上
关于他们的返回值问题,在这位博主的博客里面有较好的解释 -
next_permutation(iterator begin,iterator end)
bool类型
进行原序列的全排
若原数组有序,该函数可以输出包括原序列在内的排列
反之,则不包括
对应prev_permutation(…) -
min_element(iterator begin,iterator end)
返回该区间内最小值 -
max_element(iterator begin,iterator end)
同理
三.格式化读取字符串
int res = sscanf(cstr, "%d(%d)", &integer1, &integer2)
//若res值为2,说明读到了两个这样格式的整数
四.输入输出的优化
template<class T>
inline void read(T& x) {
x = 0;
T icon = 1;
char c = getchar();
for (; c < '0' || c>'9'; c = getchar()) {
if (c == '-') {
icon = -1;
}
}
for (; c >= '0' && c <= '9'; c = getchar()) {
x = x * 10 + (c - '0');
}
x *= f;
}
template<class T>
inline void write(T x) {
if (x < 0) {
putchar('-');
x *= -1;
}
if (x >= 10)write(x / 10);
putchar(x % 10 + '0');
}