코딩도장

상품 is 뭔들 (2016 인하대 프로그래밍 경진대회 B번)

출처: https://www.acmicpc.net/problem/12779

정보통신처에서는 2016년 6월 4일 인하 광장에서 이벤트를 진행하려고 한다. 정보통신처에서 인하 광장에 올린 게시글에 N번째로 댓글을 단 모든 학생에게 상품을 지급하기로 하였다. 단, N은 약수의 개수가 홀수여야 한다. 인하 광장을 즐겨보는 찬미는 이 이벤트에 참가하기로 하였다. 찬미는 댓글을 작성한 후 자신이 상품을 받을 확률이 얼마나 되는지 궁금해졌다. 찬미가 댓글을 작성하기 전의 총 댓글 수가 a개이고, 댓글을 작성 후의 총 댓글 수가 b개일 때 찬미의 댓글은 a보다 크고 b보다 작거나 같은 범위 안에 존재한다고 한다. 예를 들어 a가 1이고, b가 4인 경우 [2, 3, 4] 중 한 곳에 댓글이 존재한다. 이 중 약수의 개수가 홀수인 숫자는 4, 한 개이므로 상품을 받을 확률은 1/3이다. 찬미를 도와 찬미가 상품을 받을 확률을 구하는 프로그램을 작성하라.

입력

입력의 첫 줄에는 정수 a와 b가 주어진다. (1 ≤ a, b ≤ 2^60) b는 a보다 항상 크다

출력

찬미가 상품을 지급받을 확률을 기약분수 형태로 출력한다. 만약 확률이 0인 경우 0을 출력한다.

예제 입력

1 4

예제 출력

1/3
수학 유클리드 호제법

2016年06月02日 15:54

iljimae

(追記) (追記ここまで)
댓글 작성은 로그인이 필요합니다.
(注記) 상대에게 상처를 주기보다 서로에게 도움이 될 수 있는 댓글을 달아 주세요.

7개의 풀이가 있습니다.

public class Anything {
 public static void main(String[] args) {
 Scanner sc = new Scanner(in);
 BigDecimal aa = new BigDecimal(sc.next());
 BigDecimal bb = new BigDecimal(sc.next());
 double aaa = aa.doubleValue();
 double bbb = bb.doubleValue();
 double ccc = bbb - aaa;
 int ddd = (int) Math.sqrt(bbb) - (int) Math.sqrt(aaa);
 if (ddd == 0L) {
 System.out.println(0);
 } else {
 System.out.printf("%d / %.0f\n", ddd, ccc);
 }
 }
}

2017年03月30日 11:34

genius.choi

댓글 작성은 로그인이 필요합니다.
(注記) 상대에게 상처를 주기보다 서로에게 도움이 될 수 있는 댓글을 달아 주세요.
package coding_2017_5;
/*
 * 약수의 갯수가 홀 수일 경우 -> 제곱수
 * 그러니까 (sqrt(n))^2 = n이 되는 수를 찾습니다.
 * 하지만 1부터 시작해서 a~b 사이를 찾고자 할 때 a가 너무 크면 시간이 오래걸리니까
 * sqrt(a)를 해 a와 가장 가까운 제곱수부터 찾기 시작합니다.
 * 
 * 확률의 기약분수는 유클리드 호제법을 이용했습니다.
 * (출처 : https://ko.wikipedia.org/wiki/%EC%9C%A0%ED%81%B4%EB%A6%AC%EB%93%9C_%ED%98%B8%EC%A0%9C%EB%B2%95)
 *
 * 1. 입력으로 두 수 m,n(m>n)이 들어온다.
 * 2. n이 0이라면, m을 출력하고 알고리즘을 종료한다.
 * 3. n이 m을 나누어떨어지면, n을 출력하고 알고리즘을 종료한다.
 * 4. 그렇지 않으면, m을 n으로 나눈 나머지를 새롭게 m에 대입하고, m과 n을 바꾸고 3으로 돌아온다.
 */
