题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3987
题意:
给你两个数n和m,你要将n拆成m个数,使得这m个数或和尽可能小
思路:
只要有一个数某一位是1,那么最后答案那一位就是1
所以一定要所有数中最高位的1尽可能在低位
也就是找到一个k(k=2^x)满足k*m<=n && (k+1)*m>n
这个时候ans += k,然后尽可能让m个数的最高位都为k
这样问题的规模就可以变小,递归直到0为止
//#pragma comment(linker, "/STACK:102400000,102400000")
#include<stdio.h>
#include<string.h>
#include<string>
#include<iostream>
using namespace std;
#define LL long long
int ten[4] = {1,10,100,1000};
typedef struct BigNumber
{
int d[1200];
BigNumber(string s)
{
int i, j, k, len;
len = s.size();
d[0] = (len-1)/4+1;
for(i=1;i<=1199;i++)
d[i] = 0;
for(i=len-1;i>=0;i--)
{
j = (len-i-1)/4+1;
k = (len-i-1)%4;
d[j] += ten[k]*(s[i]-'0');
}
while(d[0]>1 && d[d[0]]==0)
--d[0];
}
BigNumber()
{
*this = BigNumber(string("0"));
}
string toString()
{
int i, j, temp;
string s("");
for(i=3;i>=1;i--)
{
if(d[d[0]]>=ten[i])
break;
}
temp = d[d[0]];
for(j=i;j>=0;j--)
{
s = s+(char)(temp/ten[j]+'0');
temp %= ten[j];
}
for(i=d[0]-1;i>0;i--)
{
temp = d[i];
for(j=3;j>=0;j--)
{
s = s+(char)(temp/ten[j]+'0');
temp %= ten[j];
}
}
return s;
}
}BigNumber;
BigNumber zero("0"), d, temp, mid[15];
bool operator < (const BigNumber &a, const BigNumber &b)
{
int i;
if(a.d[0]!=b.d[0])
return a.d[0]<b.d[0];
for(i=a.d[0];i>0;i--)
{
if(a.d[i]!=b.d[i])
return a.d[i]<b.d[i];
}
return 0;
}
BigNumber operator + (const BigNumber &a, const BigNumber &b)
{
int i, x;
BigNumber c;
c.d[0] = max(a.d[0], b.d[0]);
x = 0;
for(i=1;i<=c.d[0];i++)
{
x = a.d[i]+b.d[i]+x;
c.d[i] = x%10000;
x /= 10000;
}
while(x!=0)
{
c.d[++c.d[0]] = x%10000;
x /= 10000;
}
return c;
}
BigNumber operator - (const BigNumber &a, const BigNumber &b)
{
int i, x;
BigNumber c;
c.d[0] = a.d[0];
x = 0;
for(i=1;i<=c.d[0];i++)
{
x = 10000+a.d[i]-b.d[i]+x;
c.d[i] = x%10000;
x = x/10000-1;
}
while((c.d[0]>1) && (c.d[c.d[0]]==0))
--c.d[0];
return c;
}
BigNumber operator * (const BigNumber &a, const BigNumber &b)
{
int i, j, x;
BigNumber c;
c.d[0] = a.d[0]+b.d[0];
for(i=1;i<=a.d[0];i++)
{
x = 0;
for(j=1;j<=b.d[0];j++)
{
x = a.d[i]*b.d[j]+x+c.d[i+j-1];
c.d[i+j-1] = x%10000;
x /= 10000;
}
c.d[i+b.d[0]] = x;
}
while((c.d[0]>1) && (c.d[c.d[0]]==0))
--c.d[0];
return c;
}
bool smaller(const BigNumber &a, const BigNumber &b, int delta)
{
int i;
if(a.d[0]+delta!=b.d[0])
return a.d[0]+delta<b.d[0];
for(i=a.d[0];i>0;i--)
{
if(a.d[i]!=b.d[i+delta])
return a.d[i]<b.d[i+delta];
}
return 1;
}
void Minus(BigNumber &a, const BigNumber &b, int delta)
{
int i, x;
x = 0;
for(i=1;i<=a.d[0]-delta;i++)
{
x = 10000+a.d[i+delta]-b.d[i]+x;
a.d[i+delta] = x%10000;
x = x/10000-1;
}
while((a.d[0]>1) && (a.d[a.d[0]]==0))
--a.d[0];
}
BigNumber operator * (const BigNumber &a, int k)
{
BigNumber c;
c.d[0] = a.d[0];
int i, x;
x = 0;
for(i=1;i<=a.d[0];i++)
{
x = a.d[i]*k+x;
c.d[i] = x%10000;
x /= 10000;
}
while(x>0)
{
c.d[++c.d[0]] = x%10000;
x /= 10000;
}
while((c.d[0]>1) && (c.d[c.d[0]]==0))
--c.d[0];
return c;
}
BigNumber operator / (const BigNumber &a, const BigNumber &b)
{
int i, j, temp;
BigNumber c;
d = a;
mid[0] = b;
for(i=1;i<=13;i++)
mid[i] = mid[i-1]*2;
for(i=a.d[0]-b.d[0];i>=0;i--)
{
temp = 8192;
for(j=13;j>=0;j--)
{
if(smaller(mid[j], d, i))
{
Minus(d, mid[j], i);
c.d[i+1] += temp;
}
temp /= 2;
}
}
c.d[0] = max(1, a.d[0]-b.d[0]+1);
while((c.d[0]>1) && (c.d[c.d[0]]==0))
--c.d[0];
return c;
}
int main(void)
{
int T;
string str1, str2;
scanf("%d", &T);
while(T--)
{
cin>>str1>>str2;
BigNumber n(str1), m(str2), er("1"), ans("0"), sp("2");
while(er*m<n+m)
er = er*2;
while(zero<n)
{
while((er*m<(n+m))==0)
er = er/sp;
ans = ans+er;
if(n<er*m)
{
n = n/er;
n = d;
}
else
n = n-er*m;
}
cout<<ans.toString()<<endl;
}
return 0;
}