• 2016"百度之星"


    All X

     
     Accepts: 1281
     
     Submissions: 7580
     Time Limit: 2000/1000 MS (Java/Others)
     
     Memory Limit: 65536/65536 K (Java/Others)
    Problem Description

    F(x, m)F(x,m) 代表一个全是由数字xx组成的mm位数字。请计算,以下式子是否成立:

    F(x,m) mod k equiv cF(x,m) mod k  c

    Input

    第一行一个整数TT,表示TT组数据。 每组测试数据占一行,包含四个数字x,m,k,cx,m,k,c

    1leq xleq 91x9

    1leq mleq 10^{10}1m1010​​

    0leq c< kleq 10,0000c<k10,000

    Output

    对于每组数据,输出两行: 第一行输出:"Case #i:"。ii代表第ii组测试数据。 第二行输出“Yes” 或者 “No”,代表四个数字,是否能够满足题目中给的公式。

    Sample Input
    3
    1 3 5 2
    1 3 5 1
    3 5 99 69
    
    Sample Output
    Case #1:
    No
    Case #2:
    Yes
    Case #3:
    Yes
    Hint
    对于第一组测试数据:111 mod 5 = 1,公式不成立,所以答案是”No”,而第二组测试数据中满足如上公式,所以答案是 “Yes”。
     
    思路:这题可以用矩阵快速幂,可推同模公式,貌似还有求循环节什么的,我用的是第二种。如下:
     
    m个x模k为c,则 x *(10 ^ m - 1)/ 9  %k = c
    则x *(10 ^ m - 1)  % (9 * k) = 9*c %(9 * k)
     

    用矩阵的话用此矩阵快速幂(赛后学的)

     
    代码如下:
     
    #include<iostream>
    #include<cstdio>
    #include<string>
    #include<cstring>
    #include<cstdlib>
    #include<queue>
    #include<vector>
    #include<algorithm>
    using namespace std;
    typedef long long LL;
    LL PowMod(LL a,LL b,LL MOD){
        LL ret=1;
        while(b){
            if(b&1) ret=(ret*a)%MOD;
            a=(a*a)%MOD;
            b>>=1;
        }
        return ret;
    }
    
    int main(){
        LL x, m , k, c;
        int t;
        cin>>t;
        for(int i = 1; i <= t; i++){
            scanf("%I64d %I64d %I64d %I64d", &x, &m, &k, &c);
            k *= 9;
            LL a = ((PowMod(10, m, k) + k - 1) % k) * x %k;
            LL b = (9 *c) % k;
            printf("Case #%d:
    ",i);
            if(a == b){
                printf("Yes
    ");
            }else{
                printf("No
    ");
            }
        }
        return 0;
    }
  • 相关阅读:
    php
    nginx
    docker
    pyenv 配置python虚拟环境
    [运维笔记] Nginx编译安装
    [运维笔记] Mysql单库备份脚本
    BurpSuite Intruder 4种攻击模式
    java判断一个单向链表是否有环路
    二分查找(递归和非递归)
    反转链表算法题
  • 原文地址:https://www.cnblogs.com/IMGavin/p/5518466.html
Copyright © 2020-2023  润新知