码迷,mamicode.com
首页 > 其他好文 > 详细

历届试题 买不到的数目(欧几里得 蓝桥杯)

时间:2015-04-04 16:50:37      阅读:220      评论:0      收藏:0      [点我收藏+]

标签:蓝桥杯   欧几里德   历届试题   买不到的数目   

</pre>  历届试题 买不到的数目  </h2><div id="prbinfos" style="font-family: verdana, Arial, Helvetica, sans-serif; font-size: 14px; line-height: 21px;"><h2 class="res" data-mce-style="text-align: center;" style="text-align: center;">时间限制:1.0s   内存限制:256.0MB</h2><div class="btns"><span id="viewrefcodebtn"></span></div></div><div class="des" style="font-family: verdana, Arial, Helvetica, sans-serif; font-size: 14px; line-height: 21px;"><h3 class="sec_header">问题描述</h3><div class="sec_cont"><p style="margin: 10px auto;">小明开了一家糖果店。他别出心裁:把水果糖包成4颗一包和7颗一包的两种。糖果不能拆包卖。</p><p style="margin: 10px auto;">小朋友来买糖的时候,他就用这两种包装来组合。当然有些糖果数目是无法组合出来的,比如要买 10 颗糖。</p><p style="margin: 10px auto;">你可以用计算机测试一下,在这种包装情况下,最大不能买到的数量是17。大于17的任何数字都可以用4和7组合出来。</p><p style="margin: 10px auto;">本题的要求就是在已知两个包装的数量时,求最大不能组合出的数字。</p></div><h3 class="sec_header">输入格式</h3><div class="sec_cont"><p style="margin: 10px auto;">两个正整数,表示每种包装中糖的颗数(都不多于1000)</p></div><h3 class="sec_header">输出格式</h3><div class="sec_cont"><p style="margin: 10px auto;">一个正整数,表示最大不能买到的糖数</p></div><h3 class="sec_header">样例输入1</h3><div class="sec_text">4 7</div><h3 class="sec_header">样例输出1</h3><div class="sec_text">17</div><h3 class="sec_header">样例输入2</h3><div class="sec_text">3 5</div><h3 class="sec_header">样例输出2</h3><div class="sec_text">7</div><div class="sec_text"> </div><div class="sec_text"> </div><div class="sec_text"> </div><h2 class="sec_text"><span data-mce-style="color: #0000ff;" style="color: rgb(0, 0, 255);">interesting:算是一题欧几里得的变形吧,也是求不定方程;用题目的已知数据就是:4x+7y =!C<sub>max</sub>;</span></h2><h2 class="sec_text"><span data-mce-style="color: #0000ff;" style="color: rgb(0, 0, 255);">我们可以这样想,4x+7y=c;    令4x+7y=1;求特解x<sub>0</sub>和y<sub>0</sub> 那么扩大c<sub>min</sub> =min(4,7);</span></h2><h2 class="sec_text"><span data-mce-style="color: #0000ff;" style="color: rgb(0, 0, 255);">然后特解扩大c倍,利用(x=x<sub>0</sub>+7k,y=y<sub>0</sub>-4k)或(</span><span data-mce-style="color: #0000ff;" style="color: rgb(0, 0, 255);">x=x<sub>0</sub>-7k,y=y<sub>0</sub>+4k)在</span>x<sub>0</sub>和y<sub>0</sub> 小于0的时候将一边扩大,然后判断另一边是否大于0;如果大于0,则说明符合条件,那我就记录连续满足条件的数量cnt,如果cnt比c<sub>min </sub>比大,那么,之后的数必定可以被组成,这就是以个题目的技巧;</h2><h2 class="sec_text">我用X表示不能组成,用O表示能组成;eg:(4,7)</h2><p style="margin: 10px auto;"><span data-mce-style="line-height: 1.5;" style="line-height: 1.5;"> </span></p><table class="mceItemTable   " border="0" style="cursor: default; margin: 0px auto; border: 1px dashed rgb(187, 187, 187); border-collapse: collapse;"><tbody><tr><td style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10px; margin: 8px; border: 1px dashed rgb(187, 187, 187); border-collapse: collapse; padding: 3px; cursor: text;"><span data-mce-style="font-size: 14pt;" style="font-size: 14pt;">1</span></td><td style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10px; margin: 8px; border: 1px dashed rgb(187, 187, 187); border-collapse: collapse; padding: 3px; cursor: text;"><span data-mce-style="font-size: 14pt;" style="font-size: 14pt;">2</span></td><td style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10px; margin: 8px; border: 1px dashed rgb(187, 187, 187); border-collapse: collapse; padding: 3px; cursor: text;"><span data-mce-style="font-size: 14pt;" style="font-size: 14pt;">3</span></td><td style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10px; margin: 8px; border: 1px dashed rgb(187, 187, 187); border-collapse: collapse; padding: 3px; cursor: text;"><span data-mce-style="font-size: 14pt;" style="font-size: 14pt;">4</span></td><td style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10px; margin: 8px; border: 1px dashed rgb(187, 187, 187); border-collapse: collapse; padding: 3px; cursor: text;"><span data-mce-style="font-size: 14pt;" style="font-size: 14pt;">5</span></td><td style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10px; margin: 8px; border: 1px dashed rgb(187, 187, 187); border-collapse: collapse; padding: 3px; cursor: text;"><span data-mce-style="font-size: 14pt;" style="font-size: 14pt;">6</span></td><td style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10px; margin: 8px; border: 1px dashed rgb(187, 187, 187); border-collapse: collapse; padding: 3px; cursor: text;"><span data-mce-style="font-size: 14pt;" style="font-size: 14pt;">7</span></td><td style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10px; margin: 8px; border: 1px dashed rgb(187, 187, 187); border-collapse: collapse; padding: 3px; cursor: text;"><span data-mce-style="font-size: 14pt;" style="font-size: 14pt;">8</span></td><td style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10px; margin: 8px; border: 1px dashed rgb(187, 187, 187); border-collapse: collapse; padding: 3px; cursor: text;"><span data-mce-style="font-size: 14pt;" style="font-size: 14pt;">9</span></td><td style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10px; margin: 8px; border: 1px dashed rgb(187, 187, 187); border-collapse: collapse; padding: 3px; cursor: text;"><span data-mce-style="font-size: 14pt;" style="font-size: 14pt;">10</span></td><td style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10px; margin: 8px; border: 1px dashed rgb(187, 187, 187); border-collapse: collapse; padding: 3px; cursor: text;"><span data-mce-style="font-size: 14pt;" style="font-size: 14pt;">11</span></td><td style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10px; margin: 8px; border: 1px dashed rgb(187, 187, 187); border-collapse: collapse; padding: 3px; cursor: text;"><span data-mce-style="font-size: 14pt;" style="font-size: 14pt;">12</span></td><td style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10px; margin: 8px; border: 1px dashed rgb(187, 187, 187); border-collapse: collapse; padding: 3px; cursor: text;"><span data-mce-style="font-size: 14pt;" style="font-size: 14pt;">13</span></td><td style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10px; margin: 8px; border: 1px dashed rgb(187, 187, 187); border-collapse: collapse; padding: 3px; cursor: text;"><span data-mce-style="font-size: 14pt;" style="font-size: 14pt;">14</span></td><td style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10px; margin: 8px; border: 1px dashed rgb(187, 187, 187); border-collapse: collapse; padding: 3px; cursor: text;"><span data-mce-style="font-size: 14pt;" style="font-size: 14pt;">15</span></td><td style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10px; margin: 8px; border: 1px dashed rgb(187, 187, 187); border-collapse: collapse; padding: 3px; cursor: text;"><span data-mce-style="font-size: 14pt;" style="font-size: 14pt;">16</span></td><td style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10px; margin: 8px; border: 1px dashed rgb(187, 187, 187); border-collapse: collapse; padding: 3px; cursor: text;"><span data-mce-style="font-size: 14pt;" style="font-size: 14pt;">17</span></td><td style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10px; margin: 8px; border: 1px dashed rgb(187, 187, 187); border-collapse: collapse; padding: 3px; cursor: text;"><span data-mce-style="font-size: 14pt;" style="font-size: 14pt;">18</span></td><td style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10px; margin: 8px; border: 1px dashed rgb(187, 187, 187); border-collapse: collapse; padding: 3px; cursor: text;"><span data-mce-style="font-size: 14pt;" style="font-size: 14pt;">19</span></td><td style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10px; margin: 8px; border: 1px dashed rgb(187, 187, 187); border-collapse: collapse; padding: 3px; cursor: text;"><span data-mce-style="font-size: 14pt;" style="font-size: 14pt;">20</span></td><td style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10px; margin: 8px; border: 1px dashed rgb(187, 187, 187); border-collapse: collapse; padding: 3px; cursor: text;"><span data-mce-style="font-size: 14pt;" style="font-size: 14pt;">21</span></td><td style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10px; margin: 8px; border: 1px dashed rgb(187, 187, 187); border-collapse: collapse; padding: 3px; cursor: text;"><span data-mce-style="font-size: 14pt;" style="font-size: 14pt;">22</span></td><td style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10px; margin: 8px; border: 1px dashed rgb(187, 187, 187); border-collapse: collapse; padding: 3px; cursor: text;"><span data-mce-style="font-size: 14pt;" style="font-size: 14pt;">24</span></td><td style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10px; margin: 8px; border: 1px dashed rgb(187, 187, 187); border-collapse: collapse; padding: 3px; cursor: text;"><span data-mce-style="font-size: 14pt;" style="font-size: 14pt;">....</span></td></tr><tr><td style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10px; margin: 8px; border: 1px dashed rgb(187, 187, 187); border-collapse: collapse; padding: 3px; cursor: text;"><span data-mce-style="font-size: 14pt;" style="font-size: 14pt;">X</span></td><td style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10px; margin: 8px; border: 1px dashed rgb(187, 187, 187); border-collapse: collapse; padding: 3px; cursor: text;"><span data-mce-style="font-size: 14pt;" style="font-size: 14pt;">X</span></td><td style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10px; margin: 8px; border: 1px dashed rgb(187, 187, 187); border-collapse: collapse; padding: 3px; cursor: text;"><span data-mce-style="font-size: 14pt;" style="font-size: 14pt;">X</span></td><td style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10px; margin: 8px; border: 1px dashed rgb(187, 187, 187); border-collapse: collapse; padding: 3px; cursor: text;"><span data-mce-style="font-size: 14pt;" style="font-size: 14pt;">O</span></td><td style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10px; margin: 8px; border: 1px dashed rgb(187, 187, 187); border-collapse: collapse; padding: 3px; cursor: text;"><span data-mce-style="font-size: 14pt;" style="font-size: 14pt;">X</span></td><td style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10px; margin: 8px; border: 1px dashed rgb(187, 187, 187); border-collapse: collapse; padding: 3px; cursor: text;"><span data-mce-style="font-size: 14pt;" style="font-size: 14pt;">X</span></td><td style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10px; margin: 8px; border: 1px dashed rgb(187, 187, 187); border-collapse: collapse; padding: 3px; cursor: text;"><span data-mce-style="font-size: 14pt;" style="font-size: 14pt;">O</span></td><td style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10px; margin: 8px; border: 1px dashed rgb(187, 187, 187); border-collapse: collapse; padding: 3px; cursor: text;"><span data-mce-style="font-size: 14pt;" style="font-size: 14pt;">O</span></td><td style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10px; margin: 8px; border: 1px dashed rgb(187, 187, 187); border-collapse: collapse; padding: 3px; cursor: text;"><span data-mce-style="font-size: 14pt;" style="font-size: 14pt;">X</span></td><td style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10px; margin: 8px; border: 1px dashed rgb(187, 187, 187); border-collapse: collapse; padding: 3px; cursor: text;"><span data-mce-style="font-size: 14pt;" style="font-size: 14pt;"> X</span></td><td style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10px; margin: 8px; border: 1px dashed rgb(187, 187, 187); border-collapse: collapse; padding: 3px; cursor: text;"><span data-mce-style="font-size: 14pt;" style="font-size: 14pt;"> O</span></td><td style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10px; margin: 8px; border: 1px dashed rgb(187, 187, 187); border-collapse: collapse; padding: 3px; cursor: text;"><span data-mce-style="font-size: 14pt;" style="font-size: 14pt;"> O</span></td><td style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10px; margin: 8px; border: 1px dashed rgb(187, 187, 187); border-collapse: collapse; padding: 3px; cursor: text;"><span data-mce-style="font-size: 14pt;" style="font-size: 14pt;"> X</span></td><td style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10px; margin: 8px; border: 1px dashed rgb(187, 187, 187); border-collapse: collapse; padding: 3px; cursor: text;"><span data-mce-style="font-size: 14pt;" style="font-size: 14pt;"> O</span></td><td style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10px; margin: 8px; border: 1px dashed rgb(187, 187, 187); border-collapse: collapse; padding: 3px; cursor: text;"><span data-mce-style="font-size: 14pt;" style="font-size: 14pt;"> O</span></td><td style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10px; margin: 8px; border: 1px dashed rgb(187, 187, 187); border-collapse: collapse; padding: 3px; cursor: text;"><span data-mce-style="font-size: 14pt;" style="font-size: 14pt;"> O</span></td><td style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10px; margin: 8px; border: 1px dashed rgb(187, 187, 187); border-collapse: collapse; padding: 3px; cursor: text;"><span data-mce-style="font-size: 14pt;" style="font-size: 14pt;"> <span data-mce-style="color: #ff0000;" style="color: rgb(255, 0, 0);">X</span></span></td><td style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10px; margin: 8px; border: 1px dashed rgb(187, 187, 187); border-collapse: collapse; padding: 3px; cursor: text;"><span data-mce-style="font-size: 14pt;" style="font-size: 14pt;"> O</span></td><td style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10px; margin: 8px; border: 1px dashed rgb(187, 187, 187); border-collapse: collapse; padding: 3px; cursor: text;"><span data-mce-style="font-size: 14pt;" style="font-size: 14pt;"> O</span></td><td style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10px; margin: 8px; border: 1px dashed rgb(187, 187, 187); border-collapse: collapse; padding: 3px; cursor: text;"><span data-mce-style="font-size: 14pt;" style="font-size: 14pt;"> O</span></td><td style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10px; margin: 8px; border: 1px dashed rgb(187, 187, 187); border-collapse: collapse; padding: 3px; cursor: text;"><span data-mce-style="font-size: 14pt;" style="font-size: 14pt;"> O</span></td><td style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10px; margin: 8px; border: 1px dashed rgb(187, 187, 187); border-collapse: collapse; padding: 3px; cursor: text;"><span data-mce-style="font-size: 14pt;" style="font-size: 14pt;"> O</span></td><td style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10px; margin: 8px; border: 1px dashed rgb(187, 187, 187); border-collapse: collapse; padding: 3px; cursor: text;"><span data-mce-style="font-size: 14pt;" style="font-size: 14pt;"> O</span></td><td style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10px; margin: 8px; border: 1px dashed rgb(187, 187, 187); border-collapse: collapse; padding: 3px; cursor: text;"><span data-mce-style="font-size: 14pt;" style="font-size: 14pt;">....</span></td></tr></tbody></table><p style="margin: 10px auto;"> </p><h2><span data-mce-style="color: #0000ff;" style="color: rgb(0, 0, 255);">转载请注明出处:<a target=_blank id="lnkBlogLogo" href="http://www.cnblogs.com/yuyixingkong/" data-mce-href="http://www.cnblogs.com/yuyixingkong/"><span data-mce-style="color: #0000ff;" style="color: rgb(0, 0, 255);"><img id="blogLogo" src="http://www.cnblogs.com/Skins/custom/images/logo.gif" alt="返回主页" data-mce-src="http://www.cnblogs.com/Skins/custom/images/logo.gif" style="border: 0px; cursor: default;" /></span></a><span data-mce-style="color: #0000ff;"><a target=_blank id="Header1_HeaderTitle" class="headermaintitle" href="http://www.cnblogs.com/yuyixingkong/" data-mce-href="http://www.cnblogs.com/yuyixingkong/">寻找&星空の孩子</a> </span></span></h2><h3><span data-mce-style="color: #0000ff;" style="color: rgb(0, 0, 255);"><span data-mce-style="color: #0000ff;">题目链接: <a target=_blank href="http://lx.lanqiao.org/problem.page?gpid=T31" target="_blank" data-mce-href="http://lx.lanqiao.org/problem.page?gpid=T31">http://lx.lanqiao.org/problem.page?gpid=T31</a></span></span></h3><p><span data-mce-style="color: #0000ff;" style="color: rgb(0, 0, 255);"></span></p><p><pre name="code" class="cpp">#include<stdio.h>
#define LL long long
#define NN 10000005

