zoukankan      html  css  js  c++  java
  • Codeforces 300C

    题目链接: http://codeforces.com/contest/300/problem/C

    本来是道不难的题目,还是自己的数学功底不扎实。

    从该题又一次巩固了关于乘法逆的概念,在剩余系中,如果要做除法,如 a / b%n ,  此时应该计算 a * (b在n下的逆), 而不是直接计算 a / b

    另外一点值得注意的是,如果 n 为一素数,那么 b 的逆就是 pow_mod(a, n-2, n). 模拟叫做费马小定理。

    乘法逆真的很重要。务必牢记!!!


    附AC代码:

     1 /*************************************************************************
     2     > File Name: 300C.c
     3     > Author: Stomach_ache
     4     > Mail: 1179998621@qq.com 
     5     > Created Time: 2014年01月07日 星期二 17时32分26秒
     6  ************************************************************************/
     7 
     8 #include<stdio.h>
     9 #include<string.h>
    10 #include<stdlib.h>
    11 
    12 #define mod 1000000007
    13 typedef long long LL;
    14 int a, b, n, c[1000005], rfact[1000005]; // c[] -> c(n, k).............rfack[] -> k在n下的逆
    15 
    16 // x is good ?
    17 int ok(int x) {
    18         while (x > 0) {
    19                 int tmp = x % 10;
    20                 if (tmp != a && tmp != b)
    21                     return 0;
    22                 x /= 10;
    23         }
    24 
    25         return 1;
    26 }
    27 // quick mod
    28 LL pow_mod(LL a, LL b, LL c) {
    29         LL res = 1;
    30         while (b) {
    31                 if (b & 1)
    32                     res = (res * a) % c;
    33                 a = (a * a) % c;
    34                 b >>= 1;
    35         }
    36 
    37         return res % c;
    38 }
    39 
    40 int main(void) {
    41         while (~scanf("%d %d %d", &a, &b, &n)) {
    42                 int i, cur = a * n, d = b - a;
    43                 LL cnt = ok(cur);
    44                 c[0] = 1;
    45                 for (i = 1; i <= n; i++) {
    46                         rfact[i] = pow_mod(i, mod-2, mod);
    47                         c[i] = (LL)c[i - 1] * (n - i + 1) % mod;
    48                         c[i] = (LL)c[i] * rfact[i] % mod;
    49                 }
    50                 for (cur += d, i = 1; i < n; i++, cur += d) {
    51                         if (ok(cur)) {
    52                                 cnt += c[i];
    53                                 cnt %= mod;
    54                         }
    55                 }
    56                 cnt = (cnt + ok(b * n)) % mod;
    57                 printf("%lld
    ", cnt);
    58         }
    59 
    60         return 0;
    61 }
  • 相关阅读:
    Maven3-依赖
    Maven2-坐标
    使用VS Code开发Python
    WinDbg调试分析 asp.net站点 CPU100%问题
    asp.net core2 Centos上配置守护服务(Supervisor)
    asp.net core2部署到Centos上
    IntelliJ Error:Abnormal build process termination
    EF架构~codeFirst从初始化到数据库迁移
    office web apps 实现Wopi预览编辑
    office web apps安装教程
  • 原文地址:https://www.cnblogs.com/Stomach-ache/p/3703174.html
Copyright © 2011-2022 走看看