七点的济南天已经蒙蒙亮了,我和sxy dalao从被窝里(不是一起)钻出来,开始一天的新生活,不过马上我们就遇到了一个难题
早饭吃什么?李xx当机立断带领我们去马路对过,然后,,一人买了两个包子(挺好吃的)然后开始了一天的学习
早上先讲的昨天晚上的题解,都写在昨天的博客里了,然后讲了快速幂
快速幂用的是一种二进制的思想,比如我们要算一个2^7,我不会告诉你我立刻口算出这个数为128
可以把它分解成2^4*2^2*2^1,比较容易理解
代码演示
int pow(int n,int b)
{
int ret=1;
while(b){
if(b&1)ret=ret=*a;
b>>=1;a=a*a;
}
return ret;
}
于是在介绍快速幂后y老师又给我们讲了几个好(e)玩(xin)的数论
求逆元:费马小定理 p为质数否则不成立
中午gdj的米饭香喷喷,奶茶的瓶子真是惊喜
下午讲了一些stl库的常用函数
pair(algorithm库)
定义:
pair<int, int> a;
pair<int, double> b;
pair<rec, int> c;
一些操作
printf("(%d,%d)\n",a.first,a.second);
a = make_pair(5,6);//给pair赋值
printf("(%d,%d)\n",a.first,a.second);
a.first = 3;//更改值
printf("(%d,%d)\n",a.first,a.second);
a.second = 9;
printf("(%d,%d)\n",a.first,a.second);
b = make_pair(5,0.7);
printf("(%d,%lf)\n",b.first,b.second);
c = make_pair( (rec){2, 3.8}, 5 );//结构体赋值
printf("(%d,%lf) %d\n",c.first.x,c.first.y,c.second);
vector动态数组
#include<cstdio>
#include<vector>
using namespace std;
#define For(i,l,r) for(int i=l;i<=r;i++)
vector <int> a;
int main(){
a.push_back(2);
a.push_back(23);
a.push_back(233);
a.push_back(2333);
a.push_back(23333);//压入元素
printf("a.size %d\n",a.size());//返回现在数组中有几个元素
int len = a.size();
For(i,0,len-1) printf("%d ",a[i]); printf("\n"); //长度-1
a.pop_back();//删除顶元素
printf("a.size %d\n",a.size());
len = a.size();
For(i,0,len-1) printf("%d ",a[i]); printf("\n");
a.clear();//清空
printf("a.size %d\n",a.size());
len = a.size();
For(i,0,len-1) printf("%d ",a[i]); printf("\n");
}
优先队列
priority_queue <int, vector<int>, greater<int> > Q;
int main(){
Q.push(15);
Q.push(10);
Q.push(5);
Q.push(20);
printf("%d\n",Q.top());
Q.pop();
printf("%d\n",Q.top());
Q.push(30);
printf("%d\n",Q.top());
}
队列
#include<cstdio>
#include<queue>
#include<algorithm>
using namespace std;
#define For(i,l,r) for(int i=l;i<=r;i++)
queue <int> a;
int main(){
a.push(1);
a.push(2);
a.push(3);
a.push(4);
a.push(5);
printf("%d\n",a.front());
a.pop(); a.pop();
printf("%d\n",a.front());
printf("%d\n",a.size());
while(!a.empty()){
a.pop();
}
printf("%d\n",a.size());
}
栈
#include<cstdio>
#include<stack>
#include<algorithm>
using namespace std;
#define For(i,l,r) for(int i=l;i<=r;i++)
stack <int> a;
int main(){
a.push(1);
a.push(2);
a.push(3);
a.push(4);
a.push(5);
printf("%d\n",a.top());
a.pop(); a.pop();
printf("%d\n",a.top());
printf("%d\n",a.size());
while(!a.empty()){
a.pop();
}
printf("%d\n",a.size());
/* top = 0;
stk[++top] = ;
stk[top]*/
}
stl自带排序
#include<cstdio>
#include<algorithm>
using namespace std;
#define For(i,l,r) for(int i=l;i<=r;i++)
struct rec{
int id, height, weight;
char *name;
}c[105];
bool cmp(rec a, rec b){
return a.weight < b.weight;
}
pair<int, int> a[1000];
int main(){
int n = 4;
For(i,1,n) scanf("%d%d",&a[i].first,&a[i].second);
sort(a+1,a+1+n); reverse(a+1,a+1+n);
For(i,1,n) printf("(%d,%d) ",a[i].first,a[i].second);
/* For(i,1,n) scanf("%d%d",&c[i].id,&c[i].weight);
sort(c+1, c+1+n, cmp);
For(i,1,n) printf("(%d,%d)\n",c[i].id,c[i].weight);*/
}
/*
a < b
bool cmp(){
if(a.first == b.first){
return a.second < b.second;
}
return a.first < b.first;
}
bool ret = (a.first == b.first ? a.second < b.second : a.first < b.first)
*/
结构体骚操作
#include<cstdio>
#include<queue>
#include<algorithm>
using namespace std;
#define For(i,l,r) for(int i=l;i<=r;i++)
struct rec {
int x,y;
};
struct cmp {
bool operator () (const rec &A, const rec &B) const {
return abs(A.x-A.y) < abs(B.x-B.y);
}
};
priority_queue <rec, vector<rec>, cmp > Q;
int main() {
Q.push( (rec) {
3,9
} );
Q.push( (rec) {
2,6
} );
Q.push( (rec) {
1,9
} );
Q.push( (rec) {
4,7
} );
printf("top (%d,%d)\n",Q.top().x,Q.top().y);
Q.pop();
printf("top (%d,%d)\n",Q.top().x,Q.top().y);
}