3 UVA 10852 Less Prime

先筛素数,

再枚举题目范围内质数,答案就是求最小的ans=n-n/i*i ,其中i是枚举的质数

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

int main()
{
    init();
    int T,i,n,ans,x;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        ans=0,x=n;
        for(i=2;i<=n;i++)
        {
            if(pri[i])
            {
                if(ans<n-n/i*i) ans=n-n/i*i,x=i;
            }
        }
        printf("%d\n",x);
    }
    return 0;
}
时间: 2024-10-09 21:07:03

3 UVA 10852 Less Prime的相关文章

uva 1415 - Gauss Prime(高斯素数)

题目链接:uva 1415 - Gauss Prime 题目大意:给出一个a,b,表示高斯数a+bi(i=?2 ̄ ̄ ̄√,推断该数是否为高斯素数. 解题思路: a = 0 时.肯定不是高斯素数 a != 0时,推断a2+2b2是否为素数就可以. #include <cstdio> #include <cstring> #include <cmath> bool is_prime (int n) { int m = sqrt(n+0.5); for (int i = 2;

uva 11610 Reverse Prime

Problem FReverse Prime Input: Standard Input Output: Standard Output There are a few 7 digit positive numbers whose reverse number is a prime number and less than 10^6.  For example: 1000070, 1000090 and 1000240 are first few reverse prime numbers be

uva 10539 - Almost Prime Numbers(数论)

题目链接:uva 10539 - Almost Prime Numbers 题目大意:给出范围low~high,问说在这个范围内有多少个数满足n=pb,(p为素数). 解题思路:首先处理出1e6以内的素数,然后对于每个范围,用solve(high)?solve(low?1),solve(n)用来处理小于n的满足要求的数的个数.枚举素数,判断即可. #include <cstdio> #include <cstring> typedef long long ll; const int

UVA 1415 - Gauss Prime(数论,高斯素数拓展)

UVA 1415 - Gauss Prime 题目链接 题意:给定a + bi,推断是否是高斯素数,i = sqrt(-2). 思路:普通的高斯素数i = sqrt(-1),推断方法为: 1.假设a或b为0.推断还有一个数为4 * n + 3形式的素数(用到费马平方和定理) 2.假设a.b都不为0,推断a ^ 2 + b ^ 2 是否为素数 那么这题,提取出sqrt(2)来,就和基本情况一样了. 对于2,变成: 假设a.b都不为0,推断a ^ 2 + 2 b ^ 2是否为素数 对于1.事实上仅仅

uva 10780 Again Prime? No Time. 质因子乱搞

求最大的k   使得 m^k 能被n!整除 m^k就是让m的每个质因子个数增加了k倍,只要求得n!的质因子能让m增加多少倍就够了.当然这里要取增加倍数最少的. 木桶装水的量取决于最短的木板. 预处理2-n每个数的质因子情况,由于n有10000,前10000个素数有1000+个,所以维护前缀和不划算. case只有500 所以干脆每次都算一遍. #include<stdio.h> #include<string.h> #include<iostream> #include

uva 10780 Again Prime? No Time.

POINT : 质因子分解:(仅给题解) http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1721 题意:给你两个整数m和n,求最大的k使得m^k是n!的约数 思路:首先我们先将m分解质因数,那么m^k就是各个质因数*k而已,那么我们只要判断每个质因数的指数在n!的对应的质因数的指数范围内,求所有质因数最小的一

UVA - 10780 Again Prime? No Time. (质因子分解)

Description Again Prime? No time. Input: standard input Output: standard output Time Limit: 1 second The problem statement is very easy. Given a number n you have to determine the largest power of m, not necessarily prime, that divides n!. Input The

UVA - 10539 Almost Prime Numbers (几乎是素数)

题意:输入两个正整数L.U(L<=U<1012),统计区间[L,U]的整数中有多少个数满足:它本身不是素数,但只有一个素因子. 分析: 1.满足条件的数是素数的倍数. 2.枚举所有的素数,以及其倍数,将满足条件且小于等于n的个数计算出来,solve(u) - solve(l - 1)即可. #pragma comment(linker, "/STACK:102400000, 102400000") #include<cstdio> #include<cst

UVA 10539 Almost Prime Numbers( 素数因子)

Problem AAlmost Prime NumbersTime Limit: 1 second Almost prime numbers are the non-prime numbers which are divisible by only a single prime number. In this problem your job is to write a program which finds out the number of almost prime numbers with