UVALIVE 2955 Vivian's Problem

参考: 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

时间: 2024-07-30 23:44:43

UVALIVE 2955 Vivian's Problem的相关文章

uva 1323 - Vivian&#39;s Problem(梅森素数)

题目链接:uva 1323 - Vivian's Problem 题目大意:给定N个数,然后为每个数添加一个幂ei,最后N项垒乘的结果为M,要是得M的所有因子的和可以写成2x,求x的最大值,如果没有条件满足,输出NO 解题思路:若一个数可以写成若干个不同的梅森素数的乘积,那么这个数的所以因子和可以写成2x. 232?1的范围内只有8个梅森素数,所以可以用状压处理. 梅森素数即为2^i-1形式的素数 /********************** * 梅森素数,(2^k) - 1 * * 一个数若

UVA - 1323 Vivian&#39;s Problem

Description The desire to explore the unknown has been a driving force in human history since the dawn of time. From the earliestdocumented accounts, ancient civilizations had explored the earth by sailing around. Early adventurers were motivatedby r

UVALive - 3521 Joseph&#39;s Problem (整除分块)

给定$n,k$$(1\leqslant n,k\leqslant 10^9)$,计算$\sum\limits _{i=1}^nk\: mod\:i$ 通过观察易发现$k\%i=k-\left \lfloor \frac{k}{i} \right \rfloor*i$,因此我们考虑把$\left \lfloor \frac{k}{i} \right \rfloor$的值相同的$i$分成一组直接求和,复杂度为$O(\sqrt{n})$. 整除分块原理(选自某dalao博客) 1 #include<b

UVaLive 7457 Discrete Logarithm Problem (暴力)

题意:求一个x使得 a^x%p = b p为素数: 析:从1开始扫一下就好,扫到p-1就可以了,关键是这个题为什么要用文件尾结束,明明说是0,但是不写就WA... 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib> #include <cmath> #incl

HDU 2955 Robberies(DP)

题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=2955 题目: Problem Description The aspiring Roy the Robber has seen a lot of American movies, and knows that the bad guys usually gets caught in the end, often because they become too greedy. He has decide

《算法竞赛入门经典——训练指南》第二章题库

UVa特别题库 UVa网站专门为本书设立的分类题库配合,方便读者提交: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=442 注意,下面注有"extra"的习题并没有在书中出现,但在上面的特别题库中有,属于附加习题. 基础练习 (Basic Problems) UVa11388 GCD LCM UVa11889 Benefit UVa10943 How do y

2015暑假训练赛团体赛(8.3)

    ID Origin Title     12 / 37 Problem A UVALive 4763 Sudoku Extension   23 / 80 Problem B UVALive 4764 Bing it       Problem C UVALive 4765 String of Candied Haws     4 / 19 Problem D UVALive 4766 Gold Mines       Problem E UVALive 4767 Machine Tra

UVALive - 5107 - A hard Aoshu Problem

题目链接:https://vjudge.net/problem/UVALive-5107 题目大意:用ABCDE代表不同的数字,给出形如ABBDE___ABCCC = BDBDE的东西: 空格里面可以填入+-*/的运算符,给字母赋予不同的值,问有多少种情况使得 等式成立. 题目分析: 可以直接用大模拟+暴力求解,注意对于重复情况的判重. 给出代码: 1 #include <iostream> 2 #include <set> 3 #include <algorithm>

UVALive - 7041 The Problem to Slow Down You (回文树)

https://vjudge.net/problem/UVALive-7041 题意 给出两个仅包含小写字符的字符串 A 和 B : 求:对于 A 中的每个回文子串,B 中和该子串相同的子串个数的总和. 分析 从0和1两个根节点DFS下去,如果两个相同的节点同时存在就统计答案. #include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <st