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,枚举回文串 ...
随机推荐
- protobuf2.5 iphone5s中崩溃的问题
我们的游戏用到了protobuf2.5,在其他版本中都是好的,但iphone5s中崩溃,表现为针对DescriptorPool为空了.网上也找不到什么信息,xiaozhong同学各种尝试,都没有搞定, ...
- Python Cookbook笔记
字符串:s.strip() 删除字符串开始和结尾的空白字符. s.lstrip() 删除左边的,s.rstrip() 删除右边的. 随机数:random.random() 生成0-1之间的数. ...
- html+jq实现简单的图片轮播
今天上班没事,就自己琢磨着写一下图片轮播,可是没想到,哈哈竟然写出来啦,下面就贴出来代码,作为纪念保存下下哈: <body style="text-align: center;&quo ...
- js 判断微信浏览器(转)
最近做很多HTML5的项目,很多页面会通过微信微博等SNS分享出去.在分享页面上提供公司APP的下载.但是在很多应用的浏览器中,点击下载链接无法下载应用.那么针对这些浏览器我们需要给用户提示从safa ...
- javascript 闭包的理解
1 需要明白概念: 执行环境 变量对象,活动对象 作用域,作用域链 闭包 垃圾处理机制 闭包陷阱
- nginx 配置文件解析(一)
nginx.conf user nginx; # nginx服务的运行用户 worker_processes ; # 启动进程数,通常设置成和CPU的数量相等 error_log /var/log/n ...
- jq改变DIV中图片的路径
<script src="common/jquery.js"></script> <script language="javascript ...
- unresolved import 解决办法
安装paramiko 需要先安装另两个模块 安装时未注意32bit,安装了64的,本地python是32的所以出错,重装后报错unresolved import,环境是eclipse(pydev) 用 ...
- python登陆教务管理系统
想试着模拟登陆一些网站,这次先拿学校的教务管理系统练练手,写一下登陆的流程. 1.我们登陆的url:http://222.195.8.201,但我们所填的密码不是提交到这个页面上去,检查一下页面代码 ...
- CSAPP--优化程序性能
一.编写高效的程序: 1.选择合适的算法和数据结构. 2.编写出编译器能够有效优化以转换为高效可执行的源代码. 3.并行计算.当然重点还是第一个,良好的算法和数据结构大大减小了程序的时间复杂度. 二. ...