반응형

www.acmicpc.net/problem/1208

 

1208번: 부분수열의 합 2

첫째 줄에 정수의 개수를 나타내는 N과 정수 S가 주어진다. (1 ≤ N ≤ 40, |S| ≤ 1,000,000) 둘째 줄에 N개의 정수가 빈 칸을 사이에 두고 주어진다. 주어지는 정수의 절댓값은 100,000을 넘지 않는다.

www.acmicpc.net

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <iostream>
#define rep(i,n) for(ll i=0;i<n;i++)
#define ll long long
using namespace std;
ll arr[41], a1[4000001], a2[4000001];
ll n, s, ans;
 
void calc() {
    ll mask = 0;
    ll tmp = 0;
    ll len = n / 2;
    // 배열 왼쪽
    while (mask < (1 << len)) {
        tmp = 0;
        rep(i, len) 
            tmp += ((mask >> i) & 1)* arr[i];
        
        a1[tmp + 2000000]++;
        mask++;
    }
 
    // 배열 오른쪽
    mask = 0;
    if (n % 2 == 1)
        len++;
    
    while (mask < (1 << len)) {
        tmp = 0;
        rep(i, len)
            tmp += ((mask >> i) & 1)* arr[i + n / 2];
        
        a2[tmp + 2000000]++;
        mask++;
    }
}
 
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin >> n >> s;
    rep(i, n)
        cin >> arr[i];
 
    // 1. 배열 나눠서 부분수열 계산
    calc();
    
    // 2. 투 포인터로 탐색
    ll p1 = 0, p2 = 4000000;
    if (s == 0)
        ans--;
    s += 4000000;
    while (1) {
        while (p1 + p2 > s)        // s보다 크면 p2를 이동시켜서 더 작게 만들기
            p2--;
        while (p1 + p2 < s)        // s보다 작으면 p1을 이동시켜서 더 크게 만들기
            p1++;
        if (p1 > 4000000 || p2 < 0break;    // 범위 벗어나면 종료
        if (p1 + p2 == s)        // s와 같으면 더하기
            ans += a1[p1] * a2[p2];
        p1++;                    // 다음 탐색 진행을 위해 p1 오른쪽으로 이동
    }
    cout << ans;
}
cs
반응형

'백준 > 브루트포스' 카테고리의 다른 글

백준 14225  (0) 2021.02.23
백준 14391  (0) 2021.02.15
백준 1182  (0) 2021.02.15
백준 6064  (0) 2021.02.13
백준 1107 [복습 필수]  (0) 2021.02.13
반응형

www.acmicpc.net/problem/14225

 

14225번: 부분수열의 합

수열 S가 주어졌을 때, 수열 S의 부분 수열의 합으로 나올 수 없는 가장 작은 자연수를 구하는 프로그램을 작성하시오. 예를 들어, S = [5, 1, 2]인 경우에 1, 2, 3(=1+2), 5, 6(=1+5), 7(=2+5), 8(=1+2+5)을 만들

www.acmicpc.net

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <algorithm>
#define ll long long
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
int n, arr[21];
bool visited[2000001];
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin >> n;
    rep(i, n)
        cin >> arr[i];
    sort(arr, arr + n);
 
    int mask = 0;
    while (mask < (1 << n)) {
        int temp = 0;
        rep(i,n) {
            int x = (mask >> i) & 1;    // 비트마스크 이용
            temp += x * arr[i];            // 모든 조합 찾아봄
        }
        visited[temp] = 1;                // 만들어진 숫자 체크
        mask++;
    }
    rep(i, 2000001) {
        if (!visited[i]) {                // 체크되지 않은 첫 번째 숫자 출력
            cout << i;
            break;
        }
    }
}
cs
반응형

'백준 > 브루트포스' 카테고리의 다른 글

백준 1208 [복습 필수]  (0) 2021.03.06
백준 14391  (0) 2021.02.15
백준 1182  (0) 2021.02.15
백준 6064  (0) 2021.02.13
백준 1107 [복습 필수]  (0) 2021.02.13
반응형

www.acmicpc.net/problem/14391

 

14391번: 종이 조각

영선이는 숫자가 쓰여 있는 직사각형 종이를 가지고 있다. 종이는 1×1 크기의 정사각형 칸으로 나누어져 있고, 숫자는 각 칸에 하나씩 쓰여 있다. 행은 위에서부터 아래까지 번호가 매겨져 있고,

