Stay Hungry,Stay Foolish!

D - Not Divisible -- ATCODER

D - Not Divisible

https://atcoder.jp/contests/abc170/tasks/abc170_d

 

思路

输入过程记录每个数字出现的次数,

遍历所有数字ai,

对于每一个数字,在1 .. sqrt(ai) 之间找此数的因子,

如果因子f出现过,则此数不是 not divisible

否则还需要再判断,此因子的对偶因子ai/f是否出现过, 如果出现过,则此数不是not divisible

 

Code

https://atcoder.jp/contests/abc170/submissions/35942758

#include<bits/stdc++.h>
using namespace std;
int a[200001];
map<int,int> appr;

int main(){
    int n;
    cin>>n;
    
    for(int i=0;i<n;i++){
        cin>>a[i];
        appr[a[i]]++;
    }
    
    int ans=0;
    
    for(int i=0;i<n;i++){
        bool ok = false;
        int ai = a[i];
        
        if (ai == 1 && appr[ai] == 1){
            ans++;
            continue;
        }
        
        for(int j=1; j*j<=ai; j++){
            if (ai % j != 0){
                continue;
            }
            
            if(appr[j]>0){
                ok=true;
                break;
            }

            int mirror = ai/j;

            if(appr[mirror]>0 && j!=1){
                ok=true;
                break;
            }
            
            if(j==1){
                if(appr[mirror]>1){
                    ok=true;
                    break;
                }
            }
        }
        
        if(!ok){
//            cout<<a[i]<<endl;
            ans++;
        }
    }
    
    cout<<ans<<endl;
}

 

posted @ 2022-10-25 09:25  lightsong  阅读(20)  评论(0编辑  收藏  举报
Life Is Short, We Need Ship To Travel