【注意数据范围】
大数用__int64 a;(横线是两行)
printf(“%I64d”,a);
【memset函数】
头文件#include<string.h>
给数组快速赋值,值一般是0,-1
格式:
int a[N];
memset(a,0,sizeof(a));
将数组a中所有元素赋为0.(二维数组也可以)
【素数筛选法】
#define N 10000000
int f[N+10];
void Getprime()
{
memset(f,0,sizeof(f));
f[1]=1; //1不是素数,特殊处理下
for(int i=2;i*i<=N;i++)
{
if(f[i] == 1)
continue;
for(int j=i;j*i<=N;j++)
f[j*i]=1;
}
}
//筛选完后,f[i]=0,表示i为素数,f[i]=1,表示i不是素数。
*注意:素数筛选法一般只需要执行一次即可得到数组f,而不需要每次都执行,否则会超时。
【数学函数】
1.pow函数
//要加入头文件 math.h
pow(x,y);//其作用是计算x的y次方。x、y及函数值都是double型
2.double
const
Pi =
acos
(-1);
【sort的使用】
#include<stdio.h>
#include<algorithm>
using namespace std;//注意其头文件,勿漏
bool cmp(int a,int b)
{
//按从大到小排序
if(a > b) return true;
return false;
//按从小到大排序
//if(a < b) return true;
//return false;
}
int main()
{
int f[]={2,4,3,1,9,5,6,0,8,7};
int n=10;
//将数组f前n项进行排序
//sort(f,f+n); //默认按从小到大排序
sort(f,f+n,cmp); //cmp内自己定义排序规则
for(int i=0;i<n;i++)
printf("%d\n",f[i]);
return 0;
}
//结构体延伸
struct Node{
int x,y;
};
Node p[1010];
//先按x从小到大排序,如果x相同则按y从大到小排序
bool cmp(Node a,Node b)
{
if(a.x < b.x) return true;
if(a.x == b.x && a.y > b.y) return true;
return false;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int i,n;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d%d",&p[i].x,&p[i].y);
sort(p,p+n,cmp);
for(i=0;i<n;i++)
printf("%d %d\n",p[i].x,p[i].y);
}
return 0;
}
【最大公约数】
int gcd(int a,int b){return b?gcd(b,a%b):a;}
【取余公式 bjfuoj-1070】
(a+b)%c = (a%c+b%c)%c
(a*b)%c = ((a%c)*(b%c))%c
【快速幂取余】
BJFUOJ 1056
快速幂模板
__int64 powhaha(__int64 n,__int64 m)
{
__int64 ans=1;
while(m>0)
{
if(m&1)
{
ans*=n;
}
n*=n;
m>>1;
}
return ans;
}
【矩阵快速幂】
BJFUOJ 1440
http://blog.csdn.net/y990041769/article/details/39694583
struct Node //矩阵
{
__int64 line,cal;
__int64 a[N+1][N+1];
Node(){
line=2,cal=2;
a[0][0] = 2; a[0][1] = 1;
a[1][0] = 2; a[1][1] = 0;
}
};
Node isit(Node x,__int64 c) //矩阵初始化
{
for(__int64 i=0;i<N;i++)
for(__int64 j=0;j<N;j++)
x.a[i][j]=c;
return x;
}
Node Matlab(Node x,Node s) //矩阵乘法
{
Node ans;
ans.line = x.line,ans.cal = s.cal;
ans=isit(ans,0);
for(__int64 i=0;i<x.line;i++)
{
for(__int64 j=0;j<x.cal;j++)
{
for(__int64 k=0;k<s.cal;k++)
{
ans.a[i][j] += x.a[i][k]*s.a[k][j];
ans.a[i][j]=(ans.a[i][j]+M)%M;
}
}
}
return ans;
}
__int64 Fast_Matrax(__int64 n) //矩阵快速幂
{
if(n==1)
return 3;
n-=2;
__int64 x=1,f=n,ok=1;
Node ans,tmp,ch;
ans.line = 1,ans.cal = 2;
ans.a[0][0] = 8, ans.a[0][1] = 3 ;
while(n>0)
{
if(n%2)
{
ans=Matlab(ans,tmp);
}
tmp=Matlab(tmp,tmp);
n/=2;
}
return ans.a[0][0];
}
直接输出
printf("%I64d\n",Fast_Matrax(n));
【sprintf 和 sscanf 函数】
sprintf是把格式化的数据写入某个字符串缓冲区。也就是将一些东西输出到字符串,其用法是sprintf(buf,"%d%d%d%d%d",a,b,c,d,e);
<span style="font-size:18px;">例子:char* who = "I"; char* whom = "CSDN"; sprintf(s, "%s love %s.", who, whom); //产生:"I love CSDN. " 这字符串写到s中 sprintf(s, "%10.3f", 3.1415626); //产生:" 3.142"</span>
sscanf()是将一些东西输入到字符串,利用它可以从字符串中取出整数、浮点数和字符串等等
#include <cstdio>
int main()
{
char str[512] = {0};
sscanf("123456 ", "%s", str); //读出字符串中的字符
printf("str=%s\n", str);
sscanf("123456 ", "%4s", str); //读出其中的前n个
printf("str=%s\n", str);
sscanf("12345 abcdef","%[^e],str"); //读到某个函数为止
printf("str=%s\n",str);
sscanf("123456abcFDSdedfBCDEF", "%[1-9a-z]", str); //读出其中特定的字符
printf("str=%s\n", str);
sscanf("123456abcHdedfBCDEF", "%[^A-Z]", str); //读到某个字符为止
printf("str=%s\n", str);
return 0;
}
【字符串流函数】
将一个字符串转化为数:
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
template <class T>
inline T fromString(const string &str)
{
istringstream is(str);
T v;
is>>v;
return v;
}
int main()
{
string s="465153";
int a=fromString<int>(s);
cout<<a<<endl;
return 0;
}
将一个值转化为string字符
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
template <class T>
inline string toString(const T &v)
{
ostringstream os;
os << v;
return os.str();
}
int main()
{
string s;
int a=415641;
s=toString(a);
cout<<a<<endl;
return 0;
}