abc269_f Numbered Checker 题解

Posted lw0的博客园据点

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了abc269_f Numbered Checker 题解相关的知识,希望对你有一定的参考价值。

abc269_f Numbered Checker 题解

Numbered Checker

题意

有一个 \\(n\\times m\\) 的方格矩阵,左上角是 \\((1,1)\\),右下角是 \\((n,m)\\),每个方格中都有一个整数,其中 \\((x,y)\\) 中的数字为:

  • 如果 \\(x+y\\) 是奇数,则 \\((x,y)\\) 中的数字为 \\(0\\)
  • 否则,\\((x,y)\\) 中的数字为 \\((x-1)\\times m + y\\)

\\(Q\\) 组询问,每组询问给定四个整数 \\(a,b,c,d\\),求所有满足要求的 \\((i,j)\\) 中的数字之和:

  • \\(a \\leqslant i \\leqslant b\\)
  • \\(c \\leqslant j \\leqslant d\\)

答案对 \\(998244353\\) 取模。

思路

等差数列求和。

对于每组询问

暴力时间复杂度:\\(O(n\\times m)\\),爆炸。

推导壹

就拿第一个样例举例子。

可以很明显地发现,奇数行与偶数行排列虽然不太一样,但两者都是等差数列且公差为 \\(2\\)

经过仔细地推导(不给推导过程,自己推去),我们可以发现:

  • 对于 \\(a+c\\) 为奇数的情况下:
    • \\(a\\) 行的等差数列首项为 \\((a,c+1)\\),项数为 \\(\\fracd-c+12\\)
    • \\(a+1\\) 行的等差数列首项为 \\((a+1,c)\\),项数为 \\(1+\\fracd-c2\\)
  • 对于 \\(a+c\\)​ 为偶数的情况下:
    • \\(a\\) 行的等差数列首项为 \\((a,c)\\),项数为 \\(\\fracd-c2+1\\)
    • \\(a+1\\) 行的等差数列首项为 \\((a+1,c+1)\\),项数为 \\(\\fracd-c+12\\)

(以上除法均需带有向下取整)。

时间复杂度:\\(O(n)\\),还是不够优秀。

推导贰

可以发现,行与行之间也是等差数列!公差为 \\(2m\\)

令当前询问第 \\(a\\) 行的等差数列之和为 \\(sum\\),项数为 \\(p\\);第 \\(a+1\\) 行的等差数列之和为 \\(num\\),项数为 \\(q\\)

还是不给推导过程,答案就可以变成两个等差数列:

  • 第一个:以 \\(sum\\) 为首项,公差为 \\(2\\times m\\times p\\),项数为 \\(\\fracb-a2+1\\)
  • 第二个:以 \\(num\\) 为首项,公差为 \\(2\\times m\\times q\\),项数为 \\(\\fracb-a+12\\)

求出两个等差数列之和,将其加起来就是答案啦,注意取模细节。

时间复杂度:\\(O(1)\\)

复杂度

  • 时间:\\(O(Q)\\)
  • 空间:\\(O(1)\\)

Code

#include <iostream>

using namespace std;
using ll = long long;

const int mod = 998244353;

ll n, m, t, a, b, c, d, p, q, sum, num;

ll id (ll x, ll y)  // 将坐标转数值
  return ((x - 1) * m + y) % mod;


ll Sum (ll s, ll d, ll n)  // 等差数列求和公式
  return (s * n + n * (n - 1) / 2 % mod * d % mod) % mod;


int main () 
  ios::sync_with_stdio(0), cin.tie(0);
  for (cin >> n >> m >> t; t; t--) 
    cin >> a >> b >> c >> d;
    p = (d - c + 1) / 2, q = (d - c) / 2 + 1;
    if ((a + c) % 2) 
      sum = Sum(id(a, c + 1), 2, p), num = Sum(id(a + 1, c), 2, q); // 套用公式
     else 
      swap(p, q);
      sum = Sum(id(a, c), 2, p), num = Sum(id(a + 1, c + 1), 2, q);
    
    cout << (Sum(sum, 2 * m * p % mod, (b - a) / 2 + 1) + Sum(num, 2 * m * q % mod, (b - a + 1) / 2)) % mod << \'\\n\'; // 各种取模细节
  
  return 0;

