dp - 递推
3 seconds
256 megabytes
standard input
standard output
You are given an integer array a1,a2,…,an
.
The array b
is called to be a subsequence of a if it is possible to remove some elements from a to get b
.
Array b1,b2,…,bk
is called to be good if it is not empty and for every i (1≤i≤k) bi is divisible by i
.
Find the number of good subsequences in a
modulo 109+7
.
Two subsequences are considered different if index sets of numbers included in them are different. That is, the values of the elements do not matter in the comparison of subsequences. In particular, the array a
has exactly 2n−1
different subsequences (excluding an empty subsequence).
The first line contains an integer n
(1≤n≤100000) — the length of the array a
.
The next line contains integers a1,a2,…,an
(1≤ai≤106
).
Print exactly one integer — the number of good subsequences taken modulo 109+7
.
2
1 2
3
5
2 2 1 22 14
13
In the first example, all three non-empty possible subsequences are good: {1}
, {1,2}, {2}
In the second example, the possible good subsequences are: {2}
, {2,2}, {2,22}, {2,14}, {2}, {2,22}, {2,14}, {1}, {1,22}, {1,14}, {22}, {22,14}, {14}
.
Note, that some subsequences are listed more than once, since they occur in the original array multiple times.
题意 : 给你 n 个数字,对于任意位置的数你都可以选择或者不选择,构成一个新的序列,当构成新的序列满足第一个数是1的倍数,第二个数是2的倍数..以此类推询问你方案数有多少
思路分析 :
比赛的时候写了一个 dp,顺势推过去的,用 01 数组去优化的一个,dp[i][j] 表示到达第 i 个位置,且当前构成的序列可以整除 j 的方案数,但由于 n 很大,想的是用 01 滚动数组优化个,
结果凉掉了,就是有些状态是不会被转移过去,从而造成了丢失
其实一维 dp 就够, dp[i] 表示当前数作为整除 i 的数的个数,倒着推就可以了
代码示例 :
#define ll long long
const ll maxn = 1e6+5;
const ll maxm = 1e5+5;
const ll mod = 1e9+7;
const double eps = 1e-9;
const double pi = acos(-1.0);
const ll inf = 0x3f3f3f3f; ll n;
ll a[maxm];
vector<ll>ve[maxn]; void init(){
for(ll i = 1; i <= 1e6; i++){
for(ll j = i; j <= 1e6; j += i){
ve[j].push_back(i);
}
}
}
ll dp[maxn];
void solve(){
//ll pt = 1;
//dp[0][0] = 1, dp[1][0] = 1;
dp[0] = 1;
ll ans = 0;
for(ll i = 1; i <= n; i++){
for(ll j = ve[a[i]].size()-1; j >= 0; j--){
ll x = ve[a[i]][j];
dp[x] = dp[x]+dp[x-1];
dp[x] %= mod;
}
}
for(ll i = 1; i <= n; i++) {
ans += dp[i];
ans %= mod;
// printf("++++ i = %lld %lld\n", i, dp[pt][i]);
}
cout << ans << endl;
} int main() {
//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
//init();
cin >> n ;
for(ll i = 1; i <= n; i++) scanf("%lld", &a[i]);
init();
solve();
return 0;
}
dp - 递推的更多相关文章
- hdu2089(数位DP 递推形式)
不要62 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- hdu 2604 Queuing(dp递推)
昨晚搞的第二道矩阵快速幂,一开始我还想直接套个矩阵上去(原谅哥模板题做多了),后来看清楚题意后觉得有点像之前做的数位dp的水题,于是就用数位dp的方法去分析,推了好一会总算推出它的递推关系式了(还是菜 ...
- Power oj2498/DP/递推
power oj 2498 /递推 2498: 新年礼物 Time Limit: 1000 MS Memory Limit: 65536 KBTotal Submit: 12 Accepted: 3 ...
- BZOJ4321queue2——DP/递推
题目描述 n 个沙茶,被编号 1~n.排完队之后,每个沙茶希望,自己的相邻的两 人只要无一个人的编号和自己的编号相差为 1(+1 或-1)就行: 现在想知道,存在多少方案满足沙茶们如此不苛刻的条件. ...
- Shell Necklace (dp递推改cdq分治 + fft)
首先读出题意,然后发现这是一道DP,我们可以获得递推式为 然后就知道,不行啊,时间复杂度为O(n2),然后又可以根据递推式看出这里面可以拆解成多项式乘法,但是即使用了fft,我们还需要做n次多项式乘法 ...
- hdu 1723 DP/递推
题意:有一队人(人数 ≥ 1),开头一个人要将消息传到末尾一个人那里,规定每次最多可以向后传n个人,问共有多少种传达方式. 这道题我刚拿到手没有想过 DP ,我觉得这样传消息其实很像 Fibonacc ...
- UVA 10559 Blocks(区间DP&&递推)
题目大意:给你玩一个一维版的消灭星星,得分是当前消去的区间的长度的平方,求最大得分. 现在分析一下题目 因为得分是长度的平方,不能直接累加,所以在计算得分时需要考虑前一个状态所消去的长度,仅用dp[l ...
- [NOI2009]管道取珠 DP + 递推
---题面--- 思路: 主要难点在思路的转化, 不能看见要求$\sum{a[i]^2}$就想着求a[i], 我们可以对其进行某种意义上的拆分,即a[i]实际上可以代表什么? 假设我们现在有两种取出某 ...
- HDU 2154 跳舞毯 | DP | 递推 | 规律
Description 由于长期缺乏运动,小黑发现自己的身材臃肿了许多,于是他想健身,更准确地说是减肥. 小黑买来一块圆形的毯子,把它们分成三等分,分别标上A,B,C,称之为“跳舞毯”,他的运动方式是 ...
- HDU 5366 dp 递推
The mook jong Accepts: 506 Submissions: 1281 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65 ...
随机推荐
- 【hdu 1112】The Proper Key
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s) ...
- 【js】vue 2.5.1 源码学习 (十一) 模板编译compileToFunctions渲染函数
大体思路(九) 本节内容: 1. compileToFunctions定位 1. compileToFunctions定位 ==> createCompiler = createCompiler ...
- 21个项目玩转深度学习:基于TensorFlow的实践详解03—打造自己的图像识别模型
书籍源码:https://github.com/hzy46/Deep-Learning-21-Examples CNN的发展已经很多了,ImageNet引发的一系列方法,LeNet,GoogLeNet ...
- P1006 输出第二个整数
题目描述 输入三个整数,整数之间由一个空格分隔,整数是32位有符号整数.把第二个输入的整数输出. 输入格式 输入三个整数,整数之间由一个空格分隔,整数是32位有符号整数. 输出格式 输出输入的三个整数 ...
- 关于methods、computed、watch的使用
关于methods.computed.watch的使用,前前后后我有转载过好几篇别人的文章.但始终没有自己成型的博文来记录,现自己尝试性的总结一下三者之间的区别: computed:计算属性 comp ...
- Linux 内核VLB 总线
另一个对 ISA 的扩展是 VESA Local Bus(VLB) 接口总线, 它扩展了 ISA 连接器, 通过 添加第 3 个知道长度的槽位. 一个设备可只插入这个额外的连接器(不用插入 2 个关联 ...
- 搭建个人/企业私有云盘-seafile
一.安装依赖组件 安装前的准备工作安装 Seafile 服务器之前,请确认已安装以下软件MariaDB 或者 MySQL 服务器 (MariaDB 是 MySQL 的分支),python 2.7 (从 ...
- MySQL数据库性能优化:表、索引、SQL等
一.MySQL 数据库性能优化之SQL优化 注:这篇文章是以 MySQL 为背景,很多内容同时适用于其他关系型数据库,需要有一些索引知识为基础 优化目标 减少 IO 次数IO永远是数据库最容易瓶颈的地 ...
- Java 连接 SQL Server 数据库
//连接数据库 public Connection getConnection(){ //url为绝对路径 String url="jdbc:sqlserver://127.0.0.1:14 ...
- DEVOPS技术实践_17:Jenkins使用扩展邮件功能发送邮件
一 环境准备 1.1 安装插件Email Extension 系统管理-管理插件-安装Email Extension插件 1.2 配置 配置jenkins邮箱的全局配置:系统管理-系统设置-完成邮箱配 ...