zoukankan      html  css  js  c++  java
  • Divisibility

    题目链接

    Problem Description

    You are given two 10-based integers b and x, and you are required to determine the following proposition is true or false:

    For arbitrary b-based positive integer (y=c_{1}c_{2}cdots c_{n}) ((c_{i}) is the i-th dight from left of y),define (displaystyle f(y)=sum^{n}_{i=1}c_{i}),if(underbrace{ f( f( cdots f(y)cdots ))}_{infty}) can be divided by x,then y can be divided by x,otherwise y can't be divided by x.

    Input

    The first line contains a 10-based integer((1leq t leq 10^{5}))— the number of test cases.

    For each test case,there is a single line containing two 10-based integers b and x((2leq b,xleq 10^{18}))

    output

    For each test case,if the proposition is true,print"T",otherwise print"F"(without quotes).

    Sample Input

    1
    10 3
    

    Sample Output

    T
    

    题解

    #include <bits/stdc++.h>
    using namespace std;
    
    int main() {
        int t;
        scanf("%d", &t);
        while (t--) {
            long long b, x;
            scanf("%lld %lld", &b, &x);
            if (b % x == 1) {
                printf("T
    ");
            }
            else {
                printf("F
    ");
            }
        }
        return 0;
    }
    

    这道题里边有一个命题,这个命题等价于:对于任意的b进制正整数(y=overline{c_{1}c{_2}cdots c_{n}}) ,如果(c_{1}+c_{2}+cdots +c_{n}equiv 0(mod x)) ,那么(yequiv 0(mod x)) ,否则(y otequiv 0 (mod x))

    然后我们要根据给出的b和x来判断此时这个命题是否成立。

    上面给出的这个等价命题还是和题目中的命题有点区别的,我们先看看为什么要这么等价,我们从这个等价命题开始推,只要(c_{1}到c_{n}) 的和能整除x,那么由(c_{1}到c_{n}) 组成的这个数y就能整除x,我们这里设(c_{1}到c_{n})的和为M,既然(c_{1}到c_{n}) 的和能整除x,那么也就相当于M能整除x,那么我们反向应用这个等价命题,既然M能整除x,那么M的各个位数的和也能整除x,我们设M的各个位数的和为(M_{1}) ,既然(M_{1})能整除x,那么(M_{1}) 的各个位数之和也能整除x,就这样一直套娃下去,直到最后各个位数之和只有一位数时,就相当于进行了(infty)(f(y)) ,就推出了题目中的命题。同样得,由题目中的命题也可以推出这个等价命题。所以这两个命题等价。

    所以我们只要证明这个等价命题就行了。

    那么这个等价命题其实也就是说,这个y和(c_{1}+c_{2}+cdots +c_{n}) 的和模x同余。只要(c_{1}到c_{n}) 的和模x余0的话,也就是整除的话,那么y也会模x余0,也整除。如果(c_{1}到c_{n})的和模x不余0的话,那么y也模x不余0,所以有了下面的式子。

    [yequiv (c_{1}+c_{2}+cdots +c_{n})(mod x) ]

    y还可以进一步展开写:

    [yequiv c_{1}b^{n-1}+c_{2}b^{n-2}+cdots +c_{n}b^{0}equiv (c_{1}+c_{2}+cdots +c_{n})(mod x) ]

    我们把y展开来模x:

    ((c_{1}b^{n-1}+c_{2}b^{n-2}+cdots +c_{n}b^{0})mod x)

    (=(c_{1}b^{n-1}mod x+c_{2}b^{n-2}mod x+cdots +c_{n}b^{0}mod x)mod x)------------------①

    既然我们想要让这个命题成立的话,那么我们就得把这个式子变成((c_{1}+c_{2}+cdots +c_{n})(mod x))--------------------②

    所以我们就得想想怎么样把①式中的(b^{n-1}mod x)(b^{n-2}mod x)(cdots)(b^{0}mod x)

    都变成1,只要变成1那就可以变成②式了。

    如果我们想让b的几次方模x为1的话,那么b模x必须为1,
    证明如下:

    (b^{n}mod x)

    (=underbrace{(b imes b imes b imes cdots imes b)}_{n}mod x)

    (=(b mod x imes b mod x imes cdots imes b mod x) mod x)

    如果我们想要让整个式子的结果为1的话,那么每一个(b mod x)都得为1才行,这样是一定能满足这个式子的
    那么我们就得出了结论了:

    只有当b%x=1的时候,这个定理才满足,推出这个后就容易写代码了。

  • 相关阅读:
    l1-013
    将博客搬至CSDN
    Educational Codeforces Round 25
    大组合数取余模板【Lucas定理】
    Exams(二分求左界+贪心)
    Cutting (暴力 + 滚动哈希判字符串匹配)
    Cubes(DFS+剪枝)
    Codeforces Round #409 (Div. 2)
    【复习挖坑】dp + 图
    n & n-1 和 n & -n
  • 原文地址:https://www.cnblogs.com/fate-/p/13454843.html
Copyright © 2011-2022 走看看