D - Index Trio -- 灵茶 2024-08-05

16 篇文章 0 订阅
4 篇文章 0 订阅

链接 : 

D - Index Trio (atcoder.jp)

思路 : 

枚举值域

统计每一个值出现得次数到cnt数组中

枚举 i , j , ans += cnt[i]*cnt[j]*cnt[i*j] ;

时间复杂度为O(u/1+u/2+u/3+...+u/u) = O(UlogU) , 调和级数 ;

代码 : 

#include<bits/stdc++.h>
#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;

#define endl '\n'
typedef long long LL;
#define pb push_back
#define eb emplace_back
#define PII pair<int,int>
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define int long long

const int N = 2e5 + 10 ;
const int Mod = 1e9 + 7 ;
// int xx[] = { 1,0,-1,0 };
// int yy[] = { 0,1,0,-1 };

// https://atcoder.jp/contests/abc249/tasks/abc249_d

LL qmi(LL m, LL k){LL res = 1 % Mod, t = m;while (k){if (k&1) res = res * t % Mod;t = t * t % Mod;k >>= 1;}return res;}

// 枚举值域
// 统计每一个值出现得次数到cnt数组中
// 枚举 i , j , ans += cnt[i]*cnt[j]*cnt[i*j] ;
int cnt[N] ;

inline void solve(){
    int n ; cin >> n ;
    int ma = 0 , ans = 0 ;
    for(int i=1;i<=n;i++){
        int x ; cin >> x ;
        ma = max(ma,x) ;
        cnt[x] ++ ;
    }
    for(int i=1;i<=N-1;i++){
        for(int j=1;i*j<=N-1;j++){
            ans += cnt[i]*cnt[j]*cnt[i*j] ;
        }
    }
    cout << ans << endl ;
}
 
signed main(){
    IOS
    int _ = 1;
    while(_ --) solve();
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值