标签:codeforces dp 数论
给出两个数a,b,k有两种操作,a-=1,或者a-=a%x(2<=x<=k),问最少需要多少步操作能够使a变成b
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#define MAX 400007
using namespace std;
typedef long long LL;
LL a,b,dp[MAX];
int k;
LL gcd ( LL a , LL b )
{
return b==0?a:gcd(b,a%b);
}
LL solve ( LL a , LL b )
{
memset ( dp, 0x3f , sizeof ( dp ) );
dp[a] = 0;
for ( int i = a ; i > b ; i-- )
{
dp[i-1] = min( dp[i]+1 , dp[i-1] );
for ( int j = 2 ; j <= k ; j++ )
dp[i-i%j] = min ( dp[i-i%j] , dp[i]+1 );
}
return dp[b];
}
int main ( )
{
while ( ~scanf ( "%lld%lld%d" , &a , &b , &k ) )
{
LL lcm = 2;
for ( int i = 3; i <= k ; i++ )
{
int d = gcd ( i , lcm );
lcm *= i;
lcm /= d;
}
LL ans;
if ( a/lcm == b/lcm )
ans = solve ( a%lcm , b%lcm );
else
ans = solve ( a%lcm , 0 ) + (a/lcm-b/lcm-1)*solve ( lcm , 0 ) + solve ( lcm , b%lcm );
printf ( "%lld\n" , ans );
}
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
codeforces 251C C. Number Transformation(数论+dp)
标签:codeforces dp 数论
原文地址:http://blog.csdn.net/qq_24451605/article/details/48194627