topcoder srm 701 div1 -3
1、一堆石子有$n$个,Alice,Bob轮流拿,给定每个人每次可以拿的石子的数目的集合。谁先不能拿谁输。问谁能赢?
思路:对于先手来说,输赢的局面一定是从某个数字开始呈循环状态。所以找到这个循环开始的位置和循环的长度就能判断$n$是不是赢的局面。
#include <string.h>
#include <stdio.h>
#include <vector>
#include <string>
#include <set>
#include <algorithm>
#include <map>
using namespace std; const int N=666; int f[N],g[N];
vector<int> A,B; int dfs1(int x);
int dfs2(int x); int dfs2(int x) {
if(g[x]!=-1) return g[x];
if(B.size()==0||x<B[0]) return g[x]=0;
for(int i=0;i<(int)B.size();++i) {
if(x>=B[i]&&!dfs1(x-B[i])) {
return g[x]=1;
}
}
return g[x]=0;
} int dfs1(int x) {
if(f[x]!=-1) return f[x];
if(A.size()==0||x<A[0]) return 0;
for(int i=0;i<(int)A.size();++i) {
if(x>=A[i]&&!dfs2(x-A[i])) {
return f[x]=1;
}
}
return f[x]=0;
} int check(int L,int s) {
int s1=s;
int s2=s+L;
int s3=s+L*2;
for(int i=0;i<L;++i) {
if(f[s1+i]!=f[s2+i]||f[s1+i]!=f[s3+i]) return 0;
}
return 1;
} class PartisanGame { public:
string getWinner(int n,vector<int> a,vector<int> b) {
memset(f,-1,sizeof(f));
memset(g,-1,sizeof(g));
A=a;
B=b;
sort(A.begin(),A.end());
sort(B.begin(),B.end());
for(int i=0;i<N;++i)
{
f[i]=dfs1(i);
}
if(n<N) {
if(f[n]) return "Alice";
return "Bob";
}
for(int L=50;L<N/3;++L) {
for(int i=1;i+L+L+L<N;++i) {
if(check(L,i)) {
if(f[i+(n-i)%L]) return "Alice";
return "Bob";
}
}
}
} };
2、给定一个长度为$m$的字符串$s$,按照如下的算法产生一个包含$2^{m}$个串的集合$collection$。将这个集合的字符串排序。输出第$k$个字符串。
start with an empty collection
for each subset X of the set {1,2,...,m}:
take a new string t and initialize it to the given string s
for i = 1,2,...,m:
if X contains i:
reverse the last i characters of t
add the string t to the collection
思路:对于一个最终的串$result[0~m-1]$来说,$s$的每个字符要么放在当前$result$的开头,要么放在结尾。即令$L=0,R=m-1$,那么:
for each c from s[0] to s[m-1],
1 result[L++]=c;
2 result[R--]=c
只能二选一。这种构造等同于上面反转最后若干字符的构造方法。设$s$='123456',1在前,2在后,3在前,4在后,5在前,6在前,那么最后的串为'135642',这对应上面的 $X$集合为${2,3,4,5}$。这样可以用$n^{2}$的方法判断最后的答案有前缀$p$的方案数。$f[i][j]$表示处理完了$s$的前$i$个字符其中$j$个选择了操作1,即放在前面的方案数。
#include <string.h>
#include <stdio.h>
#include <vector>
#include <string>
#include <set>
#include <algorithm>
#include <map>
using namespace std; const int N=55; long long f[N][N]; long long cal(string s,string p) {
memset(f,0,sizeof(f));
f[0][0]=1;
int n=(int)s.size();
int m=(int)p.size();
for(int i=0;i<n;++i) {
for(int j=0;j<=i;++j) {
if(j>=m||s[i]==p[j]) f[i+1][j+1]+=f[i][j];
int r=n-1-(i-j);
if(r>=m||s[i]==p[r]) f[i+1][j]+=f[i][j];
}
}
long long ans=0;
for(int i=0;i<=n;++i) ans+=f[n][i];
return ans;
} class KthStringAgain { public:
string getKth(string s,long long k) {
int n=(int)s.size();
string ans="";
for(int i=0;i<n;++i) {
for(char c='a';c<='z';++c) {
long long tmp=cal(s,ans+c);
if(tmp<k) {
k-=tmp;
}
else {
ans+=c;
break;
}
}
}
return ans;
} };
topcoder srm 701 div1 -3的更多相关文章
- Topcoder SRM 643 Div1 250<peter_pan>
Topcoder SRM 643 Div1 250 Problem 给一个整数N,再给一个vector<long long>v; N可以表示成若干个素数的乘积,N=p0*p1*p2*... ...
- Topcoder Srm 726 Div1 Hard
Topcoder Srm 726 Div1 Hard 解题思路: 问题可以看做一个二分图,左边一个点向右边一段区间连边,匹配了左边一个点就能获得对应的权值,最大化所得到的权值的和. 然后可以证明一个结 ...
- topcoder srm 714 div1
problem1 link 倒着想.每次添加一个右括号再添加一个左括号,直到还原.那么每次的右括号的选择范围为当前左括号后面的右括号减去后面已经使用的右括号. problem2 link 令$h(x) ...
- topcoder srm 738 div1 FindThePerfectTriangle(枚举)
Problem Statement You are given the ints perimeter and area. Your task is to find a triangle wi ...
- Topcoder SRM 602 div1题解
打卡- Easy(250pts): 题目大意:rating2200及以上和2200以下的颜色是不一样的(我就是属于那个颜色比较菜的),有个人初始rating为X,然后每一场比赛他的rating如果增加 ...
- Topcoder SRM 627 div1 HappyLettersDiv1 : 字符串
Problem Statement The Happy Letter game is played as follows: At the beginning, several players ...
- Topcoder SRM 584 DIV1 600
思路太繁琐了 ,实在不想解释了 代码: #include<iostream> #include<cstdio> #include<string> #include& ...
- TopCoder SRM 605 DIV1
604的题解还没有写出来呢.先上605的. 代码去practice房间找. 说思路. A: 贪心,对于每个类型的正值求和,如果没有正值就取最大值,按着求出的值排序,枚举选多少个类型. B: 很明显是d ...
- topcoder srm 575 div1
problem1 link 如果$k$是先手必胜那么$f(k)=1$否则$f(k)=0$ 通过对前面小的数字的计算可以发现:(1)$f(2k+1)=0$,(2)$f(2^{2k+1})=0$,(3)其 ...
随机推荐
- Mongodb内嵌数组的完全匹配查询
样例数据: { "cNo" : "11", "Details" : [ { &q ...
- Hessian---java远程通讯 (zhuan)
http://blog.csdn.net/harderxin/article/details/22669383 (zhuan)
- factory源码分析——component_registry和object_registry
registry类主要是为object和component提供一个轻量级的代理(lightweight proxy)来方便factory实现: registry class从uvm_object_wr ...
- Maven的作用、用途、内涵、愿景
maven被许多人认为是一个构建工具.许多人最初是从熟悉ant而转到maven的,因此很自然地这样认为maven是一个构建工具.但是maven并不仅仅是一个构建工具,也不是ant的一个替代工具.mav ...
- FileInputStream FileOutputStream
FileInputStream is a stream to grab the information from files.Combined with FileOutputStream, we ca ...
- Java -cp 命令查看 zookeeper 日志
- Java注解的原理
自Java5.0版本引入注解之后,它就成为了Java平台中非常重要的一部分.开发过程中,我们也时常在应用代码中会看到诸如@Override,@Deprecated这样的注解.这篇文章中,我将向大家讲述 ...
- 模拟windows全盘搜索
循环遍历pc上的文件夹,保存到mysql数据库中,搜索时,从数据库取数据.import osimport datetimeimport pymysqlimport threading def link ...
- 如何避免Scrum敏捷开发团队反思会形式化,海星法介绍
如何避免Scrum敏捷开发团队反思会形式化? 迭代压力很大,根本没时间,而且,反思会上大家都在互相推脱责任,会议成了“批斗大会”,所以团队的人都觉得这个会很鸡肋. 很多团队在开反思会时是这么干的:产品 ...
- spring boot 概念
最近新版本迭代,一直在弄框架替换和新技术实现的事儿. 本来想仔细介绍一下Spring Boot的各种东西,后来发现没啥写的,Spring Boot 说白了就是把你开发过程中用到的各种框架给你封装了一下 ...