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 ...
随机推荐
- 获取网络IP地址
IPHostEntry iphost = Dns.GetHostEntry(txtDNS.Text);//解析并返回IPHostEntry对象 foreach (IPAddress ip in iph ...
- JavaScript读写脚txt文件
1.cmd切换到“C:\Windows\System32>”下,执行“regsvr32 Scrrun.dll” 2.JavaScript读写txt文本代码如下,注意要发布到服务器上 <!D ...
- cluster集群
http://zwfang09.blog.sohu.com/entry/ Cluster 集群 ,群集 --- 负载均衡 LB ,load-balance --- 高可用集群 HA ,High Ava ...
- struts请求基本类型参数接收
01:导包,web.xml配置 <?xml version="1.0" encoding="UTF-8"?> <web-app version ...
- C#访问PostGreSQL数据库的方法 http://www.jb51.net/article/35643.htm
这次的项目中的一个环节要求我把PostGreSQL数据取出来,然后放到SqlServer里,再去处理分析. http://www.jb51.net/article/35643.htm - 我对Post ...
- Bishop的大作《模式识别与机器学习》Ready to read!
久仰Bishop的大作“Pattern Recognition and Machine Learning”已久,在我的硬盘里已经驻扎一年有余,怎奈惧其页数浩瀚,始终未敢入手.近日看文献,屡屡引用之.不 ...
- 安装 Apache 出现 <OS 10013> 以一种访问权限不允许的方式做了一个访问套接字的尝试
在安装Apache的过程中出现: 仔细查看提示: make_sock: could not bind to address 0.0.0.0:80 恍然大悟,计算机上安装了IIS7,80端口已占用. 打 ...
- windows下nginx和php环境的配置
至于php的配置,与之前博文中使用apache服务器时一样. 对于nginx的配置,来看看如何修改配置文件: #user nobody; worker_processes ; #error_log l ...
- Android扫描文件
扫描文件及文件夹 package com.bwie.demo; import java.io.File; import java.io.FileFilter; import java.util.Arr ...
- 人工智能大数据,公开的海量数据集下载,ImageNet数据集下载,数据挖掘机器学习数据集下载
人工智能大数据,公开的海量数据集下载,ImageNet数据集下载,数据挖掘机器学习数据集下载 ImageNet挑战赛中超越人类的计算机视觉系统微软亚洲研究院视觉计算组基于深度卷积神经网络(CNN)的计 ...