www.acmicpc.net

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <iostream>
#include <algorithm>
#include <cstring>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
int arr[5][5], direction[5][5], visited[5][5];
int n, m;
int val = 0;
int total = 0;
int ans = -1;
int cur;
int temp = 0;
void func(int x, int y) {
    while(1){
        if (x >= n || y >= m || cur != direction[x][y]) {
            total += temp;
            temp = 0;
            return;
        }
        if (visited[x][y])
            return;
        visited[x][y] = 1;
        temp = temp * 10 + arr[x][y];
        if (direction[x][y] == 0)
            y++;
        else
            x++;
    }
}
int main() {
    cin >> n >> m;
    rep(i, n) {
        rep(j, m) {
            char c;
            cin >> c;
            arr[i][j] = c - '0';
        }
    }
 
    while (val < (1 << (n * m))) {
        // 1. val -> direction[][]
        int idx = 0;
        while (idx < n*m) {
            int nx = idx / m;
            int ny = idx % m;
            direction[nx][ny] = (val >> idx) & 1;
            idx++;
        }
        // 2. arr, direction, visited
        memset(visited, 0sizeof(visited));
        total = 0;
        rep(i, n) {
            rep(j, m) {
                if (!visited[i][j]) {
                    cur = direction[i][j];
                    func(i,j);
                }
            }
 
        }
        ans = max(ans, total);
        val++;
    }
    cout << ans;
}
cs
반응형

'백준 > 브루트포스' 카테고리의 다른 글

백준 1208 [복습 필수]  (0) 2021.03.06
백준 14225  (0) 2021.02.23
백준 1182  (0) 2021.02.15
백준 6064  (0) 2021.02.13
백준 1107 [복습 필수]  (0) 2021.02.13
반응형

www.acmicpc.net/problem/1182

 

1182번: 부분수열의 합

첫째 줄에 정수의 개수를 나타내는 N과 정수 S가 주어진다. (1 ≤ N ≤ 20, |S| ≤ 1,000,000) 둘째 줄에 N개의 정수가 빈 칸을 사이에 두고 주어진다. 주어지는 정수의 절댓값은 100,000을 넘지 않는다.

www.acmicpc.net

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
int arr[21];
int n, s, sum, ans = 0;
 
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cin >> n >> s;
    rep(i, n)
        cin >> arr[i];
    
    int mask = 1;
    while (mask < (1 << n)) {
        sum = 0;
        for (int i = 0;i < n;i++)
            sum += arr[i] * ((mask >> i) & 1);
        if (sum == s)
            ans++;
        mask++;
    }
    cout << ans;
}
cs
반응형

'백준 > 브루트포스' 카테고리의 다른 글

백준 14225  (0) 2021.02.23
백준 14391  (0) 2021.02.15
백준 6064  (0) 2021.02.13
백준 1107 [복습 필수]  (0) 2021.02.13
백준 1476  (0) 2021.02.13
반응형

www.acmicpc.net/problem/6064

 

6064번: 카잉 달력

입력 데이터는 표준 입력을 사용한다. 입력은 T개의 테스트 데이터로 구성된다. 입력의 첫 번째 줄에는 입력 데이터의 수를 나타내는 정수 T가 주어진다. 각 테스트 데이터는 한 줄로 구성된다.

www.acmicpc.net

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
using namespace std;
int t, m, n, x, y;
int gcd(int x, int y) {
    int r;
    while (y != 0) {
        r = x % y;
        x = y;
        y = r;
    }
    return x;
}
int lcm(int x, int y) {
    int g = gcd(x, y);
    return (x / g) * g * (y / g);
}
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin >> t;
    while (t--) {
        cin >> m >> n >> x >> y;
        int last = lcm(m,n);
        while (1) {
            if (x > last) {
                cout << -1 << '\n';
                break;
            }
            
            if ((x - 1) % n == y - 1) {
                cout << x << '\n';
                break;
            }
            x += m;
        }
    }
}
cs
반응형

'백준 > 브루트포스' 카테고리의 다른 글

백준 14391  (0) 2021.02.15
백준 1182  (0) 2021.02.15
백준 1107 [복습 필수]  (0) 2021.02.13
백준 1476  (0) 2021.02.13
백준 3085  (0) 2021.02.13
반응형

www.acmicpc.net/problem/1107

 

1107번: 리모컨

첫째 줄에 수빈이가 이동하려고 하는 채널 N (0 ≤ N ≤ 500,000)이 주어진다.  둘째 줄에는 고장난 버튼의 개수 M (0 ≤ M ≤ 10)이 주어진다. 고장난 버튼이 있는 경우에는 셋째 줄에는 고장난 버튼

www.acmicpc.net

반례 모음 www.acmicpc.net/board/view/46120

 

글 읽기 - [반례모음]

댓글을 작성하려면 로그인해야 합니다.

