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<=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)