URAL 1036
题目大意:求前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的更多相关文章
- URAL 1036(dp+高精度)
Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Status Pract ...
- DP+高精度 URAL 1036 Lucky Tickets
题目传送门 /* 题意:转换就是求n位数字,总和为s/2的方案数 DP+高精度:状态转移方程:dp[cur^1][k+j] = dp[cur^1][k+j] + dp[cur][k]; 高精度直接拿J ...
- Ural 1036 Lucky Tickets
Lucky Tickets Time Limit: 2000ms Memory Limit: 16384KB This problem will be judged on Ural. Original ...
- URAL DP第一发
列表: URAL 1225 Flags URAL 1009 K-based Numbers URAL 1119 Metro URAL 1146 Maximum Sum URAL 1203 Scient ...
- 【hihoCoder】1036 Trie图
题目:http://hihocoder.com/problemset/problem/1036 给一个词典dict,词典中包含了一些单词words.要求判断给定的一个文本串text中是否包含这个字典中 ...
- BZOJ 1036: [ZJOI2008]树的统计Count [树链剖分]【学习笔记】
1036: [ZJOI2008]树的统计Count Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 14302 Solved: 5779[Submit ...
- Windows Locale Codes - Sortable list(具体一个语言里还可具体细分,中国是2052,法国是1036)
Windows Locale Codes - Sortable list NOTE: Code page is an outdated method for character encoding, y ...
- BZOJ 1036: [ZJOI2008]树的统计Count
1036: [ZJOI2008]树的统计Count Time Limit: 10 Sec Memory Limit: 162 MB Submit: 14354 Solved: 5802 [Subm ...
- 后缀数组 POJ 3974 Palindrome && URAL 1297 Palindrome
题目链接 题意:求给定的字符串的最长回文子串 分析:做法是构造一个新的字符串是原字符串+反转后的原字符串(这样方便求两边回文的后缀的最长前缀),即newS = S + '$' + revS,枚举回文串 ...
随机推荐
- JAXB--学习1
一.简介 JAXB(Java Architecture for XML Binding) 是一个业界的标准,是一项可以根据XML Schema产生Java类的技术.该过程中,JAXB也提供了将XML实 ...
- 【C#爬虫】抓取XX网站mp4资源地址
抓取小视频的url地址,然后将地址信息拷贝到迅雷里批量下载就ok了 主程序 代码 //yazhouqingseAV 35 //zhifusiwaAV 29 //zipaishipin 30 //oum ...
- sql server 2008 在安装了活动目录以后无法启动服务了
软件环境: windows server 2008 r2 ms sql server 2008 r2 在安装活动目录以前,数据库是正常运行的. 安装了活动目录以后,数据库启动时就提示无法启动.出错的信 ...
- WinXP 无线提示“区域中找不到无线网络”的一种可能原因!
貌似WinXP还是无限经典,我也一直还在用,不知道哪天才会放弃.这次遇见的问题,或许也有XP爱好者也遇得见,记下点文字备忘.----------------------- 单调的切割线 ------- ...
- c++ 连接两个字符串实现代码 实现类似strcat功能(转)
想实现strcat功能,直接网上找一个. 第一种: #include "stdafx.h" #include<iostream> using namespace std ...
- IE下判断IE版本语法使用
先摆一下判断IE版本语法 <!--[if lte IE 6]> <![endif]--> IE6及其以下版本可见 <!--[if lte IE 7]> <![ ...
- css布局&初始化&基准样式
学习css布局比较好的网站 学习css布局 1.css设置模块 typography(字体) colour(颜色) link(链接) forms(表单) layout(布局) navigation(导 ...
- box-shadow全面解析
一.box-shadow语法: box-shadow: none | inset(可选值,不设置,为外投影,设置,为内投影) x-offset(阴影水平偏移量,正方向为right) y-offset( ...
- openfire spark 二次 开发 服务插件
==================== 废话 begin ============================ 最近老大让我为研发平台增加即时通讯功能.告诉我用comet 在web端实现即 ...
- Swift中扩展的使用
import Foundation /* 扩展 1.使用扩展添加属性, 方法, 可变方法, 构造器, 下标, 嵌套类型 2.可以使一个已有类型符合一个或者多个协议 3.扩展与OC的Category类似 ...