SRM 599 1A 2013.12.6
DIV 1
250
1.学到了do-while语句
写成这种样子更直观
2.题目大意:
X=1经过以下的多少次步骤可以得到A的B次方,令p= A的B次方。
步骤一:乘以一个素数;
步骤二:乘以一个自己的因数;
求最小的步骤数。
3.解题思路:
(这道题是看了别人的代码写出来的,自己只想到了分解因数还有找多少个平方数,但是在平方数的地方卡了,看了别人的代码,才醒悟过来。)
分开统计步骤一op1和步骤二op2分别需要几次。
(1)步骤一的次数:
但凡有新的素数加入,op1++;
(2)步骤二的次数:
1>当有新的素数i加入时,先计算出A中最多包含i的几次方count。则p中最多包含i的index=count*B次方。
2>按照以下的方式选择步骤二,可以达到最少的次数
因数àX 当前最大的指数j
1 i à i op1=1 1
2 i à i^2 op2++=1 2
3 i^2à i^4 op2++=2 4
4 i^4 à i^8 op2++=3 8
5 i^8 à i^16 op2++=4 16
………..
直至j>=index , 最后一次 选择的指数应该是index-j/2 但是因为计算的是次数,所以这样就行了。
比如,
已经添加了素数因子2 ,而2需要达到21次方
temp 取出的因子 相乘后的X含有的2
1 2 2^2
2 2^2 2^4
3 2^4 2^8
4 2^8 2^16
5 2^5 2^21 (21-16=5)
Problem Statement
This problem statement contains supersciptsthat may not display properly outside the applet.
Lunthe dog loves very large integers. Her favorite is AB (A to the power ofB). She has an integer variable X.Initially, the value of X is set to 1. She can perform the following two kindsof operations in any order, any number of times.
Operation 1: choose a prime number p, thenmultiply X by p.
Operation 2: choose a positive divisor d ofthe value of X at that point, then multiply X by d.
Youare given two ints A and B. Return the minimum number of operations Lun needsto perform in order to obtain X = AB from the initial state X = 1.
Definition
Class:
BigFatInteger
Method:
minOperations
Parameters:
int, int
Returns:
int
Method signature:
int minOperations(int A, int B)
(be sure your method is public)
Constraints
-
A will be between 2 and 1,000,000 (106),inclusive.
-
B will be between 1 and 1,000,000 (106),inclusive.
Examples
0)
6
1
Returns: 2
Here, AB = 61 = 6. Here is one of theoptimal sequences of operations:
Perform operation 1 by choosing p=2. X isnow 1*2 = 2.
Perform operation 1 by choosing p=3. X isnow 2*3 = 6.
1)
162
1
Returns: 4
One of the optimal sequences of operations:
Perform operation 1 by choosing p=3. X isnow 1*3 = 3.
Perform operation 1 by choosing p=3. X isnow 3*3 = 9.
Perform operation 2 by choosing d=9. X isnow 9*9 = 81.
Perform operation 1 by choosing p=2. X isnow 81*2 = 162.
2)
999983
9
Returns: 5
Here, A is prime. One of the optimalsequences of operations:
Perform operation 1 by choosing p=A. X isnow A.
Perform operation 1 by choosing p=A. X isnow A2.
Perform operation 1 by choosing p=A. X isnow A3.
Perform operation 2 by choosing d=A3. X isnow A6.
Perform operation 2 by choosing d=A3. X isnow A9.
3)
360
8
Returns: 8
This problem statement is the exclusive andproprietary property of TopCoder, Inc. Any unauthorized use or reproduction ofthis information without the prior written consent of TopCoder, Inc. isstrictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.
109.94
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
class BigFatInteger
{
public:int minOperations(int A, int B)
{
int op1=0,op2=0;
for(int i=2;i<=A;i++)
if (A%i==0)
{
op1++;
int count=0;
while (A%i==0)
{
count++;
A/=i;
}
int index=count*B;
int temp=0;
for(int j=1;j<index;j*=2)
temp++;
if (temp>op2) op2=temp;
}
returnop1+op2;
}
};