bool num[NN];
void exgcd(LL a,LL b,LL &d,LL &x,LL &y)
{
    if(b==0)
    {
        d=a;
        x=1;
        y=0;
        return ;
    }
    else
    {
        exgcd(b,a%b,d,y,x);
        y-=x*(a/b);
    }
}
LL gcd(LL a,LL b)
{
    if(!b)return a;
    else
        gcd(b,a%b);
}
int main()
{
    LL m,n,d,x,y;
    scanf("%I64d%I64d",&m,&n);
    exgcd(m,n,d,x,y);
    LL mmin=m<n?m:n;
    LL i=mmin;
 //   printf("x=%I64d\ty=%I64d\n",x,y);
    if(d==1&&mmin>1);
    {
        int cnt=0;
        while(1)
        {
            if(cnt>=mmin){printf("%I64d\n",i-mmin-1);break;}
            LL xx=i*x;
            LL yy=i*y;
 //           printf("xx=%I64d\tyy=%I64d\n",xx,yy);
            if(xx<0)
            {
                while(xx<0)
                {
                    xx+=n;
                    yy-=m;
                }
                if(yy>=0) cnt++;
                else cnt=0;
            }
            else if(yy<0)
            {
                while(yy<0)
                {
                    xx-=n;
                    yy+=m;
                }
                if(xx>=0) cnt++;
                else cnt=0;
            }
            i++;
        }

    }
    return 0;
}


历届试题 买不到的数目(欧几里得 蓝桥杯)

标签:蓝桥杯   欧几里德   历届试题   买不到的数目   

原文地址:http://blog.csdn.net/u010579068/article/details/44873361

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!