EOJ2108 小强函数

链接:http://www.acm.cs.ecnu.edu.cn/problem.php?problemid=2108

题意是约瑟夫环问题的嵌套。由于这题的数据多大,导致不得不用公式法,设2^k<=n<2^(k+1),则题解为(n-2^k)+1

这题由于n可以到2^63-1,所以在写函数的时候必须要用unsigned long long,不然会溢出

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstdlib>
 4 #include <cstring>
 5 #include <string>
 6 #include <algorithm>
 7 using namespace std;
 8  
 9 unsigned long long f(unsigned long long n)
10 {
11     unsigned long long i = 1;
12     while (n >= 2 * i)
13         i *= 2;
14     return (n - i) * 2 + 1;
15 } 
16 
17 int main()
18 {
19     unsigned long long a, b;
20     while (scanf("%llu%llu", &a, &b) != EOF)
21     {
22         while (b--)
23         {
24             if (a == f(a))
25                 break;
26             a = f(a);
27         }
28         printf("%llu\n", a);
29     }
30     return 0;
31 }
View Code
原文地址:https://www.cnblogs.com/KimKyeYu/p/3129191.html