HDU 5355 Cake, hdu5355cake
HDU 5355 Cake
Updated code:
I thought a lot of ideas when I re-wrote this question today.
Finally, I finally came up with a thought of perfection, but the result timed out.
I feel like I am not saving myself.
Finally, we added the memory-based search and the AC
Okay, let's talk about it first. I don't know if everyone is paying attention to m <= 10.
We can write most of the data in pairs, for example, n = 27 m = 6.
1st copies 27 16
2nd copies 26 17
3rd copies 25 18
4th copies 24 19
5th copies 23 20
6th copies 22 21
1 ~ 15. Search for 6 equal parts to all users.
In this way, we try to add as many even numbers as possible.
Ensure that the remaining number is greater than or equal to 2 * m and less than 4 * m
So we only need to search for the remaining number smaller than 4 m (less than 40 ).
So the range of n is 1 ~ 40, m range 1 ~ 10. record these results in this way (to prevent such data from repeated occurrence)
In this case, if we have data after 15 or 6 calculations
N = 27 m = 6
N = 39 m = 6
N = 15 + (arbitrary 12) m = 6
We do not need to search any more.
In this way, the problem is solved.
# Include <algorithm> # include <iostream> # include <cstring> # include <cstdio> # include <vector> # define maxn 100005 # define ll _ int64 # define cnm c [nu] [m] using namespace std; int v [15] [maxn]; ll m, n; // fun Function Definition divides 1-num evenly to l-r individual int a [15] [50]; int si [15]; bool B [50]; int dp [50] [10] = {0 }; int c [50] [10] [15] [50] = {0}; ll ave, nu; bool dfs (int sum, int g, int s) {if (sum = ave) {g ++; sum = 0; s = 1 ;}if (g = m-1) {for (int I = 1; I <= nu; I ++) if (! B [I]) a [g] [++ a [g] [0] = I; return true;} for (int I = s; I <= nu; I ++) {if (sum + I> ave) return false; if (! B [I]) {B [I] = true; a [g] [++ a [g] [0] = I; if (dfs (sum + I, g, I + 1) return true; B [I] = false; a [g] [0] --;} return false;} bool fun (ll num) {if (num> = 4 m-1) {for (int I = 0, j = 0; I <m; I ++, j ++) {v [I] [si [I] ++] = num-j; v [I] [si [I] ++] = num-2 * m + 1 + j ;} return fun (num-2 * m);} else {nu = num; ave = (num + 1) * num/2/m; int B; if (dp [num] [m] = 0) {if (dfs (0, 0, 1) {dp [num] [m] = 1; for (int I = 0; I <m; I ++) for (int j = 0; j <= a [I] [0]; j ++) cnm [I] [j] = a [I] [j];} else dp [num] [m] =-1 ;} if (dp [nu] [m] = 1) return true; return false ;}} void out (int n) {printf ("YES \ n "); for (int I = 0; I <n; I ++) {printf ("% d", si [I] + cnm [I] [0]); for (int j = 0; j <si [I]; j ++) printf ("% d", v [I] [j]); for (int j = 0; j <cnm [I] [0]; j ++) printf ("% d", cnm [I] [j + 1]); printf ("\ n") ;}} void Init () {memset (si, 0, sizeof (si); memset (a, 0, sizeof ()); memset (B, 0, sizeof (B);} int main () {int T; scanf ("% d", & T); while (T --) {scanf ("% I64d % I64d", & n, & m); ll su = n * (n + 1)/2; ll av = su/m; if (su % m | av <n) printf ("NO \ n"); else {Init (); bool OK = fun (n); if (OK) out (m); else printf ("NO \ n") ;}} return 0 ;}
/*
This code has limitations. After data is updated
To be updated ......
*/
The first thought of this question seems to be brute force, and the result is actually over again.
#include<algorithm>#include<iostream>#include<cstring>#include<cstdio>#include<vector>#define maxn 100005#define ll __int64using namespace std;int a[maxn];vector <int > v[maxn];int main(){ int T; scanf("%d",&T); while(T--){ ll n,m; scanf("%I64d%I64d",&n,&m); ll sum =(n+1)*n/2; if(sum%m==0){ ll ave=sum/m; if(ave<n) printf("NO\n"); else{ memset(v,0,sizeof(v)); memset(a,0,sizeof(a)); int t=ave,A=0; int flag=0; for(int i=n;!flag&&i>=1;i--){ if(a[i]==0){ t-=i; v[A].push_back(i); a[i]=1; } if(i-1>t){ for(int j=i-1;t&&j>=1;j--){ if(t>=j&&a[j]==0){ v[A].push_back(j); t-=j; a[j]=1; } } } if(t==0){ t=ave; A++; } } if(A==m){ printf("YES\n"); for(int i=0;i<m;i++){ printf("%d",v[i].size()); for(int j=0;j<v[i].size();j++) printf(" %d",v[i][j]); printf("\n"); } } else printf("NO\n"); } } else printf("NO\n"); } return 0;}
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.