#include<iostream>
#include<vector>
#include<algorithm>
#include<cstdio>
#include<queue>
#include<stack>
#include<string>
#include<map>
#include<set>
#include<cmath>
#include<cassert>
#include<cstring>
#include<iomanip>
using namespace std;
#ifdef _WIN32
#define i64 __int64
#define out64 "%I64d\n"
#define in64 "%I64d"
#else
#define i64 long long
#define out64 "%lld\n"
#define in64 "%lld"
#endif
/************ for topcoder by zz1215 *******************/
#define FOR(i,a,b) for( int i = (a) ; i <= (b) ; i ++)
#define FFF(i,a) for( int i = 0 ; i < (a) ; i ++)
#define FFD(i,a,b) for( int i = (a) ; i >= (b) ; i --)
#define S64(a) scanf(in64,&a)
#define SS(a) scanf("%d",&a)
#define LL(a) ((a)<<1)
#define RR(a) (((a)<<1)+1)
#define pb push_back
#define CL(Q) while(!Q.empty())Q.pop()
#define MM(name,what) memset(name,what,sizeof(name))
#define read freopen("in.txt","r",stdin)
#define write freopen("out.txt","w",stdout)
const int inf = 0x3f3f3f3f;
const i64 inf64 = 0x3f3f3f3f3f3f3f3fLL;
const double oo = 10e9;
const double eps = 10e-9;
const double pi = acos(-1.0);
// i64 gcd(i64 a,i64 b){return b?gcd(b,a%b):a;}
i64 gcd(i64 _a, i64 _b)
{
if (!_a || !_b)
{
return max(_a, _b);
}
i64 _t;
while ((_t = _a % _b))
{
_a = _b;
_b = _t;
}
return _b;
}
i64 ext_gcd (i64 _a, i64 _b, i64 &_x, i64 &_y)
{
if (!_b)
{
_x = 1;
_y = 0;
return _a;
}
i64 _d = ext_gcd (_b, _a % _b, _x, _y);
i64 _t = _x;
_x = _y;
_y = _t - _a / _b * _y;
return _d;
}
i64 invmod (i64 _a, i64 _p)
{
i64 _ans, _y;
ext_gcd (_a, _p, _ans, _y);
_ans < 0 ? _ans += _p : 0;
return _ans;
}
vector<int>p;
bool isp(int x)
{
for(int i=0;p[i]*p[i]<=x;i++)
{
if(x%p[i] == 0)
{
return false;
}
}
return true;
}
void init()
{
p.clear();
p.pb(2);
for(int i=3;i<100000;i++)
{
if(isp(i))
{
p.pb(i);
}
}
return ;
}
int oula(int x)
{
int temp = x;
for(int i=0;p[i]*p[i] <= temp;i++)
{
if(x%p[i] == 0)
{
x/=p[i];
x*=p[i]-1;
while(temp%p[i]==0)
{
temp/=p[i];
}
}
}
if(temp>1)
{
x/=temp;
x*=temp-1;
}
return x;
}
i64 mod(i64 a,i64 x,i64 p)
{
i64 ans = 1;
i64 temp = a%p;
while(x)
{
if(x&1)
{
ans *= temp;
ans %= p;
}
x>>=1;
temp *= temp;
temp %= p;
}
return ans;
}
int n,m;
vector<i64>v;
vector<i64>c;
int main()
{
init();
int tt = 1;
while(scanf("%d/%d",&n,&m)!=EOF)
{
cout<<"Case #"<<tt++<<": ";
int dc = gcd(n,m);
n/=dc;
m/=dc;
v.clear();
while(m%2==0)
{
m/=2;
if(n>m)
{
n-=m;
v.pb(1);
}
else if(n==m)
{
v.pb(1);
cout<<v.size()+1<<",0"<<endl;
goto end;
}
else
{
v.pb(0);
}
}
i64 f;
f = oula(m);
c.clear();
for(int i=1;i*i<=f;i++)
{
if(f%i==0)
{
c.pb(i);
c.pb(f/i);
}
}
sort(c.begin(),c.end());
i64 tc;
for(int i=0;i<c.size();i++)
{
if(mod(2,c[i],m) == 1)
{
tc = c[i];
break;
}
}
i64 temp;
for(int i=tc;i>=1;)
{
temp=n*mod(2,i-1,m);
temp%=m;
temp*=2;
if(temp>=m)
{
temp = 1;
}
else
{
temp = 0;
}
if(v.empty())
{
break;
}
if(temp==v.back())
{
v.pop_back();
}
else
{
break;
}
if(i==1)
{
i = tc;
}
else
{
i--;
}
}
cout<<v.size()+1<<","<<tc<<endl;
end:
continue;
}
return 0;
}
pku3358 Period of an Infinite Binary Expansion
最新推荐文章于 2015-10-04 17:02:00 发布