UVA151 Power Crisis

嘟嘟嘟

 

这道题被评为紫题完全是在假(虽然我也跟风评了紫题),顶多黄题难度。

评黄题的主要原因是得知道约瑟夫递推公式,即fn = (fn - 1 +m) % n。表示n个人报数最后的获胜者,需要注意的是编号从0~n - 1,答案加1即可。

那么这道题就是枚举m,然后O(n)代入公式验证,总复杂度O(Tn2)。然后题中规定第一个人得先出去,所以实际上是n - 1个人,使12号是获胜者。

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<cmath>
 4 #include<algorithm>
 5 #include<cstring>
 6 #include<cstdlib>
 7 #include<cctype>
 8 #include<vector>
 9 #include<stack>
10 #include<queue>
11 using namespace std;
12 #define enter puts("") 
13 #define space putchar(' ')
14 #define Mem(a, x) memset(a, x, sizeof(a))
15 #define rg register
16 typedef long long ll;
17 typedef double db;
18 const int INF = 0x3f3f3f3f;
19 const db eps = 1e-8;
20 //const int maxn = ;
21 inline ll read()
22 {
23   ll ans = 0;
24   char ch = getchar(), last = ' ';
25   while(!isdigit(ch)) last = ch, ch = getchar();
26   while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
27   if(last == '-') ans = -ans;
28   return ans;
29 }
30 inline void write(ll x)
31 {
32   if(x < 0) x = -x, putchar('-');
33   if(x >= 10) write(x / 10);
34   putchar(x % 10 + '0');
35 }
36 
37 int n, m;
38 
39 int main()
40 {
41   while(scanf("%d", &n) && n)
42     {
43       n--;
44       for(int i = 1; i < n; ++i)
45     {
46       int x = 0;
47       for(int j = 2; j <= n; ++j) x = (x + i) % j;
48       if(x + 1 == 12) {write(i), enter; break;}
49     }
50     }
51   return 0;
52 }
View Code

 

转载于:https://www.cnblogs.com/mrclr/p/9949428.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值