您的位置:首页 > 其它

NOIP2005普及组 循环

2016-08-05 19:46 232 查看


A1154. 循环

时间限制:1.0s   内存限制:256.0MB  
总提交次数:221  
AC次数:40   平均分:32.71

将本题分享到:
 

      
     

查看未格式化的试题   提交   试题讨论

试题来源

  NOIP2005 普及组

问题描述

  乐乐是一个聪明而又勤奋好学的孩子。他总喜欢探求事物的规律。一天,他突然对数的正整数次幂产生了兴趣。

  众所周知,2的正整数次幂最后一位数总是不断的在重复2,4,8,6,2,4,8,6……我们说2的正整数次幂最后一位的循环长度是4(实际上4的倍数都可以说是循环长度,但我们只考虑最小的循环长度)。类似的,其余的数字的正整数次幂最后一位数也有类似的循环现象:
数字
循环
循环长度
2
2、4、8、6
4
3
3、9、7、1
4
4
4、6
2
5
5
1
6
6
1
7
7、9、3、1
4
8
8、4、2、6
4
9
9、1
2
  这时乐乐的问题就出来了:是不是只有最后一位才有这样的循环呢?对于一个整数n的正整数次幂来说,它的后k位是否会发生循环?如果循环的话,循环长度是多少呢?

  注意:

  1. 如果n的某个正整数次幂的位数不足k,那么不足的高位看做是0。

  2. 如果循环长度是L,那么说明对于任意的正整数a,n的a次幂和a + L次幂的最后k位都相同。

输入格式

  只有一行,包含两个整数n(1 <= n < 10100)和k(1 <= k <= 100),n和k之间用一个空格隔开,表示要求n的正整数次幂的最后k位的循环长度。

输出格式

  t包括一行,这一行只包含一个整数,表示循环长度。如果循环不存在,输出-1。

样例输入

32 2

样例输出

4

数据规模和约定

  对于30%的数据,k <= 4;

  对于全部的数据,k <= 100。

解析:我直接举一个例子进行说明吧:111 3(由于1的循环长度就是1,所以我直接从末两位循环开始)

          我们来看111的末两位循环:

          111  -> 321 -> 631 -> 041 -> 551 -> 161 -> 871 -> 681 -> 591 -> 601 -> 711 

          711处末两位出现循环,循环长度为10,循环节为11-> ...-> 01

          现在我们再来看末三位循环:

          111->...->601 (10个数)

          711->...->201 (201就是601^2的末三位)

          311->...->801 (801就是601^3的末三位)

          911->...->401 (401就是601^4的末三位)

          511->...->001 (001就是601^5的末三位)

          111->...->601 (601就是601^6的末三位)  

          末三位的循环长度就是5*10=50;

          好了,现在来讲具体的做法,并假设我们现在求数字n的后k位循环。

          朴素的做法就是直接求n^2,n^3,n^4.。。。,并判断是否出现循环。但观察上面的演示例子,我们发现可以不必这样,以n=111为例,末两位循环节长度为10,即表示n与(n^10)*n的末两位是相同的。对于末三位的循环,肯定是(m*n^10),即m*(n^10)与n^10的末三位是相同的,m*(n^10)*n的末三位与n相同。 于是,计算末三位循环的时候,我们就直接将n^10作为第一个数,然后每次乘n^10,共乘5次,末三位出现循环,即m=5,所以末三位循环街长度就为5*10=50。

        

代码:(代码与原作者不同 思想一样 建议点开链接参考下原作程序)
<pre style="margin-top: 0px; margin-bottom: 0px; word-wrap: break-word; word-break: break-all; font-family: "YaHei Consolas Hybrid", Consolas, "Lucida Console", "Bitstream Vera Sans Mono", "Courier New", Courier, monospace, 宋体; color: rgb(51, 51, 51); font-size: 14px; background-color: rgba(255, 255, 255, 0.298039);"><code style="margin: 0px; word-wrap: break-word; word-break: break-all; font-family: "YaHei Consolas Hybrid", Consolas, "Lucida Console", "Bitstream Vera Sans Mono", "Courier New", Courier, monospace, 宋体;"><span class="hljs-preprocessor" style="color: rgb(153, 153, 153); font-weight: bold;">#<span class="hljs-keyword" style="color: rgb(51, 51, 51);">include</span> <cstdio></span>
<span class="hljs-preprocessor" style="color: rgb(153, 153, 153); font-weight: bold;">#<span class="hljs-keyword" style="color: rgb(51, 51, 51);">include</span> <cstring></span>
<span class="hljs-preprocessor" style="color: rgb(153, 153, 153); font-weight: bold;">#<span class="hljs-keyword" style="color: rgb(51, 51, 51);">include</span> <iostream></span>

