原题链接:http://codeforces.com/gym/100431/attachments/download/2421/20092010-winter-petrozavodsk-camp-andrew-stankevich-contest-37-asc-37-en.pdf

题意

给你一个n,问你有多少a和x满足:x在a中二分会返回true,其中a的长度是n

题解

考虑到二分的过程不是向左就是向右,所以可以暴力搜索搞到若干序列,这些序列都是由向左或者向右组成的。枚举x,设向左的有i个,向右的有j个,这样的序列有cnt[i][j]个,表明整个序列,确定有i个数小于等于x,有j个数大于x,那么答案就是cnt[i][j] * (x ^ (i - 1)) * ((n - x) ^ j) * (n ^ (n - i - j)),当x等于0是需要特判一下。代码是队友用java写的。

代码

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.*; public class Main {
static int n;
static BigInteger ans = new BigInteger("0");
static int xs[][] = new int[15][15];
static BigInteger tmp[] = new BigInteger[1010];
public Main() throws FileNotFoundException{
Scanner cin = new Scanner(new File("binary.in"));
PrintWriter cout = new PrintWriter(new File("binary.out"));
n = cin.nextInt();
tmp[0] = new BigInteger("1");
tmp[1] = new BigInteger(Integer.toString(n));
for(int i=2;i<=1000;i++) tmp[i] = tmp[i-1].multiply(tmp[1]);
if(n == 1){
cout.println("1");
}
else{
dfs(0,0,0,n);
for(int i=0;i<13;i++) for(int j=0;j<13;j++) if(xs[i][j] != 0){
if(i == 0){
BigInteger tmp3 = tmp[n - i - j - 1];
for(int k=1;k<=n;k++){
BigInteger tmp1 = new BigInteger(Integer.toString(k - 1));
tmp1 = tmp1.pow(j);
ans = ans.add(tmp1.multiply(tmp3).multiply(new BigInteger(Integer.toString(xs[i][j]))));
}
}
else{
for(int k=1;k<=n;k++){
BigInteger tmp1 = new BigInteger(Integer.toString(n-k));
tmp1 = tmp1.pow(j);
if(j == 0 && n == k) tmp1 = new BigInteger("1");
BigInteger tmp2 = new BigInteger(Integer.toString(k));
tmp2 = tmp2.pow(i-1);
BigInteger tmp3 = tmp[n-i-j];
ans = ans.add(tmp1.multiply(tmp2.multiply(tmp3)).multiply(new BigInteger(Integer.toString(xs[i][j]))));
}
}
}
cout.println(ans);
}
cin.close();
cout.close();
} void dfs(int x,int y,int l,int r){
if(l + 1 >= r){
xs[x][y]++;
return ;
}
int mid = (l + r) / 2;
dfs(x+1,y,mid,r);
dfs(x,y+1,l,mid);
} public static void main(String[] args) throws FileNotFoundException {
new Main();
}
}

