栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

arc135 A(时间复杂度分析)

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

arc135 A(时间复杂度分析)

题目
题意: 将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 //QAQ
using 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 long
int 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;
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/738388.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号