2018牛课多校第四场A(欧拉降幂)

题目链接:https://www.nowcoder.com/acm/contest/142/A

思路:首先是打表找规律(。。我是直接手推了几个),然后发现对于2,指数很大而且在一直变化,所以要欧拉降幂,证明什么的太麻烦,我这里直留一个板子以后要用方便。。。

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <bitset>
#include <cmath>
#include <cctype>
#include <unordered_map>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <sstream>
#include <iomanip>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll inff = 0x3f3f3f3f3f3f3f3f;
#define FOR(i,a,b) for(int i(a);i<=(b);++i)
#define FOL(i,a,b) for(int i(a);i>=(b);--i)
#define REW(a,b) memset(a,b,sizeof(a))
#define inf int(0x3f3f3f3f)
#define si(a) scanf("%d",&a)
#define sl(a) scanf("%lld",&a)
#define sd(a) scanf("%lf",&a)
#define ss(a) scanf("%s",a)
#define mod int(1e9+7)
#define pb push_back
#define Pll pair<ll,ll>
#define P pair<int,int>
#define pi acos(-1)
string a;
ll ph[30],z=29,x=1e9+7;
ll gmod(ll a,ll b,ll c)
{
    ll res=1ll;
    while(b)
    {
        if(b&1) res=res*a%c;
        a=a*a%c,b>>=1;
    }
    return res;
}
ll oula(ll n)  //欧拉函数模板
{
    ll res=n;
    for(ll i=2;i*i<=n;i++)
    {
        if(n%i==0) res-=res/i;
        while(n%i==0) n/=i;
    }
    if(n>1) res-=res/n;
    return res;
}
ll dfs(int now,int x)
{
    if(x==1) return 0;
    if(now==-1)  return 0;
    if(a[now]=='1') return (dfs(now-1,x)*2+2)%ph[x];
    if(a[now]=='0') return (dfs(now-1,x)+1)%ph[x];
    return (3ll*(gmod(2ll,dfs(now-1,x-1)+1,ph[x])-1ll)%ph[x]+ph[x])%ph[x];
}
int main()
{
    cin.tie(0);
    cout.tie(0);
    int t;
    cin>>t;
    ph[z--]=x;
    while(true)
    {
        x=oula(x);
        ph[z--]=x;
        if(x==1) break;
    }
    while(t--)
    {
        cin>>a;
        cout<<dfs(a.size()-1,29)<<endl;
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值