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

OpenjudgeC16H:MagicalBalls快速幂+逆元问题解析

本文主要解析了OpenjudgeC16H问题中涉及到的MagicalBalls的快速幂和逆元算法,并给出了问题的解析和解决方法。详细介绍了问题的背景和规则,并给出了相应的算法解析和实现步骤。通过本文的解析,读者可以更好地理解和解决OpenjudgeC16H问题中的MagicalBalls部分。

C16H:Magical Balls

总时间限制: 
1000ms
内存限制: 
262144kB
描述

Wenwen has a magical ball. When put on an infinite plane, it will keep duplicating itself forever.

Initially, Wenwen puts the ball on the location (x0, y0) of the plane. Then the ball starts to duplicate itself right away. For every unit of time, each existing ball on the plane will duplicate itself, and the new balls will be put on the adjacent locations. The duplication rule of these balls is, during the i-th unit of time, a ball, which locates at (x, y), will duplicate uballs to (x, y+1), dballs to (x, y-1), lballs to (x-1, y) and rballs to (x+1, y).

The duplication rule has a period of M. In another words, ui=ui-M, di=di-M, li=li-M, ri=ri-M, for i=M+1,M+2,...

Wenwen is very happy because she will get many balls. It is easy to calculate how many balls she will get after N units of time. However, she wants to know the sum of x-coordinates and y-coordinates of all balls after N units of time. This is a bit difficult for her. Could you help her? Since the sum might be very large, you should give the sum modulo 1,000,000,007 to her.

输入
The first line contains an integer T (1 ≤ T ≤ 25), indicating the number of test cases.

For each test case:

The first line contains four integers N (1 ≤ N ≤ 10^18), M (1 ≤ M ≤ 20,000), x0 and y0 (-10^18 ≤ x0,y0 ≤ 10^18);

Then follows M lines, the i-th line contains four integers: ui, di, li and ri (0 ≤ ui,di,li,ri ≤ 10,000).
输出
For each test case, output one integer on a single line, indicating the sum of x-coordinates and y-coordinates of all balls after N units of time, modulo 1,000,000,007.
样例输入

1
2 2 1 1
2 0 0 0
0 0 0 1

样例输出

19

提示
In the Sample Input:

Initially, there is 1 ball on (1,1).

After 1 unit of time, there is 1 ball on (1,1) and 2 balls on (1,2);

After 2 units of time, there is 1 ball on (1,1), 2 balls on (1,2), 1 ball on (2,1) and 2 balls on (2,2).
Therefore, after 2 units of time, the sum of x-coordinates and y-coordinates of all balls is
(1+1)*1+(1+2)*2+(2+1)*1+(2+2)*2=19.
题意:
给你一个球 初始位置在x0,y0
和一个周期函数
这个周期是m天  每天向上复制Ui个球 向下复制Di个球 向左复制Li个球,向右复制Ri个球
问你n天后 所有球的横纵坐标相加总和是多少
题解:
Si表示第i天答案的总和, sumi 表示第i天球的总和
S0为初始位置的答案即x+y
设定ai = ui+ri+li+di+1 , bi = ui+ri-li-di;
很容易得出
Si = S0 *  (∏ai)+ ∑j ((∏ai)* bj / a[j]) ; 
  分别用逆元快速幂求解

#include
#include

#include

#include

#include

#include

