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;
    }

};

  

时间: 2024-08-28 00:34:48

topcoder srm 701 div1 -3的相关文章

Topcoder SRM 643 Div1 250&lt;peter_pan&gt;

Topcoder SRM 643 Div1 250 Problem 给一个整数N,再给一个vector<long long>v; N可以表示成若干个素数的乘积,N=p0*p1*p2*......*pn,我们假设p0,p1,...,pn是单调不降的,那么v里存储的是下标为偶数 的N的质因数p0,p2,p4,...,p(2k).现在要求写一个程序,返回一个vector<long long>ans; ans里存储的是p0,p1,p2,...,pn. Limits Time Limit(m

Topcoder SRM 648 Div1 250

Problem 给一个长度为N的"AB"字符串S,S只含有两种字符'A' 或 'B',设pair(i,j)(0=<i<j<N)表示一对 i,j 使得S[i]='A',S[j]='B'.现给定一个K,求字符串S,使得pair(i,j)的个数恰好为K.若不存在,则返回空串. Limits Time Limit(ms): 2000 Memory Limit(MB): 256 N: [2, 50] K: [0 , N*(N-1)/2 ] Solution 若K>(N/2

Topcoder SRM 627 div1 HappyLettersDiv1 : 字符串

Problem Statement      The Happy Letter game is played as follows: At the beginning, several players enter the field. Each player has a lowercase English letter on their back. The game is played in turns. In each turn, you select two players with dif

topcoder srm 738 div1 FindThePerfectTriangle(枚举)

Problem Statement      You are given the ints perimeter and area. Your task is to find a triangle with the following properties: The coordinates of each vertex are integers between 0 and 3000, inclusive. The perimeter of the triangle must be exactly

Topcoder SRM 603 div1题解

昨天刚打了一场codeforces...困死了...不过赶在睡前终于做完了- 话说这好像是我第一次做250-500-1000的标配耶--- Easy(250pts): 题目大意:有一棵树,一共n个节点,每个节点都有一个权值,两人A和B分别进行操作,由A先手,每人可以选择一条边,将它删掉得到两个联通块.游戏不断进行下去,最后只剩下一个节点.A希望最后的节点权值尽可能大,B希望尽可能小,求这个最后的值.数据保证n<=50. 这道题真的是博弈好题啊-(感觉放到ACM很合适啊) 我们考虑第一次A会如何选

topcoder srm 320 div1

problem1 link 两个数字后面都有阶乘符号,可以抵消. import java.util.*; import java.math.*; import static java.lang.Math.*; public class ExtraordinarilyLarge { public String compare(String x, String y) { boolean both=false; while(x.endsWith("!")&&y.endsWit

topcoder srm 712 div1 -23

1.给定两个长度为$n$的数组$A,B$.有两种操作可以作用于数组$A$.第一种,将每个元素左侧的相邻数字加到其上:第二种,将每个元素右侧的相邻数字加到其上.0的左侧是$n-1$,$n-1$的右侧是0.比如1,2,3,4执行第一种操作后是5,3,5,7.给出一个不超过100的操作序列使得$A$变成$B$.$n \leq 50$. 思路:将$a_{0},a_{1},...,a_{n-1}$看做$a_{0}x^{0}+a_{1}x^{1}+...+a_{n-1}x^{n-1}$.那么第一种操作相当于

Topcoder SRM 345 Div1 250

题意:起初在(0,0),要到(x,y)去,每次只能横向或纵向移动.横向移动时,若所在直线y为偶数,那么只能往x轴正方向移动,若为奇数,只能往x轴反方向移动:纵向移动时,若所在直线x为偶数,那么只能往y轴正方向移动,若为奇数,只能往y轴反方向移动.问从起点到终点的最短距离是多少? x,y 范围是[-1e6, 1e6] 解法:一开始想到bfs(想到很自然),将(0, 0), (x, y), (x, 0), (0, y)这4个点分别周围9个点(包括自己)作为可达点.bfs处理出(0, 0)到(x, y

Topcoder SRM 653 Div1 250

Problem N个人坐成一排,属于同一个公司的人都坐在一起(挨在一起),但不知道谁属于哪个公司.现在问其中的某些人 他属于哪个公司,他的回答是Ai,Ai=0表示此人未被询问,否则表示他的回答.题目保证一定存在一种分组方案使得属于同一公司的人都坐在一起.现问方案是否唯一? Limits TimeLimit(ms):2000 MemoryLimit(MB):256 N,Ai∈[1,100] Solution 设dp[i]表示从i位置开始往后分组的情况,dp[i]=0表示无法分组,dp[i]=1表示