题意:求出\(n\)拆分成若干个数使其连乘最大的值

本题是之江学院网络赛的原题,计算规模大一点,看到EMAXX推荐就做了

忘了大一那会是怎么用均值不等式推出结果的(还给老师系列)

结论倒还记得:贪心分解3,不够就用2凑

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<string>
#include<vector>
#include<stack>
#include<queue>
#include<set>
#include<map>
#define rep(i,j,k) for(register int i=j;i<=k;i++)
#define rrep(i,j,k) for(register int i=j;i>=k;i--)
#define erep(i,u) for(register int i=head[u];~i;i=nxt[i])
#define iin(a) scanf("%d",&a)
#define lin(a) scanf("%lld",&a)
#define din(a) scanf("%lf",&a)
#define s0(a) scanf("%s",a)
#define s1(a) scanf("%s",a+1)
#define print(a) printf("%lld",(ll)a)
#define enter putchar('\n')
#define blank putchar(' ')
#define println(a) printf("%lld\n",(ll)a)
#define IOS ios::sync_with_stdio(0)
using namespace std;
const int maxn = 1e6+11;
const int oo = 0x3f3f3f3f;
const double eps = 1e-7;
typedef long long ll;
ll read(){
ll x=0,f=1;register char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
ll fpw(ll a,ll n,ll mod){
ll ans=1;
while(n){
if(n&1) ans=(ans*a)%mod;
n>>=1;
a=(a*a)%mod;
}
return ans;
}
int main(){
int T=read();
const int mod = 1e9+7;
while(T--){
ll n=read();
ll ans=0;
if(n%3==0){
ans=fpw(3,n/3,mod);
}else if(n%3==1){
if(n==1)ans=1;
else ans=fpw(3,n/3-1,mod)*4;//3 1 -> 2 2
}else{
if(n==2)ans=2;
else ans=fpw(3,n/3,mod)*2;//3 2
}
println((ans%mod));
}
return 0;
}

SPOJ - LOCKER 数论 贪心的更多相关文章

  1. Tsinsen A1504. Book(王迪) 数论,贪心

    题目:http://www.tsinsen.com/A1504 A1504. Book(王迪) 时间限制:1.0s   内存限制:256.0MB   Special Judge 总提交次数:359   ...

  2. SPOJ - LOCKER

    SPOJ - LOCKERhttps://vjudge.net/problem/45908/origin暴力枚举2-102 23 34 2 25 2 36 3 37 2 2 38 2 3 39 3 3 ...

  3. 2018.10.31 NOIP模拟 一串数字(数论+贪心)

    传送门 把每一个数aaa质因数分解. 假设a=p1a1∗p2a2∗...∗pkaka=p_1^{a_1}*p_2^{a_2}*...*p_k^{a_k}a=p1a1​​∗p2a2​​∗...∗pkak ...

  4. 2018.10.27 codeforces402D. Upgrading Array(数论+贪心)

    传送门 唉我觉得这题数据范围1e5都能做啊... 居然只出了2000 考完听zxyzxyzxy说我的贪心可以卡但过了? 可能今天本来是0+10+00+10+00+10+0只是运气好T1T1T1骗了10 ...

  5. SPOJ ARCTAN (数论) Use of Function Arctan

    详细的题解见这里. 图片转自上面的博客 假设我们已经推导出来x在处取得最小值,并且注意到这个点是位于两个整点之间的,所以从这两个整数往左右两边枚举b就能找到b+c的最小值. 其实只用往一边枚举就够了, ...

  6. SPOJ GCDEX (数论)

    转载请注明出处,谢谢http://blog.csdn.net/ACM_cxlove?viewmode=contents    by---cxlove 题意:求sigma (gcd (i , j))  ...

  7. Codefoces 432C Prime Swaps(数论+贪心)

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/u011328934/article/details/26094917 题目连接:Codefoces ...

  8. CodeForces 124C Prime Permutation (数论+贪心)

    题意:给定一个字符串,问你能不能通过重排,使得任意一个素数p <= 字符串长度n,并且 任意的 i <= 长度n/素数p,满足s[p] == s[p*i]. 析:很容易能够看出来,只要是某 ...

  9. codeforces 402 D. Upgrading Array(数论+贪心)

    题目链接:http://codeforces.com/contest/402/problem/D 题意:给出一个a串和素数串b .f(1) = 0; p为s的最小素因子如果p不属于b , 否则 . a ...

随机推荐

  1. sql语句查询中exists中为什么要用select 1?

    select * from call_cdr_xz_200609 a where and a.ori_charge<>0 and exists(select 1 from special ...

  2. pandas 中的 多条件分割, list 排序

    main_comment_num_3m and avg_group_order_cnt_12m = 0.863230main_comment_score_1m and avg_group_order_ ...

  3. Luogu 3261 [JLOI2015]城池攻占

    BZOJ 4003 需要实现一个可并堆. 每个点维护一个小根堆,然后一开始把所有骑士加入到它所在的点的小根堆当中,实际上空间是$O(m)$的,然后我们从上到下不断合并这个小根堆,合并完之后如果遇到堆顶 ...

  4. 用CSS3.0画圆

    CSS3.0中有一个border-radius属性,这个属性允许向 div 元素添加圆角边框,也就是div边角不再一直是直角,在CSS3.0中可以做成圆角了,所以我们可以用这个属性用div画一个圆,或 ...

  5. Entity Framework Tutorial Basics(40):Validate Entity

    Validate Entity You can write custom server side validation for any entity. To accomplish this, over ...

  6. Sqlserver中的几把锁和.net中的事务级别

    当数据表被事务锁定后,我们再进行select查询时,需要为with(锁选项)来查询信息,如果不加,select将会被阻塞,直到锁被释放,下面介绍几种SQL的锁选项 SQL的几把锁 NOLOCK(不加锁 ...

  7. SQLServer查询所有子节点

    用CTE递归 ;with f as  ( select * from tab where id=1 union all select a.* from tab as a inner join f as ...

  8. C# 操作Excel基础篇(读取Excel、写入Excel)

    注意事项:Excel的数据表中最多只能储存65535行数据,超出后,需要将数据分割开来进行储存.同时对于Excel中的乱码象限,是由于编码的错误方式导致引起的! 一.读取Excel数据表,获得Data ...

  9. Http Live Streaming 实现iphone在线播放视频[转]

    http://hi.baidu.com/lphack/item/83865611c5f82c8988a956df 本人新手,难免会出错,请各位指点! 最近要做一个项目,是通过iphone来播放工厂摄像 ...

  10. 设计模式07: Bridge 桥接模式(结构型模式)

    Bridge 桥接模式(结构型模式) 抽象与实现 抽象不应该依赖于实现细节,实现细节应该依赖于抽象. 抽象B稳定,实现细节b变化 问题在于如果抽象B由于固有的原因,本身并不稳定,也有可能变化,怎么办? ...