数学思维 7.23杭电多校赛 A

1.

Maximum Multiple

Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3985 Accepted Submission(s): 926

Problem Description

Given an integer n, Chiaki would like to find three positive integers x, y and z such that: n=x+y+z, x∣n, y∣n, z∣n and xyz is maximum.

Input

There are multiple test cases. The first line of input contains an integer T (1≤T≤106), indicating the number of test cases. For each test case:The first line contains an integer n (1≤n≤106).

Output

For each test case, output an integer denoting the maximum xyz. If there no such integers, output −1 instead.

Sample Input

3
1
2
3

Sample Output

-1
-1
1

题目大意:

​ 已知n,n=x+y+z,且n可以整除x,y,z,问符合条件的xyz的最大乘积

想法:

​ ”|“原来是整除符号的意思。数学推理啊,没细想,就交给队友了。听了直播题解,是只要推式子就好了。签到题。

思路:

​ 1、n=x+y+z 1= x/n+y/n+z/n 1=1/(n/x)+1/(n/y)+1/(n/z) 因为n整除x,y,z,所以

1=1/a+1/b+1/c 且a,b,c为整数。问题就成了abc的最小乘积

2、从小到大枚举一下,有3种: 2 4 4 , 3 3 3, 2 3 6(什么?你说枚举的不够多?你再举个试试?)

3、244:32,333:27,236:36。对应的xyz就是,n*n*n/32,/27,/36,所以n整除4取第一种,整除3取第三种,整除6。。。呃,就抛弃了。

标准题解

​.

AC代码:

// A
#include 
using namespace std;
#define rep(i,a,n) for (int i=a;i=a;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
typedef vector VI;
typedef long long ll;
typedef pair PII;
const ll mod=1000000007;
ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
// head
​
int n,_;
int main() {
    for (scanf("%d",&_);_;_--) {
        scanf("%d",&n);
        if (n%3==0) printf("%lld\n",1ll*n*n*n/27);       //1ll是把long long 的1,防计算溢出
        else if (n%4==0) printf("%lld\n",1ll*n*n*n/32);
        else puts("-1");
    }
}
​

 

参考:

https://www.ideone.com/Wo55gi

你可能感兴趣的:(大二暑假集训)