zoukankan      html  css  js  c++  java
  • XidianOJ 1028 数字工程

    题目描述

    ACM实验室开启了一个数字工程项目,希望把正整数n通过一些特殊方法变成1。
    可采用的方法有:(1)减去1;(2)除以它的任意一个素因子。 每操作一次消耗一个单位的能量。
    问,把n变成1最少需要消耗多少能量?

    输入

    多组测试
    对于每组测试,输入正整数n (1<=n<=1,000,000)

    输出

    输出最少消耗的能量

    --正文

    使用Euler质数筛来求出1000000内的质数

    #include <iostream>
    #include <algorithm>
    #include <cstdio>
    #include <cstring>
    using namespace std;
    bool isPrime[1000001] = {0};
    int PrimeList[100000];
    int dp[1000001];
    int main() {
        
        int i,j;
        for (i=0;i<=1000000;i++){
            isPrime[i] = true;
        } 
        int PrimeCount = 0;
        for (i=2;i<=1000000;i++){
            if (isPrime[i]){
                PrimeCount ++;
                PrimeList[PrimeCount] = i;
            }
            int j;
            for (j=1;j<=PrimeCount;j++){
                if (i*PrimeList[j] > 1000000) break;
                isPrime[i*PrimeList[j]] = false;
                if (i % PrimeList[j] == 0) break;
            }
        }
        memset(dp,0x3f,sizeof(dp));
        dp[1] = 0;
        for (i=1;i<=1000000;i++){
            dp[i+1] = min(dp[i]+1,dp[i+1]);
            for (j=1;j<=PrimeCount;j++){
                if (i * PrimeList[j] > 1000000) break;
                else {
                    dp[i*PrimeList[j]] = min(dp[i]+1,dp[i*PrimeList[j]]);
                }
            }
        }
        int n;
        while (scanf("%d",&n) != EOF){
            printf("%d
    ",dp[n]);
        }
        return 0;
    }
  • 相关阅读:
    【Java】REST风格
    KMP(烤馍片)算法
    Lca求法 (树链剖分 与 倍增)
    hash学习笔记
    星际网络(数学)
    P3537 [POI2012]SZA-Cloakroom (背包)
    乘车路线 (二维最短路)
    渔民的烦恼 (二分)
    Jmeter 常用函数(18)- 详解 __isDefined
    Jmeter 常用函数(17)- 详解 __substring
  • 原文地址:https://www.cnblogs.com/ToTOrz/p/6077022.html
Copyright © 2011-2022 走看看