<span class="hljs-keyword" style="font-weight: bold;">using</span> <span class="hljs-keyword" style="font-weight: bold;">namespace</span> <span class="hljs-built_in" style="color: rgb(0, 134, 179);">std</span>;

<span class="hljs-keyword" style="font-weight: bold;">const</span> <span class="hljs-keyword" style="font-weight: bold;">int</span> Maxn=<span class="hljs-number" style="color: rgb(0, 128, 128);">110</span>;

<span class="hljs-keyword" style="font-weight: bold;">int</span> i,l,a[Maxn],now[Maxn],ans[Maxn],last[Maxn],temp[Maxn];
<span class="hljs-keyword" style="font-weight: bold;">char</span> s[Maxn];

<span class="hljs-keyword" style="font-weight: bold;">bool</span> judge(){
<span class="hljs-keyword" style="font-weight: bold;">for</span>(<span class="hljs-keyword" style="font-weight: bold;">int</span> k=<span class="hljs-number" style="color: rgb(0, 128, 128);">1</span>;k<=i;k++)<span class="hljs-keyword" style="font-weight: bold;">if</span>(now[k]!=a[k])<span class="hljs-keyword" style="font-weight: bold;">return</span> <span class="hljs-number" style="color: rgb(0, 128, 128);">0</span>;
<span class="hljs-keyword" style="font-weight: bold;">return</span> <span class="hljs-number" style="color: rgb(0, 128, 128);">1</span>;
}

<span class="hljs-keyword" style="font-weight: bold;">void</span> mul2(<span class="hljs-keyword" style="font-weight: bold;">int</span> c)
{
<span class="hljs-keyword" style="font-weight: bold;">int</span> x=<span class="hljs-number" style="color: rgb(0, 128, 128);">0</span>;
<span class="hljs-keyword" style="font-weight: bold;">for</span>(<span class="hljs-keyword" style="font-weight: bold;">int</span> p=<span class="hljs-number" style="color: rgb(0, 128, 128);">1</span>;p<=ans[<span class="hljs-number" style="color: rgb(0, 128, 128);">0</span>];p++){
ans[p]=ans[p]*c+x;
x=ans[p]/<span class="hljs-number" style="color: rgb(0, 128, 128);">10</span>;
ans[p]%=<span class="hljs-number" style="color: rgb(0, 128, 128);">10</span>;
}
<span class="hljs-keyword" style="font-weight: bold;">while</span>(x><span class="hljs-number" style="color: rgb(0, 128, 128);">0</span>){ans[++ans[<span class="hljs-number" style="color: rgb(0, 128, 128);">0</span>]]=x%<span class="hljs-number" style="color: rgb(0, 128, 128);">10</span>;x/=<span class="hljs-number" style="color: rgb(0, 128, 128);">10</span>;}
}

<span class="hljs-keyword" style="font-weight: bold;">void</span> mul1(<span class="hljs-keyword" style="font-weight: bold;">int</span> a[],<span class="hljs-keyword" style="font-weight: bold;">int</span> b[],<span class="hljs-keyword" style="font-weight: bold;">int</span> c[])
{
<span class="hljs-keyword" style="font-weight: bold;">int</span> i,k,x=<span class="hljs-number" style="color: rgb(0, 128, 128);">0</span>;
c[<span class="hljs-number" style="color: rgb(0, 128, 128);">0</span>]=min(a[<span class="hljs-number" style="color: rgb(0, 128, 128);">0</span>]+b[<span class="hljs-number" style="color: rgb(0, 128, 128);">0</span>]-<span class="hljs-number" style="color: rgb(0, 128, 128);">1</span>,l);
<span class="hljs-keyword" style="font-weight: bold;">for</span>(k=<span class="hljs-number" style="color: rgb(0, 128, 128);">1</span>;k<=c[<span class="hljs-number" style="color: rgb(0, 128, 128);">0</span>];k++)
{
<span class="hljs-keyword" style="font-weight: bold;">for</span>(c[k]=x,i=<span class="hljs-number" style="color: rgb(0, 128, 128);">1</span>;i<=a[<span class="hljs-number" style="color: rgb(0, 128, 128);">0</span>];i++)
<span class="hljs-keyword" style="font-weight: bold;">if</span>(k+<span class="hljs-number" style="color: rgb(0, 128, 128);">1</span>-i>=<span class="hljs-number" style="color: rgb(0, 128, 128);">1</span> && k+<span class="hljs-number" style="color: rgb(0, 128, 128);">1</span>-i<=b[<span class="hljs-number" style="color: rgb(0, 128, 128);">0</span>])c[k]+=a[i]*b[k+<span class="hljs-number" style="color: rgb(0, 128, 128);">1</span>-i];
x=c[k]/<span class="hljs-number" style="color: rgb(0, 128, 128);">10</span>,c[k]%=<span class="hljs-number" style="color: rgb(0, 128, 128);">10</span>;
}
<span class="hljs-keyword" style="font-weight: bold;">if</span>(x)c[++c[<span class="hljs-number" style="color: rgb(0, 128, 128);">0</span>]]=x;
}

