程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> hdu 4952 Number Transformation

hdu 4952 Number Transformation

編輯:C++入門知識

hdu 4952 Number Transformation


Number Transformation

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 617 Accepted Submission(s): 313


Problem Description Teacher Mai has an integer x.

He does the following operations k times. In the i-th operation, x becomes the least integer no less than x, which is the multiple of i.

He wants to know what is the number x now.
Input There are multiple test cases, terminated by a line "0 0".

For each test case, the only one line contains two integers x,k(1<=x<=10^10, 1<=k<=10^10).

Output For each test case, output one line "Case #k: x", where k is the case number counting from 1.
Sample Input
2520 10
2520 20
0 0

Sample Output
Case #1: 2520
Case #2: 2600

Source 2014 Multi-University Training Contest 8
題解及代碼:

這道題目在比賽時是打表看出來的規律,我把i,n/i,n暴力輸出出來,看到當i*i>=n時,n/i不再發生變化,由於數據最大是10^10,所以我們每次最多計算10^5次,不會超時,因此我們每次最多計算到i*i>=n時就可以了。


#include 
#include 
using namespace std;

int main()
{
    long long n,k;
    int cas=1;
    while(scanf("%I64d%I64d",&n,&k)!=EOF)
    {
        if(!n&&!k)
            break;
        for(long long i=1;i<=k;i++)
        {
            if(n%i)
            {
                long long t=n/i;
                t++;
                n=t*i;
            }
            if(i*i>=n)
            {
                n=(n/i)*k;
                break;
            }
        }
        printf("Case #%d: %I64d\n",cas++,n);
    }
    return 0;
}






  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved