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<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
using namespace std;
const int N = 2e5+, M = 1e2+, MOD = 1e9+, 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 ;
ll ans = quick_pow(x,p>>);
ans = ans*ans%MOD;
if(p & ) ans = ans*x%MOD;
return ans;
} ll inv(ll x,ll mo)
{
return quick_pow(x,mo-);
} 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 = (x+y )%MOD;
ll allA = ;
for(int i=;i<=m;i++) {
scanf("%lld%lld%lld%lld",&U[i],&D[i],&L[i],&R[i]);
A[i] = (U[i] + R[i] + L[i] + D[i] + ) % MOD;
B[i] = (U[i] + R[i] - L[i] - D[i]) % MOD;
allA = allA * A[i] % MOD;
} ll W = ;
for(int i=;i<=m;i++) W = (W + B[i] * inv(A[i],MOD) ) % MOD;
//cout<<W<<endl;
ll ans = S0 * update(quick_pow(allA, n / m)) % MOD + update(n/m) * W % MOD * quick_pow(allA, n/m)% MOD;
ans %= MOD;
ll sum = quick_pow(allA,n/m);
//cout<<ans<<" "<<sum<<endl;
for(int i=;i<=n%m;i++) {
ans = (ans * (A[i]) % MOD + sum * B[i] % MOD) % MOD;
sum = sum * (A[i]) % MOD;
}
printf("%lld\n",(ans+MOD )%MOD); }
return ;
}

Open judge C16H:Magical Balls 快速幂+逆元的更多相关文章

  1. HDU 5685 Problem A | 快速幂+逆元

    Problem A Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total S ...

  2. hdu5698瞬间移动(杨辉三角+快速幂+逆元)

    瞬间移动 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submis ...

  3. HDU 5868 Different Circle Permutation Burnside引理+矩阵快速幂+逆元

    题意:有N个座位,人可以选座位,但选的座位不能相邻,且旋转不同构的坐法有几种.如4个座位有3种做法.\( 1≤N≤1000000000 (10^9) \). 题解:首先考虑座位不相邻的选法问题,如果不 ...

  4. 51nod 1013【快速幂+逆元】

    等比式子: Sn=(a1-an*q)/(1-q) n很大,搞一发快速幂,除法不适用于取膜,逆元一下(利用费马小定理) 假如p是质数,且gcd(a,p)=1,那么 a^(p-1)≡1(mod p).刚好 ...

  5. 【牛客小白月赛6】F 发电 - 树状数组&快速幂&逆元

    题目地址:https://www.nowcoder.com/acm/contest/136/F 树状数组.快速幂.逆元的模板运用: #include<iostream> #include& ...

  6. 【牛客小白月赛6】 J 洋灰三角 - 快速幂&逆元&数学

    题目地址:https://www.nowcoder.com/acm/contest/136/J 解法一: 推数学公式求前n项和: 当k=1时,即为等差数列,Sn = n+pn(n−1)/2 当k≠1时 ...

  7. HDU 5793 A Boring Question (找规律 : 快速幂+逆元)

    A Boring Question 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5793 Description Input The first l ...

  8. 2016 Asia Jakarta Regional Contest J - Super Sum UVALive - 7720 【快速幂+逆元】

    J-Super Sum 题目大意就是给定N个三元组<a,b,c>求Σ(a1^k1*a2^k2*...*ai^ki*..an^kn)(bi<=ki<=ci) 唉.其实题目本身不难 ...

  9. POJ 1845:Sumdiv 快速幂+逆元

    Sumdiv Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 16466   Accepted: 4101 Descripti ...

随机推荐

  1. 【leetcode】Word Ladder

    Word Ladder Total Accepted: 24823 Total Submissions: 135014My Submissions Given two words (start and ...

  2. C#之键值对

    1.初始化一个键值对 //初始化定义一个键值对,注意最后的括号 Dictionary<int, string> dic = new Dictionary<int, string> ...

  3. php如何妩媚地生成执行的sql语句

    会不会碰到这样一种情况呢?每次获取数据将数据和历史版本都有一定的差别,然而用ThinkPHP的addAll()函数,却会将已有的数据删掉再重新写入.这明显不是我们想要的.但自己写sql每次几十个字段也 ...

  4. 基于Maven构建开发第一个Storm项目

    前面说过了Storm的测试项目,那么此时我们更想自己写一个小项目来练练手,首先我们自己的Windows系统上首先应该安装好maven,然后启动Eclipse for JavaEE版本,接下来开始建立项 ...

  5. ACM/ICPC 之 树形DP(POJ1192)

    将某点看做根状态,邻接点看做子状态,由子状态向根状态转移. POJ1192-最优连通子集 题解:将每一个点分成两个状态进行保存,因此可以构造一个数组dp[i][2]. dp[i][0]:不包括该点权值 ...

  6. percona-toolkit 之 【pt-slave-delay】说明

    摘要: 在主从复制的架构中,正常情况下主上操作的记录也会在从上进行操作,虽说是异步复制,但操作会“实时”的同步到从.比如在主上不小心误操作了,还没等反应过来从上也会马上执行误操作,后期只有通过二进制或 ...

  7. JSON数据格式

    JSON 数据格式 JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.JSON采用完全独立于语言的文本格式,这些特性使JSON成为理想的数据交换语言.易于人 ...

  8. 【linux】jdk安装

    1.在http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html 下载最新版的rpm文件,我 ...

  9. .NET微信公众号开发-1.0初始微信公众号

    一.前言 微信公众号是开发者或商家在微信公众平台上申请的应用账号,该帐号与QQ账号互通,通过公众号,商家可在微信平台上实现和特定群体的文字.图片.语音.视频的全方位沟通.互动 .形成了一 种主流的线上 ...

  10. xmpp笔记2(客户端到服务器的例子)--xml

    xmpp( 客户端到服务器的例子 ) 1 步:客户端初始流给服务器: <stream:stream xmlns='jabber:client' xmlns:stream='http://ethe ...