2014.12.27训练笔记
快速幂算法
7^13
指数部分:(13)10 == 1101
1 2 4 8 16 ……
7^1 7^2 7^4 7^8 7^16 ……
支持同余定理
然后,二进制每位询问(用位于运算符&),是1的,相乘:
比如上面的可以转化为
7^1 * 7^4 * 7^8 = 7^13
求7^132 mod 47
1 int a = 7, b = 132; 2 int rel = 1, cnt = 1; 3 while(b){ 4 if(b & 1){ 5 rel = (rel * a) % 47; 6 } 7 a *= a; 8 a %= 47; 9 b >>= 1; 10 }
快速求素数表(去掉合数)
1 #include <iostream> 2 #include <cmath> 3 #include <string.h> 4 5 #define SIZE 2000000 6 7 using namespace std; 8 9 int pr[SIZE]; 10 11 int main() 12 { 13 14 int i = 0, j = 0; 15 memset(pr, 0 ,sizeof(pr)); 16 int n = SIZE -1; 17 for(i = 2; i <=n;i++ ){ 18 if(!pr[i]){ 19 for(j = i + i; j <= n; j+=i){ 20 pr[j] = 1; 21 } 22 } 23 } 24 cout << "done"<<endl; 25 return 0; 26 }
文件读写
函数原型:freopen(const char* filesource, const char* mode, FILE* stream);
stream 可以是stdin , stdout;
多边形面积公式
S=(Σ(i=1, i <= n) (x[i] * y[i+1] - x[i+1] * y[i]) /2 ) + (x[n]*y[1] - x[1]*y[n]) / 2
S = abs(S);

浙公网安备 33010602011771号