반응형

https://www.acmicpc.net/problem/11311

 

11311번: Jug Hard

The first line contains T, the number of test cases (1 ≤ T 100 000). Each test case is composed of three integers: a b d where a and b (1 ≤ a, b ≤ 10 000 000) are the volumes of the two jugs, and d is the desired volume of water to be generated. You

www.acmicpc.net

 

 

[ 문제풀이 ]

 

1. a, b, d를 입력받고, d는 무조건 a 또는 b보다 작아야 d를 만들 수 있으므로 a >= d || b >= d인지 확인합니다.

 

2. a와 b를 이용해 만들 수 있는 수는 a와 b의 최대 공약수의 배수이면서 a와 b 중 더 큰 값보다 작거나 같은 수이므로 d가 a와 b의 최대공약수의 배수이면 Yes를 아니라면 No를 출력합니다. 

 

[ 소스코드 ]

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
#include<iostream>
 
using namespace std;
 
int T;
int a, b, d;
 
int euclid(int a, int b)
{
    while (b) {
        int tmp = a % b;
        a = b;
        b = tmp;
    }
 
    return a;
}
 
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
 
    cin >> T;
 
    while (T--) {
        cin >> a >> b >> d;
 
        if (a >= d || b >= d) {
            int tmp = euclid(a, b);
 
            if (d % tmp == 0) {
                cout << "Yes";
            }
            else{
                cout << "No";
            }
        }
        else {
            cout << "No";
        }
 
        cout << '\n';
    }
}
cs
반응형
반응형

https://www.acmicpc.net/problem/14881

 

14881번: 물통 문제

용량이 a, b 리터인 두 물통이 있다. 이때, 물을 적절히 부어서 정확하게 c리터를 만들 수 있는지 아닌지 구하는 프로그램을 작성하시오. 물은 무한히 많다.

www.acmicpc.net

 

 

[ 문제풀이 ]

 

1. a, b, c를 입력받고, c는 무조건 a 또는 b보다 작아야 c를 만들 수 있으므로 a >= c || b >= c인지 확인합니다.

 

2. a와 b를 이용해 만들 수 있는 수는 a와 b의 최대 공약수의 배수이면서 a와 b 중 더 큰 값보다 작거나 같은 수이므로 c가 a와 b의 최대공약수의 배수이면 YES를 아니라면 NO를 출력합니다. 

 

[ 소스코드 ]

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
#include<iostream>
 
using namespace std;
 
int T;
int a, b, c;
 
int euclid(int a, int b)
{
    while (b) {
        int tmp = a % b;
        a = b;
        b = tmp;
    }
 
    return a;
}
 
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
 
    cin >> T;
 
    while (T--) {
        cin >> a >> b >> c;
 
        if (a >= c || b >= c) {
            int tmp = euclid(a, b);
 
            if (c % tmp == 0) {
                cout << "YES";
            }
            else{
                cout << "NO";
            }
        }
        else {
            cout << "NO";
        }
 
        cout << '\n';
    }
}
cs
반응형
반응형

https://www.acmicpc.net/problem/4395

 

4395번: Steps

One steps through integer points of the straight line. The length of a step must be nonnegative and can be by one bigger than, equal to, or by one smaller than the length of the previous step. What is the minimum number of steps in order to get from x to y

www.acmicpc.net

 

 

[ 문제풀이 ]

 

1. x와 y를 입력받고, dif = y - x로 갱신합니다.

 

2. tmp = (int)sqrt(dif)로 갱신하고, ans = tmp * 2 - 1로 갱신합니다. (1 + 2 + 3 + 2 + 1 = 9, 1 + 2 + 3 + 4 + 3 + 2 + 1 = 16)

 

3. tmp * tmp < dif 라면 ans++를 해주고, tmp + 0.5 < sqrt(dif) 라면 ans++ 해줍니다.

 

4. ans를 출력합니다.

 

[ 소스코드 ]

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
#include<iostream>
#include<cmath>
 
using namespace std;
 
int n;
 
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
 
    cin >> n;
 
    while (n--) {
        int x, y;
        cin >> x >> y;
 
        int dif = y - x;
        int tmp = (int)sqrt(dif);
 
        int ans = tmp * 2 - 1;
 
        if (dif == 0) {
            cout << 0 << '\n';
        }
        else {
            if (dif - tmp * tmp != 0) {
                ans++;
                if ((double)tmp + 0.5f < sqrt(dif)) {
                    ans++;
                }
            }
 
            cout << ans << '\n';
        }
    }
}
cs
반응형
반응형

