Problem F. The Chosen One(The 2017 ACM-ICPC Asia Nanning Regional Contest, Onsite 大数)

本文介绍了一种在ACM竞赛中确定最优位置的方法,通过去除奇数位置的选手来选择最后的胜者。使用Java的大数处理,解决当参赛人数较多时的计算问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Problem F. The Chosen One
Welcome to the 2017 ACM-ICPC Asia Nanning Regional Contest.
Here is a breaking news. Now you have a chance to meet alone with the Asia Director through a game.
All boys and girls who chase their dreams have to stand in a line. They are given the numbers in the order in
which they stand starting from 1.
The host then removes all boys and girls that are standing at an odd position with several rounds.
For example if there are n = 8 boys and girls in total. Initially standing people have numbers 1; 2; 3; 4; 5; 6; 7 and
8. After the rst round, people left are 2; 4; 6 and 8. After the second round, only two people, 4 and 8, are still
there.
The one who stays until the end is the chosen one.
I know you want to become the chosen one to meet alone with your idol. Given the number of boys and girls in
total, can you nd the best place to stand in the line so that you would become the chosen one?
Input
First line of the input contains the number of test cases t (1 t 1000).
Each of the next t lines contains the integer n which is the number of boys and girls in total, where 2 n 1050.
Output
The output displays t lines, each containing a single integer which is the place where you would stand to win the
chance.
Sample
4
5
12
23
35
4
8
16
32
Page 11

大数,用java处理。

import java.math.BigInteger;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
  Scanner scan=new Scanner(System.in);
  int t;
  t=scan.nextInt();
  for(int i=0;i<t;i++)
  {
	 BigInteger n=scan.nextBigInteger();
	
     BigInteger a=new BigInteger("1");
     BigInteger b=new BigInteger("2");
     BigInteger c=a.multiply(b);
     if(b.compareTo(n)==0){
		 System.out.println("2");
		continue;
     }
     while(c.compareTo(n)<=0)
     {
    	 c=a.multiply(b);
    	 a=c;
     }
     if(c.compareTo(n)==0)
        System.out.println(c);
     
     else
    	 c=c.divide(b);
     System.out.println(c);
  }
}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值