一个经典的级数时间复杂度


要点:

for i (1 to n) i ++;for j(i to n) j += i;

这个的时间复杂度是O(nlogn)


题意:

  • 给一个长度为n的子数组a,找出最长的子序列最大公因数不超过m

解发:

  • 观察n,m的范围(1e6),那么最大公因数必然在这之内
  • 定义f[i]表示以 i 作为倍数的数量,那么枚举不重复的数字,状态计算通过上面要点算出来
  • f中最大的就是最小公倍数和对应的数量,是答案之一

#include<bits/stdc++.h>
#define debug1(a) cout<<#a<<'='<< a << endl;
#define debug2(a,b) cout<<#a<<" = "<<a<<"  "<<#b<<" = "<<b<<endl;
#define debug3(a,b,c) cout<<#a<<" = "<<a<<"  "<<#b<<" = "<<b<<"  "<<#c<<" = "<<c<<endl;
#define debug4(a,b,c,d) cout<<#a<<" = "<<a<<"  "<<#b<<" = "<<b<<"  "<<#c<<" = "<<c<<"  "<<#d<<" = "<<d<<endl;
#define debug5(a,b,c,d,e) cout<<#a<<" = "<<a<<"  "<<#b<<" = "<<b<<"  "<<#c<<" = "<<c<<"  "<<#d<<" = "<<d<<"  "<<#e<<" = "<<e<<endl;
#define endl "\n"
#define fi first
#define se second

#define int long long
using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;
typedef pair<LL,LL> PLL;

//#pragma GCC optimize(3,"Ofast","inline")
//#pragma GCC optimize(2)
const int N = 1e6+10;
int a[N];
vector<int> idx[N];
int f[N];


void solve() 
{
    int n,m;cin >> n >> m;
    for(int i = 1;i <= n;i ++)
    {
        cin >> a[i];
        if(a[i] <= m)
        {
            idx[a[i]].push_back(i);
        }
    }
    
    for(int i = 1;i <= m;i ++)
    {
        for(int j = i;j <= m;j += i)
        {
            f[j] += idx[i].size();
        }
    }

    int tar = max_element(f + 1,f + 1 + m) - f;
    cout << tar << " " << f[tar] << endl;
    for(int i = 1;i <= n;i ++)
    {
        if(tar % a[i] == 0)
        {
            cout << i << " ";
        }
    }

}

signed main()
{
    /*
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    */
    int T = 1;//cin >> T;
    while(T--){
        //puts(solve()?"YES":"NO");
        solve();
    }
    return 0;

}
/*

*/

 

posted @ 2023-01-17 20:52  俄罗斯刺沙蓬  阅读(60)  评论(0编辑  收藏  举报
返回顶端