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立会报告+燃尽图 13

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

  2. DataReader使用

    一.DataReader含义 DataReader相比于DataSet,DataReader是一个抽象类,所以不能用DataReader DR = new DataReader(),来构造函数创建对象 ...

  3. [BZOJ3162]独钓寒江雪

    bzoj description 你要给一个树上的每个点黑白染色,要求白点不相邻.求本质不同的染色方案数. 两种染色方案本质相同当且仅当对树重新标号后对应节点的颜色相同. \(n\le 5\times ...

  4. ppt修改默认字体

      首先,在文本框中输入文字,选中文字设置为自己需要的效果,比如文字字体设置为微软雅黑,大小设置为24,颜色设置为水绿色.   鼠标移动到到输入文本框的边上,此时鼠标形状会变成十字形,单击右键,在弹出 ...

  5. C#对Jason序列化匿名对象

    引用: using System.Web.Script.Serialization; 代码: var resp = new { flag = false, url = ConfigReader.Log ...

  6. zookeeper命名服务

    zookeeper概念 zooKeeper是一个分布式的,开放源码的分布式应用程序协调服务,底层组成单元是znode,对于zookeeper来说,所有的功能都是基于znode来实现的,因此有万物皆节点 ...

  7. 黑马Python——学习之前

    学习清单 学习态度

  8. springboot利用MockMvc测试controller控制器

    主要记录一下控制器的测试,service这些类测试相对简单些(可测试性强) API测试需求比较简单: ① 需要返回正确的http状态码 200 ② 需要返回json数据,并且不能返回未经捕获的系统异常 ...

  9. CentOS 7 安装Memcached服务

    Memcached 简介 Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态.数据库驱动网站 ...

  10. OpenSSL生成root CA及签发证书

    一.openssl 简介 openssl 是目前最流行的 SSL 密码库工具,其提供了一个通用.健壮.功能完备的工具套件,用以支持SSL/TLS 协议的实现.官网:https://www.openss ...