int main() { int q; scanf("%d", &q); while(q--) { int l, r; scanf("%d %d", &l, &r); int len = r - l + 1; printf("%I64d\n", GetSum(l * ((l & 1) ? -1 : 1), (len + 1) / 2) + GetSum((l + 1) * ((l & 1) ? 1 : -1), len / 2)); } return 0; }
int GetTotal(int n) { if (n == 1) { int ret = GetSum(1); return ret; } int res = GetTotal(n - 1); if (res != 1) printf(") "); printf("+"); return res + GetSum(n); }
bool isHappy(int n) { while (n>9) { n = GetSum(n); } if (n == 1 || n==7) return 1; return 0; }
int main() { int n; int sum; printf("PERFECTION OUTPUT\n"); while(scanf("%d", &n) != EOF) { if(n == 0) { break; } sum = GetSum(n); printf("%5d ", n); if(sum > n) { printf("ABUNDANT\n"); } else if(sum == n) { printf("PERFECT\n"); } else { printf("DEFICIENT\n"); } } printf("END OF OUTPUT\n"); return 0; }
int GetSum(struct TNode* root,int level){ if(!root){ return 0; } int sum=0; if(level==1){ return root->data; } if(level>1){ if(root->left){ sum+=GetSum(root->left,level-1); } if(root->right){ sum+=GetSum(root->right,level-1); } return sum; } }
ex row_mul::GetSum(int i, int nj) const { ex s = s_taylor; if(nj == int(nops() - 1)) return op(nj).coeff(s, i); ex res; for(int j = 0; j <= i; j++) res += binomial(j,0)*op(nj).coeff(s, j)*binomial(i, j)*GetSum(i - j, nj+1); return res; }
shared_ptr<list<string> > ProblemTweet::GetTweets() const { list<const Node*> nodes_route; auto node_goal = GetGoalNodeRandomly(); if (!node_goal) return nullptr; auto node_ptr = node_goal; while (node_ptr) { nodes_route.push_front(node_ptr); if (node_ptr && node_ptr->GetSum() == 0) break; node_ptr = GetFromNodeRandomly(node_ptr); } auto tweets = shared_ptr<list<string> >(new list<string>()); { stringstream ss; ss << "Capacities: " << GetName(); ss << " -> "; ss << "Request: " << goal_sum; tweets->push_back(ss.str()); } for_each(nodes_route.begin(), nodes_route.end(), [&](const Node* node) { stringstream ss; ss << "Bucket: " << node->GetName(false); tweets->push_back(ss.str()); }); { stringstream ss; ss << "Final result: "; for (Dimention d = 0; d < capacities.size(); d++) { if (d != 0) ss << "+"; ss << node_goal->volumes[d]; } ss << "=" << node_goal->GetSum(); tweets->push_back(ss.str()); } return tweets; }
int main(){ char a[]="1100011"; char b[]="10"; printf("\nResult : %s",GetSum(a,b)); return 0; }
ex row_power::GetSum(int i, int nj) const { ex s = s_taylor; if(!is_exactly_a<numeric>(op(1).evalf())) ERROR_15(op(1), op(0)) if(nj == int(ex_to<numeric>(op(1).eval()).to_int() - 1)) return op(0).coeff(s, i); ex res; for(int j = 0; j <= i; j++) res += binomial(j,0)*op(0).coeff(s, j)*binomial(i, j)*GetSum(i - j, nj+1); return res; }
void DistributeWeightedStrict(DISPLAY_INT *array, int start, int end, int pool) { int index, portion, sum; sum = GetSum(array, start, end); for (index=start; index<end; index++) { if (sum == 0) break; portion = (array[index] * pool) / sum; sum -= array[index]; array[index] += portion; pool -= portion; } }
int main() { freopen("1665.in" , "r", stdin ); freopen("1665.out", "w", stdout); scanf("%d %d", &n, &m); scanf("%s", buf); for (int i=1; i<=n; i++) d[i] = buf[i-1] - '0'; BuildTree(1, 1, n); for (int i=0, s, t; i<m; i++) { scanf("%d %d", &s, &t); printf("%d\n", GetSum(1, s, t)); Update(1, s, t, 1); } return 0; }
int GetSum(int len) { if (len == 1) { if (flag != 0) printf(" ( 1 "); else { printf(" 1 "); flag = 1; } return 1; } int ret = GetSum(len - 1); printf("+ %d ", len); return ret + len; }
static SRes SzFolder_Decode2(const CSzFolder *folder, const UInt64 *packSizes, ILookInStream *inStream, UInt64 startPos, Byte *outBuffer, SizeT outSize, ISzAlloc *allocMain, Byte *tempBuf[]) { UInt32 ci; SizeT tempSizes[3] = { 0, 0, 0}; SizeT tempSize3 = 0; Byte *tempBuf3 = 0; RINOK(CheckSupportedFolder(folder)); for (ci = 0; ci < folder->NumCoders; ci++) { CSzCoderInfo *coder = &folder->Coders[ci]; if (IS_MAIN_METHOD((UInt32)coder->MethodID)) { UInt32 si = 0; UInt64 offset; UInt64 inSize; Byte *outBufCur = outBuffer; SizeT outSizeCur = outSize; if (folder->NumCoders == 4) { UInt32 indices[] = { 3, 2, 0 }; UInt64 unpackSize = folder->UnpackSizes[ci]; si = indices[ci]; if (ci < 2) { Byte *temp; outSizeCur = (SizeT)unpackSize; if (outSizeCur != unpackSize) return SZ_ERROR_MEM; temp = (Byte *)IAlloc_Alloc(allocMain, outSizeCur); if (temp == 0 && outSizeCur != 0) return SZ_ERROR_MEM; outBufCur = tempBuf[1 - ci] = temp; tempSizes[1 - ci] = outSizeCur; } else if (ci == 2) { if (unpackSize > outSize) /* check it */ return SZ_ERROR_PARAM; tempBuf3 = outBufCur = outBuffer + (outSize - (size_t)unpackSize); tempSize3 = outSizeCur = (SizeT)unpackSize; } else return SZ_ERROR_UNSUPPORTED; } offset = GetSum(packSizes, si); inSize = packSizes[si]; RINOK(LookInStream_SeekTo(inStream, startPos + offset)); if (coder->MethodID == k_Copy) { if (inSize != outSizeCur) /* check it */ return SZ_ERROR_DATA; RINOK(SzDecodeCopy(inSize, inStream, outBufCur)); } else if (coder->MethodID == k_LZMA) { RINOK(SzDecodeLzma(coder, inSize, inStream, outBufCur, outSizeCur, allocMain)); } else if (coder->MethodID == k_LZMA2) { #ifdef _7ZIP_LZMA2_SUPPPORT RINOK(SzDecodeLzma2(coder, inSize, inStream, outBufCur, outSizeCur, allocMain)); #else return SZ_ERROR_UNSUPPORTED; #endif } else { #ifdef _7ZIP_PPMD_SUPPPORT RINOK(SzDecodePpmd(coder, inSize, inStream, outBufCur, outSizeCur, allocMain)); #else return SZ_ERROR_UNSUPPORTED; #endif } } else if (coder->MethodID == k_BCJ2) { UInt64 offset = GetSum(packSizes, 1); UInt64 s3Size = packSizes[1]; SRes res; if (ci != 3) return SZ_ERROR_UNSUPPORTED; RINOK(LookInStream_SeekTo(inStream, startPos + offset)); tempSizes[2] = (SizeT)s3Size; if (tempSizes[2] != s3Size) return SZ_ERROR_MEM; tempBuf[2] = (Byte *)IAlloc_Alloc(allocMain, tempSizes[2]); if (tempBuf[2] == 0 && tempSizes[2] != 0) return SZ_ERROR_MEM; res = SzDecodeCopy(s3Size, inStream, tempBuf[2]); RINOK(res) res = Bcj2_Decode( tempBuf3, tempSize3, tempBuf[0], tempSizes[0], tempBuf[1], tempSizes[1], tempBuf[2], tempSizes[2], outBuffer, outSize); RINOK(res) } else { if (ci != 1)
SRes SzDecode2(const CFileSize *packSizes, const CSzFolder *folder, /* #ifdef _LZMA_IN_CB */ ISzInStream *inStream, CFileSize startPos, /* #else const Byte *inBuffer, #endif */ Byte *outBuffer, size_t outSize, ISzAlloc *allocMain, Byte *tempBuf[]) { UInt32 ci; size_t tempSizes[3] = { 0, 0, 0}; size_t tempSize3 = 0; Byte *tempBuf3 = 0; RINOK(CheckSupportedFolder(folder)); for (ci = 0; ci < folder->NumCoders; ci++) { CSzCoderInfo *coder = &folder->Coders[ci]; if (coder->MethodID == k_Copy || coder->MethodID == k_LZMA || coder->MethodID == k_BZ2 || coder->MethodID == k_PPMD) { UInt32 si = 0; CFileSize offset; CFileSize inSize; Byte *outBufCur = outBuffer; size_t outSizeCur = outSize; if (folder->NumCoders == 4) { UInt32 indices[] = { 3, 2, 0 }; CFileSize unpackSize = folder->UnpackSizes[ci]; si = indices[ci]; if (ci < 2) { Byte *temp; outSizeCur = (size_t)unpackSize; if (outSizeCur != unpackSize) return SZ_ERROR_MEM; temp = (Byte *)IAlloc_Alloc(allocMain, outSizeCur); if (temp == 0 && outSizeCur != 0) return SZ_ERROR_MEM; outBufCur = tempBuf[1 - ci] = temp; tempSizes[1 - ci] = outSizeCur; } else if (ci == 2) { if (unpackSize > outSize) // check it return SZ_ERROR_PARAM; // check it tempBuf3 = outBufCur = outBuffer + (outSize - (size_t)unpackSize); tempSize3 = outSizeCur = (size_t)unpackSize; } else return SZ_ERROR_UNSUPPORTED; } offset = GetSum(packSizes, si); inSize = packSizes[si]; /* #ifdef _LZMA_IN_CB */ RINOK(inStream->Seek(inStream, startPos + offset, SZ_SEEK_SET)); /* #endif */ if (coder->MethodID == k_Copy) { if (inSize != outSizeCur) // check it return SZ_ERROR_DATA; /* #ifdef _LZMA_IN_CB */ RINOK(SzDecodeCopy(inSize, inStream, outBufCur)); /* #else memcpy(outBufCur, inBuffer + (size_t)offset, (size_t)inSize); #endif */ } else if(coder->MethodID == k_PPMD) { SRes res = SZ_OK; struct PPMdProps pprops; pprops.maxorder = coder->Props.data[0]; pprops.suballocsize = (coder->Props.data[1]) | (coder->Props.data[2] << 8) | (coder->Props.data[3] << 16) | (coder->Props.data[4] << 24); PPMdSubAllocatorVariantH *alloc=CreateSubAllocatorVariantH(pprops.suballocsize); PPMdModelVariantH model; StartPPMdModelVariantH(&model,inStream,alloc,pprops.maxorder,TRUE); outSizeCur = 0; while(outSizeCur < outSize) { int byte=NextPPMdVariantHByte(&model); if(byte<0) break; *outBufCur = byte; outBufCur++; outSizeCur++; } FreeSubAllocatorVariantH(alloc); // RINOK(res) } else if(coder->MethodID == k_BZ2) { int bzres = BZ_OK; SRes res = SZ_OK; bz_stream bzstrm; bzstrm.bzalloc = BzAlloc; bzstrm.bzfree = BzFree; bzstrm.opaque = NULL; void *inBuffer = NULL; size_t size = inSize; bzres = BZ2_bzDecompressInit(&bzstrm,0,0); if(bzres == BZ_OK) res = SZ_OK; else res=SZ_ERROR_MEM; RINOK(res) bzstrm.next_out = outBufCur; bzstrm.avail_out = outSizeCur; do { size=inSize; RINOK(inStream->Read(inStream, (void **)&inBuffer, &size)); inSize -= size; bzstrm.next_in = (char *)(inBuffer); bzstrm.avail_in = size; do { bzres = BZ2_bzDecompress(&bzstrm); }while(bzstrm.avail_in>0 && bzres==BZ_OK); }while (bzres==BZ_OK); BZ2_bzDecompressEnd(&bzstrm); switch(bzres) { case BZ_CONFIG_ERROR: case BZ_PARAM_ERROR: res=SZ_ERROR_FAIL; break; case BZ_MEM_ERROR: case BZ_OUTBUFF_FULL: res=SZ_ERROR_MEM; break; case BZ_DATA_ERROR_MAGIC: case BZ_UNEXPECTED_EOF: res=SZ_ERROR_DATA; break; case BZ_DATA_ERROR: res=SZ_ERROR_CRC; break; case BZ_OK: res=SZ_OK; break; default: res=SZ_OK; break; } RINOK(res) } else {
int main() { int N, i, j, Num, M; char A[300][MAXL]; scanf("%d", &N); memset(A, '0', sizeof(A)); for(i = 0; i < N; ++i) { A[i][MAXL - 1] = '\0'; scanf("%d", &Num); LOOP: M = GetSum(A[i]); if(M < Num) { Num -= M; j = 0; while(Num) { int cur = '9' - A[i][j]; if(Num > cur) { A[i][j] = '9'; Num -= cur; } else { A[i][j] += Num; break; } ++j; } } else if(M > Num) { int k; for(k = 1; k < MAXL; ++k) { A[i][k - 1] = '0'; j = k; while(A[i][j] == '9') { A[i][j] = '0'; ++j; } ++A[i][j]; if(GetSum(A[i]) <= Num) { break; } } goto LOOP; } if(i != N - 1) { memcpy(A[i + 1], A[i], sizeof(char) * MAXL); j = 0; while(A[i + 1][j] == '9') { A[i + 1][j] = '0'; ++j; } ++A[i + 1][j]; } } for(i = 0; i < N; ++i) { for(j = MAXL - 2; j >= 0; --j) { if(A[i][j] != '0') { break; } } for(; j >= 0; --j) { printf("%c", A[i][j]); } puts(""); } return 0; }
int main() { //assigning variables int numbers[5]; int size = sizeof(numbers) / sizeof(int); int total = 0; int i = 0; float averageNumber; float sum; char command; bool loopCon = true; //welcome message printf("WELCOME TO FUN WITH FIVE NUMBERS!\n"); //collect numbers, put in array for (i = 0; i < size; i++) { printf("Enter number %i: ", i + 1); scanf(" %i", &numbers[i]); } //repeat numbers in the array PrintNumbers(numbers, size); //do while x is not typed do { loopCon = true; //print options to user printf("Select one of the following commands (type x to finish):\n"); printf("1 - Show the average of the numbers\n"); printf("2 - Sort the numbers from smallest to largest\n"); printf("3 - Show the sum of the numbers\n"); printf("4 - Show the sum in 32-bit binary form\n"); printf("5 - Change the five numbers\n"); printf("x - End Program\n"); printf("Your command: "); scanf(" %c", &command); switch (command) { case '1'://average number averageNumber = GetAverage(numbers, size); printf("The average of your numbers is %.1f \n\n", averageNumber); break; case '2'://acending order SortedNumbers(numbers, size); PrintNumbers(numbers, size); break; case '3'://sum of numbers sum = GetSum(numbers, size); printf("The sum of your numbers is %.1f \n\n", sum); break; case '4'://sum in binary printf("The sum of yours numbers in binary form is: "); PrintSumAsBinary(numbers, size); printf("\n\n"); break; case '5'://new numbers for (i = 0; i < size; i++) { printf("Enter number %i: ", i + 1); scanf(" %i", &numbers[i]); } //repeat numbers in the array PrintNumbers(numbers, size); break; case 'x'://exit loopCon = false; printf("Thanks for playing!\n"); break; default: if(loopCon != false) { printf("\nInvalid command. Try again.\n"); } } } while (command != 'x'); return 0; }
SZ_RESULT SzDecode2(const CFileSize *packSizes, const CFolder *folder, #ifdef _LZMA_IN_CB ISzInStream *inStream, CFileSize startPos, #else const Byte *inBuffer, #endif Byte *outBuffer, size_t outSize, ISzAlloc *allocMain, Byte *tempBuf[]) { UInt32 ci; size_t tempSizes[3] = { 0, 0, 0}; size_t tempSize3 = 0; Byte *tempBuf3 = 0; RINOK(CheckSupportedFolder(folder)); for (ci = 0; ci < folder->NumCoders; ci++) { CCoderInfo *coder = &folder->Coders[ci]; if (coder->MethodID == k_Copy || coder->MethodID == k_LZMA) { UInt32 si = 0; CFileSize offset; CFileSize inSize; Byte *outBufCur = outBuffer; size_t outSizeCur = outSize; if (folder->NumCoders == 4) { UInt32 indices[] = { 3, 2, 0 }; CFileSize unpackSize = folder->UnPackSizes[ci]; si = indices[ci]; if (ci < 2) { Byte *temp; outSizeCur = (size_t)unpackSize; if (outSizeCur != unpackSize) return SZE_OUTOFMEMORY; temp = (Byte *)allocMain->Alloc(outSizeCur); if (temp == 0 && outSizeCur != 0) return SZE_OUTOFMEMORY; outBufCur = tempBuf[1 - ci] = temp; tempSizes[1 - ci] = outSizeCur; } else if (ci == 2) { if (unpackSize > outSize) return SZE_OUTOFMEMORY; tempBuf3 = outBufCur = outBuffer + (outSize - (size_t)unpackSize); tempSize3 = outSizeCur = (size_t)unpackSize; } else return SZE_NOTIMPL; } offset = GetSum(packSizes, si); inSize = packSizes[si]; #ifdef _LZMA_IN_CB RINOK(inStream->Seek(inStream, startPos + offset)); #endif if (coder->MethodID == k_Copy) { if (inSize != outSizeCur) return SZE_DATA_ERROR; #ifdef _LZMA_IN_CB RINOK(SzDecodeCopy(inSize, inStream, outBufCur)); #else memcpy(outBufCur, inBuffer + (size_t)offset, (size_t)inSize); #endif } else { SZ_RESULT res = SzDecodeLzma(coder, inSize, #ifdef _LZMA_IN_CB inStream, #else inBuffer + (size_t)offset, #endif outBufCur, outSizeCur, allocMain); RINOK(res) } } else if (coder->MethodID == k_BCJ)
int GetSum(const int root, const int s, const int t) { if (tree[root].add) down(root); if (t < tree[root].l || s > tree[root].r) return 0; if (s <= tree[root].l && t >= tree[root].r) return tree[root].sum; return GetSum(root * 2, s, t) + GetSum(root * 2 + 1, s, t); }
template<class T> double GetMean(const T &c) { return GetSum(c) / (double)(c.size()); }
int main(void) { for(i=1;i<31;++i) { for(ii=1;ii<31;++ii) { a=GetRandom(1,100); field[i][ii] = (a <= P) ? 1:0; } } /*以下を繰り返す*/ while(1) { /*現在のfieldを画面に出力*/ system("sleep 0.2"); system("clear"); for(i=1;i<31;++i) { for(ii=1;ii<31;++ii) { if(field[ii][i] == 1) printf("O "); else printf(" "); } printf("\n"); } printf("%d 世代 \n",t); /*fieldの上下左右を接続*/ for(i=1;i<31;++i) { field[0][i]=field[30][i]; field[31][i]=field[1][i]; field[i][0]=field[i][30]; field[i][31]=field[i][1]; } field[0][0]=field[30][30]; field[0][31]=field[30][1]; field[31][0]=field[1][30]; field[31][31]=field[1][1]; /*fieldからold_fieldを決定*/ if(t%2 == 0) { for(i=0;i<32;++i) { for(ii=0;ii<32;++ii) { old_field[i][ii] = field[i][ii]; } } } /*fieldからnext_fieldを決定*/ for(i=1;i<31;++i) { for(ii=1;ii<31;++ii) { switch(field[i][ii]) { case 0:/*セルが死んでいる場合*/ if( GetSum(i,ii) == 3) { next_field[i][ii] = 1; ++change; } else { next_field[i][ii] = 0; } break; case 1:/*セルが生きている場合*/ if( GetSum(i,ii) == 2 || GetSum(i,ii) == 3) { next_field[i][ii] = 1; } else { next_field[i][ii] = 0; ++change; } break; } } } /*fieldをnext_fieldで更新*/ for(i=1;i<31;++i) { for(ii=1;ii<31;++ii) { field[i][ii] = next_field[i][ii]; } } /*変化がない場合*/ ++t; if (change == 0) { printf("stop\n"); break; } change = 0; /*ループに入った場合*/ for(i=1;i<31;++i) { for(ii=1;ii<31;++ii) { if(old_field[i][ii] != next_field[i][ii]) { ++change; } } } if (change == 0) { ++cnt; } if (cnt >= 5) { printf("loop\n"); break; } change = 0; } return 0; }