Cf(1141A) Game 23
Polycarp plays "Game 23". Initially he has a number ?n and his goal is to transform it to ?m. In one move, he can multiply ?n by 22 or multiply ?nby 33. He can perform any number of moves.
Print the number of moves needed to transform ?n to ?m. Print -1 if it is impossible to do so.
It is easy to prove that any way to transform ?n to ?m contains the same number of moves (i.e. number of moves doesn't depend on the way of transformation).
Input
The only line of the input contains two integers ?n and ?m (1≤?≤?≤5⋅1081≤n≤m≤5⋅108).
Output
Print the number of moves to transform ?n to ?m, or -1 if there is no solution.
Examples
input
120 51840
output
7
input
42 42
output
0
input
48 72
output
-1
Note
In the first example, the possible sequence of moves is: 120→240→720→1440→4320→12960→25920→51840.120→240→720→1440→4320→12960→25920→51840. The are 77 steps in total.
In the second example, no moves are needed. Thus, the answer is 00.
In the third example, it is impossible to transform 4848 to 7272.
题意:
给两个数n、m,n只能进行乘2或者乘3操作
问:能否n通过乘2或者乘3变成m
思路:
首先如果m不是n的倍数,那么一定不可以
其次: 商为x(x=m/n)
如果x是6的倍数,就加2;如果是3的倍数,就加1;如果是2的倍数,加1;以上都不满足输出 -1
(哇了几发,脑子要清楚点啊,思路要活一点)
CODE:
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
typedef long long LL;
using namespace std;
#define memset(a,n) memset(a,n,sizeof(a))
#define INF 0x3f3f3f3f
const int MAX = 200;
int main()
{
int n,m;
cin>>n>>m;
LL x=m/n;
if(x*n!=m){
cout<<"-1"<<endl;
return 0;
}
int flag=0;
int cnt=0;
while(x>1)
{
if(x%6==0){
x/=6;
cnt+=2;
}else if(x%3==0){
x/=3;
cnt++;
}else if(x%2==0){
x/=2;
cnt++;
}else{
flag=1;
break;
}
// cout<<x<<endl;
}
if(flag)
cout<<"-1"<<endl;
else
cout<<cnt<<endl;
}
(Cf1141B). Maximal Continuous Rest
Each day in Berland consists of ?n hours. Polycarp likes time management. That's why he has a fixed schedule for each day — it is a sequence ?1,?2,…,??a1,a2,…,an (each ??ai is either 00 or 11), where ??=0ai=0 if Polycarp works during the ?i-th hour of the day and ??=1ai=1 if Polycarp rests during the ?i-th hour of the day.
Days go one after another endlessly and Polycarp uses the same schedule for each day.
What is the maximal number of continuous hours during which Polycarp rests? It is guaranteed that there is at least one working hour in a day.
Input
The first line contains ?n (1≤?≤2⋅1051≤n≤2⋅105) — number of hours per day.
The second line contains ?n integer numbers ?1,?2,…,??a1,a2,…,an (0≤??≤10≤ai≤1), where ??=0ai=0 if the ?i-th hour in a day is working and ??=1ai=1 if the ?i-th hour is resting. It is guaranteed that ??=0ai=0 for at least one ?i.
Output
Print the maximal number of continuous hours during which Polycarp rests. Remember that you should consider that days go one after another endlessly and Polycarp uses the same schedule for each day.
Examples
input
5 1 0 1 0 1
output
2
input
6 0 1 0 1 1 0
output
2
input
7 1 0 1 1 1 0 1
output
3
input
3 0 0 0
output
0
input
7
1 1 1 1 1 0 1
output
6
Note
In the first example, the maximal rest starts in last hour and goes to the first hour of the next day.
In the second example, Polycarp has maximal rest from the 44-th to the 55-th hour.
In the third example, Polycarp has maximal rest from the 33-rd to the 55-th hour.
In the fourth example, Polycarp has no rest at all.
题意:
给n个由0、1组成的时间表,0代表工作,1代表休息,某人每天重复着一样的时间表
问:最多连续休息的时间是多少?(已知至少存在一小时工作)
注意!!!该人可以从第一天的最后几小时到第二天的前几个小时都可以休息(也就是说如果第一天的最后是连续的1,并且第二天的前几个小时都是1,他可以连续休息)
思路:
连续找1的个数挺简单的,循环找就行啦(一层 for 就可以实现)
特别判断一下第一个小时是休息还是工作,如果是休息的话,只需要记录从第一小时开始连续1的个数
CODE:
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
typedef long long LL;
using namespace std;
#define memset(a,n) memset(a,n,sizeof(a))
#define INF 0x3f3f3f3f
const int MAX = 2e5+5;
int a[MAX];
int main()
{
int n;
cin>>n;
for(int i=0;i<n;i++)
cin>>a[i];
int ans=0,sum=0;
int flag=0;
int flagg=-1; // 用 flagg 记录第一个小时是1的时候连续1的个数
for(int i=0;i<n;i++){
if(a[i]==0){
ans=max(ans,sum); // 求一个最大的连续数目
if(flagg==0){ // 代表的是第一个小时是1,那么需要将第一个小时是1的情况下连续1的数目记录下来
flag=ans;
flagg=1; // 记得要改变 flagg
}
sum=0;
continue;
}
if(i==0){
if(a[i]==0)
flag=0;
else{ // 如果第一个小时是1,记录下来的位置
flagg=i;
sum++;
}
continue;
}
if(a[i]==1){
sum++;
}
}
if(a[n-1]==1){
sum+=flag;
ans=max(ans,sum);
}
cout<<ans<<endl;
}