zoukankan      html  css  js  c++  java
  • Codeforces Beta Round #17 D.Notepad 指数循环节

    D. Notepad
    time limit per test
    2 seconds
    memory limit per test
    64 megabytes
    input
    standard input
    output
    standard output

    Nick is attracted by everything unconventional. He doesn't like decimal number system any more, and he decided to study other number systems. A number system with base b caught his attention. Before he starts studying it, he wants to write in his notepad all the numbers of length n without leading zeros in this number system. Each page in Nick's notepad has enough space for c numbers exactly. Nick writes every suitable number only once, starting with the first clean page and leaving no clean spaces. Nick never writes number 0 as he has unpleasant memories about zero divide.

    Would you help Nick find out how many numbers will be written on the last page.

    Input

    The only input line contains three space-separated integers bn and c (2 ≤ b < 10106, 1 ≤ n < 10106, 1 ≤ c ≤ 109). You may consider that Nick has infinite patience, endless amount of paper and representations of digits as characters. The numbers doesn't contain leading zeros.

    Output

    In the only line output the amount of numbers written on the same page as the last number.

    Examples
    input
    2 3 3
    output
    1
    input
    2 3 4
    output
    4
    Note

    In both samples there are exactly 4 numbers of length 3 in binary number system. In the first sample Nick writes 3 numbers on the first page and 1 on the second page. In the second sample all the 4 numbers can be written on the first page.

    题意:求[ (b-1) *(b)^(n-1)]%c;

    思路:指数循环节;

    #include<bits/stdc++.h>
    using namespace std;
    #define ll __int64
    #define esp 0.00000000001
    const int N=1e5+10,M=1e6+10,inf=1e9+10;
    const int mod=1000000007;
    #define MAXN 10000010
    ll quickpow(ll x,ll y,ll z)
    {
        ll ans=1;
        while(y)
        {
            if(y&1)
            ans*=x,ans%=z;
            x*=x;
            x%=z;
            y>>=1;
        }
        return ans;
    }
    ll phi(ll n)
    {
        ll i,rea=n;
        for(i=2;i*i<=n;i++)
        {
            if(n%i==0)
            {
                rea=rea-rea/i;
                while(n%i==0)  n/=i;
            }
        }
        if(n>1)
            rea=rea-rea/n;
        return rea;
    }
    char a[M];
    char b[M];
    int main()
    {
        ll x,y,z,i,t;
        while(~scanf("%s%s%I64d",b,a,&z))
        {
            t=strlen(b);
            ll x=0;
            for(i=0;i<t;i++)
            x=(x*10+b[i]-'0')%z;
            if(x==0)
            x=z;
            t=strlen(a);
            ll p=phi(z);
            ll ans=0;
            int flag=0;
            for(i=0;i<t;i++)
            {
                ans=(ans*10+a[i]-'0');
                if(ans>=p)
                flag=1;
                ans%=p;
            }
            if(flag)
            ans+=p;
            ans--;
            ll gg=(quickpow(x,ans,z)*(x-1))%z;
            if(gg)
            printf("%I64d
    ",gg);
            else
            printf("%I64d
    ",z);
        }
        return 0;
    }
  • 相关阅读:
    CentOS FTP基于虚拟用户的配置
    CentOS6.5 FTP配置
    MAC 隐藏文件的显示
    nignx知识点总结
    什么是函数式编程
    javascript有用代码片段
    PHP百杂
    好文:javascript中的四种循环
    好文:node.js最佳实践
    好文:不使用匿名函数的三个理由
  • 原文地址:https://www.cnblogs.com/jhz033/p/5685470.html
Copyright © 2011-2022 走看看