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. L181 The microscopic structure of a cat’s tongue helps keep its fur clean

    T.S. eliot's mystery cat, Macavity, besides being a criminal mastermind able to evade the combined r ...

  2. React Native自适应设备宽度解决方案

    px:设备实际像素单位 dp/pt:逻辑像素单位(IOS的尺寸单位为pt,Android的尺寸单位为dp) 在设计和开发过程中,应该尽量使用逻辑像素尺寸来思考界面. UI 给默认 640 的图,采用 ...

  3. pyqt5事件与鼠标事件

    一,每个事件都被封装成相应的类: pyqt中,每个事件类型都被封装成相应的事件类,如鼠标事件为QMouseEvent,键盘事件为QKeyEvent等.而它们的基类是QEvent. 二,基类QEvent ...

  4. CF1082G:G. Petya and Graph(裸的最大闭合权图)

    Petya has a simple graph (that is, a graph without loops or multiple edges) consisting of n n vertic ...

  5. Session History 属性和方法

    History 接口允许操作浏览器的曾经在标签页或者框架里访问的会话历史记录. js通过window.history来访问和操作的,操作的范围是某个tab的会话历史记录. 这个tab打开后,tab内的 ...

  6. python静态方法类方法属性方法

    Python的静态方法和类成员方法都可以被类或实例访问,两者概念不容易理清,但还是有区别的: 1)静态方法无需传入self参数,类成员方法需传入代表本类的cls参数: 2)从第1条,静态方法是无法访问 ...

  7. Java编程之Map中分拣思想。

    题目:给定一个字符串,求出字符串中每一个单词在字符串中出现的次数 旨意:map的分拣思想. 每一个key的包装类,存放出现的次数 /** * 作为包装类,用来存放英文单词,和该英文单词出现的次数 * ...

  8. graphql-yoga 项目简单使用&&集成docker

      graphql-yoga 是一个实现了grahql的框架,使用简单,便捷 具体源码参考github https://github.com/rongfengliang/graphql-yoga-do ...

  9. 【转】详解硬盘MBR

    原文网址:http://hi.baidu.com/waybq/item/a4490f026f9859d21ef046a4 硬盘是现在计算机上最常用的存储器之一.我们都知道,计算机之所以神奇,是因为它具 ...

  10. Jqmobile Secha Ionic比较

    1. Jqmobile 轻量级框架,它的语言基于 jquery 语言容易上手,运行速度快,但是没有 MVC 多人协作 开发的概念,项目比较大后 代码不易维护     (中小项目  1-2 个人开发很适 ...