Codeforces Round #297 (Div. 2) Question E. Anya and Cubes (Midway encounter method)

Source: Internet
Author: User

Title Address: Anya and Cubes
When the game didn't even think of halfway through the meeting law ... This problem is also belong to think of a very simple series.
Halfway through the law is also called binary search. is to deal with the first half, store the results, process the second half, and then match the first half of the stored results.
The code is as follows:

#include <iostream>#include <string.h>#include <math.h>#include <queue>#include <algorithm>#include <stdlib.h>#include <map>#include <set>#include <stdio.h>using namespace STD;#define LL __int64#define PI ACOs ( -1.0)Const intMod=1e9+7;Const intinf=0x3f3f3f3f;Const Doubleeqs=1e-9;Const intmaxn=200000+Ten; map<ll,int>mp[ -];inta[ -], M, K, N; LL fac[ -], ans, s;voidInit () {intI fac[1]=1; for(i=2; i<= -; i++) {fac[i]=fac[i-1]*i; }}voidDfsintCur, LL tmp,intStep) {mp[step][tmp]++; for(inti=cur+1; i<m; i++) {DFS (i,tmp+a[i],step);if(a[i]<= -&&step+1&LT;=K) DFS (i,tmp+fac[a[i]],step+1); }}voidDFS1 (intCur, LL tmp,intStep) { for(intI=0; i<=k-step;i++) {if(Mp[i].find (s-tmp)!=mp[i].end ()) ans+=mp[i][s-tmp]; } for(inti=cur+1; i<n; i++) {DFS1 (i,tmp+a[i],step);if(a[i]<= -&&step+1<=k) DFS1 (i,tmp+fac[a[i]],step+1); }}intMain () {intI Init (); while(scanf("%d%d%i64d", &n,&k,&s)!=eof) {ans=0; m=n/2; for(i=0; i<n; i++) {scanf("%d", &a[i]); } for(i=0; i<=k;i++) mp[i].clear (); mp[0][0]=1; for(i=0; i<m; i++) {DFS (i,a[i),0);if(a[i]<= -) DFS (I,fac[a[i]),1); } for(i=0; i<=k; i++) {if(Mp[i].find (s)!=mp[i].end ()) ans+=mp[i][s]; } for(i=m; i<n; i++) {DFS1 (I,a[i],0);if(a[i]<= -) DFS1 (I,fac[a[i]),1); }printf("%i64d\n", ans); }return 0;}

Codeforces Round #297 (Div. 2) Question E. Anya and Cubes (Midway encounter method)

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: 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.