전체 글 142

USACO 1.4 - Arithmetic Progressions

An arithmetic progression is a sequence of the form a, a+b, a+2b, ..., a+nb where n=0,1,2,3,... . For this problem, a is a non-negative integer and b is a positive integer.Write a program that finds all arithmetic progressions of length n in the set S of bisquares. The set of bisquares is defined as the set of all integers of the form p2 + q2 (where p and q are non-negative integers).TIME LIMIT:..

Binomial coefficient

이항 계수(Binomial coefficient)는 n 개 에서 k 개를 고르는 (순서 없는)조합의 가짓수입니다. 이는 nCk 로 보통 표현하며 이항 다항식 {\displaystyle x+y} 의 거듭제곱 {\displaystyle (x+y)^{n}}에 대해서, 전개한 각 항 {\displaystyle x^{k}y^{n-k}}의 계수이기도 합니다. 그리고 위와 같이 점화식으로 표현할 수 있습니다. 따라서 이항 계수는 아래와 같은 방법으로 구할 수 있습니다. 12345678910111213141516const int MAX = 1000; //memset(cache, -1, sizeof(cache))int cache[MAX][MAX]; int getBinomialCoefficient(int n, int k)..

Sequential search, Binary search

선형적인 배열에서 특정 값을 찾는 방법으로 Sequential search 와 Binary search 방법이 있습니다.Sequential search 은 말 그대로 배열의 앞에서부터 끝까지 차례대로 찾아가는 방법이고Binary search 는 오름차순으로 정렬된 배열에서 divide and conquer algorithm 을 사용하여 찾을 대상을 절반씩 줄여가는 방법으로 시간 복잡도를 줄이는 방법입니다.Binary search 는 최악의 경우에서도 을 보장하기 때문에 크기가 큰 배열에서는 일반적으로 Sequential search 보다 빠르게 값을 찾을 수 있습니다. https://en.wikipedia.org/wiki/Linear_searchhttps://en.wikipedia.org/wiki/Bin..

Project Euler #18 - Maximum path sum I

By starting at the top of the triangle below and moving to adjacent numbers on the row below, the maximum total from top to bottom is 23.3 7 4 2 4 6 8 5 9 3That is, 3 + 7 + 4 + 9 = 23.Find the maximum total from top to bottom of the triangle below:75 95 64 17 47 82 18 35 87 10 20 04 82 47 65 19 01 23 75 03 34 88 02 77 73 07 63 67 99 65 04 28 06 16 70 92 41 41 26 56 83 40 80 70 33 41 48 72 33 47 ..

Project Euler #16 - Power digit sum

215 = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26.What is the sum of the digits of the number 21000? 출처: https://projecteuler.net/ 큰 수의 곱셈은 karatsuba algorithm 을 통해 구했고 2 power 1000 은 divide and conquer algorithm 을 통해 시간 복잡도를 줄였습니다. multiply 메써드는 karatsuba algorithm 포스팅에서 확인하실 수 있습니다. my solvingc++123456789101112131415161718192021222324252627282930#include #include #include #includ..

karatsuba algorithm

큰 수의 곱을 수행할 때 사용되는 karatsuba algorithm은 divide and conquer algorithm 을 통해 시간 복잡도를 줄인 algorithm 입니다.https://ko.wikipedia.org/wiki/카라추바_알고리즘 두 수의 길이가 an과 bn 이라 할 때 일반적인 곱셈법은 O(an*bn) 이라할 때 karatsuba algorithm은 두 수를 각각 반씩 나눠 네 개의 조각으로 만든 후 네 번의 곱을 세 번으로 줄여 시간 복잡도를 줄이는 방법을 사용했습니다. 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646..

Codility #1 - BinaryGap

문제: https://codility.com/programmers/task/binary_gap/ Codility 의 첫 번째 문제를 풀어봤습니다. 32비트의 integer가 주어졌을 때 해당 integer 를 binary 로 표현할 때 연속적으로 보이는 0의 개수를 binary gap 이라 할 때 가장 긴 binary gap 를 반환하는 문제입니다.쉬운 문제라 생각하며 별 생각없이 아래와 같이 풀었더니 답은 맞았지만 33% 의 점수를 얻었습니다. my solvingc++123456789101112131415161718192021#include using namespace std; int solution1(int num){ int count = 0; int ret = 0; while (num > 0) { ..