1. 程式人生 > >基於有理逼近演算法的序列密碼的有理分數表示

基於有理逼近演算法的序列密碼的有理分數表示

題目描述

已知一條二元序列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 <iostream>
#include <algorithm>
#include <string>
#include <fstream>
#include <cmath>
#include <sstream>
#include <cassert>
#include <cstring>
#include <fstream>
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 <iostream>
#include <algorithm>
#include <string>
#include <fstream>
#include <cmath>
#include <sstream>
#include <cstring>
#include <fstream>
#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<b ? a : b

bign::bign()
{
    memset(s, 0, sizeof(s));
    len = 1;
    sign = 1;
}

bign::bign(const char *num)
{
    *this = num;
}

bign::bign(int num)
{
    *this = num;
}

string bign::toStr() const
{
    string res;
    res = "";
    for (int i = 0; i < len; i++)
        res = (char)(s[i] + '0') + res;
    if (res == "")
        res = "0";
    if (!sign&&res != "0")
        res = "-" + res;
    return res;
}

istream &operator>>(istream &in, bign &num)
{
    string str;
    in>>str;
    num=str;
    return in;
}

ostream &operator<<(ostream &out, bign &num)
{
    out<<num.toStr();
    return out;
}

bign bign::operator=(const char *num)
{
    memset(s, 0, sizeof(s));
    char a[MAX_L] = "";
    if (num[0] != '-')
        strcpy(a, num);
    else
        for (int i = 1; i < strlen(num); i++)
            a[i - 1] = num[i];
    sign = !(num[0] == '-');
    len = strlen(a);
    for (int i = 0; i < strlen(a); i++)
        s[i] = a[len - i - 1] - 48;
    return *this;
}

bign bign::operator=(int num)
{
    if (num < 0)
        sign = 0, num = -num;
    else
        sign = 1;
    char temp[MAX_L];
    sprintf(temp, "%d", num);
    *this = temp;
    return *this;
}

bign bign::operator=(const string num)
{
    const char *tmp;
    tmp = num.c_str();
    *this = tmp;
    return *this;
}

bool bign::operator<(const bign &num) const
{
    if (sign^num.sign)
        return num.sign;
    if (len != num.len)
        return len < num.len;
    for (int i = len - 1; i >= 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);
}

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<b)
    {
        bign c=(b-a);
        c.sign=false;
        return c;
    }
    bign result;
    result.len = 0;
    for (int i = 0, g = 0; i < a.len; i++)
    {
        int x = a.s[i] - g;
        if (i < b.len) x -= b.s[i];
        if (x >= 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<<sum<<endl;
    myfile.close();
    return 0;
}

///1966000

*/
bign mixn(bign a, bign b)
{
    if(a<0)
        a *= -1;
    if(a>b)
        return a;
    else
        return b;
}

bool judge(bign a, bign b)
{
    bign c=a<b?a:b;
    for(bign i=2; i<=c; ++i)
        if(a%i==0 && b%i==0)
            return false;
    return true;
}


int main()
{
    ios::sync_with_stdio(false);
    int n=0;
    ofstream outf;
    outf.open("output.txt");
    //outf<<"n\tp\tq\tx\tpow(2,n)"<<endl;
    while(n<200)
    {
        bign a=0, b;
        for(int i=0; i<n; ++i)
        {
            bool tt = is[i] - '0';
            a += tt * pow(2, i);
        }
        b = pow(2, n);
        bign p, q, abm=0x3f3f3f3f;
        bign i=0, j=0;
        for(p=-9999999; p<=9999999; ++p)
        {
            cout<<p<<endl;
            for(q=3;q<=9999999;++q)
            {
                if((p-q*a)%b==0 && judge(q, p))
                {
                    bign x = mixn(p, q);
                    if(abm > x)
                    {
                        abm = x;
                        i=p;
                        j=q;
                        cout<<abm<<" "<<p<<" "<<q<<endl;
                    }
                }
            }
        }
        outf<<n<<"\t"<<i<<"\t"<<j<<"\t"<<a<<"\t"<<b<<endl;
        cout<<n<<endl;
        n++;
    }
    outf.close();
    return 0;
}