• 刷题总结——切蛋糕(ssoj)


    题目:

    切蛋糕 

    (cake.cpp/c/pas)
    【问题描述】
    BG 有一块细长的蛋糕,长度为�。
    有一些人要来BG 家里吃蛋糕, BG把蛋糕切成了若干块(整数长度),然后分给这些人。
    为了公平,每个人得到的蛋糕长度和必须相等,且必须是连续的一段。
    但是,BG并不知道要有多少人来。他只知道,来的人数为�的约数,且小于�。
    显然把蛋糕平均分成�块一定能满足要求。但是,BG想要分出的块数尽量少。现在BG
    想知道,他要把蛋糕分成至少多少块,才能使得不管多少人来都能满足要求。

    【输入】
    输入文件名为cake.in。
    输入共一个整数�,表示蛋糕的长度。

    【输出】
    输出文件名为cake.out。
    输出共一个整数,表示分出的最少块数。

    【输入输出样例1】
    cake.in cake.out
    6

    4


    【输入输出样例说明】
    4 块长度分别为2、1、1、2。

    【输入输出样例2】
    cake.in cake.out
    15

    7

    【输入输出样例说明】
    7 块长度分别为3、2、1、3、1、2、3。

    题解:

     答案就是n-phi(n);�中与�互质的数的个数为�(�),所以答案就是� − �(

    代码:

    #include<iostream>
    #include<algorithm>
    #include<cstdio>
    #include<cstdlib>
    #include<cmath>
    #include<ctime>
    #include<cctype>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    int n,temp,ans;
    int main()
    {
      scanf("%d",&n);
      temp=n;
      ans=n;
      for(int i=2;i*i<=n;i++)
      {
        if(temp%i==0)  ans=ans/i*(i-1);
        while(temp%i==0)  temp/=i;
      }
      if(temp>1)  ans=ans/temp*(temp-1);
      cout<<n-ans<<endl;
      return 0;
    }
  • 相关阅读:
    RSA加密系统
    安装homebrew
    go helloworld
    下载文件checksum
    6月3日
    6月1日
    5月30日
    5月28日
    5月26日
    5月24日
  • 原文地址:https://www.cnblogs.com/AseanA/p/7132286.html
Copyright © 2020-2023  润新知