Remove a Progression

问题描述:

You have a list of numbers from 1 to n written from left to right on the blackboard.

You perform an algorithm consisting of several steps (steps are 1-indexed). On the i-th step you wipe the i-th number (considering only remaining numbers). You wipe the whole number (not one digit).

When there are less than i numbers remaining, you stop your algorithm.

Now you wonder: what is the value of the x-th remaining number after the algorithm is stopped?

(翻译:黑板上有从左到右从1到n的数字列表。

执行一个由多个步骤组成的算法(步骤为1索引)。在第i步中,删除第i个数字(只考虑剩余的数字)。你擦掉整个数字(不是一个数字)。

当剩下的数字少于i时,停止您的算法。

现在你想知道:算法停止后,第x个剩余数字的值是多少?


Problem - A - Codeforces

输入内容:

The first line contains one integer T (1≤T≤100) — the number of queries. The next T lines contain queries — one per line. All queries are independent.(第一行包含一个整数t(1≤t≤100)-查询数。接下来的T行包含查询-每行一个。所有查询都是独立的。)

Each line contains two space-separated integers n and x (1≤x<n≤109) — the length of the list and the position we wonder about. It's guaranteed that after the algorithm ends, the list will still contain at least x numbers.(每行包含两个空格分隔的整数n和x(1≤x<n≤109)-列表的长度和我们想知道的位置。它保证在算法结束后,列表仍然包含至少x个数字。)

输出内容:

Print T integers (one per query) — the values of the x-th number after performing the algorithm for the corresponding queries.(打印T整数(每个查询一个)-执行相应查询的算法后的第x个数字的值。)

样例:

3

3  1

4  2

12  6

过程:

1:第一个结果:

当n=3,x=1时:

原始数字:1、2、3

第一步,去掉第一个数字:2、3

第二步,去掉第二个数字:2(只剩x=1个数字)

第x=1个数为:2。

2:第二个结果:

当n=4,x=2时:

原始数字:1、2、3、4

第一步,去掉第一个数字:2、3、4

第二步,去掉第二个数字:2、4(只剩x=2个数字)

第x=2个数为:4。

3:第三个结果:

当n=12,x=6时:

原始数字:1、2、3、4、5、6、7、8、9、10、11、12

第一步,去掉第一个数字: 2、3、4、5、6、7、8、9、10、11、12

第二步,去掉第二个数字:2、4、5、6、7、8、9、10、11、12

第三步,去掉第三个数字:2、4、6、7、8、9、10、11、12

第四步,去掉第四个数字:2、4、6、8、9、10、11、12

第五步,去掉第五个数字:2、4、6、8、10、11、12

第六步,去掉第六个数字:2、4、6、8、10、12(只剩x=6个数字)

第x=6个数为:12。

可以看出:每次删掉的都是奇数,且依次从1、3、5、7……开始删,最终剩下的第x个数为2x,所以这道题超级简单


代码如下:


©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Lua 5.1 参考手册 by Roberto Ierusalimschy, Luiz Henrique de F...
    苏黎九歌阅读 13,979评论 0 38
  • pyspark.sql模块 模块上下文 Spark SQL和DataFrames的重要类: pyspark.sql...
    mpro阅读 9,546评论 0 13
  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,490评论 0 10
  • 在C语言中,五种基本数据类型存储空间长度的排列顺序是: A)char B)char=int<=float C)ch...
    夏天再来阅读 3,433评论 0 2
  • 因为不会做饭,我总是被公司的程序员小哥嘲笑,他说他替我以后的老公感到难过,或者我根本会因为不会做饭而嫁不出去。 和...
    王大纯阅读 774评论 2 7