P2071 -- A Simple Math Problem IX

时间限制:1000MS      内存限制:262144KB

状态:Accepted      标签:    数学问题-博弈论   无   无

Description

给定a,b,n,保证a≥2,b≥1,a^b≤n。两个人在玩游戏,每个人每次可以把a加1,或者把b加1,但是不能违反a^b<=n,无法再进行操作的人就输掉了这一场游戏。

假设两个人都足够聪明,按照最优策略进行游戏,问先手是否有必胜策略。

Input Format

第一行两个正整数 n 和 m。

接下来 m 行,每行两个正整数 a 和 b。保证 a≥2,b≥1,a^b≤n。

Output Format

m 行,如果对于这一对数字 a 和 b,如果先手有必胜策略,输出 “Yes”。否则输出 “No” (不含引号)

Sample Input

  1. 5 3
  2. 2 1
  3. 2 2
  4. 3 1

Sample Output

  1. Yes
  2. No
  3. No

Hint

对于10%的数据,n=2

对于70%的数据,n<=1000

对于100%的数据,n<=10^9,m<=10^5

题解

不难想到,状态(a,b)可以转移到状态(a + 1, b)或者(a, b + 1)。设F[a][b]为该局面下先手胜败,1表示必胜,0表示必败,那么F[a][b] = (F[a + 1][b] & F[a][b +1]) ^ 1。但是数据范围太大,该怎么办呢?

首先,a和b必须满足a^b <= n,即log (a, n) >= b,又因为a >= 2,所以log (2, n) >= log(a, n),所以b <= log (2, n) <= 30。

其次,a^b <= n还可以推出当b >= 2时,a <= sqrt(n),这也是一个不大的数字。当b=1时,即便n很大,但随着a的递增,可供选择的b会逐渐减少,最后转移会变成一条链,所以根据奇偶性判断就好。

代码

  1. #include <cstdio>
  2. #include <iostream>
  3. #include <cmath>
  4. #include <cstring>
  5. #define MAXN 50010
  6. #define MAXM 50
  7. using namespace std;
  8.  
  9. int N, M;
  10. int j;
  11. long long X;
  12. int A, B;
  13. int Lim;
  14. bool F[MAXN][MAXM];
  15.  
  16. char ch; int aa, bb;
  17. int Scan() {
  18. while(ch=getchar(),(ch<''||ch>'')&&ch!='-');
  19. ch=='-' ? (bb=,aa=) : (aa=ch-,bb=);
  20. while(ch=getchar(),ch>=''&&ch<='')aa=aa*+ch-;
  21. if(bb)aa=-aa;
  22. return aa;
  23. }
  24.  
  25. int main() {
  26. memset(F, , sizeof(F)); //F必须开bool
  27. N = Scan();
  28. Lim = int (sqrt(N));
  29. if ((Lim + N) & ) F[Lim + ][] = ;
  30. for (int i = Lim; i >= ; --i) {
  31. for (j = , X = ; X <= N; X *= i, ++j); //X可能会爆int
  32. for (--j; j; --j) F[i][j] = (F[i + ][j] & F[i][j + ]) ^ ;
  33. }
  34. for (M = Scan(); M; --M) {
  35. A = Scan(); B = Scan();
  36. if (B == && A > Lim) {
  37. if ((N + A) & ) printf("Yes\n");
  38. else printf("No\n");
  39. }
  40. else {
  41. if (F[A][B]) printf("Yes\n");
  42. else printf("No\n");
  43. }
  44. }
  45. }

FZYZ-2071 A Simple Math Problem IX的更多相关文章

  1. hdu 1757 A Simple Math Problem (乘法矩阵)

    A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  2. HDU1757 A Simple Math Problem 矩阵快速幂

    A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  3. hdu------(1757)A Simple Math Problem(简单矩阵快速幂)

    A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  4. A Simple Math Problem(矩阵快速幂)(寒假闭关第一题,有点曲折啊)

    A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...

  5. HDU 1757 A Simple Math Problem (矩阵快速幂)

    题目 A Simple Math Problem 解析 矩阵快速幂模板题 构造矩阵 \[\begin{bmatrix}a_0&a_1&a_2&a_3&a_4&a ...

  6. HDU 1757 A Simple Math Problem(矩阵)

    A Simple Math Problem [题目链接]A Simple Math Problem [题目类型]矩阵快速幂 &题解: 这是一个模板题,也算是入门了吧. 推荐一个博客:点这里 跟 ...

  7. HDU 1757 A Simple Math Problem (矩阵乘法)

    A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  8. hdu 5974 A Simple Math Problem

    A Simple Math Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Ot ...

  9. HDU 1757 A Simple Math Problem 【矩阵经典7 构造矩阵递推式】

    任意门:http://acm.hdu.edu.cn/showproblem.php?pid=1757 A Simple Math Problem Time Limit: 3000/1000 MS (J ...

随机推荐

  1. asp.net利用ajax和jquery-ui实现进度条

    前台用ajax不停进行查询,直到任务完成.进度条用的是jquery-ui.后台用一般处理程序处理相应,进度信息保存在HttpContext.Application中. 代码作为简单示例,实际应用时应对 ...

  2. Android 通信机制Message、Handler 的用法

    Android中提供了通信机制,Message.Handler 等,Message用于在子线程中传递数据,Handler用于发送数据到主线程中, 下面介绍基于Message.Handler的计时器 i ...

  3. mysql 安装-编码

    mysql的安装过程相对较为简单,在这里就不阐述,我想说的问题是,关于编码的安装, 在安装到达'Please select the default character set'的时候,选择'Manul ...

  4. 使用EMMET中的小坑

    使用EMMET写HTML的时候,是一个非常爽的事情.但是今天我使用时,发现一个小坑.以前倒也没有注意,不过需要非常的小心. form[action="/process" metho ...

  5. Android Parcelable Trans byte[]

    思路: http://stackoverflow.com/questions/10898116/make-custom-parcelable-containing-byte-array 谢谢, 这位外 ...

  6. 实现HTTP跳转到HTTPS

    1 首先在您的网站下新建一个站点,名称随意,在属性中分配TCP端口为80,SSL不分配 然后在属性>主目录下配置 将此资源的内容来自: 改为 重定向到URL 然后重定向到中  输入:  HTTP ...

  7. tomcat的webapp下的root文件夹的作用是什么

    1.基本一样..只是表示不同的tomcat的http路径而已. root目录默认放的是tomcat自己的一个项目,如:http://localhost:8080/默认访问root项目 对于webapp ...

  8. .net中使用JQuery Ajax判断用户名是否存在的方法

    //第一步:新建一个(*.aspx|*.html)Index.aspx页面 添加jquery 1 <html xmlns="http://www.w3.org/1999/xhtml&q ...

  9. sqlserver-事务处理

    事务的概念:简单说就访问并可能更新数据库中各种数据项的一个程序执行单元,一旦开启事务,所有对数据的操作要么全部执行,要么全部都不执行.单条sql语句本身就是一个事务. 事务的属性: 事务是作为单个逻辑 ...

  10. SGU 130.Circle

    答案为Catalan数C(2k, k)/(k+1) #include <stdio.h> using namespace std; int k; int main() { scanf(&q ...