Game with Powers
题意:
有1~n,n个数字,两个人轮流操作,每一次一个人可以拿一个数字$x$,之后$x, x^2, x^3....x^t$全都被删掉。
给定n,问最优策略下谁赢。
解法:
考虑SG函数,可以注意到题目中取走$x$后,$x^2,x^3...$不可以取,类似石子合并问题。
对于1~n的数字可以分为两类:
1.不存在$x^t, t>1$,可以视为一堆只有一块石头的石子堆,$SG(1) = 0$。
2.存在$x^t, t>1$。
对于第一种情况,直接记录有多少个x满足条件即可,分奇偶讨论。
对于第二种情况,显然有$t<=30$,可以注意到最终$SG$值和$x$无关,这样打表预处理$sg(t)$表示$x^1,x^2...x^t$
对应的$SG$值,打表直接用$O(2^30)$状压即可(注意到$sg(t)<=30$,所以用char类型的sg数组即可节省空间)。
实际上有效的状态并不多,所以只要几秒钟(意外的快)。
最后求NIM和即可。
总效率$O(\sqrt{n}*logn)$
#include <iostream>
#include <cstdio>
#include <cstring> #define N 1000010
#define LL long long using namespace std; bool flag[N];
int n;
int SG[]={,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,}; int main()
{
scanf("%d",&n);
int sum=n;
int ans=;
for(LL i=;i*i<=n;i++)
{
if(flag[i]) continue;
int tmp=;
for(LL x=i;x<=n;x*=i)
{
if(x*x<=n) flag[x]=;
tmp++;
}
ans^=SG[tmp];
sum-=tmp;
}
ans ^= sum&;
if(ans==) puts("Petya");
else puts("Vasya");
return ;
}
Game with Powers的更多相关文章
- CodeForces 404C Ivan and Powers of Two
Ivan and Powers of Two Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & % ...
- Educational Codeforces Round 15 Powers of Two
Powers of Two 题意: 让求ai+aj=2的x次幂的数有几对,且i < j. 题解: 首先要知道,排完序对答案是没有影响的,比如样例7 1一对,和1 7一对是样的,所以就可以排序之后 ...
- Educational Codeforces Round 7 F - The Sum of the k-th Powers 拉格朗日插值
The Sum of the k-th Powers There are well-known formulas: , , . Also mathematicians found similar fo ...
- cf702B Powers of Two
B. Powers of Two time limit per test 3 seconds memory limit per test 256 megabytes input standard in ...
- (Problem 29)Distinct powers
Consider all integer combinations ofabfor 2a5 and 2b5: 22=4, 23=8, 24=16, 25=32 32=9, 33=27, 34=81, ...
- UVA 10622 - Perfect P-th Powers(数论)
UVA 10622 - Perfect P-th Powers 题目链接 题意:求n转化为b^p最大的p值 思路:对n分解质因子,然后取全部质因子个数的gcd就是答案,可是这题有个坑啊.就是输入的能够 ...
- UVALive 6472 Powers of Pascal
标题手段: 他给了一个无限Pascal阵,定义了powers,然后询问power为P的pascal矩阵里面的第R行C列的元素是多少. 最開始读错题意了...然后 就成了一个神得不得了的题了.后来请教的 ...
- Educational Codeforces Round 15_B. Powers of Two
B. Powers of Two time limit per test 3 seconds memory limit per test 256 megabytes input standard in ...
- The Super Powers
The Super Powers Time Limit: 1000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu [Subm ...
- Perfect Pth Powers poj1730
Perfect Pth Powers Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 16383 Accepted: 37 ...
随机推荐
- Objective-C中单例
单例模式,由于其简单好用容易理解.同时在出问题时也容易定位的特点,在开发中经常用到的一个设计模式. 一般在程序中,经常调用的类,如工具类.公共跳转类等,我都会采用单例模式 这个写法是苹果推荐的写法 ...
- LNMPA遇到504 Gateway time-out错误的解决方法
Nginx的特点是处理静态很给力,Apache的特点是处理动态很稳定,两者结合起来便是LNMPA,nginx处理前端,apache处理后端,这样处理静态会很快,处理动态会很稳定. 当我以为安装完成以后 ...
- cocos2dx3.0 对象池
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdzE4NzY3MTA0MTgz/font/5a6L5L2T/fontsize/400/fill/I0JBQk ...
- SD 卡PIN定义
转载:http://blog.sina.com.cn/s/blog_56e19aa70101avnw.html SD卡和TF卡接口引脚定义
- ARM和X86
嵌入式简介汇总 脚本语言 编程语言 Java C# C ++ 汇编 机器语言 语言 Unix Linux Android + 塞班 + Windows + + + ios系统 基于unix内核的图形化 ...
- 笔记08 throw e 和throw 的区别
throw e对原异常进行拷贝,然后将新的异常对象抛出,这一步拷贝就有可能导致错误啦,拷贝出来的异常对象可能和原来的异常对象不是一个类型. 比如原来的对象是个子类的异常对象,catch里声明的是父类异 ...
- Django-extra的用法
## select提供简单数据 # SELECT age, (age > 18) as is_adult FROM myapp_person; Person.objects.all().extr ...
- # kubernetes调度之nodeName与NodeSelector
系列目录 Kubernetes的调度有简单,有复杂,指定NodeName和使用NodeSelector调度是最简单的,可以将Pod调度到期望的节点上. 本文主要介绍kubernetes调度框架中的No ...
- (转)Understanding C parsers generated by GNU Bison
原文链接:https://www.cs.uic.edu/~spopuri/cparser.html Satya Kiran PopuriGraduate StudentUniversity of Il ...
- hive编程入门课程(加精)
hive编程入门课程 http://wenku.baidu.com/link?url=BfyZWjz48G_6UJImzWw39OLB0sUrIYEYxoxNpaFbADUQekmOvQy4FPY1f ...