zoukankan      html  css  js  c++  java
  • Book Reading

    题目链接:http://codeforces.com/contest/1213

    Polycarp is reading a book consisting of n pages numbered from 1 to n. Every time he finishes the page with the number divisible by m, he writes down the last digit of this page number. For example, if n=15 and m=5, pages divisible by m are 5,10,15. Their last digits are 5,0,5 correspondingly, their sum is 10.

    Your task is to calculate the sum of all digits Polycarp has written down.

    You have to answer q independent queries.

    Input
    The first line of the input contains one integer q (1≤q≤1000) — the number of queries.

    The following q lines contain queries, one per line. Each query is given as two integers n and m (1≤n,m≤1e16) — the number of pages in the book and required divisor, respectively.

    Output
    For each query print the answer for it — the sum of digits written down by Polycarp.

    Example
    Input
    7
    1 1
    10 1
    100 3
    1024 14
    998244353 1337
    123 144
    1234312817382646 13
    Output
    1
    45
    153
    294
    3359835
    0
    427262129093995

    题目大意:

    输入一个数q,表示循环的次数,在每次循环中输入两个数n和m,要求在1~n的范围内,计算m的倍数的个位数之和并输出。若m大于n,输出0.

    思路分析:

    以上基本上包含了对于题目的分析,就是在1~n的范围内找到m的倍数,然后将它们的个位数相加得到结果,若m大于n,结果为0。

    注意:

    不要一直使用for循环寻找m的倍数,然后求出它的个位数,当n很大的时候,你会发现程序运行半天都没有结果,此路不通!但是该怎么做呢?

    具体分析:

    假如n为45,m为5,那么在1~45这个范围内,5的倍数为:5,10,15,20,25,30,35,40,45,再来看这些数的个位数,分别是:5,0,,5,0,5,0,5,0,5。发现规律了没有!5,0循环重复出现!循环的长度为2。所以问题就有了突破口,使用for循环(这个循环最多有n/m次)查找m的倍数,然后开一个数组记录每次出现的个位数,同时声明一个变量len来记录这个循环长度并计算这些个位数之和存到sum中,当某个数字重复出现时就退出for循环。接下来就是计算能有多少个这样的循环,计算出次数并乘以刚刚计算出来的sum,并将这个数值赋给sum.可能这时候还有漏网之鱼,就像上面举的例子里面的最后一个5,这时候就要将构不成一个个位数出现规律循环的数加在sum中,然后输出。分析结束!
    代码:

    #include<algorithm>
    #include<cstdio>
    #include<cstring>
    #include<iostream>
    using namespace std;
    long long int n,m;    //n为书的页数,m表示每读m页就记录一次个位数
    int q;     //总循环的次数
    int i,j;  //控制循环
    int s[50];   //记录循环过的数据 
    int a[50];   //记录循环过的次数
    int  main()
    {
      cin>>q;
      while(q--){
        memset(s,0,sizeof(s));  //初始化数组,每循环一次就要清空一下数组,要不然就凉了
        memset(a,0,sizeof(a));  //初始化数组
        long long int sum=0;   //记录数据之和
        int temp=0;    //记录个位数
        int len=0;    //记录循环的长度
        cin>>n>>m;
        long long int cs=n/m;  //cs表示共可循环多少次
        if(m>n)
        {
          cout<<0<<endl;
          continue;
        }
        else{
            for(i=1;i<=cs;i++){
              temp=(m*i)%10;
              s[i]=temp;
              a[temp]++;
              if(a[temp]==2)
               break;
              sum+=temp;
              len++;
            }
        }
        sum*=(cs/len);
        if(cs/len!=0){
          for(i=1;i<=cs%len;i++)
           sum+=s[i];
        }
        cout<<sum<<endl;
      }
      return 0;
    }
    
  • 相关阅读:
    SpringBoot中Word转PDF
    使用Word模板导出标准表Word样式文件
    Chrome 80及以上版本 中 Iframe 跨域 Cookie 的 Samesite 问题
    Visual Studio 2022 Key
    《HelloGitHub》第 69 期
    重玩 40 年前的经典游戏小蜜蜂,这次通关了源码
    那些年的开源项目,你跑起来了吗?
    图片处理看这篇就完了「GitHub 热点速览 v.21.48」
    年底巩固下 CS 知识「GitHub 热点速览 v.21.49」
    误入 GitHub 游戏区,意外地收获颇丰
  • 原文地址:https://www.cnblogs.com/shmilky/p/14089001.html
Copyright © 2011-2022 走看看