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 ...
随机推荐
- OOP之单例模式
- GoF23种设计模式之行为型模式之观察者模式
一.概述 定义对象之间的一种一对多的依赖关系,当一个对象的状态发生改变时,所有依赖于它的对象都得到通知并被自动更新.二.适用性1.当一个抽象模型有两个方面,其中一个方面依赖于另一方面的时 ...
- hdu 6333
Problem Description There are n apples on a tree, numbered from 1 to n.Count the number of ways to p ...
- ghost模板总结
ghost模板的二次开发相对容易,附文档: http://themes.ghost.org/v0.6.0/docs/meta_title 这里有各行变量的说明. {{#is "home&qu ...
- SPOJ 375 树链剖分 QTREE - Query on a tree
人生第一道树链剖分的题目,其实树链剖分并不是特别难. 思想就是把树剖成一些轻链和重链,轻链比较少可以直接修改,重链比较长,用线段树去维护. 貌似大家都是从这篇博客上学的. #include <c ...
- Python虚拟机函数机制之名字空间(二)
函数执行时的名字空间 在Python虚拟机函数机制之无参调用(一)这一章中,我们对Python中的函数调用机制有个大概的了解,在此基础上,我们再来看一些细节上的问题.在执行MAKE_FUNCTION指 ...
- MongoDB学习-->Spring Data Mongodb框架之Repository
application-dev.yml server: port: 8888 mongo: host: localhost port: 27017 timeout: 60000 db: mamabik ...
- CI 分页类的使用
分页本身很简单,无非就是一个 [limit $offset, $length] 的过程. $length 是每页显示的数据量,这个是固定的.要确定的就只有 $offset了. 在CI中的分页类同样要依 ...
- rsa Round #71 (Div. 2 only)
Replace A Time limit: 1000 msMemory limit: 256 MB You are given a string SS containing only letter ...
- 【转】javascript操作Select标记中options集合
先来看看options集合的这几个方法:options.add(option)方法向集合里添加一项option对象:options.remove(index)方法移除options集合中的指定项:op ...