lwjjiayou 2015-11-29 06:05 采纳率: 0%
浏览 1406

C语言 这个好像很简单,但是怎写

Description

There are N stones in a circle, numbered from 0 to N-1. And there are k coins on the kth stone (For example, there are two coins on the stone with number 1). Now, Lasercat wants to collect as many coins as he can. However, he can leap over exactly L stones in a single step, which means he can jump from stone (j mod n) to stone ((j+L) mod n).

Lasercat starts from stone numbered 0 and he can jump as many times as he want. So how many coins can he collect in the end?

Input

The first line contain a single number C (1<=C<=1000) which means the number of test cases.
Then C lines follow, each line has two integers N(1<=N<=1000000) and L(1<=L<=1000000)

Output

For each test case, you should output two lines.
The first line is "Case #:", # means the number of the test case.
The second line output the number of the coins Lasercat can collect.

Sample Input
1
2 1

Sample Output
Case 1:
3

  • 写回答

1条回答 默认 最新

  • Quack_quack 2015-11-29 09:10
    关注

    你好像一直都在问问题,一边说好简单一边问那么多问题,你真的会吗?
    好了,这个题很明显就是看Lx%N能取到哪些数。
    看L和N的数据范围,有可能他们不互质,那么L/=gcd(L,N),N/=gcd(L,N),那么接下来讨论L和N互质的情况。
    L和N互质的时候,Lx%N只能取到N的简化剩余系里面的数。
    N的简化剩余系元素之和是Nphi(N)/2.其中phi(N)是N的欧拉函数。
    欧拉函数的求法就是分解质因数。
    时间复杂度O(sqrt(N)).
    记得最后的答案要乘回去gcd(L,N).
    完了。

    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)