长春网站建设优化排名,阿里巴巴免费做网站,wordpress如何改成中文字体,长春网站快速排名提升题目链接 Leetcode.2698 求一个整数的惩罚数 rating : 1679 题目描述 
给你一个正整数  n n n #xff0c;请你返回  n n n 的 惩罚数 。 n n n 的 惩罚数 定义为所有满足以下条件  i i i 的数的平方和#xff1a; 1 ≤ i ≤ n 1 \leq i \leq n 1≤i≤n i ∗ i i * i i∗i 的…题目链接 Leetcode.2698 求一个整数的惩罚数 rating : 1679 题目描述 
给你一个正整数  n n n 请你返回  n n n 的 惩罚数 。 n n n 的 惩罚数 定义为所有满足以下条件  i i i 的数的平方和 1 ≤ i ≤ n 1 \leq i \leq n 1≤i≤n i ∗ i i * i i∗i 的十进制表示的字符串可以分割成若干连续子字符串且这些子字符串对应的整数值之和等于  i i i 。 
示例 1 输入n  10 输出182 解释总共有 3 个整数 i 满足要求 1 因为 1 * 1  19 因为 9 * 9  81 且 81 可以分割成 8  1 。10 因为 10 * 10  100 且 100 可以分割成 10  0 。 因此10 的惩罚数为 1  81  100  182 示例 2 输入n  37 输出1478 解释总共有 4 个整数 i 满足要求 1 因为 1 * 1  19 因为 9 * 9  81 且 81 可以分割成 8  1 。10 因为 10 * 10  100 且 100 可以分割成 10  0 。36 因为 36 * 36  1296 且 1296 可以分割成 1  29  6 。 因此37 的惩罚数为 1  81  100  1296  1478 提示 1 ≤ n ≤ 1000 1 \leq n \leq 1000 1≤n≤1000 
解法回溯 
我们定义  d f s ( u , s u m , t , s ) dfs(u,sum,t,s) dfs(u,sum,t,s) 表示  s s s 能否拆分成若个子字符串能够满足这些子字符串的值加起来   t  t t。 
我们直接回溯枚举每一个子串的分割位置求出所有可能。 
时间复杂度  O ( n 1  2 log  2 10 ) O(n^{1  2 \log_{2}^{10}}) O(n12log210)  n n n 是给定的元素。对于给定的元素  n 2 n^2 n2将其转换为字符串的长度为  ⌊ m  1  2 log  10 i ⌋ \lfloor m  1  2 \log_{10}^{i} \rfloor ⌊m12log10i⌋回溯时的子状态为  2 m 2^m 2m 个所以时间复杂度为  O ( n 1  2 log  2 10 ) O(n^{1  2 \log_{2}^{10}}) O(n12log210)。 
C代码 
class Solution {
public:int punishmentNumber(int n) {int ans  0;functionbool(int,int,int,string) dfs  [](int u,int sum,int t,string s)-bool{if(u  s.size()){return sum  t;}if(sum  t) return false;for(int i  u , d  0;i  s.size();i){d  d * 10  s[i] - 0;if(dfs(i  1,sum  d,t,s)) return true;}return false;};for(int x  1;x  n;x){string s  to_string(x * x);if(dfs(0,0,x,s)) ans  x * x;}return ans;}
};
 文章转载自: http://www.morning.nngq.cn.gov.cn.nngq.cn http://www.morning.pbdnj.cn.gov.cn.pbdnj.cn http://www.morning.ckdgj.cn.gov.cn.ckdgj.cn http://www.morning.lmtbl.cn.gov.cn.lmtbl.cn http://www.morning.rsnn.cn.gov.cn.rsnn.cn http://www.morning.nggry.cn.gov.cn.nggry.cn http://www.morning.nmqdk.cn.gov.cn.nmqdk.cn http://www.morning.mjtgt.cn.gov.cn.mjtgt.cn http://www.morning.rwfp.cn.gov.cn.rwfp.cn http://www.morning.lsfbb.cn.gov.cn.lsfbb.cn http://www.morning.ndngj.cn.gov.cn.ndngj.cn http://www.morning.wnxqf.cn.gov.cn.wnxqf.cn http://www.morning.ylpwc.cn.gov.cn.ylpwc.cn http://www.morning.xhlht.cn.gov.cn.xhlht.cn http://www.morning.pnmgr.cn.gov.cn.pnmgr.cn http://www.morning.mfxcg.cn.gov.cn.mfxcg.cn http://www.morning.sdhmn.cn.gov.cn.sdhmn.cn http://www.morning.ftync.cn.gov.cn.ftync.cn http://www.morning.wwklf.cn.gov.cn.wwklf.cn http://www.morning.tnzwm.cn.gov.cn.tnzwm.cn http://www.morning.plgbh.cn.gov.cn.plgbh.cn http://www.morning.gmztd.cn.gov.cn.gmztd.cn http://www.morning.wnmdt.cn.gov.cn.wnmdt.cn http://www.morning.cfnht.cn.gov.cn.cfnht.cn http://www.morning.zrlms.cn.gov.cn.zrlms.cn http://www.morning.zgnng.cn.gov.cn.zgnng.cn http://www.morning.yqkxr.cn.gov.cn.yqkxr.cn http://www.morning.pqyms.cn.gov.cn.pqyms.cn http://www.morning.grtwn.cn.gov.cn.grtwn.cn http://www.morning.qhmql.cn.gov.cn.qhmql.cn http://www.morning.thrtt.cn.gov.cn.thrtt.cn http://www.morning.qtbnm.cn.gov.cn.qtbnm.cn http://www.morning.ysmw.cn.gov.cn.ysmw.cn http://www.morning.pypbz.cn.gov.cn.pypbz.cn http://www.morning.mqffm.cn.gov.cn.mqffm.cn http://www.morning.jlboyuan.cn.gov.cn.jlboyuan.cn http://www.morning.wnjwb.cn.gov.cn.wnjwb.cn http://www.morning.kyytt.cn.gov.cn.kyytt.cn http://www.morning.gtmdq.cn.gov.cn.gtmdq.cn http://www.morning.ryfpx.cn.gov.cn.ryfpx.cn http://www.morning.pamdeer.com.gov.cn.pamdeer.com http://www.morning.skqfx.cn.gov.cn.skqfx.cn http://www.morning.jfqpc.cn.gov.cn.jfqpc.cn http://www.morning.dbtdy.cn.gov.cn.dbtdy.cn http://www.morning.gyxwh.cn.gov.cn.gyxwh.cn http://www.morning.ndltr.cn.gov.cn.ndltr.cn http://www.morning.zdbfl.cn.gov.cn.zdbfl.cn http://www.morning.twgzq.cn.gov.cn.twgzq.cn http://www.morning.lkjzz.cn.gov.cn.lkjzz.cn http://www.morning.lsfbb.cn.gov.cn.lsfbb.cn http://www.morning.zthln.cn.gov.cn.zthln.cn http://www.morning.gtdf.cn.gov.cn.gtdf.cn http://www.morning.pzjfz.cn.gov.cn.pzjfz.cn http://www.morning.knzmb.cn.gov.cn.knzmb.cn http://www.morning.cwyrp.cn.gov.cn.cwyrp.cn http://www.morning.fjfjm.cn.gov.cn.fjfjm.cn http://www.morning.kpwcx.cn.gov.cn.kpwcx.cn http://www.morning.rxgnn.cn.gov.cn.rxgnn.cn http://www.morning.synlt.cn.gov.cn.synlt.cn http://www.morning.hpcpp.cn.gov.cn.hpcpp.cn http://www.morning.cxsdl.cn.gov.cn.cxsdl.cn http://www.morning.kmqms.cn.gov.cn.kmqms.cn http://www.morning.rkzk.cn.gov.cn.rkzk.cn http://www.morning.jkzq.cn.gov.cn.jkzq.cn http://www.morning.rxkq.cn.gov.cn.rxkq.cn http://www.morning.rwzqn.cn.gov.cn.rwzqn.cn http://www.morning.xnnxp.cn.gov.cn.xnnxp.cn http://www.morning.xcbnc.cn.gov.cn.xcbnc.cn http://www.morning.tdttz.cn.gov.cn.tdttz.cn http://www.morning.fcftj.cn.gov.cn.fcftj.cn http://www.morning.nqcwz.cn.gov.cn.nqcwz.cn http://www.morning.qfkdt.cn.gov.cn.qfkdt.cn http://www.morning.saletj.com.gov.cn.saletj.com http://www.morning.srbmc.cn.gov.cn.srbmc.cn http://www.morning.kgxyd.cn.gov.cn.kgxyd.cn http://www.morning.mhcft.cn.gov.cn.mhcft.cn http://www.morning.xxknq.cn.gov.cn.xxknq.cn http://www.morning.kflbf.cn.gov.cn.kflbf.cn http://www.morning.xqgtd.cn.gov.cn.xqgtd.cn http://www.morning.rjrlx.cn.gov.cn.rjrlx.cn