[BOJ 16409] Coprime Integers

2023. 5. 11. 14:33Baekjoon

728x90

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

 

16409번: Coprime Integers

Given intervals [a, b] and [c, d], count the number of ordered pairs of co-prime integers (x, y) such that a ≤ x ≤ b and c ≤ y ≤ d. Coprime integers have no common factor greater than 1.

www.acmicpc.net

 

- 문제 요약

 

구간 [a, b]와 [c, d]가 주어졌을 때, a ≤ x ≤ b 및 c ≤ y ≤ d가 되는 공동 소수 정수(x, y)의 순서쌍의 수를 세십시오. 서로소 정수는 1보다 큰 공통 인수를 갖지 않습니다.

 

 

- 알고리즘 정리

 

https://panty.run/boj8291/

 

BOJ 8291 Coprime Numbers

문제: https://www.acmicpc.net/problem/8291 길이가 n인 배열에서 서로소 쌍의 수를 구하는 문제 a[i]: arr[]에서 i의 개수 b[i]: arr[]에서 i의 배수의 개수 c[i]: gcd(arr[], arr[])가 i의 배수인 쌍의 개수 d[i]: gcd(arr[],

panty.run

이 분의 블로그를 참고해서 풀었습니다.

arr[i] : gcd(x, y)가 i의 배수인 쌍의 개수

brr[i] : gcd(x, y)가 i인 쌍의 개수

 

 

- 코드 작성

 

#include<bits/stdc++.h>
using namespace std;

#define MAX 10000001
typedef long long ll;
ll a,b,c,d,arr[MAX],brr[MAX];

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	cin>>a>>b>>c>>d;
	a--;
	c--;
	for(int i=1;i<=MAX-1;i++){
		arr[i]=(b/i-a/i)*(d/i-c/i);
	}
	for(int i=MAX-1;i>=1;i--){
		brr[i]=arr[i];
		for(int j=2*i;j<=MAX-1;j+=i){
			brr[i]-=brr[j];
		}
	}
	cout<<brr[1];
}

코드 제출 결과

728x90

'Baekjoon' 카테고리의 다른 글

[BOJ 13710] XOR 합 3  (0) 2023.05.11
[BOJ 1114] 통나무 자르기  (0) 2023.05.11
[BOJ 18436] 수열과 쿼리 37  (0) 2023.05.09
[BOJ 11985] 오렌지 출하  (0) 2023.05.07
[BOJ 1135] 뉴스 전하기  (0) 2023.05.07