1 UVA 10789 Prime Frequency

先筛素数,再统计给定字符的出现频率。

#include<cstdio>
#include<cstring>
using namespace std;
bool pri[2010];
char s[2010];
void init()
{
    int i,j;
    memset(pri,1,sizeof pri);
    pri[0]=pri[1]=0;
    for(i=2;i<=2000;i++)
    {
        if(!pri[i]) continue;
        for(j=i+i;j<=2000;j+=i)
            pri[j]=0;
    }
}

int main()
{
    int T,i,cnt[100],f,len,cas=1;
    init();
    scanf("%d",&T);
    while(T--)
    {
        scanf("%s",s);
        len=strlen(s);
        printf("Case %d: ",cas++);
        memset(cnt,0,sizeof cnt);
        for(i=0;i<len;i++)
        {
            if(s[i]>='0'&&s[i]<='9') cnt[s[i]-'0']++;
            else if(s[i]>='A'&&s[i]<='Z') cnt[s[i]-'A'+10]++;
            else if(s[i]>='a'&&s[i]<='z') cnt[s[i]-'a'+36]++;
        }
        f=0;
        for(i=0;i<10;i++)
        {
            if(pri[cnt[i]]) f=1,printf("%c",i+'0');
        }
        for(i=10;i<36;i++)
        {
            if(pri[cnt[i]]) f=1,printf("%c",i-10+'A');
        }
        for(i=36;i<62;i++)
        {
            if(pri[cnt[i]]) f=1,printf("%c",i-36+'a');
        }

        if(!f) puts("empty");
        else puts("");
    }
    return 0;
}
时间: 2024-11-02 01:02:24

1 UVA 10789 Prime Frequency的相关文章

[2016-02-19][UVA][524][Prime Ring Problem]

UVA - 524 Prime Ring Problem Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu Submit Status Description A ring is composed of n (even number) circles as shown in diagram. Put natural numbers  into each circle separately, and the

UVA 10140 - Prime Distance(数论)

10140 - Prime Distance 题目链接 题意:求[l,r]区间内最近和最远的素数对. 思路:素数打表,打到sqrt(Max)即可,然后利用大的表去筛素数,由于[l, r]最多100W,所以可以去遍历一遍,找出答案.注意1的情况,一开始没判断1,结果WA了 代码: #include <stdio.h> #include <string.h> #include <algorithm> using namespace std; #define INF 0x3f

UVa 524 - Prime Ring Problem

题目:把1-n,连续的放到一个环里,使相邻的数字和为素数,输出所有结果. 分析:搜索+剪枝.如果裸搜,用dancing-links那种拆装的链表,应该差不多满足16的数据量. 这里利用一个性质进行剪枝:相邻的数字一定是奇偶性不同的数字. (如果上述假设不成立,则存在相邻的奇数或偶数,那么他们的和一定是大于2的偶数,不是素数) 根据上面的假设,还有一条推论:只有n为偶数时才有解. (n为奇数,一直至少有一对奇数相邻,同上,矛盾(鸽巢原理)) 因此,每次搜索的数据其实是n/2,时间复杂度为O((n/

UVA - 524 Prime Ring Problem(dfs回溯法)

UVA - 524 Prime Ring Problem Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Description A ring is composed of n (even number) circles as shown in diagram. Put natural numbers  into each circle separately, and the sum of number

uva 1404 - Prime k-tuple(数论)

题目链接:uva 1404 1404 - Prime k-tuple 题目大意:如果k个相邻的素数p1,p2,-,pk,满足pk?p1=s,称这些素数组成一个距离为s的素数k元组,给定区间a,b,求有多少个距离s的k元组. 解题思路:筛选素数法,先预处理出[1, sqrt(inf)]的素数表,然后对给定区间[a,b]根据预处理出的素数表筛选出素数即可. #include <cstdio> #include <cstring> #include <cmath> #incl

UVA 1404 - Prime k-tuple(素树筛选)

UVA 1404 - Prime k-tuple 题目链接 题意:找出a-b之间有多少个素数k元组,并且最后一个元素减第一个元素为s 思路:先筛出sqrt的素数,然后对于每个区间,在用这些素数去筛出区间的素数,然后twopointer搞一下即可 代码: #include <cstdio> #include <cstring> #include <algorithm> #include <cstdlib> #include <vector> usi

UVA10789 Prime Frequency【筛选法】

Given a string containing only alpha-numerals (0-9, A-Z and a-z) you have to count the frequency (the number of times the character is present) of all the characters and report only those characters whose frequency is a prime number. A prime number i

UVA 10789 题解

Prime Frequency Given a string containing only alpha-numerals (0-9,A-Z and a-z) you have to count the frequency (thenumber of times the character is present) of all thecharacters and report only those characters whose fre-quency is a prime number. A

UVA 10200 Prime Time 水

题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1141 题意:判断区间[a,b]的f(i)是否为素数,f(i)=i*i+i+40: 思路:打个表,然后注意精度: #include<bits/stdc++.h> using namespace std; #define ll long long #define esp