题目链接: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;
}