Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions 9월 23일 - 정수론 및 조합론/11050.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

#include <iostream>

using namespace std;

int main(){
int n, k, ans = 1;
cin >> n >> k;

for(int i=0; i<k; i++){
ans *= n;
n--;
}

int temp = k;
for(int i=0; i<k; i++){
ans /= temp;
temp--;
}
Comment on lines +10 to +19
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이 문제는 간단해서 이렇게 풀어도 괜찮지만, 사실 이건 하드코딩에 가깝습니다!
이항계수 공식이나 파스칼의 삼각형을 이용해서 코드를 정리하는게 좋아요


cout << ans;
}
41 changes: 41 additions & 0 deletions 9월 23일 - 정수론 및 조합론/2841.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include <iostream>
#include <stack>

using namespace std;

int main(){
int n, p;
cin >> n >> p;

stack<int> line[7];

int ans = 0;
for(int i=0; i<n; i++){
int k, v;
cin >> k >> v;
//줄에 눌린 프렛이 없을 때
if(line[k].empty()){
line[k].push(v);
ans++;
}
//더 낮은 프렛이 눌려있을 때
else if(v > line[k].top()){
Comment on lines +17 to +22
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이 두 조건도 같은 일을 하고 있으니 합칠 수 있구요!

line[k].push(v);
ans++;
continue;
}
//더 높은 프렛이 눌려있을 때
else if(v < line[k].top()){
while(!line[k].empty() && v < line[k].top()){
Comment on lines +28 to +29
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이 2개는 합칠 수 있겠네요!

line[k].pop();
ans++;
}
if(line[k].empty() || line[k].top() != v) {
line[k].push(v);
ans++;
}
}
}
cout << ans;
}

40 changes: 40 additions & 0 deletions 9월 23일 - 정수론 및 조합론/6588.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include <iostream>
#include <cmath>
#include <vector>

using namespace std;

int main(){
cin.tie(NULL);
ios::sync_with_stdio(0);

vector<bool> is_prime(1000001, true);
Comment on lines +10 to +11
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

숫자를 바로 쓰는 것보단 상수로 선언해서 쓰는게 좋아요!

//에라토스테네스의 체
for(int i=2; i<=sqrt(1000001); i++){
if(is_prime[i]){
for(int j=i*i; j<=1000001; j += i){
is_prime[j] = false;
}
}
}
Comment on lines +12 to +19
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이 부분은 에라토스테네스의 체 연산 부분이니 함수로 만들어주세요


while(true){
int n, a, b;
cin >> n;

if(n == 0)
break;

for(int i=3; i<=n/2; i++){
Comment on lines +27 to +28
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

n/2까지만 돌아도 된다는걸 캐치하셨네요! 3부터 시작해도 된다는 것두요!
근데 연산을 줄일 수 있는 방법이 하나 더 있어요

if(is_prime[i] && is_prime[n-i]) {
a = i;
b = n - i;
break;
}
}

cout << n << " = " << a << " + " << b << '\n';

}
}

32 changes: 32 additions & 0 deletions 9월 23일 - 정수론 및 조합론/9613.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include <iostream>
#include <set>
#include <vector>

using namespace std;

int getGcd(int a, int b){
if(b == 0)
return a;
return getGcd(b, a%b);
}

int main(){
int t;
cin >> t;

for(int i=0; i<t; i++){
int n, sum = 0;
cin >> n;
vector<int> num(n, 0);
for(int j=0; j<n; j++){
cin >> num[j];
}
for(int j=0; j<n-1; j++){
for(int k=j+1; k<n; k++){
sum += (getGcd(num[j], num[k]));
}
}

cout << sum << '\n';
}
}