Prime Number CodeForces - 359C (属于是数论)
Simon has a prime number x and an array of non-negative integers a1, a2, ..., an.
Simon loves fractions very much. Today he wrote out number on
a piece of paper. After Simon led all fractions to a common denominator and summed them up, he got a fraction: ,
where number t equals xa1 + a2 + ... + an.
Now Simon wants to reduce the resulting fraction.
Help him, find the greatest common divisor of numbers s and t. As GCD can be rather large, print it as a remainder after dividing it by number 1000000007 (109 + 7).
The first line contains two positive integers n and x (1 ≤ n ≤ 105, 2 ≤ x ≤ 109)
— the size of the array and the prime number.
The second line contains n space-separated integers a1, a2, ..., an (0 ≤ a1 ≤ a2 ≤ ... ≤ an ≤ 109).
Print a single number — the answer to the problem modulo 1000000007 (109 + 7).
2 2
2 2
8
3 3
1 2 3
27
2 2
29 29
73741817
4 5
0 0 0 0
1
In the first sample . Thus, the answer to the problem is 8.
In the second sample, . The answer to the problem is 27,
as351 = 13·27, 729 = 27·27.
In the third sample the answer to the problem is 1073741824 mod 1000000007 = 73741817.
In the fourth sample . Thus, the answer to the problem is 1.
#include<queue>
#include<stack>
#include<vector>
#include<math.h>
#include<stdio.h>
#include<numeric>//STL数值算法头文件
#include<stdlib.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<functional>//模板类头文件
using namespace std; const long long INF=1e9+7;
const long long maxn=101000; long long n,x;
long long a[maxn]; long long quick_mod(long long a,long long b)
{
long long ans=1;
a=a%INF;
while(b)
{
if(b&1)
ans=ans*a%INF;
a=a*a%INF;
b>>=1;
}
return ans;
} int main()
{
while(~scanf("%d %d",&n,&x))
{
long long sum1=0;
for(long long i=0; i<n; i++)
{
scanf("%d",&a[i]);
sum1+=a[i];
}
for(long long i=0; i<n; i++)
a[i]=sum1-a[i];
sort(a,a+n);
long long ans,j=1,cot=1,t;
for(j=1; j<=n; j++)
{
if(a[j]!=a[j-1])
{
if(cot%x)
{
ans=a[j-1];
break;
}
else
{
cot/=x;
a[j-1]+=1;
j--;
}
}
else cot++;
}
printf("%d\n",quick_mod(x,min(ans,sum1)));
}
return 0;
} #include<queue>
#include<stack>
#include<vector>
#include<math.h>
#include<stdio.h>
#include<numeric>//STL数值算法头文件
#include<stdlib.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<functional>//模板类头文件
using namespace std; const long long INF=1e9+7;
const long long maxn=101000; long long n,x;
long long a[maxn]; long long gcd(long long a,long long b)
{
long long ans=1;
ans=ans%INF;
while(b)
{
if(b&1)
ans=ans*a%INF;
a=a*a%INF;
b>>=1;
}
return ans;
} int main()
{
while(~scanf("%I64d %I64d",&n,&x))
{
long long sum1=0;
for(long long i=0; i<n; i++)
{
scanf("%I64d",&a[i]);
sum1+=a[i];
}
for(long long i=0; i<n; i++)
a[i]=sum1-a[i];
sort(a,a+n);
long long ans,j=1,cot=1,t;
while(j<=n)
{
if(a[j]!=a[j-1])
{
if(cot%x)
{
ans=a[j-1];
break;
}
long long f=a[j-1]+1;
t=cot/x;
for(long long k=j-1,s=t; s>0; s--,k--)
a[k]=f;
j-=t;
j++;
cot=1;
}
else cot++,j++;
}
printf("%I64d\n",gcd(x,min(ans,sum1)));
}
return 0;
}
Prime Number CodeForces - 359C (属于是数论)的更多相关文章
- CodeForce 359C Prime Number
Prime Number CodeForces - 359C Simon has a prime number x and an array of non-negative integers a1, ...
- Relatively Prime Powers CodeForces - 1036F (莫比乌斯函数容斥)
Relatively Prime Powers CodeForces - 1036F Consider some positive integer xx. Its prime factorizatio ...
- FZU 1649 Prime number or not米勒拉宾大素数判定方法。
C - Prime number or not Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%I64d & % ...
- 每日一九度之 题目1040:Prime Number
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:6732 解决:2738 题目描述: Output the k-th prime number. 输入: k≤10000 输出: The k- ...
- LintCode-Kth Prime Number.
Design an algorithm to find the kth number such that the only prime factors are 3, 5, and 7. The eli ...
- 10 001st prime number
这真是一个耗CPU的运算,怪不得现在因式分解和素数查找现在都用于加密运算. By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13 ...
- [LeetCode] Prime Number of Set Bits in Binary Representation 二进制表示中的非零位个数为质数
Given two integers L and R, find the count of numbers in the range [L, R] (inclusive) having a prime ...
- [Swift]LeetCode762. 二进制表示中质数个计算置位 | Prime Number of Set Bits in Binary Representation
Given two integers L and R, find the count of numbers in the range [L, R] (inclusive) having a prime ...
- 10_ for 练习 _ is Prime Number ?
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...
随机推荐
- LightOJ 1089 - Points in Segments (II) 线段树区间修改+离散化
http://www.lightoj.com/volume_showproblem.php?problem=1089 题意:给出许多区间,查询某个点所在的区间个数 思路:线段树,由于给出的是区间,查询 ...
- k8s+docker学习连接汇总
http://guide.daocloud.io/dcs/docker-9153982.html http://www.dczou.com/viemall/802.html https://wangl ...
- JAVA中反射机制四
声明:如需转载请说明地址来源:http://www.cnblogs.com/pony1223 反射四 利用反射获取类的属性 1.通过反射也可以获取到类中的属性,假设我们继续使用Person这个类,然后 ...
- LintCode 402: Continuous Subarray Sum
LintCode 402: Continuous Subarray Sum 题目描述 给定一个整数数组,请找出一个连续子数组,使得该子数组的和最大.输出答案时,请分别返回第一个数字和最后一个数字的下标 ...
- Java面试中常问的Spring方面问题(涵盖七大方向共55道题,含答案)
1.一般问题 1.1. 不同版本的 Spring Framework 有哪些主要功能? VersionFeatureSpring 2.5发布于 2007 年.这是第一个支持注解的版本.Spring 3 ...
- 商城项目(ssm+dubbo+nginx+mysql统合项目)总结(4)
我不会在这里贴代码和详细步骤什么的,我觉得就算我把它贴出来,你们照着步骤做还是会出很多问题,我推荐你们去看一下黑马的这个视频,我个人感觉很不错,一步一步走下来可以学到很多东西.另外,视频和相关文档的话 ...
- 使用 Xtrabackup 在线对MySQL做主从复制【转】
1. 说明 1.1 xtrabackup mysqldump对于导出10G以下的数据库或几个表,还是适用的,而且更快捷.一旦数据量达到100-500G,无论是对原库的压力还是导出的性能,mysqldu ...
- Tutorial 2: Requests and Responses
转载自:http://www.django-rest-framework.org/tutorial/2-requests-and-responses/ Tutorial 2: Requests and ...
- C/C++——库函数strcpy和strdup比较
版权声明:原创文章,禁止转载. 1. strcpy 原型: extern char *strcpy(char *dest,char *src); 用法: #include <string.h&g ...
- csu 1803(余数分类)
1803: 2016 Time Limit: 5 Sec Memory Limit: 128 MBSubmit: 565 Solved: 364[Submit][Status][Web Board ...