/*
设r是个2^k 进制数,并满足以下条件:
(1)r至少是个2位的2^k 进制数。
(2)作为2^k 进制数,除最后一位外,r的每一位严格小于它右边相邻的那一位。
(3)将r转换为2进制数q后,则q的总位数不超过w。
在这里,正整数k(1≤k≤9)和w(k〈w≤30000)是事先给定的。
问:满足上述条件的不同的r共有多少个?
我们再从另一角度作些解释:设S是长度为w 的01字符串(即字符串S由w个“0”或“1”组成),S对应于上述条件(3)中的q。将S从右起划分为若干个长度为k 的段,每段对应一位2k进制的数,如果S至少可分成2段,则S所对应的二进制数又可以转换为上述的2k 进制数r。
例:设k=3,w=7。则r是个八进制数(2^3=8)。由于w=7,长度为7的01字符串按3位一段分,可分为3段(即1,3,3,左边第一段只有一个二进制位),则满足条件的八进制数有:
2位数:高位为1:6个(即12,13,14,15,16,17),高位为2:5个,…,高位为6:1个(即67)。共6+5+…+1=21个。
3位数:高位只能是1,第2位为2:5个(即123,124,125,126,127),第2位为3:4个,…,第2位为6:1个(即167)。共5+4+…+1=15个。
所以,满足要求的r共有36个。
输入
只有1行,为两个正整数,用一个空格隔开:
k w
输出
1行,是一个正整数,为所求的计算结果,即满足条件的不同的r的个数(用十进制数表示),要求最高位不得为0,各数字之间不得插入数字以外的其他字符(例如空格、换行符、逗号等)。
(提示:作为结果的正整数可能很大,但不会超过200位)
样例输入
3 7
样例输出
36
思路: 动态规划 与 K好数 差不多 再加上 对 大数相加 的办法 因为数字极大
//按题目要求 就是 r限定在2进制位
#include<stdio.h>
#include<string.h>
int main()
{
long int k,w,i,j,m,t=0,le,lemax=0,add;unsigned long long int dp[3000][10];//dp[i][j] 代表以第i位位高位是j时的可行方案
unsigned long long int sum=0;int ans[202];//储存答案
memset(dp,0,sizeof(dp));
scanf("%ld%ld",&k,&w);
t=w%k;// 获取特殊位 数值
w=w/k;//确定 可以按2^k 进制数完整的分组 有几组 即 除了最高位余下还有几位 2^3 7 2进制中
//7分成 1 3 3 有两组完整
k=(1<<k)-1;//确定进制数 k 为最大该进制数 8进制 最大进制数为 7;
t=(1<<t)-1;// 特殊组 的可取范围 7 分 1 3 3 特殊的是 1为2进制 最高位只能 取 1 如果是 2^3 8 的
// 话分 2 3 3 特殊 2的2进制位 则最高为可为 1 2 3 非特殊高位为 则可为 1 2 3 4 5 6 7
for(i=1;i<=k;i++)//初始化
dp[w+1][i]=1;//以1~k 位最低位可取记 1 为 基础值
for(i=w;i>1;i–)//从 低位向高位处理
for(j=k-(w+1-i);j>=1;j–) //高位为 j
{ for(m=j;m<k;m++)// 相邻位可行 数
dp[i][j]+=dp[i+1][m+1];
sum+=dp[i][j];
if(sum>10000000)
{ le=0;add=0;
while(sum)//大数 数据处理
{ ans[le++]+=sum%10+add;
add=ans[le-1]/10;
sum/=10;
if(lemax<le)lemax=le;
}
}
}
<span class="token keyword">for</span><span class="token punctuation">(</span>j<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span>j<span class="token operator"><=</span>t<span class="token punctuation">;</span>j<span class="token operator">++</span><span class="token punctuation">)</span><span class="token comment" spellcheck="true">//特殊位处理 </span>
<span class="token punctuation">{</span> <span class="token keyword">for</span><span class="token punctuation">(</span>m<span class="token operator">=</span>j<span class="token punctuation">;</span>m<span class="token operator"><</span>k<span class="token punctuation">;</span>m<span class="token operator">++</span><span class="token punctuation">)</span>
dp<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token operator">+</span><span class="token operator">=</span>dp<span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">[</span>m<span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
sum<span class="token operator">+</span><span class="token operator">=</span>dp<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">if</span><span class="token punctuation">(</span>sum<span class="token operator">></span><span class="token number">10000000</span><span class="token punctuation">)</span><span class="token comment" spellcheck="true">//大数 数据处理 </span>
<span class="token punctuation">{</span> le<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>add<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>
<span class="token keyword">while</span><span class="token punctuation">(</span>sum<span class="token punctuation">)</span>
<span class="token punctuation">{</span> ans<span class="token punctuation">[</span>le<span class="token operator">++</span><span class="token punctuation">]</span><span class="token operator">+</span><span class="token operator">=</span>sum<span class="token operator">%</span><span class="token number">10</span><span class="token operator">+</span>add<span class="token punctuation">;</span>
add<span class="token operator">=</span>ans<span class="token punctuation">[</span>le<span class="token number">-1</span><span class="token punctuation">]</span><span class="token operator">/</span><span class="token number">10</span><span class="token punctuation">;</span>
sum<span class="token operator">/</span><span class="token operator">=</span><span class="token number">10</span><span class="token punctuation">;</span>
<span class="token keyword">if</span><span class="token punctuation">(</span>lemax<span class="token operator"><</span>le<span class="token punctuation">)</span>lemax<span class="token operator">=</span>le<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
le<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>add<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>
<span class="token keyword">while</span><span class="token punctuation">(</span>sum<span class="token punctuation">)</span>
<span class="token punctuation">{</span> ans<span class="token punctuation">[</span>le<span class="token operator">++</span><span class="token punctuation">]</span><span class="token operator">+</span><span class="token operator">=</span>sum<span class="token operator">%</span><span class="token number">10</span><span class="token operator">+</span>add<span class="token punctuation">;</span>
add<span class="token operator">=</span>ans<span class="token punctuation">[</span>le<span class="token number">-1</span><span class="token punctuation">]</span><span class="token operator">/</span><span class="token number">10</span><span class="token punctuation">;</span>
sum<span class="token operator">/</span><span class="token operator">=</span><span class="token number">10</span><span class="token punctuation">;</span>
<span class="token keyword">if</span><span class="token punctuation">(</span>lemax<span class="token operator"><</span>le<span class="token punctuation">)</span>lemax<span class="token operator">=</span>le<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token keyword">for</span><span class="token punctuation">(</span>i<span class="token operator">=</span>lemax<span class="token number">-1</span><span class="token punctuation">;</span>i<span class="token operator">>=</span><span class="token number">0</span><span class="token punctuation">;</span>i<span class="token operator">--</span><span class="token punctuation">)</span>
<span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"%d"</span><span class="token punctuation">,</span>ans<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
return 0;
}