https://www.acmicpc.net/problem/17433

 

17433번: 신비로운 수

첫째 줄에 테스트 케이스의 개수 T가 주어진다. 각 테스트 케이스는 두 줄로 이루어져 있고, 첫째 줄에 N, 둘째 줄에 N개의 정수가 주어진다.

www.acmicpc.net

 

 

[ 문제풀이 ]

 

1. 입력받은 수들을 정렬하고, 인접한 수들의 차를 dif 배열에 저장합니다.

 

2. dif 배열의 인접한 수들끼리 유클리드 호제법을 사용하여 최대 공약수를 ans에 저장합니다.

 

3. ans가 0이라면 INFINITY를 출력하고, 그렇지 않다면 ans를 출력합니다. 

 

[ 소스코드 ]

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
#include<iostream>
#include<algorithm>
 
using namespace std;
 
int arr[2000];
int dif[2000];
int T, N;
int ans;
 
int euclid(int a, int b)
{
    while (b) {
        int tmp = a % b;
        a = b;
        b = tmp;
    }
 
    return a;
}
 
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
 
    cin >> T;
 
    while (T--) {
        cin >> N;
 
        for (int i = 0; i < N; i++) {
            cin >> arr[i];
        }
 
        sort(arr, arr + N);
 
        for (int i = 0; i < N - 1; i++) {
            dif[i] = arr[i + 1- arr[i];
        }
 
        ans = dif[0];
 
        for (int i = 1; i < N - 1; i++) {
            ans = euclid(ans, dif[i]);
        }
 
        if (ans == 0) {
            cout << "INFINITY\n";
        }
        else {
            cout << ans << '\n';
        }
    }
}
cs
반응형
반응형

https://www.acmicpc.net/problem/1684

 

1684번: 같은 나머지

첫째 줄에 n(1 ≤ n ≤ 1,000)이 주어진다. 다음 줄에는 절댓값이 1,000,000을 넘지 않는 n개의 정수들이 주어진다.

www.acmicpc.net

 

 

[ 문제풀이 ]

 

1. 입력받은 수들을 정렬하고, 인접한 수들의 차를 dif 배열에 저장합니다.

 

2. dif 배열의 인접한 수들끼리 유클리드 호제법을 사용하여 최대 공약수를 ans에 저장합니다.

 

3. ans를 출력합니다.

 

[ 소스코드 ]

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
#include<iostream>
#include<algorithm>
 
using namespace std;
 
int n;
int arr[1000];
int dif[1000];
int ans;
 
int euclid(int a, int b)
{
    while (b) {
        int tmp = a % b;
        a = b;
        b = tmp;
    }
 
    return a;
}
 
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
 
    cin >> n;
 
    for (int i = 0; i < n; i++) {
        cin >> arr[i];
    }
 
    sort(arr, arr + n);
 
    for (int i = 0; i < n - 1; i++) {
        dif[i] = arr[i + 1- arr[i];
    }
 
    ans = dif[0];
 
    for (int i = 1; i < n - 1; i++) {
        ans = euclid(ans, dif[i]);
    }
 
    cout << ans;
}
cs
반응형
반응형

https://www.acmicpc.net/problem/16936

 

16936번: 나3곱2

나3곱2 게임은 정수 하나를 이용한다. 가장 먼저, 정수 x로 시작하고, 연산을 N-1번 적용한다. 적용할 수 있는 연산은 두 가지 있고, 아래와 같다. 나3: x를 3으로 나눈다. x는 3으로 나누어 떨어져야

www.acmicpc.net

 

 

[ 문제풀이 ]

 

1. 수열을 입력받고, arr에 저장합니다.

 

2. 재귀함수를 이용하여 수열을 채워 나가는데, 이전 값에 2를 곱한 값이나 3을 나눈 값이 현재 수열의 값일 때만 수열에 넣어줍니다.

 

3. 모든 자리가 다 찬다면 그대로 출력해 줍니다.

 

[ 소스코드 ]

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
#include<iostream>
#define ll long long
 
using namespace std;
 
int N;
ll arr[100];
ll ans[100];
int visited[100];
bool flag = false;
 
