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\n",t,n);
21         else{n/=(i-1); printf("Case #%d: %lld\n",t,n*k);}
22     }
23     return 0;
24 }

 

20160812

posted @ 2016-08-16 14:39  YuanZiming  阅读(127)  评论(0编辑  收藏  举报