这次写了两道水题,然后被数论给难住了,就到这吧
求和:
解题思路很简单,a+aa+aaa+aa..aa 可以写成 (...((a*10^L+a)*10^L+a)*10L...+a)
注意数据类型用long long ,每次乘法操作后要取余就没什么问题了
代码如下:
#include
using namespace std;
#define r 1000000007
#define LL long long
LL a, n;
LL getD(LL l)
{
LL ans = 1;
while(l>0)
{
l/=10;
ans*=10;
}
return ans;
}
int main()
{
LL ans = 0;
LL sum=0;
LL d;
while(cin>>a>>n)
{
ans=a;
sum=0;
sum+=ans;sum%=r;
d = getD(a);
for(int i=1;i
回文距离:
这题这样做比较简单(AC了才想到):
先取a的前一半长度的数字b, 举个例子,3456 就取 34, 65432 就取 654
然后分别求以b, b-1, b+1 为前缀的数字c1, c2, c3
还是用上面的例子
b=34 的话
c1=3443, c2=3333, c3=3553
b=654的话
c1=65456, c2=65356, c3=65556
然后找c1, c2, c3 和a 差最小就行了
很简单吧
这题我的用时是7363ms……这都能过,我也是醉了
上代码:
#include
using namespace std;
#define LL long long
int lent(LL l)
{
int ans=0;
while(l>0)
{
l/=10;ans++;
}
return ans;
}
LL getPali(LL l, bool j)
{
LL tmp = l;
if(j)tmp/=10;
while(tmp>0)
{
l*=10;
l += (tmp%10);
tmp/=10;
}
return l;
}
LL getFix(int c)
{
LL ans=1;
while(c--){
ans*=10;
}
return ans;
}
int main()
{
LL l, ltmp, lLarger, lSmaller, lFirst;
while(cin>>l)
{
ltmp = l;
int len = lent(l);
int halfLen = len/2;
LL fix = getFix(halfLen);
l/=fix;
lFirst = getPali(l, len&1);
if(lFirst >= ltmp){
lSmaller = getPali(l-1, len&1);
lLarger = lFirst;
}
if(lFirst < ltmp){
lSmaller = lFirst;
lLarger = getPali(l+1, len&1);
}
int dif = lLarger-ltmp;
int dif2 = ltmp-lSmaller;
dif = dif