NOIP card tricks constant

Gangster taken from someone's blog ( http://www.cnblogs.com/widerg/p/7353866.html )
1. IO optimization
fread and fwrite, if you want to have a re-optimization mmap .... (But do not use, seems useless ...)
read optimization (this is very important !!!!!!!)

inline int Read()
{
    int x=0,f=1;char c=getchar();
    while(c>'9'||c<'0') {if(c=='-') f=-1;c=getchar();}
    while(c>='0'&&c<='9') {x=x*10+c-'0'; c=getchar();}
    return x*f;
}

Another course, but this only applies to non-negative read as follows:

#inline int read(){
    int num;
    char ch;
    while((ch=getchar())<'0' || ch>'9');
    num=ch-'0';
    while((ch=getchar())>='0' && ch<='9'){
        num=num*10+ch-'0';
    }
    return 0;
}

Output Optimization:

inline void out(int x){
    if(x>=10){
        out(x/10);
    }
    putchar(x%10+'0');
}

I have your attention quickly read and quickly as possible burst int output data to try not to use my original flash output is lost but using printf WA no problem
2inline
before declaring write inline function modifier (as above Read ( ) the same), it can speed up function calls, but only for some simple manipulation functions. Involves very complex recursive function, the large circulation, the compiler will automatically ignore inline.
3register
before the definition of the variable write register modifier for variables into the CPU registers, apply some frequently used variables:

register int n,m;

Space is limited register, if you put too many variables, the extra variables will be placed in the general memory;
fast, is not an ordinary fast, approaching what extent? :

register int a=0;
for(register int i=1;i<=999999999;i++)
a++;
int a=0;
for(int i=1;i<=999999999;i++)
a++;

Results:
Optimization: 0.2826 second
does not optimize: 1.944 second
horror ah! ! ! !
4 Expand loop
unrolling perhaps only surface, in the case of a cache and internal registers allow large expansion computation statement stimulates CPU concurrently (provided that your CPU is not a CPU) ...
. 5 modulo optimization (only O2)

//设模数为 mod
inline int inc(int x,int v,int mod){x+=v;return x>=mod?x-mod:x;}//代替取模+
inline int dec(int x,int v,int mod){x-=v;return x<0?x+mod:x;}//代替取模-

6 Front ++

后置 ++ 需要保存临时变量以返回之前的值,在 STL 中非常慢。事实上,int 的后置 ++ 在实测中也比前置 ++  0.5 倍左右(UOJ 上自定义测试)

7 Do not open bool, bool changed all char, int is the fastest (for unknown reasons).
8if () else statement than ()? () :() statement to be slow, comma operator to operator number score fast.
9 instead of an array data structure with a pointer (not matter personally feel)

数组在用方括号时做了一次加法才能取地址!
所以在那些计算量超大的数据结构中,你每次都多做了一次加法!!!在 64 位系统下是   long long 相加,效率可想而知。

Thank God for the unknown Ben

Published 41 original articles · won praise 58 · views 60000 +

Guess you like

Origin blog.csdn.net/a1351937368/article/details/78162078