www.acmicpc.net

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
#include <algorithm>
#include <cstring>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
int n, m, now = 100, ans = 987654321;
int button[11];
void func(int num, int cnt) {
    // main에서 처음 넘겨주는 num이 0이므로
    // 숫자를 하나도 추가하지 않았을 때(cnt==0)는 검사 안함
    // 처리 안해주면 반례 발생
    if(cnt > 0)
        ans = min(ans, abs(n-num) + cnt);
    
    // 종료 조건
    if (cnt == 6) {
        return;
    }
    // 백트래킹으로 숫자 증가
    for (int i = 0; i < 10; i++) {
        if (button[i]) {
            num = num * 10 + i;
            func(num, cnt +1);
            num = num - i;
            num /= 10;
        }
    }
}
int main() {
    memset(button, 1sizeof(button));
    cin >> n >> m;
    rep(i, m) {
        int x;
        cin >> x;
        button[x] = 0;
    }
    ans = abs(n - 100);
    
    func(00);
    cout << ans;
}
cs
반응형

'백준 > 브루트포스' 카테고리의 다른 글

백준 1182  (0) 2021.02.15
백준 6064  (0) 2021.02.13
백준 1476  (0) 2021.02.13
백준 3085  (0) 2021.02.13
백준 1436  (0) 2021.01.25
반응형

www.acmicpc.net/problem/1476

 

1476번: 날짜 계산

준규가 사는 나라는 우리가 사용하는 연도와 다른 방식을 이용한다. 준규가 사는 나라에서는 수 3개를 이용해서 연도를 나타낸다. 각각의 수는 지구, 태양, 그리고 달을 나타낸다. 지구를 나타

www.acmicpc.net

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
using namespace std;
int a, b, c;
int main() {
    cin >> a >> b >> c;
    if (a == b == c) {
        cout << a;
        return 0;
    }
    int i = 1;
    while (1) {
        if ((i % 15 + 1== a && (i % 28 + 1== b && (i % 19 + 1== c) {
            cout << i+1;
            break;
        }
        i++;
    }
}
cs
반응형

'백준 > 브루트포스' 카테고리의 다른 글

백준 6064  (0) 2021.02.13
백준 1107 [복습 필수]  (0) 2021.02.13
백준 3085  (0) 2021.02.13
백준 1436  (0) 2021.01.25
백준 1018  (0) 2021.01.25
반응형

www.acmicpc.net/problem/3085

 

3085번: 사탕 게임

첫째 줄에 상근이가 먹을 수 있는 사탕의 최대 개수를 출력한다.

www.acmicpc.net

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <iostream>
#include <algorithm>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
int n;
char arr[51][51];
void swap(int& a, int& b) {
    int tmp = a;
    a = b;
    b = tmp;
}
int chk(int i, int j) {
    int hor = 0, ver = 0, x, y;
    char c = arr[i][j];
    // 가로 확인
    // 좌
    x = i;
    y = j;
    while (y - 1 >= 0 && arr[x][y - 1== c) {
        hor++;
        y--;
    }
    // 우
    x = i;
    y = j;while (y + 1 < n && arr[x][y + 1== c) {
        hor++;
        y++;
    }
    //세로 확인
    // 상
    x = i;
    y = j;while (x - 1 >= 0 && arr[x-1][y] == c) {
        ver++;
        x--;
    }
    // 하
    x = i;
    y = j;
    while (x + 1 < n  && arr[x+1][y] == c) {
        ver++;
        x++;
    }
    return max(hor, ver);
}
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin >> n;
    rep(i, n) {
        rep(j, n)
            cin >> arr[i][j];
    }
    int ans = -1, ret;
    rep(i, n) {
        rep(j, n) {
            if (j + 1 < n) {
                swap(arr[i][j], arr[i][j + 1]);
                ret = chk(i, j);
                ans = max(ans, ret);
                ret = chk(i, j+1);
                ans = max(ans, ret);
                swap(arr[i][j], arr[i][j + 1]);    // roll back
            }
            if (i + 1 < n) {
                swap(arr[i][j], arr[i + 1][j]);
                ret = chk(i, j);
                ans = max(ans, ret);
                ret = chk(i+1, j);
                ans = max(ans, ret);
                swap(arr[i][j], arr[i + 1][j]);    // roll back
            }
        }
    }
    cout << ans + 1;
}
cs
반응형

'백준 > 브루트포스' 카테고리의 다른 글

백준 6064  (0) 2021.02.13
백준 1107 [복습 필수]  (0) 2021.02.13
백준 1476  (0) 2021.02.13
백준 1436  (0) 2021.01.25
백준 1018  (0) 2021.01.25

+ Recent posts