void BS(int l,int r,vector<Item> &vs){ //答案該<l會有的已經做完了 if(l==r)整個vs的答案=l;//?????? int mid=(l+r)/2; do_thing(l,mid);//做答案<=mid會做的事 vector<Item> left=vs裡滿足的; vector<Item> right=vs-left; undo_thing(l,mid); BS(l,mid,left); do_thing(l,mid); BS(mid+1,r,right);//?????? }
vector<int> searchRange(vector<int>& nums, int target) { vector<int> range(2,-1); int low = BS(nums, target); int high = BS(nums, target+1); high = nums[high] == target? high: high-1; //In case, the vector nums only contains target (Ex. [8,8,8], 8) //The output index of 9 is located at the rightmost of 8. if (nums[low] != target || nums[high] != target) return range; range[0] = low; range[1] = high; return range; }
Vect BlackScholes(int N) {// Black-Scholes: dX = x(r*dt + sigma dW) // Euler Method: S_{n+1}=S_n(1 + r*d_t + sigma*g_n*d_t^{1/2}) // d_t = 1, r, g_n const Vect BS(N); BS(0)=1.; double d_t = 0.01; double b = 1; double sigm = 1.; for ( int i = 1; i < N; ++i ){ BS(i) = BS(i-1)*(1. + b * d_t + sigm * Gaussian( 0., 1. )*pow(d_t,0.5)); } return BS; }
bool BS(int *arr, int key, int left, int right) { if(right >= left) { int mid = left + (right - left) / 2; if(arr[mid] == key) return 1; if(arr[mid] > key) return BS(arr, key, left, mid-1); else return BS(arr, key, mid+1, right); } return 0; }
void create() { ::create(); SetProp(P_SHORT, "Ein kleiner Pixie"); SetProp(P_SIZE, 55+random(11)); SetProp(P_LONG, BS( "Der Pixie ist etwa "+QueryProp(P_SIZE)+"cm gross und sieht vermutlich einem " +"kleinen pumeligen Menschenkind am aehnlichsten. Wie alle Pixies scheint er " +"sehr verspielt zu sein und nichts als Unfug im Kopf zu haben, Du solltest " +"Dich also vor ihm ihn acht nehmen. Auch wenn er aussieht wie ein Kind, sich " +"benimmt wie ein Kind, so kann er Dir mit seiner Magie sicher uebel zu spieln.")); SetProp(P_INFO, /* kleine Warnung fuer die Kaempfer :) */ "Man sollte sich immer vor der Magie der Pixies in acht nehmen, sie ist nicht\n" +"unbedingt gefaehrlich, aber es koennen die unerwartesten Dinge geschehn. So\n" +"ist durchaus von Leuten bekannt die einen Kampf als kleine Ratte beendeten,\n" +"oder deren Schwert im Kampf ploetzlich zu Scheisse zerfloss...\n"); SetProp(P_NAME_ADJ, "klein"); SetProp(P_NAME, "Pixie"); SetProp(P_GENDER, MALE); SetProp(P_RACE, "pixie"); SetProp(P_ATTRIBUTES, (["int":30,"con":20,"str":15,"dex":30]) ); SetProp(P_LEVEL, 20); SetProp(P_MAX_HP, 600); SetProp(P_HP, 600); SetProp(P_MAX_SP, 800); SetProp(P_SP, 800); SetProp(P_ALIGN, 250); SetProp(P_HANDS, ({" mit seinen kleinen Haenden", 150, DT_BLUDGEON}) );
bool Method1Search(int *arr, int key, int left, int right) { int pivot = Method1FindPivot(arr, left, right); if(pivot == -1) // no rotation { return BS(arr, key, left, right); } else // with rotation { bool sub1 = BS(arr, key, left, pivot); bool sub2 = BS(arr, key, pivot+1, right); return sub1 || sub2; } }
void Query(int x) { int k, cur; k = BS(x); cur = nodep[k]; splay(cur, 0); printf("%d\n", size[left[cur]] + 1); }
//--------------------------------------------------------------------------- // ADIF header, 7 or 9 bytes // syncword 12 bits // id (Mpeg version) 1 bit (0=MPEG-4, 1=MPEG-2) // layer 2 bits (should be 0) // protection_absent 1 bit // profile 2 bits // sampling_frequency_index 4 bits // private 1 bit // channel_configuraton 3 bits // original 1 bit // home 1 bit // copyright_id 1 bit // copyright_id_start 1 bit // aac_frame_length 13 bits // adts_buffer_fullness 11 bits // num_raw_data_blocks 2 bits // CRC_check 16 bits (if protection_absent==0) void File_Aac::ADTS () { //Parsing BitStream BS(Buffer+Buffer_Offset, Buffer_Size-Buffer_Offset); BS.Skip(12); //syncword, 0xFFF size_t id=BS.Get(1); //id BS.Skip(2); //layer size_t protection_absent=(bool)BS.Get(1); //protection_absent size_t profile=BS.Get(2); //profile size_t sampling_frequency_index=BS.Get(4); //sampling_frequency_index BS.Skip(1); //private size_t channel_configuraton=BS.Get(3); //Channels BS.Skip(1); //original BS.Skip(1); //home BS.Skip(1); //copyright_id BS.Skip(1); //copyright_id_start size_t aac_frame_length=BS.Get(13); //aac_frame_length size_t adts_buffer_fullness=BS.Get(11); //adts_buffer_fullness BS.Skip(2); //num_raw_data_blocks if (protection_absent) BS.Skip(16); //CRC_check //Details if (Config.Details_Get()) { Details_Add_Element(0, _T("AAC")); Details_Add_Info(0, "id", id); Details_Add_Info(0, "protection_absent", protection_absent); Details_Add_Info(0, "profile", profile); Details_Add_Info(0, "sampling_frequency_index", sampling_frequency_index); Details_Add_Info(0, "channel_configuraton", channel_configuraton); Details_Add_Info(0, "aac_frame_length", aac_frame_length); Details_Add_Info(0, "adts_buffer_fullness", adts_buffer_fullness); } //Filling Stream_Prepare(Stream_General); Fill("Format", "AAC"); Stream_Prepare(Stream_Audio); Fill("Codec", ADTS_Profile[profile]); Fill("SamplingRate", ADTS_SamplingRate[sampling_frequency_index]); Fill("Channel(s)", channel_configuraton); if (adts_buffer_fullness==0x7FF) Fill("BitRate_Mode", "VBR"); else Fill("BitRate_Mode", "CBR"); Fill("Resolution", 16); //size_t FramePerSecond=(float)SamplingRate/1024; //For raw AAC, there are always 1024 samples per frame (but 960 in MP4?) //size_t BytesPerFrame=FileSize/(float)(FrameCount*1000); //BitRate=8*BytesPerFrame*FramePerSecond+0.5); }
//--------------------------------------------------------------------------- // AAC in ES, SBR part, 3 bytes // Sync 11 bits (0x2B7) // sbr_object_type 5 bits // has_sbr 1 bit // SamplingRateIndex 4 bits (only if has_sbr==0) // SamplingRate 24 bits (only if has_sbr==0 && SamplingRateIndex==0xF) void File_Aac::ES_SBR () { BitStream BS(Buffer+Buffer_Offset, Buffer_Size-Buffer_Offset); size_t Sync=BS.Get(11); if (Sync!=0x2B7) return; size_t ObjectType=BS.Get(5); size_t HasSBR=BS.Get(1); size_t SamplingRate_Index=0; size_t SamplingRate=0; if (HasSBR) { SamplingRate_Index=BS.Get(4); if (SamplingRate_Index==0xF) SamplingRate=BS.Get(24); else SamplingRate=ADTS_SamplingRate[SamplingRate_Index]; } //Details if (Config.Details_Get()) { Details_Add_Info(0, "Sync", Sync, 16); Details_Add_Info(0, "ObjectType", ObjectType); Details_Add_Info(0, "HasSBR", HasSBR); if (HasSBR) { Details_Add_Info(0, "SamplingRateIndex", SamplingRate_Index); if (SamplingRate_Index==0xF) Details_Add_Info(0, "SamplingRate", SamplingRate); } } //Filling if (HasSBR) { (Stream[StreamKind_Last]->at(StreamPos_Last))(_T("Codec")).append(_T("/SBR")); Fill("SamplingRate", ""); //Clear it Fill("SamplingRate", ADTS_SamplingRate[SamplingRate_Index]); } //Next element Buffer_Offset+=BS.Offset_Get(); //SBR-PS stuff if (Buffer_Size-Buffer_Offset>=2) { ES_SBR_PS(); } }
void Terminal::printEditLine(LineEditor &editor, bool erase_char) { CR(); displayPrompt(); // print the new edit line const char *buf = editor.buf().c_str(); while (*buf) { putch(*buf++); } // erase the extra character if the user shortened the input line if (erase_char) { putch(' '); BS(); } repositionPrinter(editor); }
int BS(int* arr,int left,int right){ if(left<=right){ if(arr[left]==left){ return left; } int mid=(left+right)/2; int l=BS(arr,left,mid-1); if(l!=-1){ return l; }else{ if(arr[mid]==mid){ return mid; }else{ return BS(arr,mid+1,right); } } } return -1; }
void Top(int x) { int k, cur; k = BS(x); cur = nodep[k]; splay(cur, 0); if(left[T] == 0 || right[T] == 0) { T = left[T] + right[T]; pre[T] = 0; } else Delete(T, 0); Insert(T, k, 0); splay(node, 0); }
void create() { ::create(); customizeMe(GLOCKENBLUME); SetProp(P_NAME, "Glockenblume"); SetProp(P_NAME_ADJ, "schoen"); SetProp(P_GENDER, FEMALE); SetProp(P_LONG, BS( "Die Bluetensterne in blau-violett sind ein toller Blickfang, wie " +"sie am langen Stiel im Wind hin- und hernicken. Die " +"glockenfoermige Blumenkrone mit ihren fuenf breiten, nicht bis zur " +"Mitte reichenden Zipfeln erfreut Dich mit ihrer Schoenheit.")); SetProp(PLANT_ROOMDETAIL, "Eine anmutige, schoene Glockenblume nickt dir auffordernd zu. Ob man sie " +"pfluecken kann?\n"); SetProp(P_SHORT, "Eine Glockenblume"); AddId(({ "blume", "glockenblume" }));
//--------------------------------------------------------------------------- // ADIF header // "ADIF" 32 bits // Copyright_Present 1 bit // Copyright_ID 70 bits (only if Copyright_Present=true) // Original 1 bit // Home 1 bit // Home 1 bit // BitStream_Type 1 bit (0=CBR, 1=VBR) // BitRate 23 bits (For CBR: bitrate, for VBR: peak bitrate, 0 means unknown) // num_program_config_elements 4 bits (the next 2 fields come num_program_config_elements+1 times) // buffer_fullness 20 bits (only if bitstream_type==0) // program_config_element VAR // ... // void File_Aac::ADIF () { //Parsing BitStream BS(Buffer+Buffer_Offset, Buffer_Size-Buffer_Offset); BS.Skip(32); //ID: "ADIF" size_t Copyright_Present=(bool)BS.Get(1); //Copyright_Present if (Copyright_Present) {BS.Skip(32); BS.Skip(32); BS.Skip(8);} //Copyright_ID BS.Skip(1); //Original BS.Skip(1); //Home size_t BitStream_Type=BS.Get(1); //BitStream_Type size_t BitRate=BS.Get(23); // //Details if (Config.Details_Get()) { Details_Add_Element(0, _T("AAC")); Details_Add_Info(0, "Copyright_Present", Copyright_Present); Details_Add_Info(0, "BitStream_Type", BitStream_Type); Details_Add_Info(0, "BitRate", BitRate); } //Filling Stream_Prepare(Stream_General); Fill ("Format", "AAC"); Stream_Prepare(Stream_Audio); Fill ("Codec", "AAC"); if (BitStream_Type==0) Fill("BitRate_Mode", "CBR"); else Fill("BitRate_Mode", "VBR"); if (BitRate>0) { if (BitStream_Type==0) //CBR Fill("BitRate", BitRate); else //VBR Fill("BitRate_Max", BitRate); } Fill("Resolution", 16); }
void get_envE(unsigned char ** buf, const unsigned char * len, size_t lenlen, const char * name) { Env(name); assume_intype("bitstring"); Dup(); Len(); BS(false, lenlen); Done(); // Assume that variable length fits in its bitstring representation. assume_intype("bitstring"); StoreBuf(len); fresh_ptrE(len, lenlen); StoreBuf(buf); StoreBuf(*buf); }
void readenvE(const unsigned char * buf, const unsigned char * len, size_t lenlen, const char * name) { if(lenlen == 0) { proxy_fail("readenvE: you certainly don't want lenlen = 0\n"); } Env(name); assume_intype("bitstring"); Dup(); Len(); BS(false, lenlen); Done(); // Assume that variable length fits in its bitstring representation. assume_intype("bitstring"); if(len != NULL) StoreBuf(len); else Clear(1); StoreBuf(buf); }
int foo4 (uint64_t a, uint64_t b) { if (BS (a) != BS (b)) return 1; return 0; }
int foo3 (uint64_t a, uint64_t b) { if (BS (a) == BS (b)) return 1; return 0; }
int foo2 (uint64_t a) { if (BS (a) != 0xA00000000) return 1; return 0; }
return input == NULL ? false : true; } bool Use_LCG(_RNG_Parameters_){ LC_RNG rng(seed); rng.GenerateBlock(input,size); return input == NULL ? false : true; } /************************库信息说明*****************************/ //其他数值分别是BLOCK,STREAM,HASH,MAC,RNG extern const int DLL_ALGORITHM_TYPE = 0; /************************算法信息*****************************/ //数组AMOUNT中第一个数为分组密码算法的数量,以后依次为流密码,HASH,MAC,RNG的数量 extern const int AMOUNTS[] = {21,6,18,6,2}; extern const Cipher CIPHERS[] = { BlockInfo("AES",KL(AES),BS(AES)),BlockInfo("Blowfish",KL(Blowfish),BS(Blowfish)), BlockInfo("Camellia",KL(Camellia),BS(Camellia)),BlockInfo("CAST256",KL(CAST256),BS(CAST256)), BlockInfo("DES",KL(DES),BS(DES)),BlockInfo("MARS",KL(MARS),BS(MARS)), BlockInfo("IDEA",KL(IDEA),BS(IDEA)),BlockInfo("RC5",KL(RC5),BS(RC5)), BlockInfo("RC6",KL(RC6),BS(RC6)),BlockInfo("SEED",KL(SEED),BS(SEED)), BlockInfo("Serpent",KL(Serpent),BS(Serpent)),BlockInfo("SHACAL2",KL(SHACAL2),BS(SHACAL2)), BlockInfo("SKIPJACK",KL(SKIPJACK),BS(SKIPJACK)),BlockInfo("TEA",KL(TEA),BS(TEA)), BlockInfo("Twofish",KL(Twofish),BS(Twofish)),BlockInfo("XTEA",KL(XTEA),BS(XTEA)), BlockInfo("DES_EDE3",KL(DES_EDE3),BS(DES_EDE3)),BlockInfo("GOST",KL(GOST),BS(GOST)), BlockInfo("SHARK",KL(SHARK),BS(SHARK)),BlockInfo("Square",KL(Square),BS(Square)), BlockInfo("ThreeWay",KL(ThreeWay),BS(ThreeWay)), StreamInfo("Sosemanuk",KL(Sosemanuk),IL(Sosemanuk)), StreamInfo("Salsa20",KL(Salsa20),IL(Salsa20)), StreamInfo("XSalsa20",KL(XSalsa20),IL(XSalsa20)), StreamInfo("SEAL",KL(SEAL<>),IL(SEAL<>)),
void Terminal::repositionPrinter(LineEditor &editor) { size_t back = editor.buf().length() - editor.cursor(); while (back) { BS(); back--; } }
uint64_t foo1 (uint64_t a) { return BS (~ BS (a)); }
uint64_t foo7 (uint64_t a, uint64_t b) { return BS (BS (a) ^ BS (b)); }
uint64_t foo6 (uint64_t a, uint64_t b) { return BS (BS (a) | BS (b)); }
uint64_t foo5 (uint64_t a, uint64_t b) { return BS (BS (a) & BS (b)); }
uint64_t foo4 (uint64_t a) { return BS (BS (a) ^ 0xA00000000); }
uint64_t foo3 (uint64_t a) { return BS (BS (a) | 0xA00000000); }
int foo1 (uint64_t a) { if (BS (a) == 0xA00000000) return 1; return 0; }
void test(Tp proto = Tp{}) { const auto _S_max_log = __gnu_cxx::__log_max(proto); std::cout.precision(__gnu_cxx::__digits10(proto)); auto w = 8 + std::cout.precision(); __gnu_cxx::_BasicSum<Tp> BS; __gnu_cxx::_AitkenDeltaSquaredSum<__gnu_cxx::_BasicSum<Tp>> ABS; __gnu_cxx::_AitkenDeltaSquaredSum<__gnu_cxx::_KahanSum<Tp>> AKS; __gnu_cxx::_WinnEpsilonSum<__gnu_cxx::_BasicSum<Tp>> WBS; __gnu_cxx::_WinnEpsilonSum<__gnu_cxx::_KahanSum<Tp>> WKS; __gnu_cxx::_BrezinskiThetaSum<__gnu_cxx::_BasicSum<Tp>> BTS; __gnu_cxx::_LevinTSum<__gnu_cxx::_BasicSum<Tp>> LTS; __gnu_cxx::_WenigerDeltaSum<__gnu_cxx::_BasicSum<Tp>> WDS; __gnu_cxx::_WenigerDeltaSum<__gnu_cxx::_VanWijngaardenSum<Tp>> WDvW; auto s = Tp{1.2}; auto zetaterm = [s, _S_max_log](std::size_t k) -> Tp { return (s * std::log(Tp(k + 1)) < _S_max_log ? std::pow(Tp(k + 1), -s) : Tp{0}); }; auto VwT = __gnu_cxx::_VanWijngaardenCompressor<decltype(zetaterm)>(zetaterm); //auto zeta = Tp{5.591582441177750776536563193423143277642L}; std::cout << "\n\nzeta(1.2) = 5.59158244117775077653\n"; std::cout << std::setw(w) << "k" << std::setw(w) << "Basic" << std::setw(w) << "Aitken-Basic" << std::setw(w) << "Aitken-Kahan" << std::setw(w) << "Winn-Basic" << std::setw(w) << "Winn-Kahan" << std::setw(w) << "BrezinskiT-Basic" << std::setw(w) << "LevinT-Basic" << std::setw(w) << "WenigerD-Basic" << std::setw(w) << "WenigerD-vW" << '\n'; std::cout << std::setw(w) << "----------------" << std::setw(w) << "----------------" << std::setw(w) << "----------------" << std::setw(w) << "----------------" << std::setw(w) << "----------------" << std::setw(w) << "----------------" << std::setw(w) << "----------------" << std::setw(w) << "----------------" << std::setw(w) << "----------------" << std::setw(w) << "----------------" << '\n'; for (auto k = 0; k < 100; ++k) { auto term = std::pow(Tp(k + 1), -s); BS += term; ABS += term; AKS += term; WBS += term; WKS += term; BTS += term; LTS += term; WDS += term; WDvW += VwT[k]; std::cout << std::setw(w) << k << std::setw(w) << BS() << std::setw(w) << ABS() << std::setw(w) << AKS() << std::setw(w) << WBS() << std::setw(w) << WKS() << std::setw(w) << BTS() << std::setw(w) << LTS() << std::setw(w) << WDS() << std::setw(w) << WDvW() << '\n'; } // 2F0(1,1;;-1/3) std::cout << "\n\n2F0(1,1;;-1/3) = 0.78625122076596\n"; auto a = Tp{1}; auto b = Tp{1}; auto z = Tp{-1} / Tp{3}; auto term = Tp{1}; BS.reset(term); ABS.reset(term); AKS.reset(term); WBS.reset(term); WKS.reset(term); BTS.reset(term); LTS.reset(term); WDS.reset(term); std::cout << std::setw(w) << "k" << std::setw(w) << "Basic" << std::setw(w) << "Aitken-Basic" << std::setw(w) << "Aitken-Kahan" << std::setw(w) << "Winn-Basic" << std::setw(w) << "Winn-Kahan" << std::setw(w) << "BrezinskiT-Basic" << std::setw(w) << "LevinT-Basic" << std::setw(w) << "WenigerD-Basic" << '\n'; std::cout << std::setw(w) << "----------------" << std::setw(w) << "----------------" << std::setw(w) << "----------------" << std::setw(w) << "----------------" << std::setw(w) << "----------------" << std::setw(w) << "----------------" << std::setw(w) << "----------------" << std::setw(w) << "----------------" << std::setw(w) << "----------------" << '\n'; for (auto k = 1; k < 100; ++k) { std::cout << std::setw(w) << (k - 1) << std::setw(w) << BS() << std::setw(w) << ABS() << std::setw(w) << AKS() << std::setw(w) << WBS() << std::setw(w) << WKS() << std::setw(w) << BTS() << std::setw(w) << LTS() << std::setw(w) << WDS() << '\n'; term *= (a + k - 1) * (b + k - 1) * z / k; BS += term; ABS += term; AKS += term; WBS += term; WKS += term; BTS += term; LTS += term; WDS += term; } }
uint64_t foo2 (uint64_t a) { return BS (BS (a) & 0xA00000000); }