题意:

给你一个数n,你需要输出它可以由那几个数相乘构成,我们设可以由x个数构成,这x个数中最小值为minn,最大值为maxx,那么要求maxx-minn<=1

问你满足上面要求的情况有多少种。如果一个数的构成方式由无数种就输出-1

样例解释:

输入:

12

输出:

3

1 12

3 2 3 2

2 4 3

12有三种满足题意的构成方式,分别是12、2*3*2、4*3

输入

1

输出

-1

因为1的构成可以是1、1*1、1*1*1、1*1*1*1无数种

题解:

你会发现输出-1的情况都是2的次幂,例如1、2、4、8、16.特判一下就可以了

其他情况首先n本身算一次。后面就是两个数相乘得到n,这种情况设定x=sqrt(n)

判断一下x*x==n、x*(x-1)==n、x*(x+1)==n

之后就是多个数相乘(大于两个),n最大是1e18,那么我们枚举1到n1/3的所有数,判断就可以

代码:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
using namespace std;
typedef long long ll;
const int maxn = 1e4 + 5;
const int mod = 1000000007;
vector<ll>r[maxn],que;
int main()
{
ll n;
freopen("little.in","r",stdin);
freopen("little.out","w",stdout);
scanf("%lld",&n);
if((n&(-n))==n)
{
printf("-1\n");
return 0;
}
ll pos=0;
//que.push_back(1);
que.push_back(n);
r[pos]=que;
pos++;
que.clear();
for(ll i=2;i*i*i<=n;++i)
{
ll tmp=n;
if(tmp%i==0)
{
while(tmp%i==0)
que.push_back(i),tmp/=i;
while(tmp%(i+1)==0)
que.push_back(i+1),tmp/=(i+1); if(tmp==1)
{
r[pos]=que;
pos++;
}
que.clear();
}
}
ll tmp=sqrt(n);
if(tmp*tmp==n)
{
que.push_back(tmp);
que.push_back(tmp);
r[pos]=que;
pos++;
que.clear();
}
if(tmp*(tmp+1)==n)
{
que.push_back(tmp);
que.push_back(tmp+1);
r[pos]=que;
pos++;
que.clear();
}
if(tmp*(tmp-1)==n)
{
que.push_back(tmp);
que.push_back(tmp-1);
r[pos]=que;
pos++;
que.clear();
}
printf("%lld\n",pos);
for(ll i=0;i<pos;++i)
{
ll len=r[i].size();
printf("%lld ",len);
for(ll j=0;j<len-1;++j)
{
printf("%lld ",r[i][j]);
}
printf("%lld\n",r[i][len-1]);
}
return 0;
}

最新文章

  1. Caliburn.Micro学习笔记(五)----协同IResult
  2. 学习WebSocket(二):使用Spring WebSocket做一个简单聊天室
  3. ubuntu14.04配置中文latex完美环境(texlive+texmaker+lyx)
  4. php读取excel日期类型数据的例子
  5. Failed to load unit &#39;PATM&#39; (VERR_SSM_FIELD_NOT_CONSECUTIVE)
  6. sql第一课笔记
  7. js监控键盘大小写事件
  8. hibernate学习(一)
  9. 响应式WEB设计的基本原则大总结
  10. Linux配置成网关
  11. C# 合并只要有交集的所有集合
  12. c# 界面自适应大小
  13. Android--自定义半圆环型进度(带动画)
  14. Confluence 6 从外部目录中同步数据手动同步缓存
  15. 背景颜色 - bootStrap4常用CSS笔记
  16. homogeneous clip space and NDC
  17. 使用Properties去读取配置文件,并获得具体内容值
  18. Linux : 从私钥中提取公钥
  19. Mybatis单个参数的if判断(针对异常:There is no getter for property..)------mybatis的内置对象
  20. 解决:SyntaxError: Non-ASCII character in file

热门文章

  1. LeetCode498 对角线遍历
  2. 剑指offer 查找和排序的基本操作:查找排序算法大集合
  3. 解决Cannot find module &#39;@angular/compiler-cli&#39;
  4. 18.java设计模式之中介者模式
  5. mysql—group_concat函数
  6. os.system(&#39;cmd&#39;)在linux和windows系统下返回值的差异
  7. oracle优化求生指南脚本记录
  8. 实现所有SAP设备打印机并行打印
  9. 消息队列之rabbitmq学习使用
  10. uni-app开发经验分享三: Vuex实现登录和用户信息留存