Gym 100952 H. Special Palindrome
http://codeforces.com/gym/100952/problem/H
A sequence of positive and non-zero integers called palindromic if it can be read the same forward and backward, for example:
15 2 6 4 6 2 15
20 3 1 1 3 20
We have a special kind of palindromic sequences, let's call it a special palindrome.
A palindromic sequence is a special palindrome if its values don't decrease up to the middle value, and of course they don't increase from the middle to the end.
The sequences above is NOT special, while the following sequences are:
1 2 3 3 7 8 7 3 3 2 1
2 10 2
1 4 13 13 4 1
Let's define the function F(N), which represents the number of special sequences that the sum of their values is N.
For example F(7) = 5 which are : (7), (1 5 1), (2 3 2), (1 1 3 1 1), (1 1 1 1 1 1 1)
Your job is to write a program that compute the Value F(N) for given N's.
The Input consists of a sequence of lines, each line contains a positive none zero integer N less than or equal to 250. The last line contains 0 which indicates the end of the input.
Print one line for each given number N, which it the value F(N).
1
3
7
10
0
1
2
5
17
动态规划题目,动态规划不是很懂,此代码是参考大牛的代码敲得,自己过时是打表过的。
#include <iostream> #include <cstdio> #include <cstring> #include <queue> #include <cmath> #include <map> #include <set> #include <vector> #include <algorithm> using namespace std; #define lowbit(x) (x&(-x)) #define max(x,y) (x>y?x:y) #define min(x,y) (x<y?x:y) #define MAX 100000000000000000 #define MOD 1000000007 #define PI 3.141592653589793238462 #define INF 0x3f3f3f3f3f #define mem(a) (memset(a,0,sizeof(a))) typedef long long ll; ll dp[300][300]; ll vis[300],n; void init() { dp[0][1]=dp[1][1]=dp[2][1]=1; dp[2][2]=dp[3][1]=dp[3][3]=1; vis[1]=1;vis[2]=vis[3]=2; for(int i=4;i<=250;i++) { ll ans=0; for(int j=1;j<=i/2;j++) { if(j==1) dp[i][j]=vis[i-2]; else { ll pos=i-j*2; if(pos==0) dp[i][j]=1; else { ll cnt=0; for(int k=j;k<=pos;k++) { cnt+=dp[pos][k]; } dp[i][j]=cnt; } } ans+=dp[i][j]; } dp[i][i]=1; ans+=1; vis[i]=ans; } } int main() { init(); while(scanf("%lld",&n) && n) { printf("%lld\n",vis[n]); } return 0; }
再来一个打表代码
#include <iostream> #include <cstdio> #include <cstring> #include <queue> #include <cmath> #include <map> #include <vector> #include <algorithm> using namespace std; #define lowbit(x) (x&(-x)) #define max(x,y) (x>y?x:y) #define min(x,y) (x<y?x:y) #define MAX 100000000000000000 #define MOD 1000000007 #define PI 3.141592653589793238462 #define INF 0x3f3f3f3f3f #define mem(a) (memset(a,0,sizeof(a))) typedef long long ll; ll n; ll vis[300]={1,2,2,4,3,7,5,11,8,17,12,26,18,37,27,54,38,76,54,106,76, 145,104,199,142,266,192,357,256,472,340,621,448,809,585,1053,760,1354,982,1740,1260,2218,1610,2818,2048, 3559,2590,4485,3264,5616,4097,7018,5120,8728,6378,10826,7917,13373,9792,16484,12076, 20236,14848,24793,18200,30275,22250,36886,27130,44810,32992,54329,40026,65683,48446,79265,58499,95419, 70488,114650,84756,137447,101698,164496,121792,196445,145578,234221,173682,278720,206848,331143,245920,392722,291874,465061, 345856,549781,409174,649019,483330,764959,570078,900373,671418,1058191,789640,1242061,927406,1455820,1087744,1704261,1274118,1992458,1490528, 2326608,1741521,2713398,2032290,3160899,2368800,3677789,2757826,4274556,3207086,4962526,3725410,5755174,4322816, 6667228,5010688,7716070,5802008,8920663,6711480,10303379,7755776,11888671,8953856,13705118, 10327156,15784173,11899934,18162385,13699699,20879933,15757502,23983452,18108418,27524280,20792120, 31561603,23853318,36160845,27342421,41397124,31316314,47353396,35839008,54124796,40982540,61816437,46828032,70548311,53466624, 80453313,61000704,91682668,69545358,104403741,79229676,118806744,90198446,135102223, 102614114,153528658,116658616,174350347,132535702,197865953,150473568,224406247, 170727424,254344551,193582642,288094273,219358315,326120818,248410816,368939881,281138048,417130912,317984256,471335560,359444904, 532274004,406072422,600743477,458482688,677637038,517361670,763943462,583473184,860768675, 657667584,969336374,740890786,1091013811,834194700,1227313238,938748852,1379921672,1055852590,1550704877,1186949056,1741741564,1333640710, 1955329266,1497705768,2194025352,1681116852,2460655086,1886061684,2758359212,2114965120,3090606588,2370513986,3461249193, 2655684608,3874538905,2973772212,4335193118,3328423936,4848416380,3723675326,5419976831,4163989458,6056235989,4654300706, 6764237552,5200062976,7551745299,5807301632,8427348786,6482671322,9400510845,7233519619,10481691022, 8067955712,11682407480,8994926602,13015380900,10024300890,14494611559,11166959338,16135550148,12434895064}; int main() { while(scanf("%lld",&n) && n) { cout<<vis[n-1]<<endl; } return 0; }