题目大意:求前N位与后N位各个位和相等且总和等于S的2N位数的个数。

Time Limit:2000MS     Memory Limit:16384KB     64bit IO Format:%I64d & %I64u

数据规模:1<=N<=50,0<=S<=1000。

理论基础:无。

题目分析:用dp[i][j]表示前i位和为j的数的个数,那么答案就是:dp[N][S/2]*dp[N][S/2]。一定要注意,当S为奇数时无解的单独处理。

状态转移方程:dp[i][j]=sum(dp[i-1][j-k],0<=k<=9)。可想而知最后的数一定会很大。所以注意精度问题。

代码如下:

#include<iostream>
#include<cstring>
#include<string>
#include<cstdlib>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<queue>
#include<ctime>
#include<vector>
using namespace std;
typedef double db;
#define DBG 1
#define maa (1<<31)
#define mii ((1<<31)-1)
#define ast(b) if(DBG && !(b)) { printf("%d!!|\n", __LINE__); while(1) getchar(); } //调试
#define dout DBG && cout << __LINE__ << ">>| "
#define pr(x) #x"=" << (x) << " | "
#define mk(x) DBG && cout << __LINE__ << "**| "#x << endl
#define pra(arr, a, b) if(DBG) {\
dout<<#arr"[] |" <<endl; \
for(int i=a,i_b=b;i<=i_b;i++) cout<<"["<<i<<"]="<<arr[i]<<" |"<<((i-(a)+1)%8?" ":"\n"); \
if((b-a+1)%8) puts("");\
}
template<class T> inline bool updateMin(T& a, T b) { return a>b? a=b, true: false; }
template<class T> inline bool updateMax(T& a, T b) { return a<b? a=b, true: false; }
typedef long long LL;
typedef long unsigned int LU;
typedef long long unsigned int LLU;
#define N 100
#define M 50
#define S 1000
typedef struct bign
{
int digit[N+1];
int lenth;
void valid()
{
int i=0,n=lenth-1;
while(i<n)
{
if(digit[i]>=1000)
{
digit[i+1]=digit[i]/1000+digit[i+1];
digit[i]=digit[i]%1000;
}
i++;
}
while(digit[i])
{
if(digit[i]>=1000)
{
digit[i+1]=digit[i]/1000;
digit[i]=digit[i]%1000;
lenth++;
}
i++;
}
}
bign(){memset(digit,0,sizeof(digit));lenth=1;}
bign operator * (const bign& b)const
{
bign c;
for(int i=0;i<lenth;i++)
{
for(int j=0;j<lenth;j++)
{
c.digit[i+j]=c.digit[i+j]+digit[i]*b.digit[j];
}
}
c.lenth=lenth+b.lenth-1;
c.valid();
return c;
}
bign operator + (const bign& b)const
{
bign c;
c.lenth=max(b.lenth,lenth);
for(int i=0;i<c.lenth;i++)
{
c.digit[i]=digit[i]+b.digit[i];
}
c.valid();
return c;
}
bign operator = (bign a)
{
lenth=a.lenth;
for(int i=0;i<a.lenth;i++)
{
digit[i]=a.digit[i];
}
return *this;
}
bign operator = (int a)
{
digit[0]=a;
valid();
return *this;
}
}Ans;
Ans dp[M+1][S+1],ans;
int n,s;
int main()
{
scanf("%d%d",&n,&s);
if(s&1)
{
printf("0\n");
exit(0);
}
s/=2;
for(int i=0;i<=9;i++)dp[1][i]=1;
for(int i=2;i<=n;i++)
{
for(int j=0;j<=s;j++)
{
for(int k=0;k<=9&&k<=j;k++)
{
dp[i][j]=dp[i][j]+dp[i-1][j-k];
}
}
}
ans=dp[n][s]*dp[n][s];
printf("%d",ans.digit[ans.lenth-1]);
for(int i=ans.lenth-2;i>=0;i--)
{
printf("%03d",ans.digit[i]);
}
puts("");
return 0;
}

其中,对于高精度的运算只涉及到赋值与乘法,加法运算,自己编写代码也不是很难。

