QUESTION:

把(2, 3, 5, 7)称为primes,能被primes整除的我们称之为Walprimes,比如

-21, -30, 0, 5, 14 是, 而-121, 1, 143 etc不是Walprimes。现在给一个由数字组成的字符串,请在数字之间用+,或-,或什么符号都不用,来组成一个表达式。判断有多少个表达式的值是Walprimes。

SOLUTION:
首先,使用Inclusion–exclusion principle。F(s,{2, 3, 5, 7}) 表示从字符串s有Walprimes的数量。那么F(i,{2, 3, 5, 7}) = F(s, 2) + F(s, 3) + F(s, 5) + F(s, 7)
       - F(s, 2 * 3) - F(s, 2 * 5) - F(s, 2 * 7) - F(s, 3 * 5) - F(s, 3 * 7) - F(s, 5 * 7)
       + F(s, 3 * 5 * 7) + F(s, 2 * 5 * 7) + F(s, 2 * 3 * 7) + F(s, 2 * 3 * 5)
       - F(s, 2 * 3 * 5 * 7)
其次我们使用动态规划求F。R[i][j]表示字符串在i位置之后,j是被primes除余数是j。
int void F(string s, int prime){
int R[s.length()][prime];
memset(R, , sizeof(R));
for(int i= s.length()-;i>=; i--){
int first = s[i] - '';
for(int k = i+; k < s.length(); j++){ //对R的第一维进行遍历
for(int j = ; j < primes; j ++){ //对R的第二维进行遍历
R[i][j] += R[k][(prime+(j-first)%prime)%prime]; //i+...
R[i][j] += R[k][(prime+(i-first)%prime)%prime]; //i-...
}
first = *first +s[k]; //i后面既没+也没-
}
R[i][first % p] += ; //最后一个first
}
return return R[][];
} int walprimes(const string& s) {
return F(s, ) + F(s, ) + F(s, ) + F(s, )
- F(s, * ) - F(s, * ) - F(s, * ) - F(s, * ) - F(s, * ) - F(s, * )
+ F(s, * * ) + F(s, * * ) + F(s, * * ) + F(s, * * )
- F(s, * * * );
}

Inclusion–exclusion principle(动态规划)的更多相关文章

  1. 2016 Multi-University Training Contest 9 solutions BY 金策工业综合大学

    A Poor King Tag: Reversed BFS Preprocessing is needed to calculate answers for all positions (states ...

  2. Robots on a grid(DP+bfs())

    链接:http://www.bnuoj.com/bnuoj/problem_show.php?pid=25585 Current Server Time: 2013-08-27 20:42:26 Ro ...

  3. I am Nexus Master!(虽然只是个模拟题。。。但仍想了很久!)

    I am Nexus Master!  The 13th Zhejiang University Programming Contest 参见:http://www.bnuoj.com/bnuoj/p ...

  4. UESTC 1852 Traveling Cellsperson

    找规律水题... Traveling Cellsperson Time Limit: 1000ms Memory Limit: 65535KB This problem will be judged ...

  5. UESTC 1851 Kings on a Chessboard

    状压DP... Kings on a Chessboard Time Limit: 10000ms Memory Limit: 65535KB This problem will be judged ...

  6. SPOJ 375. Query on a tree (树链剖分)

    Query on a tree Time Limit: 5000ms Memory Limit: 262144KB   This problem will be judged on SPOJ. Ori ...

  7. Cellphone Typing 字典树

    Cellphone Typing Time Limit: 5000ms Memory Limit: 131072KB   This problem will be judged on UVA. Ori ...

  8. The Hundred Greatest Theorems

    The Hundred Greatest Theorems The millenium seemed to spur a lot of people to compile "Top 100& ...

  9. 第12届北师大校赛热身赛第二场 A.不和谐的长难句1

    题目链接:http://www.bnuoj.com/bnuoj/problem_show.php? pid=17121 2014-04-25 22:59:49 不和谐的长难句1 Time Limit: ...

随机推荐

  1. Beta阶段第2周/共2周 Scrum立会报告+燃尽图 09

    作业要求[https://edu.cnblogs.com/campus/nenu/2018fall/homework/2389] 版本控制:https://git.coding.net/liuyy08 ...

  2. 折叠纸片PFold.js

    PFold.js是一款折叠纸片插件,支持定义折叠纸牌数量.折叠动画效果.折叠方向,而且还支持折叠结束后回调方法. 在线实例 效果一 效果二 效果三 使用方法 <div id="uc-c ...

  3. react拖拽(表格拖拽排序、普通拖拽排序以及树形拖拽排序)

    表格拖拽排序:组件地址:https://reactabular.js.org/#/drag-and-drop 拖动的排序是用React-DnD:React-DnD:http://react-dnd.g ...

  4. Leetcode 1022. Sum of Root To Leaf Binary Numbers

    dfs class Solution: def sumRootToLeaf(self, root: TreeNode) -> int: stack=[(root,0)] ans=[] bi_st ...

  5. Mysql 时间日期函数运用与总结

    Mysql 中的时间与日期常常会用到,但是每次都得找,这里结合工作日常总结一下. |--获取当前时间[正常时间] 1. MySQL 获得当前时间函数:current_timestamp, curren ...

  6. Luogu2435 染色【状压qwq】【轮廓线DP】

    LINK 题目大意 有一个 n 行 m 列的格点图,你需要给每个点上染上 k 种颜色中的一种,要求没有两个相邻点颜色相同.给定第一行与最后一行的染色,试求总染色方案数. 思路 暴力预处理状态暴力转移可 ...

  7. 7-17 Hashing(25 分)

    The task of this problem is simple: insert a sequence of distinct positive integers into a hash tabl ...

  8. 《DSP using MATLAB》Problem 2.19

    代码: %% ------------------------------------------------------------------------ %% Output Info about ...

  9. Python 字典(Dictionary) update()方法

    refer to: http://www.runoob.com/python/att-dictionary-update.html

  10. StreamSets 设计Edge pipeline

    edge pipeline 运行在edge 执行模式,我们可以使用 data collector UI 进行edge pipeline 设计, 设计完成之后,你可以部署对应的pipeline到edge ...