tyvj 1934 高精度

「Poetize3」Heaven Cow与God Bull

From wwwwodddd

背景 Background

__int64 ago,there‘s a heaven cow called sjy...
A god bull named wzc fell in love with her...
As an OI & MOer,wzc gave sjy a quesiton...

描述 Description

给定一个整数n,求一个整数m,满足m<=n,并且m/phi(m)的值最大。
注:phi(m)代表m的欧拉函数,即不大于m且与m互质的数的个数。

输入格式 InputFormat

第一行是一个整数T,表示该测试点有T组数据。
接下来T行,每行一个整数n,意义如上所述。

输出格式 OutputFormat

输出一共T行,每行一个整数m。
若对于某个n,有不止一个满足条件的m,则输出最小的m。

样例输入 SampleInput [复制数据]

1
10

样例输出 SampleOutput [复制数据]

6

数据范围和注释 Hint

对于10%的数据, n<=1000
对于30%的数据, n<=10^10
对于60%的数据, n<=10^2000
对于100%的数据,T<=100,n<=10^25000。

本题只是用来存一个高精类模板。

另外复习一下,phi(x)=x × (1-1/p1)×(1-1/p2) × ... × (1-1/pn)

本题是求 maximize {1/ [(1-1/p1)×(1-1/p2) × ... × (1-1/pn)] }

即 minimize{   (1-1/p1)×(1-1/p2) × ... × (1-1/pn)  }

然后就是水题了。

