codeforces1471 D. Strange Definition
D. Strange Definition
大佬題解
由lcm(x,y)=xygcd(x,y)lcm(x,y)=\frac{xy}{gcd(x,y)}lcm(x,y)=gcd(x,y)xy?可知,如果lcm(x,y)gcd(x,y)=xygcd2(x,y)\frac{lcm(x,y)}{gcd(x,y)}=\frac{xy}{gcd^2(x,y)}gcd(x,y)lcm(x,y)?=gcd2(x,y)xy?是完全平方數,那么xyxyxy是完全平方數。
xyxyxy是完全平方數意味著將其質因數分解后,質因數的次數都是偶數次。不難知道將x,yx,yx,y分別分解質因數后,各自質因數次冪奇偶性相同,也就是如果兩個數分解質因數后,質數次冪奇偶性相同,則他們滿足題目中的“相鄰”條件。
對于0時刻,只需要統計奇偶性相同數的個數即可。
 注意:如何記錄?借用hash的思想記錄,詳細看代碼
 對于1時刻,如果上述其中一個組內元素個數是奇數,那么將他們相乘后得到的數分解質因數仍然是奇數次冪(1除外即此組不代表元素1)
 如果組內元素個數是偶數,那么下一時刻相乘后得到的數分解質因數全部都是偶數次冪,它與可以其他組內元素個數是偶數的組或者某組代表的數是1合并。
不難知道其他時刻與1時刻狀態相同。
#define IO ios::sync_with_stdio(false);cin.tie();cout.tie(0) #pragma GCC optimize(1) #pragma GCC optimize(2) #pragma GCC optimize(3,"Ofast","inline") #include<cstdio> #include<iostream> #include<algorithm> #include<map> using namespace std; typedef long long ll; typedef unsigned long long ull; const ull P=19260817; map<ull,int> mp; void divide(int x) {ull hash=1;for(int i=2;i<=x/i;i++)if(x%i==0){int cnt=0;while(x%i==0) x/=i,cnt++;if(cnt&1) hash=hash*i;}if(x>1) hash=hash*x;mp[hash]++; } int main() {int T=1;scanf("%d",&T);while(T--){mp.clear();int n;cin>>n;for(int i=1;i<=n;i++){int a;scanf("%d",&a);divide(a);}int res1=0,res2=0;for(auto &t:mp){res1=max(res1,t.second);if(t.second%2==0||t.first==1) res2+=t.second;}res2=max(res2,res1);int q;scanf("%d",&q);while(q--){ll op;scanf("%lld",&op);if(op==0) printf("%d\n",res1);else printf("%d\n",res2);}}return 0; }cf好像會卡掉hash,最開始我按照上述博客的方式記錄,用base=131或13331都被卡錯了,不過博客里面的base=19260817竟然沒被卡,神奇!
總結:如果不知道如何記錄狀態就考慮hash。
菜就多練,要不然訓練總掛機
 要加油哦~
總結
以上是生活随笔為你收集整理的codeforces1471 D. Strange Definition的全部內容,希望文章能夠幫你解決所遇到的問題。
                            
                        - 上一篇: I. Space Station(has
 - 下一篇: 谪仙人