牛客网 Wannafly挑战赛 A 找一找 思考题

链接:https://www.nowcoder.com/acm/contest/71/A
来源:牛客网

题目描述

给定n个正整数,请找出其中有多少个数x满足:在这n个数中存在数y=kx,其中k为大于1的整数

输入描述:

第一行输入一个n
接下来一行输入n个正整数a i

输出描述:

输出符合条件个数
示例1

输入

5
1 2 3 4 5

输出

2

说明

5个数中1和2符合条件,1是后面每个数的因子,2是4的因子

备注:

1≤n,a
i
≤1000000


emmmmm,直接上代码把,用下标记数组,第二层循环用倍增,因为是倍增,所以最坏的时间复杂度是n/2+n/3+n/4...+n/n,在10^8左右所以可以通过题目了

#include<queue>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define maxn 1000010
#define debug(a) cout << #a << " " << a << endl
using namespace std;
typedef long long ll;
int a[maxn];
int main() {
    int n;
    while( cin >> n ) {
        memset(a,0,sizeof(a));
        for( int i=0; i<n; i++ ) {
            int x;
            cin >> x;
            a[x] ++;    //打标记
        }
        int ans = 0;
        for( int i=1;i<=1000000;i++) {
            if( a[i] ) {
                for( int j=i+i; j<=1000000; j+=i ) { //倍增找倍数指
                    if( a[j] ) {
                        ans += a[i];
                        break;
                    }
                }
            }
        }
        cout << ans << endl;
    }
    return 0;
}

 




转载于:https://www.cnblogs.com/l609929321/p/8408916.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值