素数

B. T-primes
time limit per test  2 seconds
memory limit per test  256 megabytes

We know that prime numbers are positive integers that have exactly two distinct positive divisors. Similarly, we'll call a positive integertТ-prime, ift has exactly three distinct positive divisors.

You are given an array of n positive integers. For each of them determine whether it is Т-prime or not.

Input

The first line contains a single positive integer, n (1 ≤ n ≤ 105), showing how many numbers are in the array. The next line containsn space-separated integers xi (1 ≤ xi ≤ 1012).

Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is advised to use thecin,cout streams or the%I64d specifier.

Output

Print n lines: the i-th line should contain "YES" (without the quotes), if numberxi is Т-prime, and "NO" (without the quotes), if it isn't.

Sample test(s)
Input
3
4 5 6
Output
YES
NO
NO
Note

The given test has three numbers. The first number 4 has exactly three divisors — 1, 2 and 4, thus the answer for this number is "YES". The second number 5 has two divisors (1 and 5), and the third number 6 has four divisors (1, 2, 3, 6), hence the answer for them is "NO".

题意:题目是要确定给出的一个数看是否是Tprime,就是一个数只有三个约数;


意解:我们知道任意一个自然数(1和0除外),都至少有两个约数(1和它本身),而对于要找出一个有只有三个约数的数,很自然的可以知道它只能包含素数的平方约数.

        这样问题就得解了,只要预处理1e6内的素数,然后对每个数开方,验证就行了;


AC代码:


#include <iostream>
#include <cstdio>
#include <cmath>

using namespace std;
typedef long long LL;
const LL M = 1e6 + 5;
LL a[M];
int to = 0;

void unit()
{
    a[0] = a[1] = 1;
    for(int i = 4; i < M; i += 2) a[i] = 1;
    for(int i = 3; i * i < M; i += 2)
    {
        if(a[i]) continue;
        for(int j = i * 2; j < M; j += i)
            a[j] = 1;
    }
}

int main()
{
    LL x,n,tp;
    unit();
    cin>>n;
    while(n--)
    {
        cin>>x;
        tp = x;
        x = (LL)sqrt((double)x);
        if(a[x] || x * x != tp) puts("NO");
        else puts("YES");
    }
    return 0;
}

C. Primes on Interval
time limit per test 1 second
memory limit per test   256 megabytes

You've decided to carry out a survey in the theory of prime numbers. Let us remind you that a prime number is a positive integer that has exactly two distinct positive integer divisors.

Consider positive integers a, a + 1, ..., b (a ≤ b). You want to find the minimum integerl(1 ≤ l ≤ b - a + 1) such that for any integerx(a ≤ x ≤ b - l + 1) amongl integersx,x + 1,..., x + l - 1 there are at leastk prime numbers.

Find and print the required minimum l. If no value l meets the described limitations, print -1.

Input

A single line contains three space-separated integersa, b, k (1 ≤ a, b, k ≤ 106a ≤ b).

Output

In a single line print a single integer — the required minimuml. If there's no solution, print -1.

Sample test(s)
Input
2 4 2
Output
3
Input
6 13 1
Output
4
Input
1 4 3
Output
-1

意解: 素数筛选和二分答案; 我们可以知道这样一个事实,对于一个确定的区间,如果这个区间里的数小于k,则l偏小,如果符合大于等于k则l可以继续缩小,
         这样就符合二分的思路了;

AC代码:

#include <iostream>
#include <cstdio>

using namespace std;
const int M = 1e6 + 5;
int A[M],sum[M],a,b,k;

void unit()
{
    for(int i = 4; i < M; i += 2) A[i] = 1;
    for(int i = 3; i * i < M; i += 2)
    {
        if(!A[i])
        {
            for(int j = i * 2; j < M; j += i)
                A[j] = 1;
        }
    }
    for(int i = 2; i < M; i++)
        sum[i] += sum[i - 1] + !A[i];
}

bool check(int mid)
{
    for(int i = a; i <= b - mid + 1; i++)
        if(sum[i + mid - 1] - sum[i - 1] < k) return 0;
    return 1;
}

int main()
{
    unit();
    while(~scanf("%d %d %d",&a,&b,&k))
    {
        if(sum[b] - sum[a - 1] < k) puts("-1");
        else
        {
            int mid,ua,ub;
            ua = 1; ub = b - a + 1;
            while(ua < ub)
            {
                mid = (ua + ub) >> 1;
                if(check(mid)) ub =mid;
                else ua = mid + 1;
            }
            printf("%d\n",ua);
        }
    }
    return 0;
}

C. Bear and Prime Numbers
time limit per test  2 seconds
memory limit per test  512 megabytes

Recently, the bear started studying data structures and faced the following problem.

You are given a sequence of integers x1, x2, ..., xn of lengthn and m queries, each of them is characterized by two integersli, ri. Let's introducef(p) to represent the number of such indexesk, that xk is divisible byp. The answer to the query li, ri is the sum:, whereS(li, ri) is a set of prime numbers from segment[li, ri] (both borders are included in the segment).

Help the bear cope with the problem.

Input

