码迷,mamicode.com
首页 > 其他好文 > 详细

UVALIVE 2955 Vivian's Problem

时间:2014-10-03 12:28:44      阅读:188      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   io   os   ar   for   sp   

参考: http://blog.csdn.net/acm_cxlove/article/details/7860735

感觉这里需要记录一下

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <stack>
#include <queue>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <climits>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long
#define PI 3.1415926535897932626
using namespace std;
int gcd(int a, int b) {return a % b == 0 ? b : gcd(b, a % b);}
int mason[] = {3,7,31,127,8191,131071,524287,2147483647};
int cnt[] = {2,3,5,7,13,17,19,31};
bool dp[1<<8];
int K,src[110];
int trans(int x)
{
    int ans = 0;
    for (int i = 0; i < 8; i++)
    {
        if (x % mason[i] == 0)
        {
            x /= mason[i];
            if (x % mason[i] == 0) return 0;
            ans |= (1<<i);
        }
    }
    if (x != 1) return 0;
    return ans;
}
int calcu(int sta)
{
    int ans = 0;
    for (int i = 0; i < 8; i++)
        if (sta & (1<<i))
            ans += cnt[i];
    return ans;
}
int main()
{
    while (scanf("%d",&K)!=EOF)
    {
        //if (K == 0) break;
        int cnt = 0;
        for (int i = 0; i < K; i++)
        {
            scanf("%d",&src[i]);
            src[i] = trans(src[i]);
            if (src[i] == 0) cnt++;
        }
        if (cnt == K) { puts("NO"); continue; }
        memset(dp,false,sizeof(dp));
        dp[0] = true;
        for (int i = 0; i < K; i++)
            for (int j = 0; j < (1<<8); j++)
            {
                if (!(src[i] & j))
                    if (dp[j]) dp[j|src[i]] = true;
            }
        int ans = 0;
        for (int i = 0; i < (1<<8); i++)
            if (dp[i])
            ans = max(ans,calcu(i));
        printf("%d\n",ans);
    }
    return 0;
}

 

UVALIVE 2955 Vivian's Problem

标签:style   blog   http   color   io   os   ar   for   sp   

原文地址:http://www.cnblogs.com/Commence/p/4004764.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!