只是编的时候TLE了很久,原因是素数表开小了。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#include<fstream>
using namespace std;
#define MAXL 10000
#define VAL1 10000
#define MAXN 20100
class number//四位
{
        public:
                number()
                {
                        clear();
                }
                bool is_odd()
                {
                        return numb[0]%2==1;
                }
                bool is_even()
                {
                        return numb[0]%2==0;
                }
                void lsh_bin()
                {
                        int i;
                        for (i=topn;i>0;i--)
                        {
                                if (numb[i]%2==1)
                                {
                                        numb[i-1]+=VAL1;
                                }
                                numb[i]/=2;
                        }
                        numb[0]/=2;
                        while (topn&&!numb[topn])topn--;
                }
                bool equal_to(int x)
                {
                        if (topn==0)
                        {
                                return x==numb[0];
                        }
                        if (topn==1)
                        {
                                return x==numb[0]+numb[1]*VAL1;
                        }
                        return false;
                }
                int size()
                {
                        return topn;
                }
                int length()
                {
                        int x=numb[topn];
                        int ret=0;
                        while (x)
                        {
                                ret++;
                                x/=10;
                        }
                        int y=0;
                        x=VAL1;
                        while (x)
                        {
                                y++;
                                x/=10;
                        }
                        y--;
                        ret+=topn*y;
                        return ret;
                }
                void operator =(int x)//{{{
                {
                        int now=0;
                        clear();
                        numb[now]=x;
                        while (numb[now]>=VAL1)
                        {
                                numb[now+1]+=numb[now]/VAL1;
                                numb[now]%=VAL1;
                                now++;
                                if (now>topn)topn=now;
                        }
                }//}}}
                void operator =(number num)
                {
                        topn=num.topn;
                        memcpy((this->numb),num.numb,sizeof(num.numb[0])*(topn+1));
                }
                void operator +=(number &num)//{{{
                {
                        int i;
                        topn=max(topn,num.topn);
                        for (i=0;i<=topn;i++)
                        {
                                numb[i]+=num.numb[i];;
                                if (numb[i]>=VAL1)
                                {
                                        numb[i+1]+=numb[i]/VAL1;
                                        numb[i]%=VAL1;
                                }
                        }
                        while (numb[topn+1])
                        {
                                topn++;
                                numb[topn+1]+=numb[topn]/VAL1;
                                numb[topn]%=VAL1;
                        }
                }//}}}
                void operator +=(int x)//{{{
                {
                        int now=0;
                        if (topn==-1)topn=0;
                        numb[now]+=x;
                        while (numb[now]>=VAL1)
                        {
                                numb[now+1]+=numb[now]/VAL1;
                                numb[now]%=VAL1;
                                now++;
                                if (now>topn)topn=now;
                        }
                }//}}}
                void operator *=(int x)//{{{
                {
                        int i;
                        for (i=0;i<=topn;i++)
                        {
                                numb[i]*=x;
                        }
                        for (i=0;i<=topn;i++)
                        {
                                if (numb[i]>=VAL1)
                                {
                                        numb[i+1]+=numb[i]/VAL1;
                                        numb[i]%=VAL1;
                                }
                        }
                        while (numb[topn+1])
                        {
                                topn++;
                                numb[topn+1]+=numb[topn]/VAL1;
                                numb[topn]%=VAL1;
                        }
                }//}}}
                void operator -=(number &num)//{{{
                {
                        if (*this<num)throw "Error!\n->void operator -=(number &num)\n";
                        int i;
                        for (i=0;i<=topn;i++)
                        {
                                numb[i]-=num.numb[i];
                        }
                        for (i=0;i<=topn;i++)
                        {
                                while (numb[i]<0)
                                {
                                        numb[i]+=VAL1;
                                        numb[i+1]--;
                                }
                        }
                        while (topn&&!numb[topn])topn--;
                }//}}}
                void operator --(int)//{{{
                {
                        if (topn==0&&numb[0]==0)throw "Error!\n->void operator --(int)\n";
                        int now=0;
                        numb[now]--;
                        while (numb[now]<0)
                        {
                                numb[now+1]--;
                                numb[now]+=VAL1;
                        }
                        while (topn&&!numb[topn])topn--;
                }//}}}
        private:
                int numb[MAXL];
                int topn;
                void clear()
                {
                        topn=0;
                        memset(numb,0,sizeof(numb));

                }
                friend bool operator <(number num1,number num2);
                friend bool operator <=(number num1,number num2);
                friend bool operator ==(number num1,number num2);
                friend ostream& operator <<(ostream &out,number &num);
                friend istream& operator >>(istream &in,number &num);
                friend number operator *(number &num1,number &num2);
                friend number operator *(number num,int x);
                friend number operator +(number num1,number num2);
                //a=a+b远没有a+=b快
};
bool operator <(number num1,number num2)//{{{
{
        if (num1.topn!=num2.topn)
        {
                return num1.topn<num2.topn;
        }
        int i;
        for (i=num1.topn;i>=0;i--)
        {
                if (num1.numb[i]!=num2.numb[i])
                {
                        return num1.numb[i]<num2.numb[i];
                }
        }
        return false;
}//}}}
bool operator <=(number num1,number num2)//{{{
{
        if (num1.topn!=num2.topn)
        {
                return num1.topn<num2.topn;
        }
        int i;
        for (i=num1.topn;i>=0;i--)
        {
                if (num1.numb[i]!=num2.numb[i])
                {
                        return num1.numb[i]<num2.numb[i];
                }
        }
        return true;
}//}}}
bool operator ==(number num1,number num2)//{{{
{
        if (num1.topn!=num2.topn)return false;
        for (int i=0;i<=num1.topn;i++)
        {
                if (num1.numb[i]!=num2.numb[i])return false;
        }
        return true;
}//}}}
ostream& operator <<(ostream &out,number &num)//{{{
{
        int i;
        out<<num.numb[num.topn];
        for (i=num.topn-1;i>=0;i--)
        {
                //压六位时
        //        if (num.numb[i]<100000)out<<"0";
        //        if (num.numb[i]<10000)out<<"0";
                if (num.numb[i]<1000)out<<"0";
                if (num.numb[i]<100)out<<"0";
                if (num.numb[i]<10)out<<"0";
                out<<num.numb[i];
        }
        return out;
}//}}}
istream& operator >>(istream &in,number &num)//{{{
{
        string str;
        in>>str;
        int i;
        num.clear();
        for (i=(int)str.length()-1,num.topn=0;i>=0;i-=4,num.topn++)
        {
                if (i-3<str.length())
                {
                        num.numb[num.topn]=(str[i]-‘0‘)+10*(str[i-1]-‘0‘)+100*(str[i-2]-‘0‘)+1000*(str[i-3]-‘0‘);
                }else
                {
                        if (i-2<str.length())num.numb[num.topn]+=100*(str[i-2]-‘0‘);
                        if (i-1<str.length())num.numb[num.topn]+=10*(str[i-1]-‘0‘);
                        if (i  <str.length())num.numb[num.topn]+=(str[i]-‘0‘);
                }
        }
        num.topn--;
        return in;
}//}}}
number operator *(number num,int x)//{{{
{
        number ret;
        ret=num;
        ret*=x;
        return ret;
}//}}}
number operator +(number num1,number num2)//{{{
{
        number ret;
        ret=num1;
        ret+=num2;
        return ret;
}//}}}
number x,y,z;
bool pflag[400000];
int prime[10200],topp=-1;
void init()
{
        int i,j;
        for (i=2;;i++)
        {
                if (!pflag[i])
                {
                        prime[++topp]=i;
                        if (topp==7100)break;
                }
                for (j=0;j<=topp&&prime[j]*i<400000;j++)
                {
                        pflag[prime[j]*i]=1;
                }
        }
}
struct aaa
{
        number qq;
        int id;
}qur[102];
bool cmp1(aaa a1,aaa a2)
{
        return a1.qq<a2.qq;
}
bool cmp2(aaa a1,aaa a2)
{
        return a1.id<a2.id;
}

bool sved[102];
int main()
{
        //freopen("input.txt","r",stdin);
        //freopen("output.txt","w",stdout);
        int n,i;
        init();
        cin>>n;
        for (i=0;i<n;i++)
        {
                cin>>qur[i].qq;
                qur[i].id=i;
        }
        z=1;
    /*    for (i=0;i<6500;i++)z*=prime[i];
        cout<<z.length()<<endl;;
        for (i=0;i<500000;i++)
        {
                y=z;
        }    */
        sort(qur,&qur[n],cmp1);
        y=1;
        int now=0;
        for(i=0;;i++)
        {
                z=y*prime[i];
                while (qur[now].qq<z)
                {
                        qur[now].qq=y;
                        now++;
                        if (now==n)break;
                }
                if (now==n)break;
                y=z;
        }
        sort(qur,&qur[n],cmp2);
        for (i=0;i<n;i++)
        {
                cout<<qur[i].qq<<endl;
        }
}

tyvj 1934 高精度,布布扣,bubuko.com

时间: 2024-08-10 19:18:08

tyvj 1934 高精度的相关文章

TYVJ 矩阵取数 Label:高精度+dp

题目描述 帅帅经常跟同学玩一个矩阵取数游戏:对于一个给定的n*m的矩阵,矩阵中的每个元素aij均为非负整数.游戏规则如下: 1.每次取数时须从每行各取走一个元素,共n个.m次后取完矩阵所有元素: 2.每次取走的各个元素只能是该元素所在行的行首或行尾: 3.每次取数都有一个得分值,为每行取数的得分之和,每行取数的得分 = 被取走的元素值*2^i,其中i表示第i次取数(从1开始编号): 4.游戏结束总得分为m次取数得分之和. 帅帅想请你帮忙写一个程序,对于任意矩阵,可以求出取数后的最大得分. 输入输

hdu 5718 Oracle 高精度

Oracle Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Problem Description There is once a king and queen, rulers of an unnamed city, who have three daughters of conspicuous beauty. The youngest and most beautifu

问题 A: 【高精度】被限制的加法

