题链:

https://www.codechef.com/problems/SEAGM
题解:

概率dp,博弈论
详细题解:http://www.cnblogs.com/candy99/p/6504340.html
本体的先手胜与不胜(第一问)以及胜的概率(第二问)都是通过dp完成的,记忆化搜索实现。

定义了一个非常妙的dp状态:(第一问)
dp[g][c]表示当前选的数的gcd为g,且选了c个数时当前操作的人胜还是不胜。
有了这个状态,配合预处理的cnt[g]数组(表示有cnt[g]个数为g的倍数),
就可以很巧妙的实现dp转移:
0.if(g==1) dp[g][c]=1
1.if(c<cnt[g]&&!dp[g][c+1]) dp[g][c]=1
2.if(gcd(A[i],g)!=g&&!dp[gcd(A[i],g)][c+1]) dp[g][c]=1
第二问与第一问的方法相同,只是换成了随机情况下求概率而已。
(如果看不太懂各种解释,建议直接服用代码。2333)

代码:

#include<bits/stdc++.h>
#define MAXN 105
using namespace std;
int N,Case;
int A[MAXN],cnt[MAXN];
int opt[MAXN][MAXN];
double rad[MAXN][MAXN];
int gcd(int a,int b){
while(b^=a^=b^=a%=b);
return a;
}
int dfs_optimaly(int g,int c){
int &ret=opt[g][c];
if(g==1) ret=1;
if(ret!=-1) return ret;
ret=0;
if(c<cnt[g]&&!dfs_optimaly(g,c+1)) ret=1;
for(int i=1;i<=N;i++){
int gg=gcd(g,A[i]);
if(gg==g) continue;
if(!dfs_optimaly(gg,c+1)) ret=1;
}
return ret;
}
double dfs_randomly(int g,int c){
double &ret=rad[g][c];
if(g==1) ret=1;
if(ret>-0.5) return ret;
ret=0;
if(c<cnt[g]) ret+=1.0*(cnt[g]-c)/(N-c)*(1-dfs_randomly(g,c+1));
for(int i=1;i<=N;i++){
int gg=gcd(g,A[i]);
if(gg==g) continue;
ret+=1.0/(N-c)*(1-dfs_randomly(gg,c+1));
}
return ret;
}
int main(){
for(scanf("%d",&Case);Case;Case--){
scanf("%d",&N); int g=0;
for(int i=1;i<=N;i++) scanf("%d",&A[i]),g=gcd(g,A[i]);
if(g!=1){printf("%d %.4lf\n",N&1,1.0*(N&1)); continue;}
for(int i=0;i<=100;i++){
cnt[i]=0;
for(int j=0;j<=N;j++)
opt[i][j]=-1,rad[i][j]=-1;
}
for(int i=2;i<=100;i++)
for(int j=1;j<=N;j++)
if(gcd(i,A[j])==i) cnt[i]++;
printf("%d ",dfs_optimaly(0,0));
printf("%.4lf\n",dfs_randomly(0,0));
}
return 0;
}

  

●CodeChef Sereja and Game的更多相关文章

  1. CodeChef Sereja and Game [DP 概率 博弈论]

    https://www.codechef.com/problems/SEAGM 题意: n个数(可能存在相同的数),双方轮流取数.如果在一方选取之后,所有已选取数字的GCD变为1,则此方输.问:1 若 ...

  2. CodeChef Sereja and LCM(矩阵快速幂)

    Sereja and LCM   Problem code: SEALCM   Submit All Submissions   All submissions for this problem ar ...

  3. CodeChef Sereja and GCD

    Sereja and GCD   Problem code: SEAGCD   Submit All Submissions   All submissions for this problem ar ...

  4. codechef January Challenge 2014 Sereja and Graph

    题目链接:http://www.codechef.com/JAN14/problems/SEAGRP [题意] 给n个点,m条边的无向图,判断是否有一种删边方案使得每个点的度恰好为1. [分析] 从结 ...

  5. Codechef SEAARC Sereja and Arcs (分块、组合计数)

    我现在真的什么都不会了呢...... 题目链接: https://www.codechef.com/problems/SEAARC 好吧,这题其实考察的是枚举的功力-- 题目要求的是\(ABAB\)的 ...

  6. codechef September Challenge 2017 Sereja and Commands

    ———————————————————————————— 这道题维护一下原序列的差分以及操作的差分就可以了 记得倒着差分操作 因为题目保证操作2的l r 小与当前位置 #include<cstd ...

  7. codechef January Challenge 2017 简要题解

    https://www.codechef.com/JAN17 Cats and Dogs 签到题 #include<cstdio> int min(int a,int b){return ...

  8. contest0 from codechef

    A  CodeChef - KSPHERES 中文题意  Mandarin Chinese Eugene has a sequence of upper hemispheres and another ...

  9. 【BZOJ-3514】Codechef MARCH14 GERALD07加强版 LinkCutTree + 主席树

    3514: Codechef MARCH14 GERALD07加强版 Time Limit: 60 Sec  Memory Limit: 256 MBSubmit: 1288  Solved: 490 ...

随机推荐

  1. 项目Alpha冲刺Day10

    一.会议照片 二.项目进展 1.今日安排 解决前后台联调问题,完善全局的请求和路由跳转处理,添加空文件完善路由信息,优化界面跳转等待.完成个人信息和修改密码.修改前台数据组织和方法调用方式.解决登录和 ...

  2. Flask Session 详解

    会话session ,允许你在不同请求 之间储存信息.这个对象相当于用密钥签名加密的 cookie ,即用户可以查看你的 cookie ,但是如果没有密钥就无法修改它. from flask impo ...

  3. java unicode和字符串间的转换

    package ykxw.web.jyf; /** * Created by jyf on 2017/5/16. */ public class unicode { public static voi ...

  4. bzoj千题计划275:bzoj4817: [Sdoi2017]树点涂色

    http://www.lydsy.com/JudgeOnline/problem.php?id=4817 lct+线段树+dfs序 操作1:access 操作2:u到根的-v到根的-lca到根的*2+ ...

  5. XML之自动生成类,添加,修改,删除类的属性

    1. class ClassHelperDemo { public static void Main() { #region 演示一:动态生成类. //生成一个类t. Type t = ClassHe ...

  6. OO第一次作业总结

    OO第一次学习总结 1.第一次作业:多项式加法 从未接触过java的我,在从输入输出开始学了几天后,按照C语言的思路,写出了一个与面向过程极其接近的程序. 在这个程序中,存在两个类:一个是Comput ...

  7. centos7.4下离线安装CDH5.7

    (一)安装前的规划 (1)操作系统版本:centos7.4(64bit) [root@hadoop22 etc]# more /etc/centos-release CentOS Linux rele ...

  8. Mego(08) - 高级建模

    对于模型建立Mego还提供了一些高级主题 数据库函数映射 我们可以将现有的CLR方法映射到指定数据库的标题函数上,如下所示 public class OrderManageEntities : DbC ...

  9. AngularJS1.X学习笔记13-路由

    ThinkPHP框架有路由的概念,看起来路由更多的是后端的事情,Angular怎么也会跑出个路由呢?事实上,Angular是着眼于单页应用的,他的一个应用一般来说是一个页面,你所看到的页面内容的改变, ...

  10. Python内置函数(16)——ord

    英文文档: ord(c) Given a string representing one Unicode character, return an integer representing the U ...