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;
    }
  • 相关阅读:
    ASP.NET MVC 异常捕获
    Jquery 扩展方法
    Spring.NET笔记1
    ASP.NET MVC Ninject 实现依赖注入
    ASP.NET MVC Unity实现依赖注入
    windows service
    反射用法
    抽象工厂核心反射
    (C#)中的DataSet、string、DataTable等对象转换成Json
    .NET批量删除代码前的行号
  • 原文地址:https://www.cnblogs.com/jhz033/p/5685470.html
Copyright © 2011-2022 走看看