using namespace std;
const int N = 2e5+20, M = 1e2+10, MOD = 1e9+7, inf = 2e9;
typedef
long long ll;ll update(ll x) {return ((x % MOD)+MOD)%MOD;
}ll quick_pow(ll x,ll p) {
if(!p) return 1;ll ans = quick_pow(x,p>>1);ans = ans*ans%MOD;if(p & 1) ans = ans*x%MOD;return ans;
}ll inv(ll x,ll mo)
{
return quick_pow(x,mo-2);
}
int T;
ll n;
ll m;
ll U[N],D[N],R[N],L[N],A[N],B[N],Y[N];
int main()
{scanf(
"%d",&T);while(T--) {ll x,y;scanf("%lld%lld%lld%lld",&n,&m,&x,&y);ll S0 &#61; (x&#43;y )%MOD;ll allA &#61; 1;for(int i&#61;1;i<&#61;m;i&#43;&#43;) {scanf("%lld%lld%lld%lld",&U[i],&D[i],&L[i],&R[i]);A[i] &#61; (U[i] &#43; R[i] &#43; L[i] &#43; D[i] &#43; 1 ) % MOD;B[i] &#61; (U[i] &#43; R[i] - L[i] - D[i]) % MOD;allA &#61; allA * A[i] % MOD;}ll W &#61; 0;for(int i&#61;1;i<&#61;m;i&#43;&#43;) W &#61; (W &#43; B[i] * inv(A[i],MOD) ) % MOD;//cout<ll ans &#61; S0 * update(quick_pow(allA, n / m)) % MOD &#43; update(n/m) * W % MOD * quick_pow(allA, n/m)% MOD;ans %&#61; MOD;ll sum &#61; quick_pow(allA,n/m);//cout<for(int i&#61;1;i<&#61;n%m;i&#43;&#43;) {ans &#61; (ans * (A[i]) % MOD &#43; sum * B[i] % MOD) % MOD;sum &#61; sum * (A[i]) % MOD;}printf("%lld\n",(ans&#43;MOD )%MOD);}return 0;
}

 


转:https://www.cnblogs.com/zxhl/p/5682667.html



推荐阅读
  • 题目描述:给定n个半开区间[a, b),要求使用两个互不重叠的记录器,求最多可以记录多少个区间。解决方案采用贪心算法,通过排序和遍历实现最优解。 ... [详细]
  • Explore how Matterverse is redefining the metaverse experience, creating immersive and meaningful virtual environments that foster genuine connections and economic opportunities. ... [详细]
  • 使用 Azure Service Principal 和 Microsoft Graph API 获取 AAD 用户列表
    本文介绍了一段通用代码示例,该代码不仅能够操作 Azure Active Directory (AAD),还可以通过 Azure Service Principal 的授权访问和管理 Azure 订阅资源。Azure 的架构可以分为两个层级:AAD 和 Subscription。 ... [详细]
  • 本章将深入探讨移动 UI 设计的核心原则,帮助开发者构建简洁、高效且用户友好的界面。通过学习设计规则和用户体验优化技巧,您将能够创建出既美观又实用的移动应用。 ... [详细]
  • 扫描线三巨头 hdu1928hdu 1255  hdu 1542 [POJ 1151]
    学习链接:http:blog.csdn.netlwt36articledetails48908031学习扫描线主要学习的是一种扫描的思想,后期可以求解很 ... [详细]
  • 本文将介绍如何编写一些有趣的VBScript脚本,这些脚本可以在朋友之间进行无害的恶作剧。通过简单的代码示例,帮助您了解VBScript的基本语法和功能。 ... [详细]
  • 在 Windows 10 中,F1 至 F12 键默认设置为快捷功能键。本文将介绍几种有效方法来禁用这些快捷键,并恢复其标准功能键的作用。请注意,部分笔记本电脑的快捷键可能无法完全关闭。 ... [详细]
  • 技术分享:从动态网站提取站点密钥的解决方案
    本文探讨了如何从动态网站中提取站点密钥,特别是针对验证码(reCAPTCHA)的处理方法。通过结合Selenium和requests库,提供了详细的代码示例和优化建议。 ... [详细]
  • 本文基于刘洪波老师的《英文词根词缀精讲》,深入探讨了多个重要词根词缀的起源及其相关词汇,帮助读者更好地理解和记忆英语单词。 ... [详细]
  • 1.如何在运行状态查看源代码?查看函数的源代码,我们通常会使用IDE来完成。比如在PyCharm中,你可以Ctrl+鼠标点击进入函数的源代码。那如果没有IDE呢?当我们想使用一个函 ... [详细]
  • 深入解析Spring Cloud Ribbon负载均衡机制
    本文详细介绍了Spring Cloud中的Ribbon组件如何实现服务调用的负载均衡。通过分析其工作原理、源码结构及配置方式,帮助读者理解Ribbon在分布式系统中的重要作用。 ... [详细]
  • 本文深入探讨了 Java 中的 Serializable 接口,解释了其实现机制、用途及注意事项,帮助开发者更好地理解和使用序列化功能。 ... [详细]
  • DNN Community 和 Professional 版本的主要差异
    本文详细解析了 DotNetNuke (DNN) 的两种主要版本:Community 和 Professional。通过对比两者的功能和附加组件,帮助用户选择最适合其需求的版本。 ... [详细]
  • 本文介绍如何使用阿里云的fastjson库解析包含时间戳、IP地址和参数等信息的JSON格式文本,并进行数据处理和保存。 ... [详细]
  • 本文介绍如何通过更改软件源来提前体验Ubuntu 8.10,包括详细的配置步骤和相关注意事项。 ... [详细]
author-avatar
SIX2FOUR
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有