zoukankan      html  css  js  c++  java
  • bzoj3858Number Transformation*

    bzoj3858Number Transformation

    题意:

    给一个数n,对其进行k次变换,第i次变换是将当前的n变成大于等于n的最小的i的倍数。求k次变换后n为多少。n≤10^10,k≤10^10。

    题解:

    对n的变换可以表示成ceil(n/i)*i。有一个结论,当i第一次大于sqrt(当前的n)后,以后的i将永远大于sqrt(那时的n),且从这以后ceil(n/i)都相等。因此可以先暴力变换n,当i大于sqrt(当前n)后,求出ceil(n/i),直接乘k就是最后答案。

    代码:

     1 #include <cstdio>
     2 #include <cstring>
     3 #include <algorithm>
     4 #include <cmath>
     5 #define inc(i,j,k) for(int i=j;i<=k;i++)
     6 #define ll long long
     7 using namespace std;
     8 
     9 inline ll read(){
    10     char ch=getchar(); ll f=1,x=0;
    11     while(ch<'0'||ch>'9'){if(ch=='-')f=-1; ch=getchar();}
    12     while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
    13     return f*x;
    14 }
    15 ll n,k; int t;
    16 int main(){
    17     while(1){
    18         n=read(); k=read(); if(n==0&&k==0)break; t++; int i;
    19         for(i=1;i<=k&&i<=(int)sqrt(n)+1;i++)n=(n+i-1)/i*i;
    20         if(i==k+1)printf("Case #%d: %lld
    ",t,n);
    21         else{n/=(i-1); printf("Case #%d: %lld
    ",t,n*k);}
    22     }
    23     return 0;
    24 }

    20160812

  • 相关阅读:
    第三方登录的原理
    浅谈算法的时间复杂度和空间复杂度
    python3的全局变量和局部变量
    python3的嵌套函数
    HTTP协议学习-03
    HTTP协议学习-02
    HTTP协议学习-01
    织梦模板修改方法大全
    java常用用代码
    java学用代码
  • 原文地址:https://www.cnblogs.com/YuanZiming/p/5776322.html
Copyright © 2011-2022 走看看