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. win7下解压安装mysql的方法

    在win7下通过解压安装mysql 5.7一直出现启动不成功,网上找了好久终于找到一个解决方法,记录一下: 1.解压下载的压缩包 2.在解压目录下,将my-default.ini改名为my.ini, ...

  2. Sqlserver 按照时间段统计数据

    WITH t1 ( [hour], title ) , ' 0:00:00--- 1:00:00' UNION ALL , ' 1:00:00--- 2:00:00' UNION ALL , ' 2: ...

  3. Hadoop/Spark入门学习笔记(完结)

    Hadoop基础及演练 ---第1章 初识大数据 大数据是一个概念也是一门技术,是在以Hadoop为代表的大数据平台框架上进行各种数据分析的技术. ---第2章 Hadoop核心HDFS Hadoop ...

  4. vue图片上传组件

    前言:很多项目中都需要用到图片上传功能,而其多处使用的要求,为了避免重复造轮子,让我决定花费一些时间去深入了解,最终封装了一个vue的图片上传组件.现将总结再次,希望有帮助. Layout <d ...

  5. BZOJ2938: [Poi2000]病毒(AC自动机)

    2938: [Poi2000]病毒 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 1678  Solved: 849[Submit][Status][D ...

  6. 用Navicat复制数据库到本地(导入.sql文件运行)

    今天装数据库的机子没开,项目运行不了,于是还是决定在自己电脑上装数据库,由于新学数据库操作,记录一下 一.转储sql文件 右键点击数据库,转储sql文件,点击结构和数据 存放在本地,开始转储 转储完成 ...

  7. asp.net(C#)链接Oracle连接字符串

    在NET环境中链接Oracle数据库有两种组建链接方式: 1)使用OleDB组件是通过Oracle OleDB驱动程序(OraOLEDB.dll)连接和访问Oracle数据库2)使用System.Da ...

  8. spring下的多线程

    链接 1,http://haidaoqi3630.iteye.com/blog/1920944 2,http://www.importnew.com/27440.html .............. ...

  9. Django models中关于blank与null

    建立一个简易Model class Person(models.Model): GENDER_CHOICES=( (1,'Male'), (2,'Female'), ) name=models.Cha ...

  10. GCC 三

    前记: 经常浏览博客园的同学应该会觉得本文有标题党之嫌,这个标题的句式来自于MiloYip大牛的大作<C++强大背后>,在此,向Milo兄致意. GCC,全称GNU Compiler Co ...