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,枚举回文串 ...
随机推荐
- github atom创建自己的语法高亮
使用atom一段时间了,有些插件还不是很成熟.比如项目中使用protobuf,早就有人写了语法高亮(https://github.com/podgib/atom-protobuf),但是效果不是很好. ...
- 一、spark 数据类型(Data Types)
Data Types - MLlib(数据类型) MLlib支持存储在单机上的局部向量和局部矩阵,也可以支持通过一个或多个RDD(可伸缩数据集)表示的分布式矩阵.局部向量和局部矩阵是用作公 ...
- iOS语音识别,语音播报,文字变语音播报,语音变文字
首先使用的是科大讯飞的sdk 1.语音识别部分 AppDelegate.m #import "AppDelegate.h" #import <iflyMSC/iflyMSC. ...
- tomcat端口占用后的解决办法
学 习网页设计的同学都会用到tomcat这个软件,在安装的时候我们一般都会选择端口为8080端口,这个端口一般情况下是不会有程序占用的,所以我们运行 tomcat不会出现什么问题,但是如果一旦别占用, ...
- jquery之营销系统
// //////////////////////////优惠券开始//////////////////////////// // 给附加条件选择框添加事件 function inputFuJia() ...
- [Linux命令]tar命令
tar 命令的解释: tar(bsdtar): manipulate archive files First option must be a mode specifier: -c Create -r ...
- 重学《C#高级编程》(继承)
前两天重新看了<C#高级编程>里的第四章:继承与第六章:数组.OOP三大特性:封装,继承,多态,个人感觉继承是实现多态的基础,包括以后接触的设计模式,都是继承特性的衍生. 继承特性有两种, ...
- Cretiria查询应用(二)
1.条件查询,动态查询 public void conditionQuery(){ Session session=null; try { session=HibernateUtil.currentS ...
- 1.Android Studio系列教程1——下载和安装
链接:http://stormzhang.com/devtools/2014/11/25/android-studio-tutorial1/ 一.Android Studio优点 1.Google推出 ...
- VC实用小知识总结 (一),转http://blog.csdn.net/myiszjf/article/details/10007431
在上一篇中,我们以经介绍了程序的流程和框架,在本篇将详细讨论各个功能的实现主要包括 1.获取磁盘信息2.获取目录信息3.获取文件信息4.运行指定文件5.删除指定文件6.删除指定目录7.创建指定目录8. ...