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

HDU3292【佩尔方程求解&&矩阵快速幂】

任意门:http:acm.hdu.edu.cnshowproblem.php?pid3292Nomoretricks,MrNanguoTimeLimit:30001000

任意门:http://acm.hdu.edu.cn/showproblem.php?pid=3292

No more tricks, Mr Nanguo

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 587    Accepted Submission(s): 400


Problem Description
Now Sailormoon girls want to tell you a ancient idiom story named “be there just to make up the number”. The story can be described by the following words.
In the period of the Warring States (475-221 BC), there was a state called Qi. The king of Qi was so fond of the yu, a wind instrument, that he had a band of many musicians play for him every afternoon. The number of musicians is just a square number.Beacuse a square formation is very good-looking.Each row and each column have X musicians.
The king was most satisfied with the band and the harmonies they performed. Little did the king know that a member of the band, Nan Guo, was not even a musician. In fact, Nan Guo knew nothing about the yu. But he somehow managed to pass himself off as a yu player by sitting right at the back, pretending to play the instrument. The king was none the wiser. But Nan Guo's charade came to an end when the king's son succeeded him. The new king, unlike his father, he decided to divide the musicians of band into some equal small parts. He also wants the number of each part is square number. Of course, Nan Guo soon realized his foolish would expose, and he found himself without a band to hide in anymore.So he run away soon.
After he leave,the number of band is Satisfactory. Because the number of band now would be divided into some equal parts,and the number of each part is also a square number.Each row and each column all have Y musicians.
 

 

Input
There are multiple test cases. Each case contains a positive integer N ( 2 <= N <29). It means the band was divided into N equal parts. The folloing number is also a positive integer K ( K <10^9).
 

 

Output
There may have many positive integers X,Y can meet such conditions.But you should calculate the Kth smaller answer of X. The Kth smaller answer means there are K – 1 answers are smaller than them. Beacuse the answer may be very large.So print the value of X % 8191.If there is no answers can meet such conditions,print “No answers can meet such conditions”.
 

 

Sample Input
2 999888
3 1000001
4 8373
 

 

Sample Output
7181
600
No answers can meet such conditions
 

 

Author
B.A.C
 

 

Source
2010 “HDU-Sailormoon” Programming Contest

 

题意概括:

滥竽充数的故事,一开始所有人可以排成一个 X*X 的方阵, 去掉一个人后 所有人可以排成 N 个 Y*Y 的方阵,

求满足上述条件的第K大的总人数。

 

解题思路:

佩尔方程模板题

可根据关系列出方程: x*x - D*( y*y) = 1;

暴力求出特解;

 

解的递推式为:

Xn  = Xn-1  × X1 + d × Yn-1 ×Y1

Yn  = Xn-1  × Y1 + Yn-1  × X1

 

矩阵快速幂递推:

 

AC code:

 1 #include 
 2 #define INF 0x3f3f3f3f
 3 #define LL long long
 4 using namespace std;
 5 const int MAXN = 2;
 6 const int mod = 8191;
 7 typedef struct
 8 {
 9     int m[MAXN][MAXN];
10 }Matrix;
11 Matrix per, d;
12 int x, y, D;
13 
14 void Find_ans()
15 {
16     y = 1;
17     while(1){
18         x = (int)sqrt(D*y*y+1.0);
19         if(x*x - D*y*y == 1) break;
20         y++;
21     }
22 }
23 
24 void init()
25 {
26     d.m[0][0] = x%mod;
27     d.m[0][1] = D*y%mod;
28     d.m[1][0] = y%mod;
29     d.m[1][1] = x%mod;
30     for(int i = 0; i )
31         for(int j = 0; j )
32         per.m[i][j] = (i==j);
33 }
34 
35 Matrix multi(Matrix a, Matrix b)
36 {
37     Matrix c;
38     for(int i = 0; i )
39     for(int j = 0; j ){
40         c.m[i][j] = 0;
41         for(int k = 0; k )
42             c.m[i][j] += a.m[i][k] * b.m[k][j];
43         c.m[i][j]%=mod;
44     }
45     return c;
46 }
47 
48 Matrix qpow(int k)
49 {
50     Matrix p = d, ans = per;
51     while(k){
52         if(k&1){
53             ans = multi(ans, p);
54             k--;
55         }
56         k>>=1;
57         p = multi(p, p);
58     }
59     return ans;
60 }
61 
62 int main()
63 {
64     int K;
65     while(~scanf("%d %d", &D, &K)){
66         int ad = (int)sqrt(D+0.0);
67         if(ad*ad == D){
68             puts("No answers can meet such conditions");
69             continue;
70         }
71         Find_ans();
72         init();
73         d = qpow(K-1);
74         printf("%d\n", (d.m[0][0]*x%mod+ d.m[0][1]*y%mod)%mod);
75     }
76     return 0;
77 }
View Code

 


