HDU 4945 2048 (DP + Fast Power modulo)

Source: Internet
Author: User

Question: Give You A sequence for you to find out how many combinations can get 2048. The result is to get more than 998244353.

Solution: Find the number of solutions that cannot meet the conditions, and then subtract the total number from the non-satisfied number and then multiply it with other unrelated combinations. For example, the numbers 3 and 5 are useless in the process of making up 2048, so multiply these combinations.

DP [I] [J] indicates that the number of I 2 ^ I is obtained, and its maximum sum is from J * 2 ^ I to (j + 1) * 2 ^ number of solutions for I-1.

So DP [I] [J] + = (DP [I-1] [k] + dp [I-1] [k + 1]) * use [I] [J-k/2]) % Mod. Indicates the maximum sum of the number of J solutions at the I position. Some of them are obtained before, and some are newly added at the I position.

This question is very time-consuming, so it is necessary to use the reverse element for the modulo of the combined number and then add the quick power modulo.

PS: I think it's a good question. I will do more in the future.

2048 Time Limit: 3000/1500 MS (Java/others) memory limit: 65536/65536 K (Java/Others)
Total submission (s): 1052 accepted submission (s): 276


Problem descriptionteacher Mai is addicted to game 2048. But finally he finds it's too hard to get 2048. So he wants to change the rule:

You are given some numbers. Every time you can choose two numbers of the same value from them and merge these two numbers into their sum. And these two numbers disappear meanwhile.

If we can get 2048 from a set of numbers with this operation, teacher Mai think this Multiset is good.

You have n numbers, A1,..., An. Teacher Mai ask you how many subsequences of a are good.

The number can be very large and just output the number modulo 998244353.
Inputthere are multiple test cases, terminated by a line "0 ".

For each test case, the first line contains an integer N (1 <= n <= 10 ^ 5 ), the next line contains N integers AI (0 <= AI <= 2048 ).
Outputfor each test case, output one line "case # K: ANS", where k is the case number counting from 1, ANS is the number module 998244353.
 
Sample Input
41024 512 256 25641024 1024 1024 102451024 512 512 512 10
 
Sample output
Case #1: 1Case #2: 11Case #3: 8HintIn the first case, we should choose all the numbers.In the second case, all the subsequences which contain more than one number are good. 
 
Authorxudyh
Source2014 multi-university training contest 8
# Include <algorithm> # include <iostream> # include <stdlib. h> # include <string. h> # include <iomanip> # include <stdio. h> # include <string> # include <queue> # include <cmath> # include <stack> # include <map> # include <set> # define EPS 1e-10 ///# define M 1000100 // # define ll _ int64 # define ll long // # define INF 0x7ffffff # define INF 0x3f3f3f # define PI 3.1415926535898 # define zero (X) (FABS (x) <EPS )? 0: X) using namespace STD; const int maxn = 100010; # define mod 992134366int num [21]; ll DP [15] [2100]; ll use [15] [2100]; ll f [maxn]; int N; int add (int x) {If (x = 0) return 20; int ans = 0; while (X! = 1) {If (X & 1) return 20; X >>= 1; ans ++;} return ans;} ll q_mod (ll a, LL B, ll N) {ll ret = 1; ll TMP = A % N; while (B) {// The base has if (B & 0x1) ret = RET * TMP % N; TMP = TMP * TMP % N; B >>= 1;} return ret;} ll del (INT N, int m) {If (M> N) return 0ll; ll ans = f [N]; ans = (ANS * q_mod (f [m] * f [n-M]) % mod, mod-2, MoD )) % MOD; return ans;} void select () {for (INT I = 1; I <= 11; I ++) for (Int J = 0; j <2048; j ++) use [I] [J] = del (Num [I-1], J);} void change () {select (); For (INT I = 0; I <2048; I ++) DP [1] [I] = use [1] [I]; for (INT I = 2; I <= 11; I ++) {int CNT = (1 <(12-i); For (Int J = 0; j <CNT; j ++) {for (int K = 0; k <= 2 * j + 1; k + = 2) {If (! Use [I] [J-k/2] |! (DP [I-1] [k] + dp [I-1] [k + 1]) continue; DP [I] [J] + = (DP [I-1] [k] + dp [I-1] [k + 1]) * use [I] [J-k/2]) % MOD;} DP [I] [J] % = MOD;} ll ans = 0ll; ans = q_mod (2, num [20], MoD); ll ps = (DP [11] [0] + dp [11] [1]) % MOD; PS = q_mod (2, N-num [20], MoD)-PS; PS = (PS + mod) % MOD; ans = (ANS * PS) % MOD; printf ("% i64d \ n", ANS);} void Init () {f [0] = 1ll; For (ll I = 1; I <maxn; I ++) f [I] = (F [I-1] * I) % MOD;} int main () {int case = 1; init (); While (~ Scanf ("% d", & N) {memset (Num, 0, sizeof (Num); memset (DP, 0, sizeof (DP )); int X; For (INT I = 0; I <n; I ++) {scanf ("% d", & X); num [add (x)] ++;} printf ("case # % d:", Case ++); change ();} return 0 ;}


HDU 4945 2048 (DP + Fast Power modulo)

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.