日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

HDU Problem - 5976 Detachment(逆元,阶乘打表,数学)

發(fā)布時間:2024/4/18 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 HDU Problem - 5976 Detachment(逆元,阶乘打表,数学) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

題目鏈接

Problem Description

In a highly developed alien society, the habitats are almost infinite dimensional space.In the history of this planet,there is an old puzzle.You have a line segment with x units’ length representing one dimension.The line segment can be split into a number of small line segments: a1,a2a1,a2, … (x= a1+a2a1+a2+…) assigned to different dimensions. And then, the multidimensional space has been established. Now there are two requirements for this space: 1.Two different small line segments cannot be equal ( aiajai≠aj when i≠j).2.Make this multidimensional space size s as large as possible (s= a1?a2a1?a2*…).Note that it allows to keep one dimension.That’s to say, the number of ai can be only one.Now can you solve this question and find the maximum size of the space?(For the final number is too large,your answer will be modulo 10^9+7)

Input

The first line is an integer T,meaning the number of test cases.Then T lines follow. Each line contains one integer x.1≤T≤10^6, 1≤x≤10^9

Output

Maximum s you can get modulo 10^9+7. Note that we wants to be greatest product before modulo 10^9+7.

Sample Input

1 4

Sample Output

4

AC

  • 一個數(shù)字分成多個不相同的數(shù)字相乘,首先分成1不合理,因為1乘以一個數(shù)結(jié)果不增加,從2枚舉所有X可以分出的連續(xù)數(shù)字,最后可能會剩余幾個數(shù)字,可以把剩余的數(shù)字看成N個1,剩下任務就是將這N個1分出去,從最后一個數(shù)字開始往前每個數(shù)字加1,這樣能保證連續(xù)的情況下乘積最大,這樣就有兩種情況,剩下的數(shù)字能分完和不能分完(多了個1),例如8 = 2 +3 +3,最后一個3分給前面兩個數(shù)字之后多了個1,這個1就分到最后一個數(shù)字上,例如8 = 2 +3 +2,這樣剛好分完
  • 數(shù)據(jù)規(guī)模大,一共有1e6的詢問,時間要求在2秒,所以每次最好O(1)算出結(jié)果,觀察上面的思路,最后的得到的數(shù)字,就是部分連續(xù)的數(shù)字相乘,可以用階乘打表處理,另外存在相除的情況,所以也要逆元打表,判斷最后一個數(shù)字是否能分完,需要用到sum前綴
#include <iostream> #include <stdio.h> #include <map> #include <vector> #include <set> #include <cstring> #include <cmath> #include <algorithm> #define N 100005 #define ll long long using namespace std; ll mod = 1e9 + 7; ll inv[N], sum[N], jie[N]; void init() {// 逆元打標 inv[1] = 1;for (int i = 2; i < N; ++i) {inv[i] = inv[mod % i] * (mod - mod / i) % mod;}// sum記錄前綴之和 sum[2] = 2;for (int i = 3; i < N; ++i) {sum[i] = sum[i - 1] + i;}// 階乘打表 jie[2] = 2;for (int i = 3; i < N; ++i) {jie[i] = jie[i - 1] * i;jie[i] %= mod;} } int main() { #ifndef ONLINE_JUDGEfreopen("in.txt", "r", stdin); #endifinit();int t;scanf("%d", &t) ;while (t--) {ll x, ans;scanf("%lld", &x);// 找到第一個大于等于sum的位置 int pos = lower_bound(sum, sum + N, x) - sum;while (sum[pos] > x) pos--;int cha = x - sum[pos];// X剛好分完 if (cha == 0) ans = jie[pos];// 多出來pos個1 (不能分完) if (cha == pos) ans = (jie[pos] * inv[2] % mod) * (pos + 2) % mod;// 多出來的1小于pos(可以分完) if (cha < pos)ans = jie[pos + 1] * inv[pos + 1 - cha] % mod;// X小于5,保持原來的長度最好 if (ans == 0) ans = x; printf("%lld\n", ans); }return 0; }

總結(jié)

以上是生活随笔為你收集整理的HDU Problem - 5976 Detachment(逆元,阶乘打表,数学)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。