POJ训练计划1035_Spell checker(串处理/暴力)

Spell checker
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 18418   Accepted: 6759

Description

You, as a member of a development team for a new spell checking program, are to write a module that will check the correctness of given words using a known dictionary of all correct words in all their forms. 
If the word is absent in the dictionary then it can be replaced by correct words (from the dictionary) that can be obtained by one of the following operations: 
?

deleting of one letter from the word; 
?replacing of one letter in the word with an arbitrary letter; 
?

inserting of one arbitrary letter into the word. 
Your task is to write the program that will find all possible replacements from the dictionary for every given word. 

Input

The first part of the input file contains all words from the dictionary. Each word occupies its own line. This part is finished by the single character ‘#‘ on a separate line. All words are different. There will be at most 10000 words in the dictionary. 
The next part of the file contains all words that are to be checked. Each word occupies its own line. This part is also finished by the single character ‘#‘ on a separate line. There will be at most 50 words that are to be checked. 
All words in the input file (words from the dictionary and words to be checked) consist only of small alphabetic characters and each one contains 15 characters at most. 

Output

Write to the output file exactly one line for every checked word in the order of their appearance in the second part of the input file. If the word is correct (i.e. it exists in the dictionary) write the message: " is correct". If the word is not correct then write this word first, then write the character ‘:‘ (colon), and after a single space write all its possible replacements, separated by spaces. The replacements should be written in the order of their appearance in the dictionary (in the first part of the input file). If there are no replacements for this word then the line feed should immediately follow the colon.

Sample Input

i
is
has
have
be
my
more
contest
me
too
if
award
#
me
aware
m
contest
hav
oo
or
i
fi
mre
#

Sample Output

me is correct
aware: award
m: i my me
contest is correct
hav: has have
oo: too
or:
i is correct
fi: i
mre: more me

Source

解题报告
怕超时写哈希。结果还真超时了。改写暴力居然过了。。。

C++只是,G++过。。

#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>

using namespace std;
struct node
{
    char s[20];
    int t;
} dic[10010];
int ctt(char *str,char *ch)
{
    int l1=strlen(str),l2=strlen(ch);
    int i,t=0;
    if(l1<l2)
    {
        for(i=0;i<l2;i++)
        {
            if(str[t]==ch[i])
                t++;
            if(t==l1)
                return 1;
        }
        return 0;
    }
    else if(l1>l2)
    {
        for(i=0;i<l1;i++)
        {
            if(str[i]==ch[t])
                t++;
            if(t==l2)
                return 1;
        }
        return 0;
    }
    else
    {
        for(i=0;i<l1;i++)
        {
            if(str[i]==ch[i])
                t++;
        }
        if(t==l1-1)
            return 1;
        else
        return 0;
    }
}
int main()
{
    int n=0,f=0,i;
    char str[20];
    while(~scanf("%s",str))
    {
        if(str[0]=='#')break;
        strcpy(dic[n].s,str);
        dic[n++].t=n;
    }
    //<<ctt("aware","award");
    while(~scanf("%s",str))
    {
        f=0;
        if(str[0]=='#')break;
        for(i=0; i<n; i++)
        {
            if(strcmp(str,dic[i].s)==0)
            {
                printf("%s is correct",str);
                f=1;
                break;
            }
        }
        if(!f)
        {
            printf("%s:",str);
            int l=strlen(str);
            for(i=0; i<n; i++)
            {
                if(strlen(dic[i].s)-l==1||strlen(dic[i].s)-l==-1||strlen(dic[i].s)-l==0)
                {
                    if(ctt(str,dic[i].s))
                        printf(" %s");
                }
            }
        }
        printf("\n");
    }
}


以上是关于abc269_f Numbered Checker 题解的主要内容,如果未能解决你的问题,请参考以下文章

uoj#269. 清华集训2016如何优雅地求和(数论)

题解 AtCoder abc154 F

abc142_f

abc270_f Transportation 题解

POJ训练计划1035_Spell checker(串处理/暴力)

bzoj千题计划269:bzoj2655: calc