B
There are n models in the shop numbered from 1 to n, with sizes s1,s2,…,sn.
Orac will buy some of the models and will arrange them in the order of increasing numbers (i.e. indices, but not sizes).
Orac thinks that the obtained arrangement is beatiful, if for any two adjacent models with indices ij and ij+1 (note that ij<ij+1, because Orac arranged them properly), ij+1 is divisible by ij and sij<sij+1.
For example, for 6 models with sizes {3,6,7,7,7,7}, he can buy models with indices 1, 2, and 6, and the obtained arrangement will be beautiful. Also, note that the arrangement with exactly one model is also considered beautiful.
Orac wants to know the maximum number of models that he can buy, and he may ask you these queries many times.
Input
The first line contains one integer t (1≤t≤100): the number of queries.
Each query contains two lines. The first line contains one integer n (1≤n≤100000): the number of models in the shop, and the second line contains n integers s1,…,sn (1≤si≤109): the sizes of models.
It is guaranteed that the total sum of n is at most 100000.
Output
Print t lines, the i-th of them should contain the maximum number of models that Orac can buy for the i-th query.
Example
inputCopy
4
4
5 3 4 6
7
1 4 2 3 6 4 9
5
5 4 3 2 1
1
9
outputCopy
2
3
1
1
Note
In the first query, for example, Orac can buy models with indices 2 and 4, the arrangement will be beautiful because 4 is divisible by 2 and 6 is more than 3. By enumerating, we can easily find that there are no beautiful arrangements with more than two models.
In the second query, Orac can buy models with indices 1, 3, and 6. By enumerating, we can easily find that there are no beautiful arrangements with more than three models.
In the third query, there are no beautiful arrangements with more than one model.
题意:类似于求最长上升序列的长度,但是加了一个限定条件就是 假设最长序列有个 a[i],a[j] 那么需要满足 j%i==0&&a[j]>a[i] 就加了个整除的条件
思路:我就是按最长上升序列写的但是超时了 唉可惜
代码里面加/**/的就是我原始的代码,我那个for写的有点死板,没变通
因为整除啊,所以j=j+i j初值为 2*i;
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<vector>
using namespace std;
#define inf 0x3f3f3f3f3f;
typedef long long ll;
int a[100001], maxx[100001];
int main()
{
int t; cin >> t;
while (t--)
{
int n; cin >> n;
for (int i = 1; i <= n; i++)
{
cin>>a[i]; maxx[i] = 1;
}
/* for (int i = 2; i <= n; i++)
{
for (int j = 1; j < i; j++)
{
if (a[i] > a[j] && i % j == 0)
maxx[i] = max(maxx[i], maxx[j] + 1);
}
}*/
for (int i=1; i <= n; i++)
for (int j = i * 2; j <= n; j=j+i)
{
if (a[j] > a[i])
maxx[j] = max(maxx[j],maxx[i] + 1);
}
int maxn = 0;
for (int i = 1; i <= n; i++)
{
maxn = max(maxx[i], maxn);
}
printf("%d\n", maxn);
}
}
C
原文链接:https 😕/blog.csdn.net/sdau_lgx/article/details/106095337
C. Orac and LCM
time limit per test3 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
For the multiset of positive integers s={s1,s2,…,sk}, define the Greatest Common Divisor (GCD) and Least Common Multiple (LCM) of s as follow:
gcd(s) is the maximum positive integer x, such that all integers in s are divisible on x.
lcm(s) is the minimum positive integer x, that divisible on all integers from s.
For example, gcd({8,12})=4,gcd({12,18,6})=6 and lcm({4,6})=12. Note that for any positive integer x, gcd({x})=lcm({x})=x.
Orac has a sequence a with length n. He come up with the multiset t={lcm({ai,aj}) | i<j}, and asked you to find the value of gcd(t) for him. In other words, you need to calculate the GCD of LCMs of all pairs of elements in the given sequence.
Input
The first line contains one integer n (2≤n≤100000).
The second line contains n integers, a1,a2,…,an (1≤ai≤200000).
Output
Print one integer: gcd({lcm({ai,aj}) | i<j}).
Examples
inputCopy
2
1 1
outputCopy
1
inputCopy
4
10 24 40 80
outputCopy
40
inputCopy
10
540 648 810 648 720 540 594 864 972 648
outputCopy
54
Note
For the first example, t={lcm({1,1})}={1}, so gcd(t)=1.
For the second example, t={120,40,80,120,240,80}, and it’s not hard to see that gcd(t)=40.
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<vector>
using namespace std;
#define inf 0x3f3f3f3f3f;
typedef long long ll;
const int N = 1e5 + 10;
int q[N], n;
ll p[N];
ll gcd(ll a, ll b)
{
return b == 0 ? a : gcd(b, a % b);
}
ll lcm(ll a, ll b)
{
return a / gcd(a, b) * b;
}
int main()
{
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%d", &q[i]);
p[n] = q[n];
for (int i = n - 1; i >= 1; i--) p[i] = gcd(p[i + 1], q[i]);
ll res = lcm(q[1], p[2]);
for (int i = 2; i < n; i++) res = gcd(res, lcm(q[i], p[i + 1]));
printf("%lld\n", res);
return 0;
}
最小公倍数 = 两数的乘积 / 最大公约数