首页 > 代码库 > HDU 4952 Number Transformation 规律题

HDU 4952 Number Transformation 规律题

打表可以知道到后面增量都一样了,,

推论就是  i 和 i+1 互质

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;
typedef long long ll;
const ll mx = 120000;

int main() {
    int cas = 0;
    ll x, k, y, dis, i;
    while (~scanf("%I64d%I64d", &x, &k)) {
        if (0 == x && 0 == k)
            break;
            
        for (i = 2; i <= k && i <= mx; ++i) {
            if (x % i == 0)
                continue;
            x = x / i * i + i;
        }    
        if (i <= k) {
            dis = x / i * i + i - x;
            x += dis * (k - i + 1);
        }
        printf("Case #%d: %I64d\n", ++cas, x);
    }
    return 0;
}
/*
10000000 10000000
35670000000

150000 150001
65550437
*/