The first line contains integer n (1 ≤ n ≤ 106). The second line containsn integers x1, x2, ..., xn(2 ≤ xi ≤ 107). The numbers are not necessarily distinct.

The third line contains integer m (1 ≤ m ≤ 50000). Each of the following m lines contains a pair of space-separated integers,li andri(2 ≤ li ≤ ri ≤ 2·109) — the numbers that characterize the current query.

Output

Print m integers — the answers to the queries on the order the queries appear in the input.

Sample test(s)
Input
6
5 5 7 10 14 15
3
2 11
3 12
4 4
Output
9
7
0
Input
7
2 3 5 7 11 4 8
2
8 10
2 123
Output
0
7
Note

Consider the first sample. Overall, the first sample has 3 queries.

  1. The first query l = 2, r = 11 comes. You need to count f(2) + f(3) + f(5) + f(7) + f(11) = 2 + 1 + 4 + 2 + 0 = 9.
  2. The second query comes l = 3, r = 12. You need to count f(3) + f(5) + f(7) + f(11) = 1 + 4 + 2 + 0 = 7.
  3. The third query comes l = 4, r = 4. As this interval has no prime numbers, then the sum equals 0.

意解: 题目有一个处理的技巧,在统计素数的时候可以处理出所有的结果;


AC代码:


#include <iostream>
#include <cstring>
#include <vector>
#include <cstdio>
#include <cmath>


using namespace std;
const int M = 1e7 + 5;
int sum[M],a[M],b[M],to = 0;

void unit()
{
    for(int i = 2; i < M; i++)
    {
        if(!a[i])
        {
            for(int j = i; j < M; j += i)
            {
                a[j] = 1;
                if(b[j]) sum[i] += b[j];
            }
        }
    }
    for(int i = 2; i < M; i++)
        sum[i] += sum[i - 1];
}

int main()
{
    int n,x,m,l,r;
    scanf("%d",&n);
    for(int i = 0; i < n; i++)
    {
        scanf("%d",&x);
        b[x]++;
    }
    unit();
    scanf("%d",&m);
    while(m--)
    {
        scanf("%d %d",&l,&r);
        if(l > M && r > M) puts("0");
        else if(r > M) printf("%d\n",sum[M - 1] - sum[l - 1]);
        else printf("%d\n",sum[r] - sum[l - 1]);
    }
    return 0;
}


C. Jzzhu and Apples
time limit per test   1 second
memory limit per test   256 megabytes

传送门

Jzzhu has picked n apples from his big apple tree. All the apples are numbered from 1 to n. Now he wants to sell them to an apple store.

Jzzhu will pack his apples into groups and then sell them. Each group must contain two apples, and the greatest common divisor of numbers of the apples in each group must be greater than 1. Of course, each apple can be part of at most one group.

Jzzhu wonders how to get the maximum possible number of groups. Can you help him?

Input

A single integer n (1 ≤ n ≤ 105), the number of the apples.

Output

The first line must contain a single integer m, representing the maximum number of groups he can get. Each of the next m lines must contain two integers — the numbers of apples in the current group.

If there are several optimal answers you can print any of them.

Sample test(s)
Input
6
Output
2
6 3
2 4
Input
9
Output
3
9 3
2 4
6 8
Input
2
Output
0


意解:涨新姿势了,对于两两组合成的数不互质,对于偶数,是友好的,而奇数是不友好的,所以可以预先筛选出所有的素数,遍历他们
       的倍数,如果个数为奇数,则删除掉偶数. 其他配对;

AC代码:

#include <iostream>
#include <cstring>
#include <vector>
#include <cstdio>

using namespace std;
const int M = 1e5 + 6;
int a[M],prime[M],to,n,ans;
typedef pair<int,int> p;
vector<p>v;
vector<p> :: iterator it1;

void unit()
{
    for(int i = 2; i <= n; i++)
    {
        if(!a[i])
        {
            prime[to++] = i;
            if(1LL * i * i <= n)
                for(int j = i * i; j <= n; j += i) a[j] = 1;
        }
    }
    memset(a,0,sizeof(a));
}

int main()
{
    scanf("%d",&n);
    unit();
    for(int i = 1; i < to && prime[i] <= n; i++)
    {
        vector<int>tp;
        vector<int> :: iterator it;
        for(int j = prime[i]; j <= n; j += prime[i])
        {
            if(a[j] == 0)
                tp.push_back(j);
        }
        int len = tp.size();
        if(len == 1) continue;
        if(len & 1)
            for(it = tp.begin(); it != tp.end(); it++)
                if(*it % 2 == 0)
                {
                    tp.erase(it);
                    break;
                }
        for(int j = 0; j < tp.size(); j += 2)
        {
            v.push_back(make_pair(tp[j],tp[j + 1]));
            a[tp[j]] = 1;
            a[tp[j + 1]] = 1;
        }
    }
    for(int i = 2,tp = 0,v1; i <= n; i += 2)
    {
        if(a[i]) continue;
        tp++;
        if(tp & 1) v1 = i;
        else v.push_back(make_pair(v1,i));
    }
    cout<<v.size()<<endl;
    for(it1 = v.begin(); it1 != v.end(); it1++)
        printf("%d %d\n",it1 -> first,it1 -> second);
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值