题目链接:传送门

题目:

B. Math
time limit per test
second
memory limit per test
megabytes
input
standard input
output
standard output JATC's math teacher always gives the class some interesting math problems so that they don't get bored. Today the problem is as follows. Given an integer n , you can perform the following operations zero or more times: mul x : multiplies n by x (where x
is an arbitrary positive integer).
sqrt: replaces n
with n−−√ (to apply this operation, n−−√ must be an integer). You can perform these operations as many times as you like. What is the minimum value of n , that can be achieved and what is the minimum number of operations, to achieve that minimum value? Apparently, no one in the class knows the answer to this problem, maybe you can help them?
Input The only line of the input contains a single integer n
(≤n≤ ) — the initial number.
Output Print two integers: the minimum integer n that can be achieved using the described operations and the minimum number of operations required.
Examples
Input
Copy Output
Copy Input
Copy Output
Copy Note In the first example, you can apply the operation mul
to get and then sqrt to get . In the second example, you can first apply sqrt to get
, then mul to get and finally two more sqrt and you get . Note, that even if the initial value of n
is less or equal , it can still become greater than after applying one or more operations.

题目大意:

  输入一个数n,对这个数可以进行两种操作:

  (1)乘上一个任意大小的整数;

  (2)取平方根。(这里要求取平方根前必须为完全平方数

  问使得输入的n经过任意次操作变成最小值时,输出这个最小值和变成最小值所需要的最少的操作数。

  1 ≤ n ≤ 106

思路:

  ①:n的最小值为它所有质因子的乘积;(如果有一个质因子的指数大于1,那么就不是最小的,而且通过操作②③变成最小的)

  ②:操作(1)最多只需要执行一次,用这次操作把n所有质因子的指数都变成相同的,并且这个指数的值为2的幂次(取平方根时幂次会/=2);

  ③:操作(2)的次数就是②步骤结束后,log2(质因子的指数)。

然后把②和③的次数合起来就好了。

实现:

  试除法筛出n所有的质因子pi及其指数ci,则第一个答案a = $\prod_{i=1}^{fatCnt}p_{i}$。(fatCnt为质因子的数量)

  令b = max(ci),令2t为最小的不小于b的2的幂次。

  ①:如果t为0,那么答案为0;(n不可能变得更小了)

  ②:只要有一个ci < 2t,答案就为t+1;(必须要执行一次操作(1),才能不断的开方)

  ③:如果所有的ci = 2t,答案为t。(此时不执行操作(1),就可以不断开方直到最小)

代码:

  (马上去补!)

比赛代码:

#include <bits/stdc++.h>

using namespace std;
const int MAX_N = ;
int prime[MAX_N+];
void getPrime()
{
memset(prime, , sizeof prime);
for (int i = ; i <= MAX_N; i++) {
if (!prime[i]) prime[++prime[]] = i;
for (int j = ; j <= prime[] && prime[j] <= MAX_N/i; j++) {
prime[prime[j]*i] = ;
if (i%prime[j] == ) break;
}
}
} int factor[][];
int fatCnt;
int getFactors(int x)
{
fatCnt = ;
int tmp = x;
for (int i = ; prime[i] <= tmp/prime[i]; i++) {
if (tmp % prime[i] == ) {
factor[fatCnt][] = prime[i];
factor[fatCnt][] = ;
while (tmp % prime[i] == ) {
factor[fatCnt][]++;
tmp /= prime[i];
}
fatCnt++;
}
}
if (tmp > ) {
factor[fatCnt][] = tmp;
factor[fatCnt][] = ;
fatCnt++;
}
return fatCnt;
} int getbin(int x)
{
int tmp = ;
while (tmp < x) {
tmp <<= ;
}
return tmp;
} bool check(int ansb)
{//ÊÇ·ñÓв»Í¬
int cnt = factor[][];
if (cnt != (<<ansb))
return true;
for (int i = ; i < fatCnt; i++) {
if (factor[i][] != cnt)
return true;
}
return false;
} //int bin[21];
int main() {
// bin[0] = 1;
// for (int i = 1; i < 21; i++) bin[i] = bin[i-1] << 1;
getPrime();
int N;
cin >> N;
getFactors((int)N);
int a = , b = ;
for (int i = ; i < fatCnt; i++) {
a *= factor[i][];
b = max(b, getbin(factor[i][]));
}
int ansb = ;
while (b > ) {
ansb++;
b >>= ;
}
if (ansb > && check(ansb)) {
ansb++;
}
cout << a << ' ' << ansb << endl;
return ;
}

Codeforces1062B. Math(合数分解)的更多相关文章

  1. HDU 4610 Cards (合数分解,枚举)

    Cards Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

  2. HDU 4497 GCD and LCM (合数分解)

    GCD and LCM Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total ...

  3. hdu_4497GCD and LCM(合数分解)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4497 GCD and LCM Time Limit: 2000/1000 MS (Java/Other ...

  4. hdu 5317 合数分解+预处理

    RGCDQ Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submi ...

  5. hdu 4777 树状数组+合数分解

    Rabbit Kingdom Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) T ...

  6. Perfect Pth Powers pku-1730(筛+合数分解)

    题意:x可以表示为bp, 求这个p的最大值,比如 25=52, 64=26,  然后输入x 输出 p 就是一个质因子分解.算法.(表示数据上卡了2个小时.) 合数质因子分解模板. ]; ]; ; ;n ...

  7. pku1365 Prime Land (数论,合数分解模板)

    题意:给你一个个数对a, b 表示ab这样的每个数相乘的一个数n,求n-1的质数因子并且每个指数因子k所对应的次数 h. 先把合数分解模板乖乖放上: ; ans != ; ++i) { ) { num ...

  8. 莫比乌斯函数 51nod-1240(合数分解试除法)

    就是输出n时,莫比乌斯函数的值.直接将n唯一分解即可. 思路:筛出105以内的素数,因为109开方,105就差不多.当一个大数还没有被1000个素数分解,那么这个数基本上可以认为是素数(为合数为小概率 ...

  9. csu 1801(合数分解+排列组合)

    1801: Mr. S’s Romance Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 15  Solved: 5[Submit][Status][W ...

随机推荐

  1. Java IO流及应用(一)

    IO流 IO流概述及FileWriter类的使用 FileReader类使用 缓冲流介绍和使用 IO流相关案例 NO.one IO流概述及FileWriter类使用 1.1 IO流概述及分类 IO流用 ...

  2. (转载)Unity UGUI鼠标点击UI不受影响方法IsPointerOverGameObject

    这几天在做捕鱼达人游戏时发现,当鼠标点击UI时,炮台的子弹也会发射子弹,这样会影响用户体验. 然后网上百度了一波,发现在UGUI系统上,EventSystem提供了一些方法.那就是EventSyste ...

  3. 要想成为前端大神,那些你不得不知晓的web前端命名规范。

    一.Web语义化 1.1 H5的语义化 对于经验资深的前端er,在给web布局时,相信都会很注重标签和命名的规范.尤其是随着html5的普及发展,更是把web前端语义化推向一个新的台阶上.比如html ...

  4. LaF: Fast Access to Large ASCII Files

    貌似可以随机读取dataframe格式的文本文件.

  5. webpack.optimize.UglifyJsPlugin配置说明

    https://segmentfault.com/a/1190000008995453?utm_source=tuicool&utm_medium=referral

  6. Tinkoff Internship Warmup Round 2018 and Codeforces Round #475 (Div. 1)D. Frequency of String

    题意:有一个串s,n个串模式串t,问s的子串中长度最小的包含t k次的长度是多少 题解:把所有t建ac自动机,把s在ac自动机上匹配.保存每个模式串在s中出现的位置.这里由于t两两不同最多只有xsqr ...

  7. Jboss解决只能通过localhost访问而不能使用IP访问项目的问题

    之前项目都是前后端完全分离,很少使用到后端语言的开发工具,最近使用intellij+Jboss进行项目部署开发,初始用发现项目启动后只能使用localhost进行项目访问,IP地址访问则提示页面404 ...

  8. python中的模块调用

    1.简介 在python中经常需要各模块相互调用,此时会出现以下几种情况: (1)同一目录下文件的调用 (2)父目录调用子目录中的文件 (3)同级目录下文件的调用 2.示例 现有该目录结构:AB中有A ...

  9. DevExpress.Mvvm.Free

    DevExpress MVVM Framework is a set of components helping to work in the Model-View-ViewModel pattern ...

  10. .net反编译的九款神器

    本人搜集了下8款非常不错的.Net反编译利器: 1.Reflector Reflector是最为流行的.Net反编译工具.Reflector是由微软员工Lutz Roeder编写的免费程序.Refle ...