Codeforces Round #565 (Div. 3) A. Divide it!
鏈接:
https://codeforces.com/contest/1176/problem/A
題意:
You are given an integer n.
You can perform any of the following operations with this number an arbitrary (possibly, zero) number of times:
Replace n with n2 if n is divisible by 2;
Replace n with 2n3 if n is divisible by 3;
Replace n with 4n5 if n is divisible by 5.
For example, you can replace 30 with 15 using the first operation, with 20 using the second operation or with 24 using the third operation.
Your task is to find the minimum number of moves required to obtain 1 from n or say that it is impossible to do it.
You have to answer q independent queries.
思路:
優先操作2, 再3,再5.
暴力即可。
代碼:
#include <bits/stdc++.h>using namespace std;typedef long long LL; const int MAXN = 1e5 + 10; const int MOD = 1e9 + 7; LL n, m, k, t;int main() { // freopen("test.in", "r", stdin);cin >> t;while (t--){cin >> n;int res = 0;bool flag = true;while (n != 1){if (n%2 == 0)n = n/2;else if (n%3 == 0)n = (n/3)*2;else if (n%5 == 0)n = (n/5)*4;else{flag = false;break;}res++;}if (flag)cout << res << endl;elsecout << -1 << endl;}return 0; }轉載于:https://www.cnblogs.com/YDDDD/p/10998680.html
總結
以上是生活随笔為你收集整理的Codeforces Round #565 (Div. 3) A. Divide it!的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 平安车险报价明细有哪些方法查询?
- 下一篇: 领域驱动设计-3-模型的管理