UVA10940 Throwing cards away II【数学规律+约瑟夫环】

Given is an ordered deck of n cards numbered 1 to n with card 1 at the top and card n at the bottom. The
following operation is performed as long as there are at least two cards in the deck:
Throw away the top card and move the card that is now on the top of the deck to the bottom of the deck.
Your task is to find the last, remaining card.
Input
Each line of input (except the last) contains a positive number n ≤ 500000. The last line contains ‘0’ and this line should not be processed. Input will not contain more than 500000 lines.
Output
For each number from input produce one line of output giving the last remaining card.
Sample Input
7
19
10
6
0
Sample Output
6
6
4
4

问题链接UVA10940 Throwing cards away II
问题简述:(略)
问题分析
????这个问题实际上是约瑟夫环问题,用数学规律来解才是王道,模拟法则会超时。可以用模拟法打印结果,寻找规律。打表是必要的,可以加速。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的C++语言程序如下:

/* UVA10940 Throwing cards away II */

#include <bits/stdc++.h>

using namespace std;

const int N = 5e5;
int f[N + 1];

void init()
{
    f[1] = 1;
    int tmp = 1;
    for(int i = 2; i <= N; i++) {
        if(i > tmp * 2)
            tmp *= 2;
        f[i] = 2 * (i - tmp);
    }
}

int main()
{
    init();

    int n;
    while(~scanf("%d", &n) && n)
        printf("%d\n", f[n]);

    return 0;
}

原文地址:https://www.cnblogs.com/tigerisland45/p/10371592.html

时间: 2024-10-10 06:50:51

UVA10940 Throwing cards away II【数学规律+约瑟夫环】的相关文章

UVA10940 - Throwing cards away II(找规律)

题目链接 题目大意:桌上有n张牌,按照1-n的顺序从上到下,每次进行将第一张牌丢掉,然后把第二张放到这叠牌的最后.反复进行这样的操作,直到只剩下一张牌. 解题思路:只能先暴力,将前面小点的n打印出来,看看有什么规律. 规律:f[2^k + mod] = 2*mod;(mod > 0); n = 1需要特判. 代码: #include <cstdio> #include <cstring> const int maxn = 5e5 + 5; int f[maxn]; void

【数学】约瑟夫环问题中,最后剩下的人是第几个人

题目:约瑟夫环问题中,最后剩下的人是第几个人.如一共4个人,数到2的人出列,最后剩下的那个人是第1个人,返回1. 参考:http://www.cnblogs.com/EricYang/archive/2009/09/04/1560478.html int Josephus(int n, int k) { if (n<=0 || k<1) throw exception(); if (n==1) return 1;//人为规定,n为1时返回1 int res=k%2;//只有两个人时,返回的人的

UVA 10940 Throwing cards away II

题意略: 先暴力打表发现规律 N=1 ans=1N=2 ans=2N=3 ans=2N=4 ans=4N=5 ans=2N=6 ans=4N=7 ans=6N=8 ans=8N=9 ans=2N=10 ans=4N=11 ans=6N=12 ans=8N=13 ans=10N=14 ans=12N=15 ans=14N=16 ans=16N=17 ans=2N=18 ans=4N=19 ans=6N=20 ans=8N=21 ans=10N=22 ans=12N=23 ans=14N=24 an

Throwing cards away I

Throwing cards away I   Given is an ordered deck of n cards numbered 1 to n with card 1 at the top and card n at the bottom. The following operation is performed as long as there are at least two cards in the deck:  Throw away the top card and move t

紫书第五章训练3 D - Throwing cards away I

D - Throwing cards away I Given is an ordered deck of n cards numbered 1 to n with card 1 at the top and card n at the bottom. The following operation is performed as long as there are at least two cards in the deck: Throw away the top card and move

[C++]LeetCode: 114 Permutation Sequence(返回第k个阶乘序列——寻找数学规律)

题目: The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the permutations in order, We get the following sequence (ie, for n = 3): "123" "132" "213" "231" "312" &q

HDU 1005 Number Sequence (数学规律)

Number Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 104190    Accepted Submission(s): 25232 Problem Description A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A

Throwing cards away I uva1594

 Throwing cards away I Given is an ordered deck of  n  cards numbered 1 to  n  with card 1 at the top and card  n  at the bottom. The following operation is performed as long as there are at least two cards in the deck: Throw away the top card and

UVa---------10935(Throwing cards away I)

题目: Problem B: Throwing cards away I Given is an ordered deck of n cards numbered 1 to n with card 1 at the top and card n at the bottom. The following operation is performed as long as there are at least two cards in the deck: Throw away the top car