【笔记】和算法无关的那些东东
可变参数函数
以不定参数求和为例
1 #include<bits/stdc++.h> 2 using namespace std; 3 int sum2(initializer_list<int>il){ 4 int sum=0; 5 for(auto it=il.begin();it!=il.end();it++){ 6 sum += *it; 7 } 8 return sum; 9 } 10 int sum(int count,...){ 11 va_list ap; 12 va_start(ap,count); 13 int sum=0; 14 for(int i=0;i<count;i++){ 15 sum+=va_arg(ap,int); 16 } 17 va_end(ap); 18 return sum; 19 } 20 21 int main(){ 22 cout<<sum(3,1,2,5)<<endl; 23 cout<<sum2({1,2,5})<<endl; 24 }
分数类
1 struct Rat { 2 LL a, b; 3 Rat(LL a=0):a(a),b(1) { } 4 Rat(LL x, LL y):a(x),b(y) { 5 if(b < 0) a = -a, b = -b; 6 LL d = __gcd(a, b); if(d < 0) d = -d; 7 a /= d; b /= d; 8 } 9 };
Int128
1 struct Int_128{ 2 unsigned long long a,b; 3 Int_128(ll x){a=0,b=x;} 4 friend bool operator < (Int_128 x,Int_128 y) 5 { 6 return x.a<y.a||x.a==y.a&&x.b<y.b; 7 } 8 friend Int_128 operator + (Int_128 x,Int_128 y) 9 { 10 Int_128 re(0); 11 re.a=x.a+y.a+(x.b+y.b<x.b); 12 re.b=x.b+y.b; 13 return re; 14 } 15 friend Int_128 operator - (Int_128 x,Int_128 y) 16 { 17 y.a=~y.a;y.b=~y.b; 18 return x+y+1; 19 } 20 void Div2() 21 { 22 b>>=1;b|=(a&1ll)<<63;a>>=1; 23 } 24 friend Int_128 operator * (Int_128 x,Int_128 y) 25 { 26 Int_128 re=0; 27 while(y.a||y.b) 28 { 29 if(y.b&1)re=re+x; 30 x=x+x;y.Div2(); 31 } 32 return re; 33 } 34 friend Int_128 operator % (Int_128 x,Int_128 y) 35 { 36 Int_128 temp=y; 37 int cnt=0; 38 while(temp<x)temp=temp+temp,++cnt; 39 for(;cnt>=0;cnt--) 40 { 41 if(temp<x)x=x-temp; 42 temp.Div2(); 43 } 44 return x; 45 } 46 };