NYOJ 2333 A simple math problem(思路题)

传送门

问题 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));
        }
    }
}







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值