public class problem_110 {
 private static StringBuffer buffer;
 public static void main(String[] args) {
 double a = 1, b = 11;
 buffer = new StringBuffer();
 int power_count = 0;
 int i = (int) Math.sqrt(a);
 double power = i * i;
 for (; power <= b; power = i * (i++)) {
 if (power > a && power <= b)
 power_count++;
 }
 if (power_count == 0) {
 buffer.append("0");
 } else {
 double child = power_count;
 double parent = b - a;
 double gcd = euclidean(parent, child);
 parent = parent/gcd;
 child = child/gcd;
 System.out.println(gcd);
 buffer.append(child);
 buffer.append("/");
 buffer.append(parent);
 }
 System.out.println(buffer.substring(0));
 }
 // b >= a
 public static double euclidean(double b, double a) {
 double r = 0;
 double temp = 0;
 while (true) {
 r = b % a;
 if (a == 0)
 return b;
 else if(r == 0)
 return a;
 else {
 temp = b;
 b = a;
 a = temp % r;
 }
 }
 }
}

2017年05月19日 17:41

KimSeonbin

댓글 작성은 로그인이 필요합니다.
(注記) 상대에게 상처를 주기보다 서로에게 도움이 될 수 있는 댓글을 달아 주세요.
import java.util.Scanner;
public class test {
 public static void main(String[] args) {
 Scanner s = new Scanner(System.in);
 int a = s.nextInt();
 int b = s.nextInt();
 int cnt=0;
 for(int i=a+1;i<=b;i++) {
 if(IsOdd(divisor(i)))
 cnt++;
 }
 System.out.println(cnt+"/"+(b-a));
 }
 public static int divisor(int n) {
 int cnt=0 ;
 for(int i=1;i<=n;i++) {
 if(n%i==0)
 cnt++;
 }
 return cnt;
 }
 public static boolean IsOdd(int n) {
 boolean tf=false;
 if(n%2==1)
 tf=true;
 return tf;
 }
}

2017年08月01日 12:42

김재인

댓글 작성은 로그인이 필요합니다.
(注記) 상대에게 상처를 주기보다 서로에게 도움이 될 수 있는 댓글을 달아 주세요.

자바입니다.

public class itemis {
 public static void main(String[] args) {
 //단, N은 약수의 개수가 홀수여야 한다.
 Scanner sc = new Scanner(System.in);
 int a= sc.nextInt();// 댓글 작성전 총 댓글 수 
 int b = sc.nextInt();//댓글 단 후 총 댓글 수 
 //찬미 댓글은 a< <=b 
 int denominator = b-a;
 int numerator = 0;
 int GD=0;
 String result="";
 for(int i=a+1;i<=b;i++){
 if(divisorCnt(i)%2==1){
 numerator++;
 };
 }
 if(numerator==0){
 result ="0";
 }else{
 GD = GD(numerator,denominator);
 int denominator2= denominator/GD;
 int numerator2 = numerator/GD;
 result = String.valueOf(numerator2)+"/"+String.valueOf(denominator2);
 }
 System.out.println(result);
 }
 //약수의 갯수 구하는 함수
 static int divisorCnt(int x){
 int cnt=0;
 for(int i=1;i<=x;i++){
 if(x%i==0) cnt++; 
 }
 return cnt;
 }
 //최대공약수로 나눠주는 함수 
 static int GD(int a, int b){
 int tmp=0;
 if(a <b){ //b가 크다면 자리바꿈
 tmp=b;
 b=a;
 a=tmp;
 }
 //이제 항상 a가 b보다 크다.
 while(b!=a){
 a -=b;
 if(b>=a){
 while(b!=a){
 b -=a;
 }
 }
 }
 return a;
 }
}

2017年10月16日 14:00

김문수

댓글 작성은 로그인이 필요합니다.
(注記) 상대에게 상처를 주기보다 서로에게 도움이 될 수 있는 댓글을 달아 주세요.
public class test {
 public static void main(String[] args) {
 Scanner sc = new Scanner(in);
 BigDecimal aa = new BigDecimal(sc.next());
 BigDecimal bb = new BigDecimal(sc.next());
 double aaa = aa.doubleValue();
 double bbb = bb.doubleValue();
 double ccc = bbb - aaa;
 int ddd = (int) Math.sqrt(bbb) - (int) Math.sqrt(aaa);
 if (ddd == 0L) {
 System.out.println(0);
 } else {
 System.out.printf("%d / %.0f\n", ddd, ccc);
 }
 }
}

2018年06月04日 12:18

聂金鹏

