nylg 640 Geometric Sum
Geometric Sum
- 描述
- Compute (a + a^2 + … + a^n) mod m.(a+a2+…an)mod
- 输入
- Three integers a,n,m.
(1≤a,n,m≤10^18)
It ends with EOF. - 输出
- The only integer denotes the result.
- 样例输入
-
2 2 1000000000
- 样例输出
-
6
- 来源
- Lepus
- 矩阵里也求过a+a^2+a^3+a^4.......
-
#include<iostream>
#include<stdio.h>
#include<cstring>
#include<cstdlib>
#include<vector>
using namespace std;
typedef long long LL; LL sum_mod(LL a,LL n,LL p)
{
LL ans=;
n=n%p;
while(n)
{
if(n&)
{
ans=ans+a;
if(ans>=p) ans=ans-p;
}
n=n>>;
a=(a+a)%p;
}
return ans;
}
LL solve(LL a,LL n,LL p)
{
LL p1=a,p2=a,ans,i;
vector<LL>Q;
while(n)
{
ans=(n&);
Q.push_back(ans);
n=n>>;
}
ans=Q.size()-;
for(i=ans;i>=;i--)
{
p1=sum_mod(p1,p2+,p);
p2=sum_mod(p2,p2,p);
if(Q[i]==)
{
p2=sum_mod(p2,a,p);
p1=(p1+p2)%p;
}
}
return p1;
}
int main()
{
LL a,n,p;
while(scanf("%lld%lld%lld",&a,&n,&p)>)
{
if(n==){
printf("%lld\n",a%p);
continue;
}
LL ans=solve(a,n,p);
printf("%lld\n",ans);
}
return ;
}
nylg 640 Geometric Sum的更多相关文章
- 基本概率分布Basic Concept of Probability Distributions 3: Geometric Distribution
PDF version PMF Suppose that independent trials, each having a probability $p$, $0 < p < 1$, o ...
- geometric median
The geometric median of a discrete set of sample points in a Euclidean space is the point minimizing ...
- Codeforces Round #Pi (Div. 2) C. Geometric Progression map
C. Geometric Progression Time Limit: 2 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/5 ...
- 补写:Best Coder #85 1001 Sum(前缀和)
sum Accepts: 640 Submissions: 1744 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/13107 ...
- sum (bestcoder)
sum Accepts: 640 Submissions: 1744 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/13107 ...
- Codeforces Round #Pi (Div. 2) C. Geometric Progression
C. Geometric Progression time limit per test 1 second memory limit per test 256 megabytes input stan ...
- codeforces 789 B. Masha and geometric
链接 B. Masha and geometric depression 题意 给你一个等比数列的首项和公比q,然后给出一个上限l,m个数字,在这个等比数列里,小于l且没有在m个数字里面出现过的可以写 ...
- Java实现 LeetCode 640 求解方程(计算器的加减法计算)
640. 求解方程 求解一个给定的方程,将x以字符串"x=#value"的形式返回.该方程仅包含'+',' - '操作,变量 x 和其对应系数. 如果方程没有解,请返回" ...
- 图神经网络 PyTorch Geometric 入门教程
简介 Graph Neural Networks 简称 GNN,称为图神经网络,是深度学习中近年来一个比较受关注的领域.近年来 GNN 在学术界受到的关注越来越多,与之相关的论文数量呈上升趋势,GNN ...
随机推荐
- paper 25 :SVM支持向量机是什么意思?
转载来源:https://www.zhihu.com/question/21094489 作者:余洋链接:https://www.zhihu.com/question/21094489/answer/ ...
- paper 6:支持向量机系列三:Kernel —— 介绍核方法,并由此将支持向量机推广到非线性的情况。
前面我们介绍了线性情况下的支持向量机,它通过寻找一个线性的超平面来达到对数据进行分类的目的.不过,由于是线性方法,所以对非线性的数据就没有办法处理了.例如图中的两类数据,分别分布为两个圆圈的形状,不论 ...
- java socket 发送文件
客户端: package tt; import java.io.DataOutputStream; import java.io.File; import java.io.FileInputStrea ...
- 夺命雷公狗—angularjs—1—三种数据版定方式
我们在实际开发中几乎都是脱离不掉数据绑定方式的,绑定方法如下所示: <!DOCTYPE html> <html lang="en" ng-app> < ...
- 【《zw版·Halcon与delphi系列原创教程》Halcon图层与常用绘图函数
[<zw版·Halcon与delphi系列原创教程>Halcon图层与常用绘图函数 Halcon的绘图函数,与传统编程vb.c.delphi语言完全不同, 传统编程语言,甚至cad ...
- 用grunt搭建自动化的web前端开发环境实战教程(详细步骤)
用grunt搭建自动化的web前端开发环境实战教程(详细步骤) jQuery在使用grunt,bootstrap在使用grunt,百度UEditor在使用grunt,你没有理由不学.不用!前端自动化, ...
- centos安装apache
查询是否已安装apache: rpm -qa httpd 安装apache: yum install httpd -y 查询: chkconfig httpd on 启动: service http ...
- 兼容IE, Chrome的ajax function
gAjax.js var gAjax = (function () { /* paramObj:{ url: request url, method: GET or POST, encode: cha ...
- Web Token JWT
基于Token的WEB后台认证机制 JSON Web Token in ASP.NET Web API 2 using Owin 翻译:Token Authentication in ASP.NET ...
- iOS 证书申请和使用详解(详细版)
对于iOS开发者来说,apple开发者账号肯定不会陌生.在开发中我们离不开它.下面我简单的为大家分享一下关于iOS开发中所用的证书相关知识. 第一部分:成员介绍 1.Certification(证书) ...