传送: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(枚举+二分)的更多相关文章

  1. Codeforces 626E Simple Skewness(暴力枚举+二分)

    E. Simple Skewness time limit per test:3 seconds memory limit per test:256 megabytes input:standard ...

  2. Codeforces C. Maximum Value(枚举二分)

    题目描述: Maximum Value time limit per test 1 second memory limit per test 256 megabytes input standard ...

  3. CSU OJ PID=1514: Packs 超大背包问题,折半枚举+二分查找。

    1514: Packs Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 61  Solved: 4[Submit][Status][Web Board] ...

  4. CodeForces 1117C Magic Ship (循环节+二分答案)

    <题目链接> 题目大意: 给定起点和终点,某艘船想从起点走到终点,但是海面上会周期性的刮风,船在任何时候都能够向四个方向走,或者选择不走,船的真正行走路线是船的行走和风的走向叠加的,求船从 ...

  5. HDU4430 Yukari's Birthday(枚举+二分)

    Yukari's Birthday  HDU4430 就是枚举+二分: 注意处理怎样判断溢出...(因为题目只要10^12) 先前还以为要用到快速幂和等比数列的快速求和(但肯定会超__int64) 而 ...

  6. POJ 2549 Sumsets(折半枚举+二分)

    Sumsets Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11946   Accepted: 3299 Descript ...

  7. 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 ...

  8. 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 ...

  9. Codeforces H. Prime Gift(折半枚举二分)

    题目描述: Prime Gift time limit per test 3.5 seconds memory limit per test 256 megabytes input standard ...

随机推荐

  1. 【Web】Nginx配置开机启动

    在添加nginx服务之后,大家会希望开机伴随启动nginx,避免手动路径输入启动: nginx官方提供了启动脚本:https://www.nginx.com/resources/wiki/start/ ...

  2. springMVC 学习 五 参数传递(包括restful风格)

    (一)SpringMVC Controller接受参数的方式 (1) 前端传递的参数,在springMVC的controller中使用基本数据类型或者String 类型进行接受 在前端有一个form表 ...

  3. mysql 执行计划分析三看, explain,profiling,optimizer_trace

    http://blog.csdn.net/xj626852095/article/details/52767963 step 1 使用explain 查看执行计划, 5.6后可以加参数 explain ...

  4. centos6 搭建nginx实现负载均衡

    一.安装nginx 1)准备2台服务器,环境一样,同时执行 rpm -ivh http://mirrors.aliyun.com/epel/epel-release-latest-6.noarch.r ...

  5. mybatis 中文做参数报错

    一个简单的查询,如果参数中有中文.如下: <select id="xxxx" resultType="hashmap"> select * from ...

  6. mysql 设置外键 四大属性 CASCADE SET NULL NO ACTION RESTRICT 理解

    CASCADE:主表delete.update的时候,从表会delete.update掉关联记录: SET NULL:主表delete.update的时候,从表会将关联记录的外键字段所在列设为null ...

  7. C语言常用数据类型说明

     1.取值范围: short一般占两个字节,取值范围:-32768 - 32767   int一般占两个或四个字节,取值范围:-2147483648 - 2147483647 unsigned int ...

  8. 利用url传多个参数

    刚开始接触jsp,比较偏向于用button标签的onclick方法进行页面的跳转.但是关于页面跳转的各种问题真是叫人头大,以下记录,仅仅为自己以后查看. Qone 用url传参的时候遇到中文怎么办 编 ...

  9. char类型

    1.JAVA中,char占2字节,16位.可在存放汉字 2.char赋值 char a='a';  //任意单个字符,加单引号. char a='中';//任意单个中文字,加单引号. char a=1 ...

  10. LINQ 语法

    语言集成查询 (LINQ) 是 Visual Studio 2008 和 .NET Framework 3.5 版中一项突破性的创新,它在对象领域和数据领域之间架起了一座桥梁. 传统上,针对数据的查询 ...