当前位置:网站首页>信息学奥赛一本通 1617:转圈游戏 | 1875:【13NOIP提高组】转圈游戏 | 洛谷 P1965 [NOIP2013 提高组] 转圈游戏
信息学奥赛一本通 1617:转圈游戏 | 1875:【13NOIP提高组】转圈游戏 | 洛谷 P1965 [NOIP2013 提高组] 转圈游戏
2022-07-28 08:27:00 【君义_noip】
【题目链接】
ybt 1617:转圈游戏
ybt 1875:【13NOIP提高组】转圈游戏
洛谷 P1965 [NOIP2013 提高组] 转圈游戏
【题目考点】
1. 取模运算
【解题思路】
第x号小伙伴第一轮后走到 x + m x+m x+m位置,第二轮后走到 x + 2 m x+2m x+2m位置,如果该位置的值超过了n,位置编号要减n,使得位置编号只能在0~n-1范围内。因此 1 0 k 10^k 10k轮后,第x号小伙伴所在的位置为 ( x + 1 0 k ⋅ m ) % n (x+10^k\cdot m)\%n (x+10k⋅m)%n。
已知x、m都小于n。根据同余定理的乘法应用: a ⋅ b % m = ( a % m ⋅ b % m ) % m a\cdot b \% m = (a\%m \cdot b\%m)\%m a⋅b%m=(a%m⋅b%m)%m
有:
( x + 1 0 k ⋅ m ) % n (x+10^k\cdot m)\%n (x+10k⋅m)%n
= ( x % n + ( 1 0 k % n ⋅ m % n ) % n ) % n =(x\%n+(10^k\%n\cdot m\%n)\%n)\%n =(x%n+(10k%n⋅m%n)%n)%n
= ( x + ( 1 0 k % n ⋅ m ) % n ) % n =(x+(10^k\%n\cdot m)\%n)\%n =(x+(10k%n⋅m)%n)%n
主要问题是求 1 0 k % n 10^k\%n 10k%n
k达到 1 0 9 10^9 109,这里必须使用快速幂算法(快速幂算法求幂的复杂度为 O ( l o g n ) O(logn) O(logn),n是指数的大小)。
根据同余定理,有: a b % m = ( a % m ) b a^b\%m = (a\%m)^b ab%m=(a%m)b。在使用快速幂的过程中,对于底数a和结果r,每次运算后都对m取模即可。
【题解代码】
解法1:快速幂
#include<bits/stdc++.h>
using namespace std;
int fastPowMod(int a, int b, int m)//快速幂求a^b%m
{
int r = 1;//结果
while(b > 0)
{
if(b % 2 == 1)
r = (r * a) % m;
a = (a * a) % m;
b /= 2;
}
return r;
}
int main()
{
int n, m, k, x;
cin >> n >> m >> k >> x;
cout << (x + fastPowMod(10, k, n) * m % n) % n;
return 0;
}
边栏推荐
- 3D全景展示新模式,成为破局的关键
- Chapter 2-14 sum integer segments
- Prometheus TSDB analysis
- The chess robot pinched the finger of a 7-year-old boy, and the pot of a software testing engineer? I smiled.
- Post it notes -- 45 {packaging of the uniapp component picker, for data transmission and processing -- Based on the from custom packaging that will be released later}
- Among China's top ten national snacks, it is actually the first
- 台大林轩田《机器学习基石》习题解答和代码实现 | 【你值得拥有】
- Modify virtual machine IP address
- [advanced drawing of single cell] 07. Display of KEGG enrichment results
- [English postgraduate entrance examination vocabulary training camp] day 15 - analyze, general, avoid, surveillance, compared
猜你喜欢

A new method of exposing services in kubernetes clusters

Business digitalization is running rapidly, and management digitalization urgently needs to start

站在大佬的肩膀上,你可以看的更远

Warehouse of multiple backbone versions of yolov5

Machine learning: self paced and fine tuning
![[activity registration] User Group Xi'an - empowering enterprise growth with modern data architecture](/img/92/88be42faf0451cb19067672dab69c8.jpg)
[activity registration] User Group Xi'an - empowering enterprise growth with modern data architecture

【英语考研词汇训练营】Day 15 —— analyst,general,avoid,surveillance,compared

478-82(56、128、718、129)

C #, introductory tutorial -- debugging skills and logical error probe technology and source code when the program is running

Eight ways to solve EMC and EMI conducted interference
随机推荐
[cloud computing] several mistakes that enterprises need to avoid after going to the cloud
When I use MySQL CDC, there are 100 million pieces of data in the source table. In the full volume phase, when I synchronize 10 million, I stop, and then pass
golang 协程的实现原理
2022年危险化学品经营单位安全管理人员上岗证题目及答案
Go interface advanced
Prometheus TSDB analysis
Code management platform SVN deployment practice
Principle of line of sight tracking and explanation of the paper
1299_ Task status and switching test in FreeRTOS
快速上手Flask(一) 认识框架Flask、项目结构、开发环境
Bluetooth technology | it is reported that apple, meta and other manufacturers will promote new wearable devices, and Bluetooth will help the development of intelligent wearable devices
51单片机存储篇:EEPROM(I2C)
DAPP safety summary and typical safety incident analysis
Dry goods semantic web, Web3.0, Web3, metauniverse, these concepts are still confused? (top)
mysql主从架构 ,主库挂掉重启后,从库怎么自动连接主库
象棋机器人夹伤7岁男孩手指,软件测试工程师的锅?我笑了。。。
MDM数据质量应用说明
Hou Jie STL standard library and generic programming
SQL server time field sorting
Hyperlink label