Description

It is well-known that for any n there are exactly four n-digit numbers (including ones with leading zeros) that are self-squares: the last ndigits of the square of such number are equal to the number itself. These four numbers are always suffixes of these four infinite sequences:

...0000000000 
...0000000001 
...8212890625 
...1787109376

For example,   093762  =87909376, which ends with 09376.

You are required to count the numbers that are almost self-squares: such that each of the last n digits of their square is at most d away from the corresponding digit of the number itself. Note that we consider digits 0 and 9 to be adjacent, so for example digits that are at most 3 away from digit 8 are 5, 6, 7, 8, 9, 0 and 1.

Input

The first line contains the number of test cases t,1≤t≤72. Each of the next t lines contains one test case: two numbers n(1≤n≤ 18) and d(0≤ d≤3).

Output

For each test case, output the number of almost self-squares with length n and the (circular) distance in each digit from the square at most d in a line by itself.

Sample Input

2
5 0
2 1

Sample Output

4
12

Hint

In the second case, number 12's almost self-squares are: 00, 01, 10, 11, 15, 25, 35, 66, 76, 86, 90, 91

题意:

求满足以下条件

①.这个数为n位(可以有前导零)

②.取它的平方的后n位,与它本身每一位对应之差≤d(这里的差指的是数字之间的距离,而这个距离是将数字按圈排列,0与9相邻所求得的)

的数字的个数。

题解:

打表吧

规律就是 res[i][j] = res[i-1][j]*(2*j+1)

代码:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
#define is_lower(c) (c >= 'a' && c <= 'z')
#define is_upper(c) (c >= 'A' && c <= 'Z')
#define is_alpha(c) (is_lower(c) || is_upper(c))
#define is_digit(c) (c >= '0' && c <= '9')
#define min(a, b) ((a) < (b) ? (a) : (b))
#define max(a, b) ((a) > (b) ? (a) : (b))
#define IO                 \
  ios::sync_with_stdio(); \
  cin.tie();              \
  cout.tie();
#define For(i, a, b) for (int i = a; i <= b; i++)
typedef long long ll;
typedef unsigned long long ull;
const ll inf = 0x3f3f3f3f;
;
const ll inf_ll = (ll)1e18;
const ll mod = 1000000007LL;
;
ll res[][];
void init() {
    res[][] = res[][] = ;
    res[][] = res[][] = ;
    ; i < ; i++) {
        ;j <= ; j++)
            res[i][j] = res[i-][j] * ( * j + );
    }
}
int main() {
    init();
    int T;
    cin >> T;
    while(T--){
        int n,d;
        cin>>n>>d;
        cout << res[n][d] << endl;
    }
    ;
}

第八届省赛 B:Quadrat (打表找规律)的更多相关文章

  1. 【NOIP 模拟赛】中值滤波 打表找规律

    对于这样看起来不像什么算法也没什么知识点的题,一脸懵逼的话不是手推规律就是打表找规律......... 当然还有一些超出你能力之外的数学题...... #include <cstdio> ...

  2. 计蒜客 39279.Swap-打表找规律 (The 2019 ACM-ICPC China Shannxi Provincial Programming Contest L.) 2019ICPC西安邀请赛现场赛重现赛

    Swap There is a sequence of numbers of length nn, and each number in the sequence is different. Ther ...

  3. hdu 3032 Nim or not Nim? (SG函数博弈+打表找规律)

    Nim or not Nim? Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Sub ...

  4. HDU 5753 Permutation Bo (推导 or 打表找规律)

    Permutation Bo 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5753 Description There are two sequen ...

  5. HDU 4861 Couple doubi (数论 or 打表找规律)

    Couple doubi 题目链接: http://acm.hust.edu.cn/vjudge/contest/121334#problem/D Description DouBiXp has a ...

  6. HDU2149-Good Luck in CET-4 Everybody!(博弈,打表找规律)

    Good Luck in CET-4 Everybody! Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ...

  7. 【ZOJ】3785 What day is that day? ——浅谈KMP在ACM竞赛中的暴力打表找规律中的应用

    转载请声明出处:http://www.cnblogs.com/kevince/p/3887827.html    ——By Kevince 首先声明一下,这里的规律指的是循环,即找到最小循环周期. 这 ...

  8. HDU 5795 A Simple Nim(SG打表找规律)

    SG打表找规律 HDU 5795 题目连接 #include<iostream> #include<cstdio> #include<cmath> #include ...

  9. hdu_5894_hannnnah_j’s Biological Test(打表找规律)

    题目链接:hdu_5894_hannnnah_j’s Biological Test 题意: 有n个不同的位置围成一个圈,现在要安排m个人坐,每个人至少的间隔为k,问有多少种安排 题解: 先打表找规律 ...

  10. hdu_5795_A Simple Nim(打表找规律的博弈)

    题目链接:hdu_5795_A Simple Nim 题意: 有N堆石子,你可以取每堆的1-m个,也可以将这堆石子分成3堆,问你先手输还是赢 题解: 打表找规律可得: sg[0]=0 当x=8k+7时 ...

随机推荐

  1. [C/C++] “箭头(->)”和“点号(.)”的区别

    转自:http://blog.csdn.net/gyymen/article/details/4962873 首先介绍一下C++中的结构.对于一个结构: struct MyStruct { int m ...

  2. 2018宁夏邀请赛K Vertex Covers

    题目链接:https://nanti.jisuanke.com/t/28411 题意: 给出n(n<=36)个点的一个图.求点覆盖集数. 题解: 将n个点折半为L和R两部分.对于R内部的边,枚举 ...

  3. [Leetcode] unique paths ii 独特路径

    Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...

  4. vue2学习篇一 $mount()手动挂载

    $mount()手动挂载 //当Vue实例没有el属性时,则该实例尚没有挂载到某个dom中: //假如需要延迟挂载,可以在之后手动调用vm.$mount()方法来挂载.例如: new Vue({ // ...

  5. (转)tableview的索引设置

    .感觉tableview的索引条将表视图往左边挤了一点?别担心,只是颜色问题.只要如此设置即可 //索引条背景的颜色(清空颜色就不会感觉索引条将tableview往左边挤) [_tableView s ...

  6. 转:JVM Server与Client运行模式

    转自:http://blog.csdn.net/zhuyijian135757/article/details/38391785 JVM Server模式与client模式启动,最主要的差别在于:-S ...

  7. 有关getClassLoader().getResourceAsStream(fileName)、class.getResourceAsStream(fileName)和().getContextClassLoader().getResourceAsStream(fileName)的区别

    一:前言 在自己获取属性时,碰见了XX.class.getResourceAsStream(fileName),自己对这个其实不是很理解,上网查了下资料,又看到了上述的几个,所以就研究了下. 二:内容 ...

  8. 51Nod 1118 机器人走方格--求逆元

    (x/y) %mod =x*(y^(mod-2))%mod; 在算x,y的时候可以一直mod 来缩小 y^(mod-2)显然是个快速幂 #include <iostream> #inclu ...

  9. codechef T3 计算器

    CALC: 计算器题目描述 大厨有一个计算器,计算器上有两个屏幕和两个按钮.初始时每个屏幕上显示的都是 0.没按 一次第一个按钮,就会让第一个屏幕上显示的数字加 1,同时消耗 1 单位的能量. 每按一 ...

  10. bzoj 1060 贪心

    设根到每个叶子节点的距离为dis,比较容易的看出来,我们需要把这颗树的所有叶子节点的值都变成其中最大的内个,我们设为max,那么对于一颗子树来说,设其中dis值最大的为x,我们需要将这个子树根节点和子 ...