HDU-5211 (aF(i) mod ai = 0)
2017-01-20 20:29
369 查看
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.
InputThere 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.OutputFor each case:
Print one integer.It denotes the sum of all
F(i)
for all
1≤i<n
Sample Input
Sample Output
Hint
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);
}
}
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.
InputThere 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.OutputFor each case:
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
6
Hint
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);
}
}
相关文章推荐
- hdu 3433 A Task Process N个人,第i个人完成一个A任务需要时间ai,完成一个B任务需要时间bi, 现在又X个任务A和Y个任务B,求完成所有任务所需要的最短时间。
- HDU 5728 PowMod(数论,欧拉函数的各种性质)
- hdu 5211 Mutiple(枚举)
- HDU 4389——X mod f(x)(数位DP)
- X mod f(x) [HDU 4389] 数位DP模板
- hdu 3930 X^N=a(mod) p 求X
- hdu1420 Prepared for New Acmer (a^i mod n)
- HDU 5728 PowMod
- hdu-1104-Remainder(BFS打印路径+数论)(%与mod的区别)
- HDU 4389 X mod f(x)
- HDU 5211 Mutiple
- HDU 1017 A Mathematical Curiosity(枚举+mod)
- hdu 3930 X^N=a(mod) p 求X
- HDU 5728 PowMod
- HDU 5344(MZL's xor-(ai+aj)的异或和)
- hdu 5211 Mutiple
- hdu 1005 用mod的循环性质
- hdu 2276 矩阵 有点小发现,矩阵mod的使用太多易造成TLE
- hdu 3930 X^N=a(mod) p 求X
- HDU 5728 2016多校Contest 1 F题【数论,注意要mod】