欢迎您访问365答案网,请分享给你的朋友!
生活常识 学习资料

arc135A

时间:2023-06-03

题目
题意: 将x进行分解,使得分解后的所有数的乘积达到最大。对于x,可以将其分解为x/2(上取整)和x/2(下取整)
思路: 记忆化搜索。当x<=4,不用分。否则,一直分。
时间复杂度: O(logX) or O(logX * loglogX)
 采用unordered_map
假设n = 2 ^ k,T(n) = T(n/2)+1.T(n) = k = logn
假设n != 2^k, 2^k-1 <= n < 2^k,可以证明最多执行2k次f(x),T(n) < 2 * (log(x)+1) = O(log(x)).
 若采用map
  则记忆化需要花logn的时间,n至多为2
(log(x)+1)。logn即上述提到的loglogX时间。
代码:

// Problem: A - Floor, Ceil - Decomposition// Contest: AtCoder - AtCoder Regular Contest 135// URL: https://atcoder.jp/contests/arc135/tasks/arc135_a// Memory Limit: 1024 MB// Time Limit: 2000 ms// // Powered by CP Editor (https://cpeditor.org)#include#include#include#include#include#include#include#include #include #include#include#include#include#define OldTomato ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr)#define fir(i,a,b) for(int i=a;i<=b;++i) #define mem(a,x) memset(a,x,sizeof(a))#define p_ priority_queue// round() 四舍五入 ceil() 向上取整 floor() 向下取整// lower_bound(a.begin(),a.end(),tmp,greater()) 第一个小于等于的// #define int long long //QAQusing namespace std;typedef complex CP;typedef pair PII;typedef long long ll;// typedef __int128 it;const double pi = acos(-1.0);const int INF = 0x3f3f3f3f;const ll inf = 1e18;const int N = 2e5+10;const int M = 1e6+10;const int mod = 998244353;const double eps = 1e-6;inline int lowbit(int x){ return x&(-x);}templatevoid write(T x){ if(x<0) { putchar('-'); x=-x; } if(x>9) { write(x/10); } putchar(x%10+'0');}template void read(T &x){ x = 0;char ch = getchar();ll f = 1; while(!isdigit(ch)){if(ch == '-')f*=-1;ch=getchar();} while(isdigit(ch)){x = x*10+ch-48;ch=getchar();}x*=f;}#define int long longint n,m,k,T;map mp;int fun(int x){if(x<=4){ return x;}if(mp[x]) return mp[x];if(x&1) return mp[x] = fun(x/2) * fun((x+1)/2) % mod;else return mp[x] = fun(x/2) * fun(x/2) % mod;}void solve(){ read(n); write(fun(n)); puts("");}signed main(void){ T = 1; // OldTomato; cin>>T; // read(T); while(T--) { solve(); } return 0;}

Copyright © 2016-2020 www.365daan.com All Rights Reserved. 365答案网 版权所有 备案号:

部分内容来自互联网,版权归原作者所有,如有冒犯请联系我们,我们将在三个工作时内妥善处理。