Mutiple
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 441 Accepted Submission(s): 271
Problem Description
WLD likes playing with a sequence
a[1..N]
. One day he is playing with a sequence of
N
integers. For every index i, WLD wants to find the smallest index
F(i)
( if exists ), that
i<F(i)≤n
, and
aF(i)
mod
ai
= 0. If there is no such an index
F(i)
, we set
F(i)
as 0.
Input
There are Multiple Cases.(At MOST
10
)
For each case:
The first line contains one integers N(1≤N≤10000) .
The second line contains N integers a1,a2,...,aN(1≤ai≤10000) ,denoting the sequence WLD plays with. You can assume that all ai is distinct.
For each case:
The first line contains one integers N(1≤N≤10000) .
The second line contains N integers a1,a2,...,aN(1≤ai≤10000) ,denoting the sequence WLD plays with. You can assume that all ai is distinct.
Output
For each case:
Print one integer.It denotes the sum of all F(i) for all 1≤i<n
Print one integer.It denotes the sum of all F(i) for all 1≤i<n
Sample Input
4 1 3 2 4
Sample Output
6HintF(1)=2 F(2)=0 F(3)=4 F(4)=0
Source
Recommend
很loser的做法,但是过了......Java会一直说
Memory Limit Exceeded
决定以后写算法,尽量用Java!但不纠结语法,毕竟算法是一样的不是吗?c++大法好
老规矩中文题在后面!
1002 Mutiple 从右向左查看序列 维护一个数组 p[1..10000] 表示该数上一次出现的位置 遇到一个数就暴力查看它的所有倍数,取最小值即可 时间复杂度为 O(n/1+n/2+…+n/n)=O(nlogn)
官方提示一点都没用上......
#include <cstdio>
#include <algorithm>
#include <cstring>
int main()
{
int n,a[10005];
int sum = 0,i,j;
while(scanf("%d",&n)!=EOF)
{
memset(a,0,sizeof(a));
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
sum=0;
for(i=1;i<n;i++)
{
for(j=i+1;j<=n;j++)
{
if(a[j]%a[i] == 0)
{
sum+=j;
break;
}
}
}
printf("%d\n",sum);
}
return 0;
}
Mutiple
Accepts: 476
Submissions: 1025
Time Limit: 4000/2000 MS (Java/Others)
Memory Limit: 65536/65536 K (Java/Others)
问题描述
wld有一个序列 a[1..n] , 对于每个 1≤i<n , 他希望你求出一个最小的 j (以后用记号 F(i) 表示),满足 i<j≤n , 使 aj 为 ai 的倍数(即 aj mod ai =0),若不存在这样的 j ,那么此时令 F(i) = 0 保证 1≤n≤10000,1≤ai≤10000 对于任意 1≤i≤n , 且对于任意 1≤i,j≤n(i!=j) ,满足 ai != aj
输入描述
多组数据(最多 10 组) 对于每组数据: 第一行:一个数 n 表示数的个数 接下来一行: n 个数,依次为 a1,a2,…,an
输出描述
对于每组数据: 输出 F(i) 之和(对于 1≤i<n )
输入样例
4 1 3 2 4
输出样例
6
Hint
F(1)=2 F(2)=0 F(3)=4 F(4)=0