ural 1932 The Secret of Identifier 容斥

來源:互聯網
上載者:User

標籤:blog   http   get   2014   os   name   

題目連結:點擊開啟連結

stl+容斥

#include <cstdio>#include <cstring>#include <algorithm>#include <vector>#include <iostream>#include <set>using namespace std;#define N 65540#define ll __int64ll n;ll a[N][4], mul[4]={1,16,256,4096};ll h[N];vector<ll>G[N];set<ll>myset;set<ll>::iterator p;ll find1(ll x){ //設除了x位,其他3位相同myset.clear();for(ll i = 0; i < n; i++){ll now = 0;for(ll j = 0; j < 4; j++)if(j!=x)now += a[i][j];G[now].push_back(i);myset.insert(now);}ll ans = 0;for(p = myset.begin(); p!=myset.end(); p++) {ll siz = G[*p].size();ans += (siz*(siz-1))>>1;G[*p].clear();}return ans;}ll find2(ll x,ll y){myset.clear();for(ll i = 0; i < n; i++) {ll now = 0;for(ll j = 0; j < 4; j++)if(j!=x&&j!=y)now += a[i][j];G[now].push_back(i);myset.insert(now);}ll ans = 0;for(p = myset.begin(); p!=myset.end(); p++) {ll siz = G[*p].size();ans += (siz*(siz-1))>>1;G[*p].clear();}return ans;}ll find3(ll x){myset.clear();for(ll i = 0; i < n; i++){ll now = 0;now += a[i][x];G[now].push_back(i);myset.insert(now);}ll ans = 0;for(p = myset.begin(); p!=myset.end(); p++) {ll siz = G[*p].size();ans += (siz*(siz-1))>>1;G[*p].clear();}return ans;}int main(){ll i,j;for(ll i = 0; i < N; i++)G[i].clear();while(cin>>n) {for(i=0;i<n;i++){char s[10]; scanf("%s",s);for(j=0;j<4;j++) {a[i][j] = ('0'<=s[j]&&s[j]<='9')?s[j]-'0':s[j]-'a'+10;a[i][j] *= mul[j];}}ll ans[5] = {0};ans[1] = find1(0) + find1(1) + find1(2) + find1(3);ans[2] = find2(0,1) + find2(0,2) + find2(0,3) + find2(1,2) + find2(1,3) + find2(2,3) - ans[1]*3;ans[3] = find3(0) + find3(1) + find3(2) + find3(3) - ans[1]*3 - ans[2]*2;ll all = (ll) ((n*(n-1))/2);ans[4] = all - ans[1] - ans[2] - ans[3];cout<<ans[1]<<" "<<ans[2]<<" "<<ans[3]<<" "<<ans[4]<<endl;}return 0;}/*4abcdabcaabceabcf100000111122223333444455556666777788889999*/


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.