zoukankan      html  css  js  c++  java
  • CodeForces 327C

    Magic Five

    Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u
    Submit Status

    Description

    There is a long plate s containing n digits. Iahub wants to delete some digits (possibly none, but he is not allowed to delete all the digits) to form his "magic number" on the plate, a number that is divisible by 5. Note that, the resulting number may contain leading zeros.

    Now Iahub wants to count the number of ways he can obtain magic number, modulo 1000000007 (109 + 7). Two ways are different, if the set of deleted positions in s differs.

    Look at the input part of the statement, s is given in a special form.

    Input

    In the first line you're given a string a (1 ≤ |a| ≤ 105), containing digits only. In the second line you're given an integer k (1 ≤ k ≤ 109). The plate s is formed by concatenating k copies of a together. That is n = |ak.

    Output

    Print a single integer — the required number of ways modulo 1000000007 (109 + 7).

    Sample Input

    Input
    1256
    1
    Output
    4
    Input
    13990
    2
    Output
    528
    Input
    555
    2
    Output
    63

    Hint

    In the first case, there are four possible ways to make a number that is divisible by 5: 5, 15, 25 and 125.

    In the second case, remember to concatenate the copies of a. The actual plate is 1399013990.

    In the third case, except deleting all digits, any choice will do. Therefore there are 26 - 1 = 63 possible ways to delete digits.

    题意:

    告诉一个串,以及这个串的个数K,将这K个串连接起来,然后可以删除其中一些数字,但是不能全部删除,使得这个串表示的数能被5整除,可以存在包含前导零的情况,05 和 5是两个不同的数。问总共能有多少这种数。

    思路:

    能被5整除,那么要么是0 要么是5结尾,所以对于只有一个串的时候每次都找0 5结尾的数,它前面的可以选或者不选就是总共2^i种可能。当有多个串时,第2,3,4,。。。k个串中可能性就是第一个串中对应位置的 i+strlen(str), 第一个串中符合条件的2^i的和为tmp,那么k个串中符合条件的总和就是  tmp*(1+2^len+2^(2len)+ 2^(3len)....+2^(klen)),这是个等比数列求和问题,可以化成(1-2^(len*k))/ (1-2^(len)) %mod 

    假设 a=(1-2^(len*k))b=(1-2^(len))  由于a很大,所以这个时候就要用到逆元来求(a/b)%mod 

     1 //2016.8.14
     2 #include<iostream>
     3 #include<cstdio>
     4 #define ll long long 
     5 
     6 using namespace std;
     7 
     8 const int mod = 1e9+7;
     9 
    10 ll pow(ll a, ll b)//快速幂
    11 {
    12     ll ans = 1;
    13     while(b)
    14     {
    15         if(b&1)ans *= a, ans %= mod;
    16         a *= a, a %= mod;
    17         b>>=1;
    18     }
    19     return ans;
    20 }
    21 
    22 int main()
    23 {
    24     string a;
    25     int k;
    26     ll ans = 0;//ans = 2^i * ((i^kn)/(1-2^n))%mod
    27     while(cin>>a>>k)
    28     {
    29         ans = 0;
    30         int n = a.size();
    31         for(int i = 0; i < n; i++)
    32               if(a[i]=='0'||a[i]=='5')
    33                   ans+=pow(2, i);
    34         ll y = pow(2, n);
    35         ll x = pow(y, k);
    36         x = ((1-x)%mod+mod)%mod;
    37         y = ((1-y)%mod+mod)%mod;
    38         ans = ((ans%mod)*(x*pow(y, mod-2)%mod))%mod;//利用费马小定理求y的逆元
    39         cout<<ans<<endl;
    40     }
    41     
    42     return 0;
    43 }
  • 相关阅读:
    pyspark 基本api使用说明(一)
    584. 寻找用户推荐人
    Spring IOC 容器源码分析(了解模板方法--钩子方法在spring中的使用)
    Mybatis事务创建和管理
    Mybatis源码分析执行流程总结
    设计模式之责任链模式
    设计模式之外观模式(门面模式)以及如何利用到接口设计中
    如何在聚合支付平台,巧用模板+工厂设计模式来实现异步回调
    浅谈设计模式之代理设计模式
    Java线上问题排查思路
  • 原文地址:https://www.cnblogs.com/Penn000/p/5769675.html
Copyright © 2011-2022 走看看