博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
poj 3421 X-factor Chains 素数筛选 因子分解
阅读量:4113 次
发布时间:2019-05-25

本文共 1999 字,大约阅读时间需要 6 分钟。

题意:要你构造一个序列x0,x1,x2,,,,xk其中x0=1,xk=n(一个输入的值),序列满足xi<xi+1且xi整除xi+1,求
输入n后,满足该要求的最长的序列,并有多少条这样的最长序列;;
#include 
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std; #define MM(a) memset(a,0,sizeof(a)) typedef long long ll; typedef unsigned long long ULL; const int mod = 1000000007; const double eps = 1e-10; const int inf = 0x3f3f3f3f; const int maxn = (1<<20)+5; int pri[maxn],v[maxn]; ll fact[1000]; void prime() { memset(v, 0, sizeof(v)); memset(fact,0,sizeof(fact)); int p = 0; for (int i = 2; i <= maxn;i++) if (!v[i]) { pri[++p] = i; for (int j = i; j <= maxn; j += i) v[j] = 1; } } ll factor(int n) { if(fact[n]) return fact[n]; ll res=1; for (int i = 1; i <= n; i++) { res *= i; fact[i]=res; } return fact[n]; } int main() { prime(); int n; while (~scanf("%d", &n)) { int temp = n;ll fenzi = 0, fenmu = 1,maxl=0; for (int i = 1; temp > 1;i++) if(temp%pri[i] == 0) { int cnt = 0; while (temp%pri[i] == 0) { cnt++; temp /= pri[i]; maxl++; } fenmu *= factor(cnt); } fenzi = factor(maxl); printf("%lld %lld\n",maxl,fenzi/fenmu); } return 0; }
分析:将n分解成素数因子乘积的形式,再素数因子指数排列组合一下;
易错点:2^20,以后就写成1<<20;;还要注意提前打好素数表;暴力肯定超时
wa代码:
#include 
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std; #define MM(a) memset(a,0,sizeof(a)) typedef long long ll; typedef unsigned long long ULL; const int mod = 1000000007; const double eps = 1e-10; const int inf = 0x3f3f3f3f; int main() { int n; while (~scanf("%d", &n)) { int num=1, maxl = 1; for (int i = 2; i <= n-1 ; i++) { int cnt=1,last; if (n % i == 0) { last = i; for (int j = i; j <= n -1; j += i) if (n%j == 0&&j%last==0) { cnt++; last = j; } } if (cnt > maxl) { num = 1; maxl = cnt; } else if (cnt == maxl) num++; } printf("%d %d\n", maxl, num); } return 0; }

转载地址:http://xvgsi.baihongyu.com/

你可能感兴趣的文章
Golang面试考题记录 ━━ 反转字符串,一种思路几种细节的不同结果
查看>>
Golang面试考题记录 ━━ 整数反转 解答及扩展的三个知识点
查看>>
Golang面试考题记录 ━━ 字符串中的第一个唯一字符 ,拓展:ASCII和strings字符串查找的用法
查看>>
Golang面试考题记录 ━━ 有效的字母异位词,久违的双100%,拓展reflect.DeepEqual()用法和[26]int{}的值
查看>>
Golang面试考题记录 ━━ 验证回文串,多种方法涉及双指针、strings、unicode和regexp
查看>>
Golang面试考题记录 ━━ 字符串转换整数 (atoi),知识点ascii、rune、uint8、string、char等转换
查看>>
Golang面试考题记录 ━━ 实现 strStr() 函数,截然不同三种方案,效率都差不多,双100%
查看>>
Golang面试考题记录 ━━ 外观数列 , 了解递归、bytes.Buffer和闭包
查看>>
学习日志 ━━ 理解递归(使用go语法举例)
查看>>
Golang面试考题记录 ━━ 最长公共前缀,字符串就是切片,复习[]byte、[]rune、[]uint8、[]int32和单引号
查看>>
Golang学习日志 ━━ 单向链表
查看>>
Golang面试考题记录 ━━ 删除链表中的节点,首先明白什么是链表,其次语文要好能看懂题
查看>>
Golang面试考题记录 ━━ 删除链表的倒数第N个节点, 学习闭包递归、双指针、哨兵节点
查看>>
服务器配置篇 ━━ iis7配置php出现fastcgi的500错误,LocalSystem/LocalService/NetworkService/ApplicationPoolIdentity
查看>>
Golang学习日志 ━━ VSCode安装Go插件(代理的使用)及初用mod
查看>>
windows使用小技巧 ━━ Windows 10 HEVC扩展要收费怎么办?教你怎么免费下载HEVC扩展
查看>>
Golang学习日志 ━━ 使用bufio方法拷贝文件将导致mov视频文件出错
查看>>
Golang学习日志 ━━ Mysql相关
查看>>
Golang学习日志 ━━ goQuery 的使用
查看>>
Golang学习日志 ━━ Go 常用包整理及介绍
查看>>