댓글 작성은 로그인이 필요합니다.
(注記) 상대에게 상처를 주기보다 서로에게 도움이 될 수 있는 댓글을 달아 주세요.
public class 상품is뭔들_2016_인하대_프로그래밍_경진대회_B번 {
 public static void main(String[] args) throws IOException {
 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
 StringTokenizer st = new StringTokenizer(br.readLine());
 int a = Integer.parseInt(st.nextToken());
 int b = Integer.parseInt(st.nextToken());
 int 전체 = b-a;
 BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
 int count=0;
 for(int i=a+1; i<=b; i++) {
 for(int j=1; j<i; j++) {
 if(j*j==i) {
 count++;
 }
 }
 }
 if(count==0) {
 bw.write(0);
 bw.close();
 }
 else if(count==1) {
 bw.write(count+"/"+전체);
 bw.close();
 }
 else {
 for(int i=2; i<=count; i++) {
 if(count%i==0&&(전체)%i==0) {
 count/=i;
 전체/=i;
 }
 }
 bw.write(count+"/"+전체);
 bw.close();
 }
 }
}

버퍼를 사용해서 풀어봤습니다.

2019年11月27日 17:49

big Ko

댓글 작성은 로그인이 필요합니다.
(注記) 상대에게 상처를 주기보다 서로에게 도움이 될 수 있는 댓글을 달아 주세요.
package Calculator;
import java.util.Scanner;
import java.util.LinkedList;
import java.util.ListIterator;
public class CalculatorProblem {
 static LinkedList<Integer> list;
 public static void main(String[] args) {
 Scanner scan = new Scanner(System.in);
 list = new LinkedList<>();
 CalculatorProblem calc = new CalculatorProblem();
 while(true) {
 System.out.print("$ Input: ");
 int before = scan.nextInt();
 int after = scan.nextInt();
 // Check the bound.
 if(before<1 || before>after || after<1) {
 System.out.println("Out of bound.");
 continue;
 }
 // Initialization.
 for(int i=before+1; i<=after; i++) {
 list.add(i);
 }
 // Print list.
 ListIterator iter = list.listIterator();
 calc.printList("Before elimination", iter);
 // Removes elements with an even number of divisor.
 calc.removeElements(iter);
 calc.printList("After elimination", iter);
 // Calculate probability with winning.
 System.out.printf(" -> Probability: 1/%d\n", list.size());
 }
 }
 // Print the list.
 private void printList(String contents, ListIterator iter) {
 moveFirst(iter);
 System.out.printf(" -> %s: ", contents);
 while(iter.hasNext()) {
 System.out.printf("%d ", iter.next());
 }
 System.out.println();
 }
 // Move front of first node.
 private void moveFirst(ListIterator iter) {
 while(iter.hasPrevious()) {
 iter.previous();
 }
 }
 // Removes elements with an even number of divisor.
 private void removeElements(ListIterator iter) {
 moveFirst(iter);
 while(iter.hasNext()) {
 int index = (int)iter.next();
 if(isDivisorEven(index)%2==0) {
 iter.remove();
 } 
 }
 }
 // Check elements with an even number of divisor.
 private int isDivisorEven(int index) {
 int count = 0;
 for(int i=1; i<= index; i++) {
 if(index%i ==0)
 count++;
 }
 return count;
 }
}

리스트로 풀어봤습니다.

2022年09月15日 03:06

유로

댓글 작성은 로그인이 필요합니다.
(注記) 상대에게 상처를 주기보다 서로에게 도움이 될 수 있는 댓글을 달아 주세요.

풀이 작성

(注記) 풀이작성 안내
  • 본문에 코드를 삽입할 경우 에디터 우측 상단의 "코드삽입" 버튼을 이용 해 주세요.
  • 마크다운 문법으로 본문을 작성 해 주세요.
  • 풀이를 읽는 사람들을 위하여 풀이에 대한 설명도 부탁드려요. (아이디어나 사용한 알고리즘 또는 참고한 자료등)
  • 작성한 풀이는 다른 사람(빨간띠 이상)에 의해서 내용이 개선될 수 있습니다.
풀이 작성은 로그인이 필요합니다.
목록으로
코딩도장

코딩도장은 프로그래밍 문제풀이를 통해서 코딩 실력을 수련(Practice)하는 곳입니다.

수학 x 6
유클리드 호제법 x 1
연관 문제

언어별 풀이 현황
전 체 x 64
cpp x 2
python x 44
기 타 x 5
ruby x 1
r x 1
java x 7
javascript x 1
cs x 3
코딩도장 © 2014 · 문의 [email protected]
피드백 · 개인정보취급방침 · RSS

AltStyle によって変換されたページ (->オリジナル) /