Mutiple
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 88 Accepted Submission(s): 65
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
BestCoder Round #39 ($)
可以先开一个p数组(1-10000)从右到左来记录序列里面各个数的最小的序号。然后就可以对序列里的数进行其倍数的暴力搜索,取最小值即可。
#include<stdio.h>
#include<string.h>
int main()
{
__int64 p[10005],a[10005],i,j,k,f[10005],n,mini,sum;
while(scanf("%I64d",&n)!=EOF)
{
sum=0;
memset(p,0,sizeof(p));
memset(f,0,sizeof(f));
for(i=1;i<=n;i++)
scanf("%I64d",&a[i]);
for(i=n;i>=1;i--)
p[a[i]]=i;
for(i=1;i<n;i++)
{
mini=99999999;
for(j=1;j*a[i]<=10000;j++) //对a[i]的倍数进行搜索
{
if(p[j*a[i]]) //判断该数的倍数是否存在
{
if(mini>p[j*a[i]]&&p[j*a[i]]>i) //判断最小的那个
mini=p[j*a[i]];
// printf("%d\n",mini);
}
}
f[i]=mini;
// printf("%d\n",f[i]);
if(mini==99999999)f[i]=0;
}
for(i=1;i<=n;i++)
sum=sum+f[i];
printf("%I64d\n",sum);
}
}
可以先开一个p数组(1-10000)从右到左来记录序列里面各个数的最小的序号。然后就可以对序列里的数进行其倍数的暴力搜索,取最小值即可。
#include<stdio.h>
#include<string.h>
int main()
{
__int64 p[10005],a[10005],i,j,k,f[10005],n,mini,sum;
while(scanf("%I64d",&n)!=EOF)
{
sum=0;
memset(p,0,sizeof(p));
memset(f,0,sizeof(f));
for(i=1;i<=n;i++)
scanf("%I64d",&a[i]);
for(i=n;i>=1;i--)
p[a[i]]=i;
for(i=1;i<n;i++)
{
mini=99999999;
for(j=1;j*a[i]<=10000;j++) //对a[i]的倍数进行搜索
{
if(p[j*a[i]]) //判断该数的倍数是否存在
{
if(mini>p[j*a[i]]&&p[j*a[i]]>i) //判断最小的那个
mini=p[j*a[i]];
// printf("%d\n",mini);
}
}
f[i]=mini;
// printf("%d\n",f[i]);
if(mini==99999999)f[i]=0;
}
for(i=1;i<=n;i++)
sum=sum+f[i];
printf("%I64d\n",sum);
}
}