C ++ odd wicked craft skills

Author: pyj philippica
link: https: //www.zhihu.com/question/37692782/answer/73302430
Source: know almost
the copyright of all, reproduced please contact the author authorization.

1. Ultra-rapid read
now quickly read the bar has not black Technology, following the above read in hdu fast read speed faster than
const int = 120 << 20 is the BUFSIZE;
char Buf [the BUFSIZE +. 1], * buf = buf;
Template <class T>
inline void Scan (T & A) {
for (A = 0; * buf < '0' || * buf> '. 9'; buf ++);
the while (* buf> = '0' && * buf <= '. 9') = {A 10 + A * (* buf-'0 '); buf ++;}
}

fread (buf,. 1, the BUFSIZE, stdin);


2. expanding stack, the stack is limited in the Windows system, explosion how to do?
#pragma comment (linker, "/ STACK : 102400000,102400000")

when 3.vim write code to use c ++ 11 features always to be compiled together with -std = c ++ 11, directly in the program plus:
the GCC Diagnostic error #pragma "-std C ++ =. 11"

- ( "O3")) (optimize () 4. black & same type, which open O3 optimization program __attribute __

a selection of 5 2009 Luo Qiang mentioned dissertation. black Technology:


inline int abs(int x)
{
      int y = x >> 31;
      return (x + y) ^ y;
}


max function written as:
inline int max(int x, int y)
{
    return y&((x-y)>>31)|x&~((x-y)>>31);
}




Int calculate the average of two (also an integer), x, y two numbers together may overflow, can be written as:
int average = (x & y) + ((x ^ y) >> 1)


In order to make a higher cache hit ratio, to avoid the array is a power of 2 length

6.c / c ++ in braces {} and can be <%> exchange does not know that .......... What is the significance


7. use the following code is estimated that many people are already familiar with in some cases, but the first time I saw the following code or difficult to understand for a long time
#define st(x) do { x } while (__LINE__ == -1)


8. In response to the OJ, long long% lld some OJ is some% I64d, is not still a a change?
Can such clever use of macros:
#define LLD "%lld"


Such as the output can printf ( "output:" LLD " \ n", x);
then it can change only one of the

multiplying two long long 9. Then mod p, after multiplication may overflow, ancient fast power, existing rapid multiplication, the degree of popularity is now basically the technology is not too dark
long long multiply(long long x,long long y,long long p)// x * y % p
{
	long long ret = 0;
	for(; y; y >>= 1)
	{
		if(y & 1)ret = (ret + x) % p;
		x = (x + x) % p;
	}
	return ret;
}
Published 12 original articles · won praise 6 · views 20000 +

Guess you like

Origin blog.csdn.net/huaweizte123/article/details/54426438