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)
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.
Output
For each case:
Print one integer.It denotes the sum of all F(i) for all 1≤i<n
Sample Input
Sample Output
Hint
as 0.
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)
Print one integer.It denotes the sum of all F(i) for all 1≤i<n
4 1 3 2 4
6
F(1)=2 F(2)=0 F(3)=4 F(4)=0
aF(i) mod ai = 0是关键,思路:从这个数开始向后找它的公倍数,如果是它的公倍数,总数加上公倍数的下表,就是第几个数,如果没有这个数的公倍数,就加0;
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int a[10010];
main()
{
int n;
while(~scanf("%d",&n))
{
int sum=0;
memset(a,0,sizeof(a));
for(int i=1; i<=n; i++)
scanf("%d",&a[i]);
for(int i=1;i<=n-1;i++)
{
for(int j=i+1;j<=n;j++)
{
if(a[j]%a[i]==0)
{
sum+=j;
break;
}
}
}
printf("%d\n",sum);
}
}