热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

暂时不会优化的算法,以后有时间在完善吧。

D-MutiplesonacircleTimeLimit:1000MSMemoryLimit:65535KB64bitIOFormat:%I64d&%I64uSubmitStatu
D - Mutiples on a circle
Time Limit:1000MS     Memory Limit:65535KB     64bit IO Format:%I64d & %I64u
Submit Status

Description

Tom has a necklace with n jewels. There is a number on each jewel. Now Tom wants to select a wonderful chain from the necklace. A chain will be regarded wonderful if the wonderful value of the chain is a multiple of a key number K. Tom gets the wonderful value using this way:He writes down the number on the chain in clockwise order and concatenates them together. In this way, he gets a decimal number which is defined as the wonderful value. 
For example, consider a necklace with 5 jewels and corresponding numbers on the jewels are 9 6 4 2 8 (9 and 8 are in neighborhood). Assume we take K=7, then we can find that only five chains can be multiples of K. They are 42, 28, 896, 42896 and 89642. 

Now Tom wants to know that how many ways he can follow to select a wonderful chain from his necklace.
 

Input

The input contains several test cases, terminated by EOF. 
Each case begins with two integers n( 1 ≤ n ≤ 50000), K(1 ≤ K ≤ 200),the length of the necklace and the key number. 
The second line consists of n integer numbers, the i-th number a i(1 ≤ a i ≤ 1000) indicating the number on the ith jewel. It’s given in clockwise order.
 

Output

For each test case, print a number indicating how many ways Tom can follow to select a wonderful chain.
 

Sample Input

5 7 9 6 4 2 8
 

Sample Output

5
 
举一个例子:7开始的话就有:它是一个环,首尾相接的。
7 79 796 7964 79642 796428 7964285
 
 
思路没问题,算法还不会优化,需要继续学习。
穷举完所有可能然后一一排除,数组多了复杂度就很大。每一个都是N(N+1)/2总共要做N*(N+1)*N/2
必然超时。。。
#include
main()
{int x[100008];
int n,k,i,s,sum,m,z,t;
while(scanf("%d %d",&n,&k)!=EOF)
{z=0;
for(i=1;i<=n;i++)
{
scanf("%d",&x[i]);
}
for(i=n+1;i<=2*n;i++)
{
x[i]=x[i-n];
}
for(m=1;m<=n;m++)
{sum=x[m];
if(sum%k==0)
z++;
for(i=1;i;i++)
{
sum=sum*10+x[m+i];
if(sum%k==0)
z++;
}
}
printf("%d\n",z);}
}

暂时不会优化的算法,以后有时间在完善吧。,,

暂时不会优化的算法,以后有时间在完善吧。


推荐阅读
author-avatar
悶得咪_438
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有