void dfs(int level)
{
    if (flag) return;
 
    if (level == N) {
        for (int i = 0; i < N; i++) {
            cout << ans[i] << ' ';
        }
 
        flag = true;
    }
 
    for (int i = 0; i < N; i++) {
        if (visited[i] == 0 && (ans[level-1* 2 == arr[i] || (ans[level-1] % 3 == 0 && ans[level - 1/ 3 == arr[i]))) {
            visited[i] = 1;
            ans[level] = arr[i];
            dfs(level + 1);
            visited[i] = 0;
            ans[level] = 0;
        }
    }
}
 
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
 
    cin >> N;
 
    for (int i = 0; i < N; i++) {
        cin >> arr[i];
    }
 
    for (int i = 0; i < N; i++) {
        visited[i] = 1;
        ans[0= arr[i];
        dfs(1);
        ans[0= 0;
        visited[i] = 0;
    }
}
cs
반응형
반응형

https://www.acmicpc.net/problem/28421

 

28421번: 곱하기와 쿼리

길이가 $N$인 수열 $A_1, A_2, A_3, \cdots, A_N$이 주어진다. 다음 질의를 수행하는 프로그램을 작성하시오. 1 $x$: 수열에서 서로 다른 두 수 $i$, $j$를 골라 $A_i$, $A_j$를 곱하여 $x$를 만들 수 있으면 $1$, 없

www.acmicpc.net

 

 

[ 문제풀이 ]

 

1. 수열을 저장할 arr[ N ] 배열을 선언합니다.

 

2. 각 수의 개수를 저장할 box[ 10001 ] 배열을 선언합니다.

 

3. 수열을 입력받고, 각 숫자들의 개수를 box 배열에 더해줍니다.

 

4. 쿼리를 입력받을 때마다 1부터 i * i <= x까지 for문을 돌면서 숫자의 존재를 box 배열을 통해 체크하고, 두 수를 곱해서 x를 만들 수 있다면 1을 아니라면 0을 출력합니다.

 

[ 소스코드 ]

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
#include<iostream>
 
using namespace std;
 
int N, Q;
int arr[100001];
int box[20001];
 
bool check(int x)
{
    int i = 1;
 
    if (N == 1return false;
 
    if (x == 0) {
        if (box[0]) {
            return true;
        }
        else return false;
    }
 
    if (x > 10000) {
        i = x / 10000;
    }
 
    for (i; i * i <= x; i++) {
        if (box[i] && x % i == 0 && box[x / i]) {
            if (i * i == x && box[i] < 2) {
                continue;
            }
            return true;
        }
    }
 
    return false;
}
 
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
 
    cin >> N >> Q;
 
    for (int i = 1; i <= N; i++) {
        cin >> arr[i];
        box[arr[i]]++;
    }
 
    for (int i = 0; i < Q; i++) {
        int cmd, x;
        cin >> cmd >> x;
 
        if (cmd == 1) {
 
            if (check(x)) {
                cout << 1;
            }
            else {
                cout << 0;
            }
 
            cout << '\n';
        }
        else {
            box[0]++;
            box[arr[x]]--;
            arr[x] = 0;
        }
    }
}
cs
반응형
반응형

https://www.acmicpc.net/problem/19940

 

19940번: 피자 오븐

각각의 테스트 케이스마다 5개의 정수를 한 줄에 공백으로 구분해서 출력한다. 이 정수는 입력으로 주어진 시간을 만들기 위해서 ADDH, ADDT, MINT, ADDO, MINO 버튼을 누르는 횟수를 출력한 것이다. 최

www.acmicpc.net

 

 

[ 문제풀이 ]

 

1. 1분에서 65분까지의 최소 누르는 횟수를 저장합니다.

 

2. N을 60으로 나눈 값을 ADDH에 더해주고, N을 60으로 나눈 나머지 값을 이용하여 최소 누르는 횟수를 출력합니다.

 

[ 소스코드 ]

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
76
#include<iostream>
#include<queue>
 
using namespace std;
 
int T, N;
int visited[66];
 
struct node {
    int cur;
    int ans[5= { 0 };
};
 
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
 
    cin >> T;
 
    for (int t = 0; t < T; t++) {
        cin >> N;
        int temp = N / 60;
        N %= 60;
 
        queue<node> q;
        
        fill(visited, visited + 660);
 
        q.push({ 0,{temp} });
        visited[0= 1;
 
        while (!q.empty()) {
            const int cur = q.front().cur;
            int ans[5= { 0 };
            for (int i = 0; i < 5; i++) {
                ans[i] = q.front().ans[i];
            }
            q.pop();
 
            if (cur == N) {
                for (int i = 0; i < 5; i++) {
                    cout << ans[i] << ' ';
                }
                cout << '\n';
                break;
            }
 
            if (cur - 1 >= 0 && visited[max(0, cur - 1)] == 0) {
                visited[cur - 1= 1;
                q.push({ cur - 1, {ans[0],ans[1],ans[2],ans[3],ans[4+ 1} });
            }
 
            if (cur + 1 <= 65 && visited[cur + 1== 0) {
                visited[cur + 1= 1;
                q.push({ cur + 1, {ans[0],ans[1],ans[2],ans[3+ 1,ans[4]} });
            }
 
            if (cur - 10 >= 0 && visited[cur - 10== 0) {
                visited[cur - 10= 1;
                q.push({ cur - 10, {ans[0],ans[1],ans[2+ 1,ans[3],ans[4]} });
            }
 
            if (cur + 10 <= 65 && visited[cur + 10== 0) {
                visited[cur + 10= 1;
                q.push({ cur + 10, {ans[0],ans[1+ 1,ans[2],ans[3],ans[4]} });
            }
 
            if (cur + 60 <= 65 && visited[cur + 60== 0) {
                visited[cur + 60= 1;
                q.push({ cur + 60, {ans[0+ 1,ans[1],ans[2],ans[3],ans[4]} });
            }
        }
    }
}
cs
반응형
반응형

https://www.acmicpc.net/problem/1990

 

1990번: 소수인팰린드롬

151은 소수이면서 동시에 팰린드롬이기 때문에 소수인 팰린드롬이다. 팰린드롬이란 앞으로 읽어나 뒤로 읽으나 같은 수를 말한다. 예를 들어 1234는 앞으로 읽으면 1234지만, 뒤로 읽으면 4321이 되

www.acmicpc.net

 

 

[ 문제풀이 ]

 

1. 값이 10,000,000보다 큰 팰린드롬수는 없으므로 b = min(b, 10,000,000)을 통해 b를 갱신합니다.

 

2. a부터 b까지 for문을 통해 돌면서 팰린드롬 수인지 체크하고, 팰린드롬 수라면 소수인지 판정 후 출력합니다.

 

[ 소스코드 ]

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
#include<iostream>
#include<string>
#include<algorithm>
 
using namespace std;
 
int a, b;
int prime[10000001];
 
bool check(int num)
{
    string temp = to_string(num);
    string temp2 = temp;
    reverse(temp.begin(), temp.end());
 
    if (temp != temp2) return false;
    return true;
}
 
bool isPrime(int num)
{
    for (int i = 2; i * i <= num; i++) {
        if (num % i == 0return false;
    }
    return true;
}
 
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
 
    cin >> a >> b;
 
    b = min(b, 10000000);
 
    for (int i = a; i <= b; i++) {
        if (check(i)) {
            if (isPrime(i)) {
                cout << i << '\n';
            }
        }
    }
 
    cout << -1 << '\n';
}
cs
반응형
반응형

https://www.acmicpc.net/problem/4811

 

4811번: 알약

입력은 최대 1000개의 테스트 케이스로 이루어져 있다. 각 테스트 케이스는 한 줄이며, 병에 들어있는 약의 개수 N ≤ 30 가 주어진다. 입력의 마지막 줄에는 0이 하나 주어진다.

www.acmicpc.net

 

 

[ 문제풀이 ]

 

1. 카탈란수를 이용하여 점화식을 세웁니다.

 

2. 이때 최대 $_{60}\textrm{C}_{60}$까지 나올 수 있으므로 comb[ i ][ j ] = comb[ i - 1 ][ j ] + comb[ i - 1 ][ j - 1 ]의 점화식을 이용하여 미리 구해줍니다.

 

3. 카탈란수의 점화식인 $\frac{1}{n+1}\binom{2n}{n}$를 이용하여 답을 출력해 줍니다.

 

[ 소스코드 ]

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
#include<iostream>
#define ll long long
 
using namespace std;
 
ll comb[61][61];
int N;
 
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
 
    comb[0][0= 1;
    comb[1][0= 1;
    comb[1][1= 1;
 
    for (int i = 2; i < 61; i++) {
        for (int j = 0; j <= i; j++) {
            comb[i][j] = comb[i - 1][j] + comb[i - 1][j - 1];
        }
    }
 
    while (1) {
        cin >> N;
        if (N == 0return 0;
 
        cout << comb[2 * N][N] / (1LL * N + 1<< '\n';
    }
}
cs
반응형

+ Recent posts