LightOJ - 1170 Counting Perfect BST
2017-01-04 21:20
841 查看
题目分析
本题是卡特兰数加逆元,这里面逆元用扩展欧几里得实现。因为事例比较多还需要二分找出一共有多少个值满足。卡特兰数公式是:f[n]=f[n−1]∗(4∗n−2)/(n+1)#include <cstdio> #include <vector> #include <cstring> #include <iostream> #include <algorithm> using namespace std; #define LL long long const LL maxn = 1e10+100; const LL mod = 1e8+7; vector <LL> vec; LL ans[(int)1e6]; void exgcd(LL a, LL b, LL& x, LL& y){ //求逆元 if(!b){ x = 1; y = 0; } else{ exgcd(b, a%b, y, x); y -= x*(a/b); } } void init(){ vec.clear(); for(LL i = 2; i <= 1e5; i++){ LL j = i*i; while(j < maxn){ vec.push_back(j); j *= i; } } sort(vec.begin(), vec.end()); vec.erase(unique(vec.begin(), vec.end()), vec.end()); ans[0] = 0; ans[1] = 1; for(LL i = 2; i < 1e6; i++){ LL x, y; exgcd(i+1, mod, x, y); ans[i] = ans[i-1]*(4*i-2)%mod*(x%mod+mod)%mod; } } int main(){ init(); int T; scanf("%d", &T); LL a, b; for(int kase = 1; kase <= T; kase++){ scanf("%lld %lld", &a, &b); int tot = 0; int l1 = 0, r1 = vec.size()-1; while(l1 < r1){ int mid = (l1+r1)/2; if(vec[mid] >= a) r1 = mid; else l1 = mid+1; } if(vec[l2] >= a && vec[l2] <= b && vec[r1] >= a && vec[r1] <= b) printf("Case %d: %lld\n", kase, ans[l2-r1+1]); else printf("Case %d: 0\n", kase); } return 0; }
相关文章推荐
- lightoj 1170 Counting Perfect BST
- lightoj 1170 - Counting Perfect BST 数学+DP
- LightOJ1170 - Counting Perfect BST(卡特兰数)
- LightOj 1170 - Counting Perfect BST (折半枚举 + 卡特兰树)
- 1170 - Counting Perfect BST
- Light OJ 1170 Counting Perfect BST (DP+数学)
- PAT_A 1115. Counting Nodes in a BST (30)
- 1115. Counting Nodes in a BST (30)
- PAT 1115 Counting Nodes in a BST
- 1115. Counting Nodes in a BST (构建二叉排序树,输出最后两层的节点数)
- LightOJ 1288 Subsets Forming Perfect Squares(高斯消元法)
- lightoj 1201 - A Perfect Murder 树形DP
- lightoj-1058 - Parallelogram Counting
- 1115. Counting Nodes in a BST 解析
- 1115. Counting Nodes in a BST (30)(建立二叉搜索树)
- PAT--1115. Counting Nodes in a BST
- Counting Nodes in a BST
- 1115. Counting Nodes in a BST (30)
- PAT (Advanced Level) 1115. Counting Nodes in a BST (30) 解题报告
- PAT 甲级 1115. Counting Nodes in a BST (30)