传送门
问题 I: A simple math problem
时间限制: 1 Sec 内存限制: 512 MB题目描述
Given a number n, you should calculate 123456 . . . 11121314 . . . n module 11.
输入
A single line with an integer n (0 < n < 1e17) 单组数据测试.(//数据范围应该是这,oj上的范围不对)
输出
Output one integer, 123456 . . . 11121314 . . . n module 11
样例输入
1
20
21
样例输出
1
5
4
提示
1 ≡ 1( mod 11)
1234567891011121314151617181920 ≡ 5( mod 11)
123456789101112131415161718192021 ≡ 4( mod 11)
题意很简单。
思路:打表看规律。1到9是一个规律a0,10到99是一个规律a1,100到999是一个规律a2,…………………1e16到1e17-1是一个规律a16。模拟一个一个算。
a1求出来,就能通过a1求出99的,然后求a2,就能通过a2求出999的,然后…………。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<map>
#include<stdlib.h>
#define LL long long
using namespace std;
LL a[20][30];//存每个数量级的规律。
int b[20];//存每个数量级的循环长度,与a【】对应
LL kl(LL o)
{
LL p=1;
while(o)
{
o/=10LL;
p*=10LL;
}
return p;
}
int kr(LL o)
{
int w=-1;
while(o)
{
o/=10;
w++;
}
return w;
}
LL xa(LL p,int x)//求123456789101112……p对11的余数。
{
LL q=p-kl(p)/10+1;//求p是第x数量级的第几个;
if(q%(LL)b[x]==0)
return a[x][b[x]-1];
return a[x][q%b[x]-1];
}
int main()
{
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
int x=1;//数量级
LL y=10;//枚举数字y,直到找到规律
LL yu=5;//余数
while(x<17)
{
while(y)
{
yu=(yu*kl(y)+y)%11;
a[x][b[x]++]=yu;
if(b[x]>2&&a[x][0]==a[x][b[x]-2]&&a[x][1]==a[x][b[x]-1])//判断是否循环
{
b[x]-=2;
break;
}
y++;
}
y=kl(y);
yu=xa(y-1,x);
x++;
}
LL n;
while(~scanf("%lld",&n))
{
if(n<10LL)
{
if(n%2==0) printf("%lld\n",n/2LL);
else printf("%lld\n",n/2LL+1LL);
}
else
{
int s=kr(n);
printf("%d\n",xa(n,s));
}
}
}