题解:P17189 [ICPC 2017 Hong Kong R] Count the Even Integers
问题定义
给定正整数
的有序正整数对
Lucas 定理的转化
把
由 Lucas 定理,在模
当
因此:
此问题可用数位 DP 在二进制下直接统计。
具体实现
考虑设计状态:
设
直接记搜实现即可。
::::warning[警告]
题目十分毒瘤,数据范围极大,long long 存不下。
于是就有两种方式。
:::success[我会实现高精度]
几乎所有变量都要用高精度,因为 dp 数组中的最大值接近
:::success[我会用 pyhton 或者 java] 自带高精度,直接写即可。 ::: ::::
:::info[代码( python )]
def dec_to_bin(s):
if s=="0": return "0"
bin=""
while s!="0":
rem=0
q=""
for i in range(len(s)):
cur=rem*10+int(s[i])
d=cur//2
rem=cur%2
if q!="" or d>0:
q+=str(d)
bin=str(rem)+bin
s=q if q!="" else "0"
return bin
def dfs(cur,can,dif,lia,lib,lima,limb,dp,k):
if cur==0:
return 1 if can else 0
if dp[cur][can][dif][lia][lib]!=-1:
return dp[cur][can][dif][lia][lib]
up1=lima[cur] if lia else k-1
up2=limb[cur] if lib else k-1
res=0
for i in range(up1+1):
for j in range(up2+1):
if (not dif) and j>i:
continue
res+=dfs(cur-1,can or (i<j),dif or (i!=j),lia and (i==up1),lib and (j==up2),lima,limb,dp,k)
dp[cur][can][dif][lia][lib]=res
return res
def solve():
n=input().strip()
bin=dec_to_bin(n)
length=len(bin)
lima=[0]*(length+1)
limb=[0]*(length+1)
for i in range(1,length+1):
lima[i]=int(bin[length-i])
limb[i]=int(bin[length-i])
dp=[[[[[-1]*2 for _ in range(2)] for _ in range(2)] for _ in range(2)] for _ in range(length+1)]
ans=dfs(length,0,0,1,1,lima,limb,dp,2)
print(ans)
:::
可以做一下这个题的扩展版,思路是一样的:P6669 [清华集训 2016] 组合数问题。
蒟蒻第一次写题解,求过。