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 ...
随机推荐
- POJ3061 Subsequence 尺取or二分
Description A sequence of N positive integers (10 < N < 100 000), each of them less than or eq ...
- Prufer Code
1069. Prufer Code Time limit: 0.25 secondMemory limit: 8 MB A tree (i.e. a connected graph without c ...
- 【LibreOJ】#6257. 「CodePlus 2017 12 月赛」可做题2
[题意]数列满足an=an-1+an-2,n>=3.现在a1=i,a2=[l,r],要求满足ak%p=m的整数a2有多少个.10^18. [算法]数论(扩欧)+矩阵快速幂 [题解]定义fib(i ...
- mysql查询日期相关的
今天 select * from 表名 where to_days(时间字段名) = to_days(now()); 昨天 SELECT * FROM 表名 WHERE TO_DAYS( NOW( ) ...
- wampserver 虚拟主机
转载:http://blog.csdn.net/knight_quan/article/details/51830683 1.背景: 在进行网站开发的时候,通常需要以http://localhost或 ...
- IntelliJ Idea key shortcuts
>Default explaination Official IntelliJ Idea 常用快捷键列表 Shortcuts Ctrl+Shift + Enter,语句完成 "!&qu ...
- Calendar 日期类介绍
Calendar c = Calendar.getInstance();//创建实例 默认是当前时刻 c.get(Calendar.YEAR); c.get(Calendar.MONTH); c.ge ...
- 【洛谷 P4219】 [BJOI2014]大融合(LCT)
题目链接 维护子树信息向来不是\(LCT\)所擅长的,所以我没搞懂qwq 权当背背模板吧.Flash巨佬的blog里面写了虽然我没看懂. #include <cstdio> #define ...
- 大端小端转换,le32_to_cpu 和cpu_to_le32
字节序 http://oss.org.cn/kernel-book/ldd3/ch11s04.html 小心不要假设字节序. PC 存储多字节值是低字节为先(小端为先, 因此是小端), 一些高级的平台 ...
- elk系列2之multiline模块的使用【转】
preface 上回说道了elk的安装以及kibana的简单搜索语法,还有logstash的input,output的语法,但是我们在使用中发现了一个问题,我们知道,elk是每一行为一个事件,像Jav ...