问题 A: [高精度]被限制的加法 时间限制: 1 Sec  内存限制: 16 MB提交: 56  解决: 30[提交][状态][讨论版] 题目描述 据关押修罗王和邪狼监狱的典狱长吹嘘,该监狱自一千年前建成以来,尚未有一个囚犯能够成功地越狱.当然这应该要归功于对囚犯们严格的信息管制,例如囚犯们虽然可以自由地使用计算机,但计算机的内存被密码锁设置为仅有100KB大小,显然,在这小得可怜的内存上想编程进行任何大规模的魔法运算,几乎是不可能完成的任务.但修罗王信奉的格言是“一切皆有可能!”,为了破解掉

大数相乘-高精度乘法

一.算法简要描述 给定两个数,相乘如何得到高精度的结果,给定的两个数,不确定是不是浮点数,即可能一个数带多位小数,另一个带小数,或者两个数都带多位小数,或都不带小数,针对这些情况,程序应该都要考虑,所谓的高精度其实就是看两个数的小数位有多少,那么其结果的小数位数应该为两数小数位数之和. 二.算法思路 针对上述描述,其实大部分思路首先想到的应该是用字符串来表示这两个数,带小数点和不带小数点最终都可转换成类似于两个大整数相乘的情况,在最后考虑把小数点放到结果的合适位置即可 三.算法代码 /* two

TYVJ 2049 魔法珠 sg函数

题意:链接 方法:sg函数 解析: tyvj的题大部分都没题解啊- - 不过这样貌似会更好?感觉做这的题都需要自己动脑啊- - 虽然嘴上说着好烦然而心里觉得好评? 回归正题 设sg[x]表示数x的sg值,这好像是废话 然后对于读入的a[i],将所有的a[i]的sg值异或起来如果不是零则先手赢反之后手 维护的时候有个坑. 每次求约数的时候,数组要在sg里开,因为如果递归下去的话,全局变量的话会被更改,会被坑死. 然后就是怎么维护了 对于x,先求约数 之后枚举哪个数不取,将其他的异或(或者先都异或起

如何构建高精度室内定位系统

高精度室内定位需要的技术 室内GIS展示 室内导航技术 高精度定位传感器 高精度定位算法 平面gis 的三维转换技术 6.  平台网络管理 这里的关键是高精度定位传感技术. 可定位的方式gps,北斗gps,蓝牙,空间定位算法

HDU 6206 Apple ( 高精度 &amp;&amp; 计算几何 &amp;&amp; 三点构圆求圆心半径 )

题意 : 给出四个点,问你第四个点是否在前三个点构成的圆内,若在圆外输出"Accepted",否则输出"Rejected",题目保证前三个点不在一条直线上. 分析 : 简单的计算几何问题,如果能够知道圆心和半径(Radius)以及第四个点和圆心的距离(Distance),我们就能够判断第四个点是否在圆外,例如Distance > Radius则在圆外.三点构圆 的圆心和半径是能够推导出公式的 (参考==> http://blog.csdn.net/dea

bzoj 3224: Tyvj 1728 普通平衡树.

3224: Tyvj 1728 普通平衡树 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 15114  Solved: 6570[Submit][Status][Discuss] Description 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作:1. 插入x数2. 删除x数(若有多个相同的数,因只删除一个)3. 查询x数的排名(若有多个相同的数,因输出最小的排名)4. 查询排名为x的数5. 求x的前驱(前驱定义为

codeforces gym 100357 H (DP 高精度)

题目大意 有r*s张扑克牌,数字从1到 r,每种数字有s种颜色. 询问对于所有随机的d张牌,能选出c张组成顺子的概率和组成同花的概率. 解题分析 对于组成顺子的概率,令dp[i][j][k]表示一共选出了i张牌,数字从1~j,最后有k张牌是顺子.对于每个数字进行考虑,有0~s种选法.要保证连续c张牌的顺子. 对于组成同花的概率,令dp[i][j]表示一共选出了i张牌,颜色从1~j,.对于每种颜色进行考虑,有0~r种选法.要保证没有c张牌是相同颜色的. 最后用高精度来输出答案. 参考程序 1 #i