Codeforces gym101612 L.Little Difference(枚举+二分)
传送:http://codeforces.com/gym/101612
题意:给定一个数n(<=1e18),将n分解为若干个数的成绩。要求这些数两两之间的差值不能大于1。
分析:
若n==2^k,则答案一定是-1。
然后,考虑若n==a^k,枚举k,二分求a。若n==a^x*(a+1)^y,枚举x,y,二分求解a。
注意:两数相乘可能>1e18,特判。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> pll;
typedef pair<pair<ll,ll>,ll> plll;
const int maxn=1e6+;
const ll inf=1e18+1e6;
ll n;
vector<pll> g1;
vector<plll> g2;
ll mul(ll a,ll b){
if (a>=1.0*inf/b) return inf;
else return a*b;
}
ll _pow(ll a,ll b){
ll res=,base=a;
while (b){
if (b&) res=mul(res,base);
base=mul(base,base);
b>>=;
}
return res;
}
ll solve(int k){
ll l=,r=n,ans,mid;
while (l<=r){
mid=(l+r)>>;
ll tmp=_pow(mid,k);
if (tmp==n) return mid;
if (tmp<n) l=mid+; else r=mid-;
}
return ;
}
ll solve2(int x,int y){
ll l=,r=n,ans,mid;
while (l<=r){
mid=(l+r)>>;
ll tmp=_pow(mid,x),tmp2=_pow(mid+,y);
if (mul(tmp,tmp2)==n) return mid;
if (mul(tmp,tmp2)<n) l=mid+; else r=mid-;
}
return ;
}
int main(){
//freopen("little.in","r",stdin);freopen("little.out","w",stdout);
ios::sync_with_stdio(false);
cin >> n;
if (n==(n&(-n))) return cout << - << endl,;
g1.clear(); g2.clear();
ll num=;
// a^k
for(int i=;i<=;i++){
ll a=solve(i);
if (_pow(a,i)==n){
g1.push_back({i,a});
}
}
// a^x * (a+1)^y
for(int i=;i<=;i++){
for (int j=;j<=;j++){
ll a=solve2(i,j);
ll tmp=_pow(a,i),tmp2=_pow(a+,j);
if (mul(tmp,tmp2)==n){
g2.push_back({{i,j},a});
}
}
}
cout << g1.size()+g2.size() << endl;
for (auto i:g1){
ll tmp=i.first;
cout << tmp;
for (int j=;j<tmp;j++) cout << " " << i.second;
cout << endl;
}
for (auto i:g2){
ll tmp=i.first.first+i.first.second;
cout << tmp;
tmp=i.first.first;
for (int j=;j<tmp;j++) cout << " " << i.second;
tmp=i.first.second;
for (int j=;j<tmp;j++) cout << " " << i.second+;
cout << endl;
}
return ;
}
Codeforces gym101612 L.Little Difference(枚举+二分)的更多相关文章
- Codeforces 626E Simple Skewness(暴力枚举+二分)
E. Simple Skewness time limit per test:3 seconds memory limit per test:256 megabytes input:standard ...
- Codeforces C. Maximum Value(枚举二分)
题目描述: Maximum Value time limit per test 1 second memory limit per test 256 megabytes input standard ...
- CSU OJ PID=1514: Packs 超大背包问题,折半枚举+二分查找。
1514: Packs Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 61 Solved: 4[Submit][Status][Web Board] ...
- CodeForces 1117C Magic Ship (循环节+二分答案)
<题目链接> 题目大意: 给定起点和终点,某艘船想从起点走到终点,但是海面上会周期性的刮风,船在任何时候都能够向四个方向走,或者选择不走,船的真正行走路线是船的行走和风的走向叠加的,求船从 ...
- HDU4430 Yukari's Birthday(枚举+二分)
Yukari's Birthday HDU4430 就是枚举+二分: 注意处理怎样判断溢出...(因为题目只要10^12) 先前还以为要用到快速幂和等比数列的快速求和(但肯定会超__int64) 而 ...
- POJ 2549 Sumsets(折半枚举+二分)
Sumsets Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11946 Accepted: 3299 Descript ...
- 4 Values whose Sum is 0(枚举+二分)
The SUM problem can be formulated as follows: given four lists A, B, C, D of integer values, compute ...
- Codeforces Round #379 (Div. 2) C. Anton and Making Potions 枚举+二分
C. Anton and Making Potions 题目连接: http://codeforces.com/contest/734/problem/C Description Anton is p ...
- Codeforces H. Prime Gift(折半枚举二分)
题目描述: Prime Gift time limit per test 3.5 seconds memory limit per test 256 megabytes input standard ...
随机推荐
- /bin/sh^M:bad interpreter:
/bin/sh^M:bad interpreter: No such file or directory 这个错误发生在你在windows下编写文件上传到linux服务器去运行的时候. 错误原因:wi ...
- CSS-弹性布局-伪类选择器-复杂选择器
1.定位 1.堆叠顺序 一旦将元素变为已定位元素的话,元素们则有可能出现堆叠的效果. 如何改变堆叠顺序? 属性:z-index 取值:无单位的数字,数字越大越靠上. 注意: 1.父子元素间,z-ind ...
- innodb_log_buffer_size和innodb_buffer_pool_size参数说明
innodb_log_buffer_size Command-Line Format --innodb_log_buffer_size=# System Variable Name innodb ...
- 前端之javascript的节点操作和Event
一 节点的增删改查 创建节点:createElement(标签名):创建一个指定名称的节点. 追加节点: 追加一个子节点:somenode.appendChild(标签名) 指定某个位置前面添加一个节 ...
- ios 懒加载详解
iOS开发之懒加载 在iOS开发中几乎经常用到懒加载技术,比如我们存放网络数据的数组,控制器的view,控件的自定义,复杂的运算逻辑等等情况下都会用到懒加载技术,那么什么是懒加载呢?? 他又有什么样的 ...
- 2019.01.21 NOIP训练 可持久化序列【模板】(可持久化treap)
传送门 题意简述:支持在把某个数插入到某版本的第k个位置,删除某版本第k个数,询问第k个数. 思路:用可持久化treaptreaptreap维护区间第kkk个位置的数是啥就可以了. 代码
- Ubuntu下 QT添加外部链接库(.so文件)示例
参考:https://blog.csdn.net/KKALL1314/article/details/81915354 https://forum.qt.io/topic/80301/file-not ...
- android bug笔记
昨天集成一个第三方的联盟sdk,然后执行它的代码,程序就会crash,这个第三方的sdk报错日志后面,紧跟着一个友盟分享的报错,我现在也不明白这里是为何把友盟给扯进来了,因为我这步骤操作上并没有调用任 ...
- Basic Router Architecture
from the book principles and practices of interconnection networks the chapter router architecture ...
- s4-9 二层设备
二层(数据链路层)设备有哪些? 网卡 网桥 交换机 NIC 网卡 Nework Interface Card 为主机提供介质的访问. MAC地址烧在网卡的 ROM中 NIC 网 ...