<span class="hljs-keyword" style="font-weight: bold;">int</span> main()
{
<span class="hljs-built_in" style="color: rgb(0, 134, 179);">scanf</span>(<span class="hljs-string" style="color: rgb(221, 17, 68);">"%s%d"</span>,s+<span class="hljs-number" style="color: rgb(0, 128, 128);">1</span>,&l);
a[<span class="hljs-number" style="color: rgb(0, 128, 128);">0</span>]=<span class="hljs-built_in" style="color: rgb(0, 134, 179);">strlen</span>(s+<span class="hljs-number" style="color: rgb(0, 128, 128);">1</span>);
ans[<span class="hljs-number" style="color: rgb(0, 128, 128);">1</span>]=<span class="hljs-number" style="color: rgb(0, 128, 128);">1</span>;
ans[<span class="hljs-number" style="color: rgb(0, 128, 128);">0</span>]=<span class="hljs-number" style="color: rgb(0, 128, 128);">1</span>;
<span class="hljs-keyword" style="font-weight: bold;">for</span>(i=<span class="hljs-number" style="color: rgb(0, 128, 128);">1</span>;i<=l;i++)a[i]=s[a[<span class="hljs-number" style="color: rgb(0, 128, 128);">0</span>]-i+<span class="hljs-number" style="color: rgb(0, 128, 128);">1</span>]-<span class="hljs-number" style="color: rgb(0, 128, 128);">48</span>;
<span class="hljs-built_in" style="color: rgb(0, 134, 179);">memcpy</span>(last,a,<span class="hljs-keyword" style="font-weight: bold;">sizeof</span>(a));

<span class="hljs-keyword" style="font-weight: bold;">for</span>(i=<span class="hljs-number" style="color: rgb(0, 128, 128);">1</span>;i<=l;i++){
<span class="hljs-built_in" style="color: rgb(0, 134, 179);">memcpy</span>(last,a,<span class="hljs-keyword" style="font-weight: bold;">sizeof</span>(a));
<span class="hljs-keyword" style="font-weight: bold;">int</span> cur=-<span class="hljs-number" style="color: rgb(0, 128, 128);">1</span>;

<span class="hljs-keyword" style="font-weight: bold;">for</span>(<span class="hljs-keyword" style="font-weight: bold;">int</span> j=<span class="hljs-number" style="color: rgb(0, 128, 128);">1</span>;j<=<span class="hljs-number" style="color: rgb(0, 128, 128);">10</span>;j++){
mul1(last,a,now);
<span class="hljs-keyword" style="font-weight: bold;">if</span>(judge()){
cur=j;
<span class="hljs-keyword" style="font-weight: bold;">break</span>;
}
<span class="hljs-keyword" style="font-weight: bold;">else</span>{
<span class="hljs-keyword" style="font-weight: bold;">for</span>(<span class="hljs-keyword" style="font-weight: bold;">int</span> p=<span class="hljs-number" style="color: rgb(0, 128, 128);">1</span>;p<=now[<span class="hljs-number" style="color: rgb(0, 128, 128);">0</span>];p++)last[p]=now[p];
}
}
<span class="hljs-keyword" style="font-weight: bold;">if</span>(cur!=-<span class="hljs-number" style="color: rgb(0, 128, 128);">1</span>){
mul2(cur);
<span class="hljs-built_in" style="color: rgb(0, 134, 179);">memcpy</span>(a,last,<span class="hljs-keyword" style="font-weight: bold;">sizeof</span>(last));
}
<span class="hljs-keyword" style="font-weight: bold;">else</span>{<span class="hljs-built_in" style="color: rgb(0, 134, 179);">printf</span>(<span class="hljs-string" style="color: rgb(221, 17, 68);">"-1"</span>);<span class="hljs-keyword" style="font-weight: bold;">return</span> <span class="hljs-number" style="color: rgb(0, 128, 128);">0</span>;}
}
<span class="hljs-keyword" style="font-weight: bold;">for</span>(<span class="hljs-keyword" style="font-weight: bold;">int</span> i=ans[<span class="hljs-number" style="color: rgb(0, 128, 128);">0</span>];i>=<span class="hljs-number" style="color: rgb(0, 128, 128);">1</span>;i--)<span class="hljs-built_in" style="color: rgb(0, 134, 179);">printf</span>(<span class="hljs-string" style="color: rgb(221, 17, 68);">"%d"</span>,ans[i]);
}</code>


转自点击打开链接
End。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息