zoukankan      html  css  js  c++  java
  • hdu 5646 DZY Loves Partition

    http://acm.hdu.edu.cn/showproblem.php?pid=5646

    题意:

    给n分成k个不同的数,求其乘积最大的数:

    思路:先判断最小k个数的乘积是否比n大,如果是,那么就不存在。

            要使k个数乘积最大,那么这k个数必须是接近的,就是a[i]和a[i+1]相差1,最多只有一个相差2:

     1 #include<iostream>
     2 #include<cstring>
     3 using namespace std;
     4 
     5 int a[1000000],b[1000000];
     6 
     7 int main()
     8 {
     9     long long t,n,k,i;
    10     for (i=1;i<100000;i++) a[i]=i;
    11     cin>>t;
    12     while (t--)
    13     {
    14         cin>>n>>k;
    15         long long cut=(1+k)*k/2;
    16         if (n<cut)
    17         {
    18             cout<<"-1"<<endl;
    19             continue;
    20         }
    21         n-=cut;
    22         long long x=n/k,y=n%k;
    23         if (!y)
    24         {
    25             for (i=1;i<=k;i++) b[i]=a[i]+x;
    26         }
    27         else
    28         {
    29             for (i=1;i<=k-y;i++) b[i]=a[i]+x;
    30             for (;i<=k;i++) b[i]=a[i]+x+1;
    31         }
    32         long long ans=1;
    33         for (i=1;i<=k;i++)
    34         {
    35             ans=(ans*b[i])%1000000007;
    36            // cout<<b[i]<<"****"<<endl;
    37         }
    38         cout<<ans<<endl;
    39     }
    40 }
  • 相关阅读:
    Rust语言学习笔记(11)
    Rust语言学习笔记(10)
    Rust语言学习笔记(9)
    Rust语言学习笔记(8)
    趣味编程:静夜思(Rust版)
    Python sorted()
    Python 魔法方法
    Python filter()
    Python的map和reduce
    Python函数的参数
  • 原文地址:https://www.cnblogs.com/pblr/p/5296401.html
Copyright © 2011-2022 走看看