void bitsetDivide(std::bitset<N> x, std::bitset<N> y, std::bitset<N>& q, std::bitset<N>& r) { if (y.none( )) { throw std::domain_error("division by zero undefined"); } q.reset( ); r.reset( ); if (x.none( )) { return; } if (x == y) { q[0] = 1; return; } r = x; if (bitsetLt(x, y)) { return; } // count significant digits in divisor and dividend unsigned int sig_x; for (int i=N-1; i>=0; i--) { sig_x = i; if (x[i]) break; } unsigned int sig_y; for (int i=N-1; i>=0; i--) { sig_y = i; if (y[i]) break; } // align the divisor with the dividend unsigned int n = (sig_x - sig_y); y <<= n; // make sure the loop executes the right number of times n += 1; // long division algorithm, shift, and subtract while (n--) { // shift the quotient to the left if (bitsetLtEq(y, r)) { // add a new digit to quotient q[n] = true; bitsetSubtract(r, y); } // shift the divisor to the right y >>= 1; } }
void Sync::choose_threads(){ int ref = -1; for (UINT ii=0; ii<num_threads; ii++){ unsigned int i = (ii + rrcounter) % num_threads; if ( !t[i].ignore && !was_executed.test(i) ){ ref = i; break; } } if (ref == -1){ was_executed.reset(); for (UINT ii=0; ii<num_threads; ii++){ unsigned int i = (ii + rrcounter) % num_threads; if (!t[i].ignore){ ref = i; break; } } } for (UINT i=0; i<num_threads; i++){ t[i].is_active = !t[i].ignore && t[i].pc == t[ref].pc; if (t[i].is_active){ was_executed.set(i); } } // Next rrcounter = (ref + 1) % num_threads; }
template<size_t N> void fillSieveOfAtkin(std::bitset<N>& primes) { primes.reset(); const size_t ROOT_MAX = static_cast<size_t>( sqrtf(N) ); typedef unsigned long ulong; for (size_t x = 1; x < ROOT_MAX; x++) { for (size_t y = 1; y < ROOT_MAX; y++) { ulong x_2 = x * x; ulong y_2 = y * y; ulong n = 4 * x_2 + y_2; ulong n_mod_12 = n % 12; if (n <= N && (n_mod_12 == 1 || n_mod_12 == 5)) primes[n] = !primes[n]; n = 3 * x_2 + y_2; if (n <= N && (n % 12) == 7) primes[n] = !primes[n]; n = 3 * x_2 - y_2; if (x > y && n <= N && (n % 12) == 11) primes[n] = !primes[n]; } } for (size_t n = 5; n < ROOT_MAX; n++) { if (primes[n]) { ulong k = n * n; for (size_t j = k; j <= N; j += k) primes[j] = false; } } primes[2] = primes[3] = true; }
void updateInput() { pressed.reset(); released.reset(); while (al_get_next_event(keyboardEventQueue, &tempEvent)) { if (tempEvent.type == ALLEGRO_EVENT_KEY_DOWN) { pressed[tempEvent.keyboard.keycode] = 1; down[tempEvent.keyboard.keycode] = 1; } if (tempEvent.type == ALLEGRO_EVENT_KEY_UP) { released[tempEvent.keyboard.keycode] = 1; down[tempEvent.keyboard.keycode] = 0; } } }
void OnRehash(User*) { /* from m_sethost: use the same configured host character map if it exists */ std::string hmap = ServerInstance->Config->ConfValue("hostname")->getString("charmap", "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.-_/0123456789"); hostmap.reset(); for (std::string::iterator n = hmap.begin(); n != hmap.end(); n++) hostmap.set(static_cast<unsigned char>(*n)); }
void cal(int l, int r) { int *now = buf; int tot = cnt[l][r]; if(tot != 0) return; if(l == r) { buf[tot++] = ch[s[l]]; dp[l][r][cnt[l][r]++] = ch[s[l]]; return ; } for(int i = l; i < r; i++) { cal(l, i); cal(i + 1, r); } vis.reset(); for(int i = l; i < r; i++) { int *left = dp[l][i], *right = dp[i + 1][r]; int tot1 = cnt[l][i], tot2 = cnt[i + 1][r]; for(int j = 0; j != tot1; j++) { int cur = dp[l][i][j]; int x = left[j]; int st = std::upper_bound(right, right + tot2, 5001 - x) - right; for(int k = 0; k != st; k++) { int y = right[k]; if(y > x) break; if(!vis[(x + y)]) now[tot++] = (x + y); vis[(x + y)] = true; } } } for(int i = l; i < r; i++) { int *left = dp[l][i], *right = dp[i + 1][r]; int tot1 = cnt[l][i], tot2 = cnt[i + 1][r]; for(int k = 0; k != tot2; k++) { for(int j = 0; j < tot1; j++) { int x = left[j], y = right[k]; if(x >= y) break; if(!vis[(y - x)]) now[tot++] = (y - x); vis[(y - x)] = true; } } } std::sort(now, now + tot); tot = std::unique(now, now + tot) - now; int &cur = cnt[l][r]; cur = 0; for(int i = 0; i < tot; i++) dp[l][r][cur++] = now[i]; }
void rebuild() { p.reset(); p.flip(); p[0] = p[1] = 1; for ( size_t n=2; n < PRIMES; ++n ) if ( p[n] ) { v.push_back(n); for ( size_t m=n<<1; m < PRIMES; m += n ) p[m] = 0; } }
void bitsetMultiply(std::bitset<N>& x, const std::bitset<N>& y) { std::bitset<N> tmp = x; x.reset( ); // we want to minimize the number of times we shift and add if (tmp.count( ) < y.count( )) { for (int i=0; i < N; i++) if (tmp[i]) bitsetAdd(x, y << i); } else { for (int i=0; i < N; i++) if (y[i]) bitsetAdd(x, tmp << i); } }
bool _on_value_changed() { if(!_set.all()) return false; _set.reset(); switch(_operation) { case ADD: return output(_value0+_value1); case SUBTRACT: return output(_value0-_value1); case MULTIPLY: return output(_value0*_value1); case DIVIDE: return output(_value0/_value1); } }
int main(){ // do you mind telling us in the problem statement // that there is more than one test case per input while(~scanf("%d%d",&N,&M)){ V.clear(); for(int i=1;i<=N;++i){ scanf("%d",W+i); V.emplace_back(W[i]); } std::sort(all(V)); V.erase(std::unique(all(V)),V.end()); for(int i=1;i<=N;++i) W[i]=std::lower_bound(all(V),W[i])-V.begin(); for(int i=1;i<=N;++i) adj[i].clear(); for(int i=1,a,b;i<N;++i){ scanf("%d%d",&a,&b); adj[a].emplace_back(b); adj[b].emplace_back(a); } dfs(1,-1); build(); for(int i=0,a,b,l;i<M;++i){ scanf("%d%d",&a,&b); l=lca(a,b); if(ein[b]<ein[a]) std::swap(a,b); if(a==l||b==l) qrys[i]={ein[a],ein[b],-1,i}; else qrys[i]={eout[a],ein[b],l,i}; } std::sort(qrys,qrys+M); vis.reset(); memset(cnt,0,sizeof cnt); res=0; chk(tour[1]); for(int i=0,l=1,r=1;i<M;++i){ while(r<qrys[i].r) chk(tour[++r]); while(l>qrys[i].l) chk(tour[--l]); while(r>qrys[i].r) chk(tour[r--]); while(l<qrys[i].l) chk(tour[l++]); if(~qrys[i].lca) chk(qrys[i].lca); ans[qrys[i].i]=res; if(~qrys[i].lca) chk(qrys[i].lca); } for(int i=0;i<M;printf("%d\n",ans[i++])); } }
int main() { while(scanf("%d%d%d", &n, &m, &q) == 3 && n + m + q) { for(int i = 0; i < n; ++i) scanf("%d%d%d%d", &cir[i].x, &cir[i].y, &cir[i].z, r + i); for(int i = 0; i < m; ++i) scanf("%d%d%d%d", &lht[i].x, &lht[i].y, &lht[i].z, val + i); scanf("%d%d%d", &obj.x, &obj.y, &obj.z); for(int i = 0; i < n; ++i) cir[i] = cir[i] - obj; for(int i = 0; i < m; ++i) { lht[i] = lht[i] - obj; dis[i] = lht[i].mode(); cnt[i].reset(); for(int j = 0; j < n; ++j) { int sgn1 = sgn((lht[i] - cir[j]).mode() - r[j] * r[j]); int sgn2 = sgn(cir[j].mode() - r[j] * r[j]); if(sgn1 * sgn2 < 0 || sgn1 > 0 && sgn2 > 0 && lht[i].det(cir[j]).mode() < r[j] * r[j] * (LL)dis[i] && lht[i].dot(lht[i] - cir[j]) > 0 && lht[i].dot(cir[j]) > 0) cnt[i].set(j); } } double ans = 0; for(int i = 0; i < 1 << m; ++i) { double value = 0; cost.reset(); for(int j = 0; j < m; ++j) if((i >> j) & 1) { cost |= cnt[j]; value += (double)val[j] / dis[j]; } if((int)cost.count() <= q && ans < value) ans = value; } printf("%.10f\n", ans); } return 0; }
// For correctness check out: // "A Linear Sieve Algorithm for Finding Prime Numbers" void generate_sieve() { sieve.flip(); // all bits are 1 now for (int i=2; i<n; ++i){ if (sieve[i]){ primes.push_back(i); } for (int k=0; k<(int)primes.size() && i*primes[k]<n; ++k){ // For debug purposes: // std::cout<<"i="<<i<<", k="<<k<<", "<<i*primes[k]<<std::endl; // We reset the bit of a composite number, until we find lowest // prime that divides i. sieve.reset(i*primes[k]); if (i%primes[k] == 0){ break; } } } }
BOOL CPEParser::AnalizeExport() { //флаги наличия имени у функции (определяется заблаговременно, чтобы не было warning'а из-за goto). static std::bitset<0x10000> NamesFlags; NamesFlags.reset(); //получить директорию экспорта PIMAGE_DATA_DIRECTORY pDataDirectory=mpImageOptionalHeader->DataDirectory+IMAGE_DIRECTORY_ENTRY_EXPORT; //если экспорта нет, закончить if(!pDataDirectory->VirtualAddress || pDataDirectory->Size<sizeof(IMAGE_EXPORT_DIRECTORY)) return TRUE; //считать дескриптор экспорта CYBER_ADDRESS DescriptorAddress=mSettings.BaseAddress+pDataDirectory->VirtualAddress; IMAGE_EXPORT_DIRECTORY Descriptor; try { mpMemory->Data(&Descriptor,DescriptorAddress,sizeof(IMAGE_EXPORT_DIRECTORY)); } catch(CCyberMemoryPageFaultException) { goto error; } //создать список экспорта CImageListExports* pExportsList=new CImageListExports; //добавить его в список дополнительных объектов mpExtendMap->insert(std::make_pair(IMAGE_EXTEND_LIST_EXPORTS,pExportsList)); //получить список функций std::list<CImageListExports::CExportFunction*>* pFunctionsList=pExportsList->GetList(); //получить количество функций и имен UINT FunctionsCount=Descriptor.NumberOfFunctions; if(FunctionsCount>0x10000) goto error; UINT NamesCount=Descriptor.NumberOfNames; if(NamesCount>0x10000) goto error; //получить таблицы имен, адресов и ординалов CYBER_ADDRESS AddressTable=mSettings.BaseAddress+Descriptor.AddressOfFunctions; CYBER_ADDRESS NamesTable=mSettings.BaseAddress+Descriptor.AddressOfNames; CYBER_ADDRESS OrdinalsTable=mSettings.BaseAddress+Descriptor.AddressOfNameOrdinals; //цикл по именам for(UINT i=0;i<NamesCount;++i) { //считать имя, ординал, адрес и форвард функции LPSTR szName; LPSTR szForward; WORD Ordinal; CYBER_ADDRESS Address; //флаг форварда BOOL IsForward=FALSE; try { //считать имя szName=mpMemory->ReadASCIIZ(mSettings.BaseAddress+mpMemory->Dword(NamesTable+i*sizeof(DWORD))); //считать ординал Ordinal=mpMemory->Word(OrdinalsTable+i*sizeof(WORD)); //считать адрес Address=mpMemory->Dword(AddressTable+Ordinal*sizeof(DWORD)); //если адрес находится в пределах таблицы экспорта, значит, это форвард if(Address>=pDataDirectory->VirtualAddress && Address<pDataDirectory->VirtualAddress+pDataDirectory->Size) IsForward=TRUE; Address+=mSettings.BaseAddress; if(IsForward) //считать имя форвард-функции szForward=mpMemory->ReadASCIIZ(Address); } catch(CCyberMemoryPageFaultException) { goto error; } //указать флаг наличия имени NamesFlags[Ordinal]=true; #ifdef UNICODE //преобразовать строки в Unicode UINT NameLength=strlen(szName)+1; LPTSTR szUnicodeName=new TCHAR[NameLength]; MultiByteToWideChar(CP_ACP,0,szName,-1,szUnicodeName,NameLength); SafeDeleteMassive(szName); UINT ForwardLength; LPTSTR szUnicodeForward; if(IsForward) { ForwardLength=strlen(szForward)+1; szUnicodeForward=new TCHAR[ForwardLength]; MultiByteToWideChar(CP_ACP,0,szForward,-1,szUnicodeForward,ForwardLength); SafeDeleteMassive(szForward); } #define szName szUnicodeName #define szForward szUnicodeForward #endif //добавить функцию в список if(IsForward) pFunctionsList->push_back(new CImageListExports::CExportFunction(szName,Ordinal,szForward)); else pFunctionsList->push_back(new CImageListExports::CExportFunction(szName,Ordinal,Address)); //если функция локальная if(!IsForward) { //сформировать имя и установить метку LPTSTR szLabelName=new TCHAR[NameLength+6]; _stprintf(szLabelName,TEXT("__exp_%s"),szName); CAddressData* pAddressData=mpAddressMap->OpenAddress(Address); CLabel* pLabel=new CLabel(szLabelName); SafeDeleteMassive(szLabelName); pAddressData->SetLabel(pLabel); pLabel->Release(); //создать функцию pAddressData->CreateSubroutine(); pAddressData->Release(); } //освободить память SafeDeleteMassive(szName); if(IsForward) SafeDeleteMassive(szForward); #ifdef UNICODE #undef szName #undef szForward #endif } //цикл по функциям for(UINT i=0;i<FunctionsCount;++i) { //если функция имеет имя, значит, она уже обработана if(NamesFlags[i]) continue; //считать адрес CYBER_ADDRESS Address; try { Address=mSettings.BaseAddress+mpMemory->Dword(AddressTable+i*sizeof(DWORD)); } catch(CCyberMemoryPageFaultException) { goto error; } //добавить функцию в список pFunctionsList->push_back(new CImageListExports::CExportFunction(NULL,i,Address)); } return TRUE; //ошибка error: //раздел экспорта искажен MessageBox(NULL,TEXT("The export section in this file is non-readable.\nPE Parser coudn't parse export."),TEXT("Export parsing"),MB_ICONSTOP); return FALSE; }
void OPKiller::reset() { opcodes_filter_.reset(); target_opcodes_.clear(); areas_filter_.reset(); target_areas_.clear(); }