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