推荐阅读
  • 先看官方文档TheJavaTutorialshavebeenwrittenforJDK8.Examplesandpracticesdescribedinthispagedontta ... [详细]
  • [大整数乘法] java代码实现
    本文介绍了使用java代码实现大整数乘法的过程,同时也涉及到大整数加法和大整数减法的计算方法。通过分治算法来提高计算效率,并对算法的时间复杂度进行了研究。详细代码实现请参考文章链接。 ... [详细]
  • JDK源码学习之HashTable(附带面试题)的学习笔记
    本文介绍了JDK源码学习之HashTable(附带面试题)的学习笔记,包括HashTable的定义、数据类型、与HashMap的关系和区别。文章提供了干货,并附带了其他相关主题的学习笔记。 ... [详细]
  • 海马s5近光灯能否直接更换为H7?
    本文主要介绍了海马s5车型的近光灯是否可以直接更换为H7灯泡,并提供了完整的教程下载地址。此外,还详细讲解了DSP功能函数中的数据拷贝、数据填充和浮点数转换为定点数的相关内容。 ... [详细]
  • 在Android开发中,使用Picasso库可以实现对网络图片的等比例缩放。本文介绍了使用Picasso库进行图片缩放的方法,并提供了具体的代码实现。通过获取图片的宽高,计算目标宽度和高度,并创建新图实现等比例缩放。 ... [详细]
  • VScode格式化文档换行或不换行的设置方法
    本文介绍了在VScode中设置格式化文档换行或不换行的方法,包括使用插件和修改settings.json文件的内容。详细步骤为:找到settings.json文件,将其中的代码替换为指定的代码。 ... [详细]
  • Java序列化对象传给PHP的方法及原理解析
    本文介绍了Java序列化对象传给PHP的方法及原理,包括Java对象传递的方式、序列化的方式、PHP中的序列化用法介绍、Java是否能反序列化PHP的数据、Java序列化的原理以及解决Java序列化中的问题。同时还解释了序列化的概念和作用,以及代码执行序列化所需要的权限。最后指出,序列化会将对象实例的所有字段都进行序列化,使得数据能够被表示为实例的序列化数据,但只有能够解释该格式的代码才能够确定数据的内容。 ... [详细]
  • android listview OnItemClickListener失效原因
    最近在做listview时发现OnItemClickListener失效的问题,经过查找发现是因为button的原因。不仅listitem中存在button会影响OnItemClickListener事件的失效,还会导致单击后listview每个item的背景改变,使得item中的所有有关焦点的事件都失效。本文给出了一个范例来说明这种情况,并提供了解决方法。 ... [详细]
  • 如何使用Java获取服务器硬件信息和磁盘负载率
    本文介绍了使用Java编程语言获取服务器硬件信息和磁盘负载率的方法。首先在远程服务器上搭建一个支持服务端语言的HTTP服务,并获取服务器的磁盘信息,并将结果输出。然后在本地使用JS编写一个AJAX脚本,远程请求服务端的程序,得到结果并展示给用户。其中还介绍了如何提取硬盘序列号的方法。 ... [详细]
  • LeetCode笔记:剑指Offer 41. 数据流中的中位数(Java、堆、优先队列、知识点)
    本文介绍了LeetCode剑指Offer 41题的解题思路和代码实现,主要涉及了Java中的优先队列和堆排序的知识点。优先队列是Queue接口的实现,可以对其中的元素进行排序,采用小顶堆的方式进行排序。本文还介绍了Java中queue的offer、poll、add、remove、element、peek等方法的区别和用法。 ... [详细]
  • 本文介绍了OC学习笔记中的@property和@synthesize,包括属性的定义和合成的使用方法。通过示例代码详细讲解了@property和@synthesize的作用和用法。 ... [详细]
  • 本文详细介绍了MySQL表分区的创建、增加和删除方法,包括查看分区数据量和全库数据量的方法。欢迎大家阅读并给予点评。 ... [详细]
  • Go GUIlxn/walk 学习3.菜单栏和工具栏的具体实现
    本文介绍了使用Go语言的GUI库lxn/walk实现菜单栏和工具栏的具体方法,包括消息窗口的产生、文件放置动作响应和提示框的应用。部分代码来自上一篇博客和lxn/walk官方示例。文章提供了学习GUI开发的实际案例和代码示例。 ... [详细]
  • Python爬虫中使用正则表达式的方法和注意事项
    本文介绍了在Python爬虫中使用正则表达式的方法和注意事项。首先解释了爬虫的四个主要步骤,并强调了正则表达式在数据处理中的重要性。然后详细介绍了正则表达式的概念和用法,包括检索、替换和过滤文本的功能。同时提到了re模块是Python内置的用于处理正则表达式的模块,并给出了使用正则表达式时需要注意的特殊字符转义和原始字符串的用法。通过本文的学习,读者可以掌握在Python爬虫中使用正则表达式的技巧和方法。 ... [详细]
  • Android工程师面试准备及设计模式使用场景
    本文介绍了Android工程师面试准备的经验,包括面试流程和重点准备内容。同时,还介绍了建造者模式的使用场景,以及在Android开发中的具体应用。 ... [详细]
author-avatar
茶人2502933107
这个家伙很懒,什么也没留下!
Tags | 热门标签
RankList | 热门文章
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有