Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
一次AC ,冲AC100 ,发帖留念220K 63MS C++ 2002B 注意 2个 int 1000000 相乘 ,就会整形溢出 ,同理 ,内部运算要设2000容量 重载运算符,+ 和-是一样的,* 就是各个非零位的循环移位相加,重载 << 符号 ,%= 就是当 a 的degree 大于b的degree时 ,a-b<<(a.degree-b.degree) 实例代码 #define MAXDEGREE 1000 //for * or << operation ,it should reserver 2*MAXDEGREE typedef bitset<2*MAXDEGREE+1> bitData; class MMP { private : bitData data; int degree ; public : MMP(){ data.reset(); degree = 0; } MMP(const bitData &a ,int aDegree){ data = a; degree = aDegree ; } MMP(const bitData &a){ data = a; for(int i=2*MAXDEGREE + 1 ;i>=0 ;i--) if(1 == data[i]) degree = i; } const MMP operator << (int pos) const{//must add const ,for cosnt mmp only call const fun bitData newData ; newData.reset(); int i; for(i = degree + pos ;i>=pos ;i--){ newData[i]= data[i-pos]; } return MMP(newData ,degree+pos) ; } const MMP operator * (const MMP &a) const{ MMP newMMP ; for(int i=0 ;i<=degree ;i++){ if(1 == data[i]){ newMMP += (a<<i) ; } } return newMMP ; } MMP& operator += (const MMP & a){ int mDegree =__max(degree ,a.degree) ; int i; for(i=0 ;i<=mDegree ;i++){ data[i] = data[i]^a.data[i] ; } degree=0;//min degree is 0!!! for(i=mDegree ;i>=0 ;i--) if(1 == data[i]){ degree =i; break; } return *this; } MMP& operator %= (const MMP &a){ while(degree >= a.degree){ *this += a<<(degree - a.degree) ; } return *this ; } void print() const{ printf("%d " ,degree+1); for(int i=degree ;i>=0 ;i--) printf("%d " ,data[i]); printf("\n"); } }; 使用时 : MMP result = MMP(f ,fn)*MMP(g ,gn) ; result %= MMP(h ,hn); result.print(); ok了 ! Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator