2016 ACM/ICPC Dalian Station F-detachment [maintain prefix product, prefix sum, and binary search optimization]

Source: Internet
Author: User
Tags mul
F-detachmentin a highly developed alien society, the habitats are almost infinite dimen1_space.
In the history of this planet, there is an old puzzle.
You have a line segment with X units 'length representing one dimension. The line segment can be split into a number of small line segments: A1, a2a1, A2 ,... (X = A1 + a2a1 + A2 + ...) Assigned to different dimensions. And then, the multidimen1_space has been established. Now there are two requirements for this space:
1. Two different small line segments cannot be equal (AI =ajai =aj when I =j ).
2. Make this multidimen1_space size S as large as possible (S = A1? A2a1? A2 *...). Note that it allows to keep one dimension. That's to say, the number of AI can be only one.
Now can you solve this question and find the maximum size of the space? (For the final number is too large, your answer will be modulo 10 ^ 9 + 7)

Inputthe first line is an integer T, meaning the number of test cases.
Then T lines follow. Each line contains one integer x.
1 ≤ T ≤ 10 ^ 6, 1 ≤ x ≤ 10 ^ 9 outputmaximum s you can get modulo 10 ^ 9 + 7. note that we wants to be greatest product before modulo 10 ^ 9 + 7.

Sample Input

14

Sample output

4

 

Summary:

To give a number N, it can be divided into several different numbers n = A1 + A2 + A3 + ......, find the largest S = A1 * A2 * A3 *.......

No matter how many points you want.

Solution:

At first, I thought it was a regular question,

Later I pushed fruitless results. I found a magical greedy pen on 11.

If the product is maximized, the more severable numbers, the better.

The number of numbers that each number can be divided into is regular.

X1 = 1, S1 = 1;

X2 = 2, S2 = 2;

X3 = 3, S3 = 3;

X4 = 4, S4 = 4;

 

X5 = 2 + 3, S5 = 2*3;

X6 = 2 + 4 [2 + (3 + 1)], S6 = 2*4;

X7 = 3 + 4 [(2 + 1) + (3 + 1)], S7 = 3*4;

X8 = 3 + 5 [(2 + 1) + (3 + 1 + 1)], S8 = 3*5;

 

X9 = 2 + 3 + 4, S9 = 2*3*4;

X10 = 2 + 3 + 5 [2 + 3 + (4 + 1)], S10 = 2*3*5;

X11 = 2 + 4 + 5 [2 + (3 + 1) + (4 + 1)], S11 = 2*4*5;

...

It is obvious how to construct greed,

If you split a number into two with the maximum product requirements, it must be split into two with the same number. If you split them into N, it must be split into N. If you didn't say a few, it must be split into several three, and the rest is split into two. the requirements for this question cannot be the same, so it must be arranged in the order of 2, 3, and 4. Starting from 2, it will make it smaller, so that the number will be the most, and adding one at a time will make him closer and closer. So with prefix and record, if there is surplus, it must be from the back one by one + 1, and the remaining number can be at most = 2, 3, 4... the maximum number. For example, we can see two situations: 1. for example, the remaining 5 of 2*3*4*5 is equal. The optimum is 3*4*5*7, that is, each number is added again, and then 1 is left, and the last one is added. In general, divide by 2 and multiply T + 2, (T is the value of the last number); 2. they are not equal. For example, 2, 3, 4, 5, and 2, the optimum is 2, 3, 5, and 6. The factorial of * 6 is divided by the factorial of 4. Because there are too many factorial, one multiplication is not allowed, the prefix is used for product division. If the number is too large, the reverse element is used .. Then we use binary optimization ..

If the data volume is large, prefix product + reverse element should be used, prefix and optimization should be used, and the nearest prefix and less than the current value should be searched for the remainder.

 

Refer to a proof of Daniel:

53466435

 

The key to this question is how to maximize the product S.

Based on past experience, it is inevitable that a continuous natural number can maximize the product, and this continuous natural number can start from 2 (why not start from 1? From 1, it is better to give this 1 to the last number of this continuous natural number ),

So we can get 2 + 3 + 4 +... + K (k = 2, 3 ,...) and X is any integer in 10 ^ 9. We cannot just make it into the sum of consecutive natural numbers, and we may have more △x.

The value of △x can be guaranteed to be in the range of 0 ≤ △x ≤ k. I believe it is understandable that it is greater than or equal to 0. Why is it smaller than or equal to k? Because when it is larger than K, can the original formula add one? 2 + 3 + 4 +... + K + (k + 1)

So how to deal with the extra △x? Apparently from the back forward evenly spread to the continuous Natural Number of (k-1) number, why from the back forward? Because if we used to repeat consecutive natural numbers, it would be hard to handle them.

Therefore, after we allocate △x, we will get the following two formula:

① 2*3 *... * (I-1) * (I + 1) *... * K * (k + 1) ② 3*4 *... * I * (I + 1) *... * K * (K + 2)

Obviously, to calculate this result, we can use the factorial, And we can remove the missing items in the factorial, which will involve division modulo. Obviously, we need to use the multiplication inverse element.

After the practice is explained, the following is why the continuous segment has the largest product.Approximate proof:

 

 

 

 

AC code:

1 # include <cstdio> 2 # include <iostream> 3 # include <algorithm> 4 # include <cstring> 5 # define ll long 6 using namespace STD; 7 const int maxn = 1e5 + 5; 8 const int mod = 1e9 + 7; 9 10 LL Mul [maxn], sum [maxn]; 11 void Init () 12 {13 Mul [1] = 1; 14 Sum [1] = 0; 15 for (INT I = 2; I <maxn; I ++) {16 sum [I] = sum [I-1] + I; // prefix and 17 Mul [I] = (I * Mul [I-1]) % MOD; // prefix product 18} 19} 20 21 ll inv (ll a, int B) // returns reverse element 22 {23 ll ans = 1; 24 while (B) {25 if (B & 1) ans = (ANS * A) % MOD; 26 A = (A * A) % MOD; 27 B >>= 1; 28} 29 return ans; 30} 31 32 int main () 33 {34 int t, x; 35 Init (); 36 scanf ("% d", & T ); 37 while (t --) {38 scanf ("% d", & X); 39 if (x = 1) {40 puts ("1"); 41 continue; 42} 43 int L = 2, R = maxn, mid, P; 44 While (L <= r) {45 mid = (L + r)/2; 46 If (sum [Mid] <= x) P = mid, L = Mid + 1; 47 else r = mid-1; 48} 49 // printf ("% d \ n", P); 50 int num = x-sum [p]; 51 LL ans = 0; 52 If (num = P) 53 ans = (Mul [p] * inv (2, mod-2) % mod * (p + 2) % MOD; 54 else55 ans = (Mul [p + 1] * inv (Mul [p + 1-num], mod-2) % mod * Mul [p-num]) % MOD; 56 printf ("% LLD \ n", ANS); 57} 58 return 0; 59} 60 61 F-Detachment
View code

 

2016 ACM/ICPC Dalian Station F-detachment [maintain prefix product, prefix sum, and binary search optimization]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.