hdu 5139 Formula (离线)

Problem Description

f(n)=(∏i=1nin−i+1)%1000000007
You are expected to write a program to calculate f(n) when a certain n is given.

 

 

Input

Multi test cases (about 100000), every case contains an integer n in a single line.
Please process to the end of file.

[Technical Specification]
1≤n≤10000000

 

 

Output

For each n,output f(n) in a single line.

 

 

Sample Input

 

2 100

 

 

Sample Output

 

2 148277692

 

 

Source

BestCoder Round #21

 

推出公式:f [n]=f [n-1] * n!

时间复杂度O(n),按理说打表可以过的,但是这种题卡内存,直接打表会MLE

所以要进行离线处理,整体输入、整体操作、整体输出

输入完后排个序,确定打表的精准大小

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f3f;
const int mod=1e9+7;
const int N=1e5+20;

struct node
{
    ll q,ans,id;
}s[N];

bool cmp(node x,node y)
{
    if(x.q!=y.q)
        return x.q<y.q;
}

bool cmp2(node x,node y)
{
    return x.id<y.id;
}

int main()
{
    int tot=0;
    memset(s,0,sizeof(s));
    while(~scanf("%lld",&s[++tot].q))
    {
        s[tot].id=tot;
    }
    tot--;
    sort(s+1,s+tot+1,cmp);
    ll res=1;
    ll tmp=1;
    int cnt=1;
    for(int i=1;i<=s[tot].q;i++)
    {
        tmp=(tmp*i)%mod;
        res=(res*tmp)%mod;
        while(s[cnt].q==i)
        {
            s[cnt].ans=res;
            cnt++;
        }
    }
    sort(s+1,s+tot+1,cmp2);
    for(int i=1;i<=tot;i++)
        cout<<s[i].ans<<'\n';
    return 0;
}

 

 

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
正在热议
更多
# 春招至今,你的战绩如何? #
5015次浏览 47人参与
# 你的实习产出是真实的还是包装的? #
1103次浏览 27人参与
# 米连集团26产品管培生项目 #
4127次浏览 198人参与
# 军工所铁饭碗 vs 互联网高薪资,你会选谁 #
6907次浏览 37人参与
# 简历第一个项目做什么 #
31251次浏览 312人参与
# 当下环境,你会继续卷互联网,还是看其他行业机会 #
186349次浏览 1115人参与
# MiniMax求职进展汇总 #
22891次浏览 293人参与
# 面试紧张时你会有什么表现? #
30371次浏览 188人参与
# 简历中的项目经历要怎么写? #
309379次浏览 4152人参与
# 网易游戏笔试 #
6304次浏览 83人参与
# 职能管理面试记录 #
10687次浏览 59人参与
# 把自己当AI,现在最消耗你token的问题是什么? #
6850次浏览 154人参与
# 从哪些方向判断这个offer值不值得去? #
56698次浏览 357人参与
# 腾讯音乐求职进展汇总 #
160394次浏览 1105人参与
# 小红书求职进展汇总 #
226845次浏览 1356人参与
# AI时代,哪些岗位最容易被淘汰 #
62406次浏览 728人参与
# 你怎么看待AI面试 #
179254次浏览 1163人参与
# 正在春招的你,也参与了去年秋招吗? #
362517次浏览 2631人参与
# 你的房租占工资的比例是多少? #
92123次浏览 896人参与
# 机械求职避坑tips #
94396次浏览 567人参与
# 校招笔试 #
466318次浏览 2950人参与
# 面试官最爱问的 AI 问题是...... #
27111次浏览 834人参与
牛客网
牛客网在线编程
牛客网题解
牛客企业服务