SPOJ - LOCKER 数论 贪心

题意:求出\(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;
}

原文地址:https://www.cnblogs.com/caturra/p/8457025.html

时间: 2024-07-29 05:51:32

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

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

题目:http://www.tsinsen.com/A1504 A1504. Book(王迪) 时间限制:1.0s   内存限制:256.0MB   Special Judge 总提交次数:359   AC次数:97   平均分:43.76 将本题分享到: 查看未格式化的试题   提交   试题讨论 试题来源 2013中国国家集训队第二次作业 问题描述 Wayne喜欢看书,更喜欢买书. 某天Wayne在当当网上买书,买了很多很多书.Wayne有一个奇怪的癖好,就是第一本书的价格必须恰为X,而之后

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

题目连接:Codefoces 432C Prime Swaps 题目大意:给出一个序列,长度为n,要求用5n以内的交换次数使得序列有序,并且交换的i,j两个位置的数时要满足,j?i+1为素数. 解题思路:a数组为对应的序列,b数组为对应的有序序列,p为对应数的位置.每次从有序序列最小的位置开始,该为必须放b[i]才对,所以p[b[i]]=i,否则就要将b[i]尽量往前换,直到换到i的位置为止. 哥德巴赫猜想:任何一个大于5的数都可以写成三个质数之和. #include <cstdio> #in

UVA 1521 - GCD Guessing Game(数论+贪心)

UVA 1521 - GCD Guessing Game 题目链接 题意:一个数字x在1-n之间,现在猜数字,每次猜一个数字a,告知gcd(x, a)的答案,问最坏情况下需要猜几次 思路:在素数上考虑,猜一组素数的乘积的数字,就可以把这些素数组成的数字都猜出来,答案就是组数,这样问题就是如何分组使得组数最小,每次取最后一个,尽量和前面小的合并,就能使得组数最小 代码: #include <cstdio> #include <cstring> #include <algorit

SPOJ MSTICK. Wooden Sticks 贪心 结构体排序

MSTICK - Wooden Sticks There is a pile of n wooden sticks. The length and weight of each stick are known in advance. The sticks are to be processed by a woodworking machine in one by one fashion. It needs some time, called setup time, for the machine

CF735C 数论\平衡树叶子节点的最大深度\贪心\斐波那契\条件归一化

http://codeforces.com/problemset/problem/735/C 题意..采用淘汰赛制..只要打输就退出比赛..而且只有两个选手打过的场数 相差不超过1才能比赛..最后问你..最多打几场比赛能决出冠军 那么这个题的做法是..画图..观察..分析 Tip:首先我们观察未知量的形式..它是一个复合函数的形式..max(决出冠军的所有可能的比赛方式的场数) 那么我们首先要求括号最里面的东西..即符合条件的所有可能的比赛方式的场数 那么我们可以画一画 我一开始为了保证比赛能正

BZOJ 2226: [Spoj 5971] LCMSum( 数论 )

∑lcm(i,n) = ∑ i*n/(i,n) = ∑d|n∑(x,n)=d x*n/d = ∑d|n∑(t,n/d)=1t*n = n∑d|nf(d). f(d)表示1~d中与d互质的数的和, 即f(d) = d*φ(d)/2(d>=2). 然后O(n)筛φ, 每次询问暴力算即可...最大是100w,sqrt(100w)=1000内的质数是168个, 所以复杂度是O(n + T*168), 可以AC  ----------------------------------------------

【BZOJ3850】【HDU4882】ZCC Loves Codefires 数论,叉积,贪心 “再不刷它就土了”系列

转载请注明出处http://blog.csdn.net/vmurder/article/details/42848881 这样如果转载瞎了读者还能看到干净的原文~~ 毕竟是HDU收录的题,,我怀疑它要变土.. 题意: 有n个项目,然后第一行输入损失度,第二行输入耗时 然后安排一下顺序, 每一项的消耗为当前已经做了的(包括此项)总时间*损失度. 求最小总消耗. 首先这道题是贪心,我们可以贪心排序项目. 策略: bool operator < (const YYC &a)const {retur

SPOJ:Decreasing Number of Visible Box(不错的,背包?贪心?)

Shadowman loves to collect box but his roommates woogieman and itman don't like box and so shadowman wants to hide boxes as many as possible. A box can be kept hidden inside of another box if and only if the box in which it will be held is empty and

数论 - Funny scales(SPOJ - SCALE)

Funny scales Problem's Link ---------------------------------------------------------------------------- Mean: 给定两个数n和x,有一个天平,初始时左盘为x,你需要从以下集合中选一些数字来放到两个盘中,使得两个盘相等(note:每个数字只能取一次). analyse: 即: 将X化为3进制: 但是题目说每个3^i必须为1,所以我们需要将X表示成的等式的每一项的系数变为1,这就是本题的关键