URAL 1036的更多相关文章

  1. URAL 1036(dp+高精度)

    Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Pract ...

  2. DP+高精度 URAL 1036 Lucky Tickets

    题目传送门 /* 题意:转换就是求n位数字,总和为s/2的方案数 DP+高精度:状态转移方程:dp[cur^1][k+j] = dp[cur^1][k+j] + dp[cur][k]; 高精度直接拿J ...

  3. Ural 1036 Lucky Tickets

    Lucky Tickets Time Limit: 2000ms Memory Limit: 16384KB This problem will be judged on Ural. Original ...

  4. URAL DP第一发

    列表: URAL 1225 Flags URAL 1009 K-based Numbers URAL 1119 Metro URAL 1146 Maximum Sum URAL 1203 Scient ...

  5. 【hihoCoder】1036 Trie图

    题目:http://hihocoder.com/problemset/problem/1036 给一个词典dict,词典中包含了一些单词words.要求判断给定的一个文本串text中是否包含这个字典中 ...

  6. BZOJ 1036: [ZJOI2008]树的统计Count [树链剖分]【学习笔记】

    1036: [ZJOI2008]树的统计Count Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 14302  Solved: 5779[Submit ...

  7. Windows Locale Codes - Sortable list(具体一个语言里还可具体细分,中国是2052,法国是1036)

    Windows Locale Codes - Sortable list NOTE: Code page is an outdated method for character encoding, y ...

  8. BZOJ 1036: [ZJOI2008]树的统计Count

    1036: [ZJOI2008]树的统计Count Time Limit: 10 Sec  Memory Limit: 162 MB Submit: 14354  Solved: 5802 [Subm ...

  9. 后缀数组 POJ 3974 Palindrome && URAL 1297 Palindrome

    题目链接 题意:求给定的字符串的最长回文子串 分析:做法是构造一个新的字符串是原字符串+反转后的原字符串(这样方便求两边回文的后缀的最长前缀),即newS = S + '$' + revS,枚举回文串 ...

随机推荐

  1. 重写boost内存池

    最近在写游戏服务器网络模块的时候,需要用到内存池.大量玩家通过tcp连接到服务器,通过大量的消息包与服务器进行交互.因此要给每个tcp分配收发两块缓冲区.那么这缓冲区多大呢?通常游戏操作的消息包都很小 ...

  2. github atom创建自己的语法高亮

    使用atom一段时间了,有些插件还不是很成熟.比如项目中使用protobuf,早就有人写了语法高亮(https://github.com/podgib/atom-protobuf),但是效果不是很好. ...

  3. centos下部署redis服务环境的操作记录

    Redis是一个开源的使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型.Key-Value数据库,并提供多种语言的API.从2010年3月15日起,Redis的开发工作由VMware主 ...

  4. ORACLE STUDY NOTES 01

    [JSU]LJDragon's Oracle course notes In the first semester, junior year DML数据操纵语言 DML指:update,delete, ...

  5. linux下javaEE系统安装部署

    最近公司在将服务器往阿里云上面迁移,所以需要重新在linux上面安装相关的软件以及部署项目,这里用到的linux版本为centos7.0,需要安装的软件有 jdk1.7.mysql5.6.mongo3 ...

  6. Javascript 精髓整理篇之二(函数篇)postby:http://zhutty.cnblogs.com

    今天总结的内容是javascript的function, 涉及到function顺便讲讲this. Function 是javascript的函数,也是js的执行单元.函数是JavaScript的一种 ...

  7. IOS-连接

    http://blog.sina.com.cn/s/articlelist_2659739627_0_2.html

  8. 控制uibutton的title范围

    moreBtn.contentEdgeInsets = UIEdgeInsetsMake(0,10, 0, 10);

  9. Jenkins - 持续集成

    Jenkins是一个开源软件项目,旨在提供一个开放易用的软件平台,使持续集成变成可能. Jenkins,之前叫做Hudson,是基于Java开发的一种持续集成工具,用于监控秩序重复的工作,包括: 1. ...

  10. Python的学习

    1.psutil的安装 [root@YQY-TIAN- ~]# wget https://pypi.python.org/packages/source/p/psutil/psutil-2.0.0.t ...