codeforces163D

博客围绕给定长方体体积求最小表面积的算法问题展开。输入为多组测试数据,以分解质因数形式给出体积。输出包含最小表面积及对应的长宽高。直接爆搜会超时,需剪枝,如强制边长大小关系、体积超出退出等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Large Refrigerator

 CodeForces - 163D 

给定一个长方体的体积V,求出这个长方体的最小表面积。

输入

第一行有一个整数t (1 ≤ t ≤ 500) — 测试数据的组数。

下面是t组测试数据。每一组表示一个整数V (2 ≤ V ≤ 1018),通过分解质因数的形式给出。

V = p1a1p2a2... pkak,其中pi 是不同的素数,ai是正整指数。

第一行给出一个正整数k — V中不同的质因子的个数。下面k行每行两个数字:piai,用空格隔开。每一个pi都是不同的。所有ai > 0。

输出

输出t 行,在第i行输出第i-组测试数据的答案,其中包含4个空格隔开的数:最小表面积S和对应的长宽高的长度abc。如果有多个答案,输出任意。长宽高顺序没有规定。

样例

输入
3
1
2 3
1
17 1
3
3 1
2 3
5 1
输出
24 2 2 2
70 1 1 17
148 4 6 5

注释

在第一个测试数据中体积V = 23 = 8,最小表面积可由三边相等的正方体构成。

在第二个测试数据中体积V = 17,该长方体只有一种构成方法。

 

sol:直接爆搜会T出屎,所以要剪枝,我就XJB剪了一下,先搜a再搜b,强制a<b<c,还有中间如果体积已经超出了就退出,然后就过了

#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ll;
inline ll read()
{
    ll s=0;
    bool f=0;
    char ch=' ';
    while(!isdigit(ch))
    {
        f|=(ch=='-'); ch=getchar();
    }
    while(isdigit(ch))
    {
        s=(s<<3)+(s<<1)+(ch^48); ch=getchar();
    }
    return (f)?(-s):(s);
}
#define R(x) x=read()
inline void write(ll x)
{
    if(x<0)
    {
        putchar('-'); x=-x;
    }
    if(x<10)
    {
        putchar(x+'0'); return;
    }
    write(x/10);
    putchar((x%10)+'0');
    return;
}
#define W(x) write(x),putchar(' ')
#define Wl(x) write(x),putchar('\n')
const int N=65;
const ll inf=0x7fffffffffffffffll;
int T,n,tot=0;
struct Node
{                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      
    ll p,a;
    inline bool operator<(const Node &tmp)const
    {
        return p<tmp.p;
    }
}Num[N];
ll P[N],A[N],V,ans,aa,bb,cc;
inline ll Ksm(ll x,ll y)
{
    ll Res=1;
    while(y)
    {
        if(y&1) Res=1ll*Res*x; x=1ll*x*x; y>>=1;
    }
    return Res;
}
inline bool Judge(ll a)
{
    return (1ll*(sqrt(V/a)*2*a+V/a)<=ans);
}
inline void dfs(int Now,ll a);
inline void dfs1(int Now,ll a,ll b);
inline void dfs(int Now,ll a)
{
    if(a*a*a>V) return;
    if(Now==n+1)
    {
        if(Judge(a)) dfs1(1,a,1);
        return;
    }
    int i;
    ll tmp=Ksm(P[Now],A[Now]);
    for(i=A[Now];i>=1;i--)
    {
        A[Now]-=i;
        dfs(Now+1,a*tmp);
        A[Now]+=i;
        tmp/=P[Now];
    }
    dfs(Now+1,a);
}
inline void dfs1(int Now,ll a,ll b)
{
    if(a*b*b>V) return;
    if(Now==n+1)
    {
        ll c=V/a/b,tmp; tmp=a*b+a*c+b*c;
        if(tmp<ans)
        {
            ans=tmp; aa=a; bb=b; cc=c;
        }
        return;
    }
    int i;
    ll tmp=Ksm(P[Now],A[Now]);
    for(i=A[Now];i>=1;i--)
    {
        A[Now]-=i;
        dfs1(Now+1,a,b*tmp);
        A[Now]+=i;
        tmp/=P[Now];
    }
    dfs1(Now+1,a,b);
}
int main()
{
    int i;
    R(T);
    while(T--)
    {
        V=1; ans=inf; R(n); tot=0;
        for(i=1;i<=n;i++)
        {
            R(Num[++tot].p); R(Num[tot].a); V*=Ksm(Num[tot].p,Num[tot].a);
        }
        sort(Num+1,Num+tot+1);
        for(i=1;i<=tot;i++) {P[i]=Num[i].p; A[i]=Num[i].a;}
        dfs(1,1);
        W(1ll*ans*2); W(aa); W(bb); Wl(cc);
    }
    return 0;
}
/*
Input
4
1
2 3
1
17 1
3
3 1
2 3
5 1
1
2 2
Output
24 2 2 2
70 1 1 17
148 4 6 5
16 2 2 1

input
3
4
208513 1
2 1
10753058401 1
223 1
4
17469877 1
1283 1
949261 1
47 1
4
313 1
2 1
1546412477693 1
1033 1
output
4493896846822714 446 208513 10753058401
35388330702870 60301 949261 17469877
5130996602278690 626 1033 1546412477693
*/
View Code

 

转载于:https://www.cnblogs.com/gaojunonly1/p/11167233.html

### Codeforces 1487D Problem Solution The problem described involves determining the maximum amount of a product that can be created from given quantities of ingredients under an idealized production process. For this specific case on Codeforces with problem number 1487D, while direct details about this exact question are not provided here, similar problems often involve resource allocation or limiting reagent type calculations. For instance, when faced with such constraints-based questions where multiple resources contribute to producing one unit of output but at different ratios, finding the bottleneck becomes crucial. In another context related to crafting items using various materials, it was determined that the formula `min(a[0],a[1],a[2]/2,a[3]/7,a[4]/4)` could represent how these limits interact[^1]. However, applying this directly without knowing specifics like what each array element represents in relation to the actual requirements for creating "philosophical stones" as mentioned would require adjustments based upon the precise conditions outlined within 1487D itself. To solve or discuss solutions effectively regarding Codeforces' challenge numbered 1487D: - Carefully read through all aspects presented by the contest organizers. - Identify which ingredient or component acts as the primary constraint towards achieving full capacity utilization. - Implement logic reflecting those relationships accurately; typically involving loops, conditionals, and possibly dynamic programming depending on complexity level required beyond simple minimum value determination across adjusted inputs. ```cpp #include <iostream> #include <vector> using namespace std; int main() { int n; cin >> n; vector<long long> a(n); for(int i=0;i<n;++i){ cin>>a[i]; } // Assuming indices correspond appropriately per problem statement's ratio requirement cout << min({a[0], a[1], a[2]/2LL, a[3]/7LL, a[4]/4LL}) << endl; } ``` --related questions-- 1. How does identifying bottlenecks help optimize algorithms solving constrained optimization problems? 2. What strategies should contestants adopt when translating mathematical formulas into code during competitive coding events? 3. Can you explain why understanding input-output relations is critical before implementing any algorithmic approach? 4. In what ways do prefix-suffix-middle frameworks enhance model training efficiency outside of just tokenization improvements? 5. Why might adjusting sample proportions specifically benefit models designed for tasks requiring both strong linguistic comprehension alongside logical reasoning skills?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值