Codeforces Gym 100431B Binary Search 搜索+组合数学+高精度
原题链接: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 搜索+组合数学+高精度的更多相关文章
- Codeforces Gym 100338H High Speed Trains 组合数学+dp+高精度
原题链接:http://codeforces.com/gym/100338/attachments/download/2136/20062007-winter-petrozavodsk-camp-an ...
- Codeforces Gym 100231F Solitaire 折半搜索
Solitaire 题目连接: http://codeforces.com/gym/100231/ Description 给你一个8*8棋盘,里面有4个棋子,每个棋子可以做一下某个操作之一: 1.走 ...
- codeforces gym #101161G - Binary Strings(矩阵快速幂,前缀斐波那契)
题目链接: http://codeforces.com/gym/101161/attachments 题意: $T$组数据 每组数据包含$L,R,K$ 计算$\sum_{k|n}^{}F(n)$ 定义 ...
- 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 ...
- Codeforces 1237E. Balanced Binary Search Trees
传送门 这一题是真的坑人,时间空间都在鼓励你用 $NTT$ 优化 $dp$...(但是我并不会 $NTT$) 看到题目然后考虑树形 $dp$ ,设 $f[i][0/1]$ 表示 $i$ 个节点的树,根 ...
- Binary Search 的递归与迭代实现及STL中的搜索相关内容
与排序算法不同,搜索算法是比较统一的,常用的搜索除hash外仅有两种,包括不需要排序的线性搜索和需要排序的binary search. 首先介绍一下binary search,其原理很直接,不断地选取 ...
- [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 ...
- [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 ...
- [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 ...
随机推荐
- 20.Yii2.0框架多表关联一对多查询之hasMany
目录 新手模式 hasMany关联模式查询 新建mode层Article.php 新建mode层Category.php 新建控制器HomeController.php 新手模式 用上次的查询结果,作 ...
- Python PycURL的安装使用
PycURL中文简介:https://blog.csdn.net/qq_41185868/article/details/80487014 PycURL英文简介(如下):http://pycurl.i ...
- Shell脚本完成hadoop的集群安装
虽然整体实现的自动安装,但还是有很多需要完善的地方,比如说: 1. 代码目前只能在root权限下运行,否则会出错,这方面需要加权限判断: 2.另外可以增加几个函数,减少代码冗余: 3.还有一些判断不够 ...
- adb提取安装的apk
第一步:列出所有安装的apk adb shell pm list packages 然后找到自己要取出来的apk的包名. 第二布:找到apk的位置(后面跟上包名) adb shell pm path ...
- Java-创建一个线程
第一种继承Thread类 package com.tj; public class BasicThread1 extends Thread { public void run() { System.o ...
- 贪心 - [POI2006]ORK-Ploughing
[POI2006]ORK-Ploughing 描述 Byteasar 想耕种他那块矩形的田,他每次能耕种矩形的一边(上下左右都行),在他每次耕完后,剩下的田也一定是矩形,每块小区域边长为 1,耕地的长 ...
- 大数据学习——kettle的简单使用
1 生成随机数保存到本地文件 新建转换--输入--生成随机数--输出--文本文件输出--保存到本地文件 2 在线预览生成结果 3 字段选择 4 增加常量 5 生成多条数据 右键生成随机数--改变开始复 ...
- 如何用jquery+json来写页面
以下是json数据表: [ { "p" : "银川市", "c" : [{"c1":"兴庆区"},{ ...
- bootstrap3兼容ie8浏览器
bootstrap3 兼容IE8浏览器 2016-01-22 14:01 442人阅读 评论(0) 收藏 举报 分类: html5(18) 目录(?)[+] 近期在使用bootstrap这 ...
- 使用PYTHON创建XML文档_python
当用GOOGLE查的时候,内容几乎都是一样的.但是你想要的东西,一个也没有.例如,我就找不到中国人写的如何使用PYTHON来创建一个XML文件.当然,直接用文件写的方式也能够达到同样的效果,但是毕竟容 ...