首页 > 代码库 > SRM620

SRM620

A

处理ab,处理cd。然后查找。比赛的时候用的DFS,爆栈了==

+ View Code?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
vector<pair<int,int> > V[2];
 
void deal(int x,int y,vector<pair<int,int> > &V)
{
    while(x>0&&y>0)
    {
        V.push_back(make_pair(x,y));
        if(x==y) break;
        if(x>y) x-=y;
        else y-=x;
    }
}
 
 
#define FOR0(i,n) for(i=0;i<n;i++)
 
#define SZ(x) x.size()
 
set<pair<int,int> > S;
 
int cmp(pair<int,int> a,pair<int,int> b)
{
    return a.first+a.second>b.first+b.second;
}
 
int get(int a,int b,int c,int d)
{
    V[0].clear();
    V[1].clear();
    deal(a,b,V[0]);
    deal(c,d,V[1]);
    int i;
    FOR0(i,SZ(V[0])) S.insert(V[0][i]);
    sort(V[1].begin(),V[1].end(),cmp);
    FOR0(i,SZ(V[1]))
    {
        pair<int,int> p=V[1][i];
        if(S.find(p)!=S.end()) return p.first+p.second;
    }
    return -1;
}
 
class PairGame
{
    public:
 
 
    int maxSum(int a, int b, int c, int d)
    {
        return get(a,b,c,d);
    }
};

 

B

 

枚举每一个可以作为分隔符的。

+ View Code?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
class CandidatesSelection
{
    public:
 
    string possible(vector <string> s, vector <int> v)
    {
        int i,j;
        int n=s.size();
        int m=s[0].size();
        vector<int> cl(n,0);
        vector<int> used(m,0);
 
        while(1)
        {
            int any=0;
            for(i=0;i<m;i++) if(!used[i])
            {
                int ok=1;
                for(j=1;j<n;j++)
                {
                    ok&=(cl[j]!=cl[j-1]||s[v[j]][i]>=s[v[j-1]][i]);
                    if(!ok) break;
                }
                if(ok)
                {
                    used[i]=any=1;
                    vector<int> ncl(n);
                    for(j=1;j<n;j++)
                    {
                        if(cl[j]!=cl[j-1]||s[v[j]][i]!=s[v[j-1]][i])
                        {
                            ncl[j]=ncl[j-1]+1;
                        }
                        else ncl[j]=ncl[j-1];
                    }
                    cl=ncl;
                }
 
            }
            if(!any) break;
 
        }
        for(i=1;i<n;i++)
        {
            if(cl[i]==cl[i-1]&&v[i]<v[i-1]) return "Impossible";
        }
        return "Possible";
    }
};

 C

将每个数看做一个变量,建立矩阵,高斯消元。

+ View Code?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
int n;
map<int,vector<int> > mp;
 
 
int Gauss(vector<bitset<410> > A,int m)
{
    int n=SZ(A);
    int i,j,k;
 
    vector<int> visit(n,0);
    FOR0(j,m)
    {
        FOR0(i,n) if(A[i][j]&&!visit[i])
        {
            visit[i]=1;
            FOR0(k,n) if(k!=i&&A[k][j]) A[k]^=A[i];
            break;
        }
    }
    int ans=m-n;
    FOR0(i,n) if(!visit[i])
    {
        if(A[i][m]) return -1;
        ans++;
    }
    return ans;
}
 
int cal(vector<int> x)
{
    n=1;
    while(n*n<SZ(x)) n++;
    int i,j;
    FOR0(i,n*n)
    {
        int t=x[i];
        int j;
        for(j=2;j*j<=t;j++) if(t%j==0)
        {
            int cnt=0;
            while(t%j==0) cnt^=1,t/=j;
            if(cnt) mp[j].pb(i);
        }
        if(t>1) mp[t].pb(i);
    }
    vector<bitset<410> > A;
    map<int,vector<int> >::iterator it;
    for(it=mp.begin();it!=mp.end();it++)
    {
        bitset<410> tmp;
        vector<int> p=it->second;
        FOR0(i,SZ(p)) tmp[p[i]]=1;
        A.pb(tmp);
    }
    FOR0(i,n)
    {
        bitset<410> tmp;
        FOR0(j,n) tmp[i*n+j]=1;
        tmp[n*n]=1;
        A.pb(tmp);
    }
    FOR0(i,n)
    {
        bitset<410> tmp;
        FOR0(j,n) tmp[j*n+i]=1;
        tmp[n*n]=1;
        A.pb(tmp);
    }
    int ans=Gauss(A,n*n);
    if(ans==-1) return 0;
    int res=1;
    FOR0(i,ans) res=res*2%mod;
    return res;
}
 
class PerfectSquare
{
    public:
 
    int ways(vector<int> x)
    {
        return cal(x);
    }
};