生活随笔
收集整理的這篇文章主要介紹了
nowcoder 牛牛的最大兴趣组 质因子 + 思维
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
傳送門(mén)
文章目錄
題意:
思路:
首先nnn很小的話可以暴力連邊,讓后染個(gè)色求一個(gè)顏色最多的即可。但是這個(gè)題顯然不行,由于是三次方,所以考慮質(zhì)因子入手。
首先很容易就能想到將所有的數(shù)的質(zhì)因子的冪次模上333,之后他對(duì)應(yīng)的乘起來(lái)為三次方數(shù)的數(shù)是唯一的。因?yàn)閷?duì)于同一個(gè)質(zhì)數(shù)來(lái)說(shuō),aaa的冪次為0,1,20,1,20,1,2對(duì)應(yīng)的bbb的冪次為0,2,10,2,10,2,1,所以現(xiàn)在問(wèn)題就變成了如何快速將所有數(shù)的冪次模333,并且求出來(lái)他對(duì)應(yīng)的數(shù),然后答案就是二者取一個(gè)maxmaxmax即可,因?yàn)槎咭欢ㄊ且粋€(gè)取一個(gè)不取,即染上不同的色。下面考慮如何將所有數(shù)冪次模333。
我們可以篩出來(lái)200020002000以內(nèi)的質(zhì)數(shù)的三次冪,讓后每次遍歷跑一遍即可。這樣篩出來(lái)的數(shù)xxx的質(zhì)因子冪次都<3<3<3,但是我們要求他對(duì)應(yīng)的數(shù)怎么辦呢?顯然不能直接分解質(zhì)因子,這樣的復(fù)雜度還是(2e9)\sqrt{(2e9)}(2e9)?的,這里有一個(gè)巧妙的做法,就是對(duì)x?xx*xx?x再進(jìn)行一次上面的分解,得出來(lái)的數(shù)即為xxx對(duì)應(yīng)的數(shù),因?yàn)?span id="ozvdkddzhkzd" class="katex--inline">x?xx*xx?x可以將原來(lái)111變成222,222變成4mod3=14\bmod 3=14mod3=1,正符合上面的規(guī)律。
復(fù)雜度約為O(n?200)O(n*200)O(n?200)。
#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<map>
#include<cmath>
#include<cctype>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include<sstream>
#include<ctime>
#include<cstdlib>
#define X first
#define Y second
#define L (u<<1)
#define R (u<<1|1)
#define pb push_back
#define mk make_pair
#define Mid (tr[u].l+tr[u].r>>1)
#define Len(u) (tr[u].r-tr[u].l+1)
#define random(a,b) ((a)+rand()%((b)-(a)+1))
#define db puts("---")
using namespace std
;
typedef long long LL
;
typedef unsigned long long ULL
;
typedef pair
<int,int> PII
;const int N
=1000010,mod
=1e9+7,INF
=0x3f3f3f3f;
const double eps
=1e-6;int n
;
int a
[N
];
LL prime
[N
],cnt
;
map
<LL
,int>mp
,has
;
bool st
[N
];void get_prime(int n
) {for(int i
=2;i
<=n
;i
++) {if(!st
[i
]) {prime
[++cnt
]=1ll*i
*i
*i
;for(int j
=i
+i
;j
<=n
;j
+=i
)st
[j
]=true;}}
}LL
divide(LL x
) {for(int i
=1;i
<=cnt
;i
++) {if(x
%prime
[i
]==0) {while(x
%prime
[i
]==0) x
/=prime
[i
];}if(prime
[i
]>x
) return x
;}return x
;
}int main()
{
get_prime(1300);cout
<<cnt
<<endl
;scanf("%d",&n
);for(int i
=1;i
<=n
;i
++) {scanf("%d",&a
[i
]);a
[i
]=divide(a
[i
]);mp
[a
[i
]]++;has
[a
[i
]]=divide(1ll*a
[i
]*a
[i
]);}int ans
=0;for(auto &x
:mp
) {if(x
.X
==1) {ans
++;continue;}LL fa
=has
[x
.X
];ans
+=max(mp
[fa
],x
.Y
);x
.Y
=0; mp
[fa
]=0;}cout
<<ans
<<endl
;return 0;
}
總結(jié)
以上是生活随笔為你收集整理的nowcoder 牛牛的最大兴趣组 质因子 + 思维的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。