题目描述
已知一条二元序列a(n)=(a0, a1, ..., an-1). 对1 ≤ k ≤ n, 求有限序列a(k)=(a0, a1, ..., ak-1)的有理分数表示. 序列a(n)请参见附件“sequence.txt”, 其中n = 1966000
设a(n)=(a0, a1, ..., an-1)是一条有限二元序列, 即ai ∈ {0,1},0 ≤ i ≤ n - 1. 若有理分数 p q满足q是正奇数, gcd(p,q) = 1, 并且 p≡ q(a0 +a1*2+⋯+an-1*pow(2, n-1)) mod pow(2,n), 则称 p q是序列a(n)的有理分数表示.
符号说明
同余符号 ≡: 设n是一个正整数. 对任意的整数a和b, 有a≡ b mod n当且仅 当n整除a-b.
两个整数a和b的最大公因子记为gcd(a, b).
对两个整数a和b, 记Φ(a,b) = max {|a|,|b|}.
题目分析
对于一个二元序列求出有理分数表示,其中关键因素在三点:
1.q需为正奇数。
2.p跟q不能存在公因子
3.[p-q(a0+a1*2+......+an-1*pow(2, n-1))]%pow(2, n)==0 即可被2的n次方整除
设x = a0+a1*2+......+an-1*pow(2, n-1);即 [p-qx]%pow(2,n)==0
有理逼近算法描述
其中1.a为不断累加的功能,也就是上述分析中x的值。
2.f=(f1, f2); g=(g1, g2)
f1, f2, g1, g2的值之间没有任何内在联系,只是这样写可以简化一下过程。
3.g2存在一个坑点,g2即为q的值,虽然算法描述中g2并没有给出取值范围,但题目中明确表示q为正奇数,也即是g2为正奇数
4.d的取值范围,当abs(g1)!=abs(g2)时,d在-(f1+f2)/(g1+g2)和(f1-f2)/(g1-g2)中取
否则,d的范围可设置为一个数据类型的最大值和最小值之间取。
有理逼近算法代码实现
// vs_project1.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
long long abs_max(long long a, long long b)
{
return max(abs(a), abs(b));
}
int main()
{
ios::sync_with_stdio(false);
ifstream inf;
inf.open("C://Users//49627//Desktop//sequence.txt");
if (!inf)
cout << "error" << endl;
char c;
int num = 0; //控制位数
long double t = 0, f1, f2, g1, g2;
long long mod;
bool flag = false;
while (!inf.eof() && num<45)
{
inf >> c;
int tt = c - '0';
if (tt == 0 && !flag)
{
num++;
continue;
}
else if(!flag)
{
t = pow(2, num);
f1 = 0;
f2 = 2;
g1 = t;
g2 = 1;
flag = true;
num++;
continue;
}
t += tt * pow(2, num);
mod = pow(2, num + 1);
long long sum = t*g2 - g1;
long long ss = sum % mod;
if (ss == 0)
{
cout << "the first case: " << num << endl;
f1 *= 2;
f2 *= 2;
}
else if (abs_max(g1, g2) < abs_max(f1, f2))
{
cout << "the second case: " << num << endl;
long long mixn = 0x3f3f3f3f;
long long x = 0, y = 0, a = 0, b = 0, d1 = -0x3f3f3f3f, d2 = 0x3f3f3f3f;
if (abs(g1) != abs(g2))
{
d1 = -(f1 + f2) / (g1 + g2);
d2 = (f1 - f2) / (g1 - g2);
if (d1 > d2)
{
long long d = d1;
d1 = d2;
d2 = d;
}
}
if ((d1 - 100) % 2 == 0)
d1 = d1 - 99;
else
d1 = d1 - 100;
for (long long j = d1; j <= d2 + 100; j+=2)
{
long long temp = abs_max(f1 + j*g1, f2 + j*g2);
if (mixn > temp && (f2 + j*g2) > 0)
{
mixn = temp;
x = f1 + j*g1;
y = f2 + j*g2;
a = g1;
b = g2;
}
}
g1 = x;
g2 = y;
f1 = 2 * a;
f2 = 2 * b;
}
else
{
cout << "the third case: " << num << endl;
long long mixn = 0x3f3f3f3f;
long long x = 0, y = 0, d1 = -9999, d2 = 9999;
if (abs(g1) != abs(g2))
{
d1 = -(f1 + f2) / (g1 + g2);
d2 = (f1 - f2) / (g1 - g2);
if (d1 > d2)
{
long long d = d1;
d1 = d2;
d2 = d;
}
}
if ((d1 - 100) % 2 == 0)
d1 = d1 - 99;
else
d1 = d1 - 100;
for (long long j = d1; j <= d2 + 10; j+=2)
{
long long temp = abs_max(g1 + j*f1, g2 + j*f2);
if (mixn > temp && (g2 + j*f2)>0)
{
mixn = temp;
x = g1 + j*f1;
y = g2 + j*f2;
}
}
g1 = x;
g2 = y;
f1 *= 2;
f2 *= 2;
}
num++;
}
cout << "f1:" << f1 << " " << "f2:" << f2 << " " << "g1:" << g1 << " " << "g2:" << g2 << endl;
system("pause");
inf.close();
return 0;
}
爆破验证算法正确性
#include
#include
#include
#include
#include
#include
#include
#include
#define MAX_L 2005
#define is "0100000000100111111110001000001111111111011100000000111"
using namespace std;
class bign
{
public:
int len, s[MAX_L];//数的长度,记录数组
//构造函数
bign();
bign(const char*);
bign(int);
bool sign;//符号 1正数 0负数
string toStr() const;//转化为字符串,主要是便于输出
friend istream& operator>>(istream &,bign &);//重载输入流
friend ostream& operator<<(ostream &,bign &);//重载输出流
//重载复制
bign operator=(const char*);
bign operator=(int);
bign operator=(const string);
//重载各种比较
bool operator>(const bign &) const;
bool operator>=(const bign &) const;
bool operator<(const bign &) const;
bool operator<=(const bign &) const;
bool operator==(const bign &) const;
bool operator!=(const bign &) const;
//重载四则运算
bign operator+(const bign &) const;
bign operator++();
bign operator++(int);
bign operator+=(const bign&);
bign operator-(const bign &) const;
bign operator--();
bign operator--(int);
bign operator-=(const bign&);
bign operator*(const bign &)const;
bign operator*(const int num)const;
bign operator*=(const bign&);
bign operator/(const bign&)const;
bign operator/=(const bign&);
//四则运算的衍生运算
bign operator%(const bign&)const;//取模(余数)
bign factorial()const;//阶乘
bign Sqrt()const;//整数开根(向下取整)
bign pow(const bign&)const;//次方
//辅助的函数
void clean();
~bign();
};
#define max(a,b) a>b ? a : b
#define min(a,b) a>(istream &in, bign &num)
{
string str;
in>>str;
num=str;
return in;
}
ostream &operator<<(ostream &out, bign &num)
{
out<= 0; i--)
if (s[i] != num.s[i])
return sign ? (s[i] < num.s[i]) : (!(s[i] < num.s[i]));
return !sign;
}
bool bign::operator>(const bign&num)const
{
return num < *this;
}
bool bign::operator<=(const bign&num)const
{
return !(*this>num);
}
bool bign::operator>=(const bign&num)const
{
return !(*this num || *this < num;
}
bool bign::operator==(const bign&num)const
{
return !(num != *this);
}
bign bign::operator+(const bign &num) const
{
if (sign^num.sign)
{
bign tmp = sign ? num : *this;
tmp.sign = 1;
return sign ? *this - tmp : num - tmp;
}
bign result;
result.len = 0;
int temp = 0;
for (int i = 0; temp || i < (max(len, num.len)); i++)
{
int t = s[i] + num.s[i] + temp;
result.s[result.len++] = t % 10;
temp = t / 10;
}
result.sign = sign;
return result;
}
bign bign::operator++()
{
*this = *this + 1;
return *this;
}
bign bign::operator++(int)
{
bign old = *this;
++(*this);
return old;
}
bign bign::operator+=(const bign &num)
{
*this = *this + num;
return *this;
}
bign bign::operator-(const bign &num) const
{
bign b=num,a=*this;
if (!num.sign && !sign)
{
b.sign=1;
a.sign=1;
return b-a;
}
if (!b.sign)
{
b.sign=1;
return a+b;
}
if (!a.sign)
{
a.sign=1;
b=bign(0)-(a+b);
return b;
}
if (a= 0) g = 0;
else
{
g = 1;
x += 10;
}
result.s[result.len++] = x;
}
result.clean();
return result;
}
bign bign::operator * (const bign &num)const
{
bign result;
result.len = len + num.len;
for (int i = 0; i < len; i++)
for (int j = 0; j < num.len; j++)
result.s[i + j] += s[i] * num.s[j];
for (int i = 0; i < result.len; i++)
{
result.s[i + 1] += result.s[i] / 10;
result.s[i] %= 10;
}
result.clean();
result.sign = !(sign^num.sign);
return result;
}
bign bign::operator*(const int num)const
{
bign x = num;
bign z = *this;
return x*z;
}
bign bign::operator*=(const bign&num)
{
*this = *this * num;
return *this;
}
bign bign::operator /(const bign&num)const
{
bign ans;
ans.len = len - num.len + 1;
if (ans.len < 0)
{
ans.len = 1;
return ans;
}
bign divisor = *this, divid = num;
divisor.sign = divid.sign = 1;
int k = ans.len - 1;
int j = len - 1;
while (k >= 0)
{
while (divisor.s[j] == 0) j--;
if (k > j) k = j;
char z[MAX_L];
memset(z, 0, sizeof(z));
for (int i = j; i >= k; i--)
z[j - i] = divisor.s[i] + '0';
bign dividend = z;
if (dividend < divid) { k--; continue; }
int key = 0;
while (divid*key <= dividend) key++;
key--;
ans.s[k] = key;
bign temp = divid*key;
for (int i = 0; i < k; i++)
temp = temp * 10;
divisor = divisor - temp;
k--;
}
ans.clean();
ans.sign = !(sign^num.sign);
return ans;
}
bign bign::operator/=(const bign&num)
{
*this = *this / num;
return *this;
}
bign bign::operator%(const bign& num)const
{
bign a = *this, b = num;
a.sign = b.sign = 1;
bign result, temp = a / b*b;
result = a - temp;
result.sign = sign;
return result;
}
bign bign::pow(const bign& num)const
{
bign result = 1;
for (bign i = 0; i < num; i++)
result = result*(*this);
return result;
}
bign bign::factorial()const
{
bign result = 1;
for (bign i = 1; i <= *this; i++)
result *= i;
return result;
}
void bign::clean()
{
if (len == 0) len++;
while (len > 1 && s[len - 1] == '\0')
len--;
}
bign bign::Sqrt()const
{
if(*this<0)return -1;
if(*this<=1)return *this;
bign l=0,r=*this,mid;
while(r-l>1)
{
mid=(l+r)/2;
if(mid*mid>*this)
r=mid;
else
l=mid;
}
return l;
}
bign::~bign()
{
}
///求出序列长度
/*
int main()
{
ios::sync_with_stdio(false);
ifstream myfile("C:\\Users\\49627\\Desktop\\全国高校密码竞赛\\赛题一\\sequence.txt");
string temp;
if (!myfile.is_open())
{
cout << "open file error!" << endl;
}
int sum = 0;
while(getline(myfile,temp))
{
int len = temp.length();
sum += len;
}
cout<b)
return a;
else
return b;
}
bool judge(bign a, bign b)
{
bign c=a x)
{
abm = x;
i=p;
j=q;
cout<