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 ...
随机推荐
- SVN 树冲突解决详解
https://blog.csdn.net/xgf415/article/details/75196714 SVN 冲突包括 内容冲突:当两名(或更多)开发人员修改了同一个文件中相邻或相同的行时就会发 ...
- C# 获取 PC 序列号
在 C++ 需要使用 GetSystemFirmwareTable 的方法来获得 PC 的序列号,需要写的代码很多,但是在 C# 可以使用 WMI 来拿到序列号 首先是安装 System.Manage ...
- jquery自己写的幻灯片插件,好用不解释
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- ubuntu16.04 无法wifi链接一段时间掉线且无法再连接
ubuntu16.04 无法wifi链接一段时间掉线且无法再连接,从网上搜索的确认这个一个bug. 解决方法: 1.Get details of your PCI wireless card by r ...
- Linux 内核usb_bulk_msg 接口
usb_bulk_msg 创建一个 USB 块 urb 并且发送它到特定的设备, 接着在返回到调用者之 前等待完成. 它定义为: int usb_bulk_msg(struct usb_device ...
- dotnet 使用 MessagePack 序列化对象
和很多序列化库一样,可以通过 MessagePack 序列化和反序列化,和 json 相比这个库提供了二进制的序列化,序列化之后的内容长度比 json 小很多 这个库能序列的内容不多,大多数时候建议使 ...
- Java 学习笔记(4)——java 常见类
上次提前说了java中的面向对象,主要是为了使用这些常见类做打算,毕竟Java中一切都是对象,要使用一些系统提供的功能必须得通过类对象调用方法.其实Java相比于C来说强大的另一个原因是Java中提供 ...
- final阶段20191121-5 Scrum立会报告+燃尽图 01
此作业要求参见:http://edu.cnblogs.com/campus/nenu/2019fall/homework/10065 一: 组名:组长组 组长:杨天宇 组员:魏新 罗杨美慧 王歆 ...
- 通用高效的数据修复方法:Row level repair
导读:随着大数据的进一步发展,NoSQL 数据库系统迅速发展并得到了广泛的应用.其中,Apache Cassandra 是最广泛使用的数据库之一.对于 Cassandra 的优化是大家研究的热点,而 ...
- java poi ppt 接口的基本操作
依赖 在 pom.xml中增加以下依赖 <dependency> <groupId>org.apache.poi</groupId> <artifactId& ...