LightOJ 1370 Bi-shoe and Phi-shoe 数论

题目大意:f(x)=n 代表1-x中与x互质的数字的个数。给出n个数字a[i],要求f(x)=a[i],求x的和。

思路:每个素数x 有x-1个不大于x的互质数。则f(x)=a[i],若a[i]+1为素数则x=a[i]+1,否则a[i]++直到得到素数位置。

#include<cstdio>
#include<stdio.h>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
#define INF 0x3f3f3f
#define MAX 1000005
#define MOD 1000000007

using namespace std;

long long p[MAX],v[MAX];

void GetPrime()//打素数表
{
    int i,j,cnt=1;
    memset(v,0,sizeof(v));
    memset(p,0,sizeof(p));
    p[1]=1;
    for(i=2; i<=1000005; i++)
    {
        if(!v[i])
        {
            p[i]=1;
            for(j=i; j<=1000005; j+=i)
            {
                v[j]=1;
            }
        }
    }
}

int main()
{
    GetPrime();
    int T,i,j,cnt=1,k,n;
    long long sum;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        sum=0;
        for(i=1;i<=n;i++)
        {
            scanf("%d",&k);
            j=k+1;
            while(!p[j])
                j++;
            sum+=j;
        }
        printf("Case %d: %lld Xukha\n",cnt++,sum);
    }
    return 0;
}

时间: 2024-08-18 12:05:10

LightOJ 1370 Bi-shoe and Phi-shoe 数论的相关文章

LightOJ - 1370 Bi-shoe and Phi-shoe

题目链接:http://lightoj.com/login_main.php?url=volume_showproblem.php?problem=1370 题目大意:给N个数a[i], N <= 1e6,问使 Φ(x) >= a[i] 成立的最小x的所有x的和是多少. 解题思路:我们知道的是对于素数 m 来说,phi[m] = m - 1.另一方面,对于一个合数 m 来说, phi[m] < phi[x] , x > m && x 是素数. 因此,我们可以认为,每

LightOJ 1370 欧拉函数

1.LightOJ 1370  Bi-shoe and Phi-shoe   欧拉函数 2.总结: #include<iostream> #include<cstring> #include<cmath> #include<queue> #include<algorithm> #include<cstdio> #define max(a,b) a>b?a:b #define F(i,a,b) for (int i=a;i<

LightOJ 1370 Bi-shoe and Phi-shoe(欧拉函数)

http://lightoj.com/login_main.php?url=volume_showproblem.php?problem=1370 题意: 给一些数Ai(第 i 个数),Ai这些数代表的是某个数欧拉函数的值,我们要求出数 Ni 的欧拉函数值不小于Ai.而我们要求的就是这些 Ni 这些数字的和sum,而且我们想要sum最小,求出sum最小多少. 思路:素数P的欧拉函数值为P-1. 所以对于一个给出的数,我们去寻找大于它的第一个素数即可. 1 #include<iostream>

Lightoj 1370 素数打表 +二分

1370 - Bi-shoe and Phi-shoe   PDF (English) Statistics   Time Limit: 2 second(s) Memory Limit: 32 MB Bamboo Pole-vault is a massively popular sport in Xzhiland. And Master Phi-shoe is a very popular coach for his success. He needs some bamboos for hi

LightOJ - 1370

Bi-shoe and Phi-shoe Time Limit: 2000MS   Memory Limit: 32768KB   64bit IO Format: %lld & %llu Submit Status Description Bamboo Pole-vault is a massively popular sport in Xzhiland. And Master Phi-shoe is a very popular coach for his success. He needs

lightoj 1245 Harmonic Number (II)(简单数论)

题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1245 题意:求f(n)=n/1+n/2.....n/n,其中n/i保留整数 显然一眼看不出什么规律.而且n有2e31直接暴力肯定要出事情 但是f=n/x这个函数很好关于y = x 对称对称点刚好是sqrt(n) 于是就简单了直接求sum+n/i (i*i<n && i >=1) 然后乘以2,再减去i*i即可. 这个i*i表示的是什么呢,由于对称上半部份的值完

LightOJ 1370 Bi-shoe and Phi-shoe 欧拉函数+线段树

分析:对于每个数,找到欧拉函数值大于它的,且标号最小的,预处理欧拉函数,然后按值建线段树就可以了 #include <iostream> #include <stdio.h> #include <string.h> #include <algorithm> #include <cmath> #include <map> using namespace std; typedef long long LL; const int N = 1

欧拉函数 || LightOJ 1370 Bi-shoe and Phi-shoe

给出x,求最小的y使y的欧拉函数大于等于x *解法:i).求出1e6之内的数的欧拉函数,遍历找 ii).求比x大的第一个质数--因为每个质数n的欧拉函数都是n-1 wa一次是因为SZ写小了-- #include <iostream> #include <cstdio> using namespace std; #define SZ 1000005 long long phi[SZ], a[SZ]; void getphi(int n) { for(int i = 2; i <

LightOJ - 1370 Bi-shoe and Phi-shoe (欧拉函数打表)

题意:给N个数,求对每个数ai都满足最小的phi[x]>=ai的x之和. 分析:先预处理出每个数的欧拉函数值phi[x].对于每个数ai对应的最小x值,既可以二分逼近求出,也可以预处理打表求. #include<bits/stdc++.h> using namespace std; typedef long long LL; const int maxn = 1100005; int phi[maxn]; int res[maxn]; bool isprime[maxn]; void E