Codeforces Gym 100431B Binary Search 搜索+组合数学+高精度的更多相关文章

  1. Codeforces Gym 100338H High Speed Trains 组合数学+dp+高精度

    原题链接:http://codeforces.com/gym/100338/attachments/download/2136/20062007-winter-petrozavodsk-camp-an ...

  2. Codeforces Gym 100231F Solitaire 折半搜索

    Solitaire 题目连接: http://codeforces.com/gym/100231/ Description 给你一个8*8棋盘,里面有4个棋子,每个棋子可以做一下某个操作之一: 1.走 ...

  3. codeforces gym #101161G - Binary Strings(矩阵快速幂,前缀斐波那契)

    题目链接: http://codeforces.com/gym/101161/attachments 题意: $T$组数据 每组数据包含$L,R,K$ 计算$\sum_{k|n}^{}F(n)$ 定义 ...

  4. Codeforces Gym 100418A A - A+-B java高精度

    A - A+-BTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/view.acti ...

  5. Codeforces 1237E. Balanced Binary Search Trees

    传送门 这一题是真的坑人,时间空间都在鼓励你用 $NTT$ 优化 $dp$...(但是我并不会 $NTT$) 看到题目然后考虑树形 $dp$ ,设 $f[i][0/1]$ 表示 $i$ 个节点的树,根 ...

  6. Binary Search 的递归与迭代实现及STL中的搜索相关内容

    与排序算法不同,搜索算法是比较统一的,常用的搜索除hash外仅有两种,包括不需要排序的线性搜索和需要排序的binary search. 首先介绍一下binary search,其原理很直接,不断地选取 ...

  7. [Swift]LeetCode701. 二叉搜索树中的插入操作 | Insert into a Binary Search Tree

    Given the root node of a binary search tree (BST) and a value to be inserted into the tree, insert t ...

  8. [LeetCode] Insert into a Binary Search Tree 二叉搜索树中插入结点

    Given the root node of a binary search tree (BST) and a value to be inserted into the tree, insert t ...

  9. [LeetCode] Search in a Binary Search Tree 二叉搜索树中搜索

    Given the root node of a binary search tree (BST) and a value. You need to find the node in the BST ...

随机推荐

  1. Ubuntu 16.04中安装谷歌Chrome浏览器

    1.进入 Ubuntu 16.04 桌面,按下 Ctrl + Alt + t 键盘组合键,启动终端. 2.在终端中,输入以下命令: sudo wget https://repo.fdzh.org/ch ...

  2. GoF23种设计模式之行为型模式之模板方法

    概述 定义一个操作中的算法的骨架,而将一些步骤延迟到子类中. TemplateMethod使得子类可以不改变一个算法的结构即可重定义该算法的某些特定步骤. 适用性 1.一次性实现一个算法的不变的部分, ...

  3. MediaStore类的使用

    安卓系统会在每次开机之后扫描所有文件并分类整理存入数据库,记录在MediaStore这个类里,通过这个类就可以快速的获得相应类型的文件. 当然这个类只是给你一个uri,提取文件的操作还是要通过Curo ...

  4. 软件工程师应该关注的web攻击手段

    1.SQL注入------常见的安全性问题. 解决方案:前端页面需要校验用户的输入数据(限制用户输入的类型.范围.格式.长度),不能只靠后端去校验用户数据.一来可以提高后端处理的效率,二来可以提高后端 ...

  5. Clickonce - Change deployment URL after publish

    mage.exe -Update C:\inetpub\wwwroot\aspnet40\AminoScience\Uploads\Application Files\AccUFeed_1_0_0_5 ...

  6. 【转】LAMP网站架构方案分析【精辟】

    [转]LAMP网站架构方案分析[精辟] http://www.cnblogs.com/mo-beifeng/archive/2011/09/13/2175197.html Xubuntu下LAMP环境 ...

  7. 第五部分 linux 软件安装RPM SRPM与YUM

    第五部分  linux  软件安装RPM    SRPM与YUM   软件管理员简介 RPM与DPKG两大主流 rpm: redhat       centos     suse    命令:yum ...

  8. [python IO学习篇] 补充.py文件是中文, .ini文件内容是中文

    python 代码文件的编码.py文件默认是ASCII编码,中文在显示时会做一个ASCII到系统默认编码的转换,这时就会出错:SyntaxError: Non-ASCII character.需要在代 ...

  9. WebService的简介, 原理, 使用,流程图

    WebService的简介, 原理, 使用   第一部分: 直观概述 WebService的几种概念: 以HTTP协议为基础,通过XML进行客户端和服务器端通信的框架/组件 两个关键点: 1.     ...

  10. hadoop FileSystem类和SequenceFile类实例

    Hadoop的FileSystem类是与Hadoop的某一文件系统进行交互的API,虽然我们主要聚焦于HDFS实例,但还是应该集成FileSystem抽象类,并编写代码,使其在不同的文件系统中可移植, ...