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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

LightOJ1298 One Theorem, One Year(DP + 欧拉函数性质)

發(fā)布時間:2023/12/20 编程问答 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 LightOJ1298 One Theorem, One Year(DP + 欧拉函数性质) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

題目

Source

http://www.lightoj.com/volume_showproblem.php?problem=1298

Description

A number is Almost-K-Prime if it has exactly K prime numbers (not necessarily distinct) in its prime factorization. For example, 12 = 2 * 2 * 3 is an Almost-3-Prime and 32 = 2 * 2 * 2 * 2 * 2 is an Almost-5-Prime number. A number X is called Almost-K-First-P-Prime if it satisfies the following criterions:

1. X is an Almost-K-Prime and
2. X has all and only the first P (P ≤ K) primes in its prime factorization.

For example, if K=3 and P=2, the numbers 18 = 2 * 3 * 3 and 12 = 2 * 2 * 3 satisfy the above criterions. And 630 = 2 * 3 * 3 * 5 * 7 is an example of Almost-5-First-4-Pime.

For a given K and P, your task is to calculate the summation of Φ(X) for all integers X such that X is an Almost-K-First-P-Prime.

Input

Input starts with an integer T (≤ 10000), denoting the number of test cases.

Each case starts with a line containing two integers K (1 ≤ K ≤ 500) and P (1 ≤ P ≤ K).

Output

For each case, print the case number and the result modulo 1000000007.

Sample Input

3
3 2
5 4
99 45

Sample Output

Case 1: 10
Case 2: 816
Case 3: 49939643

?

分析

題目大概說,定義,一個數(shù)為Almost-K-First-P-Prime當(dāng)且僅當(dāng)這個數(shù)由K個質(zhì)因子組成,且這K個質(zhì)因子包含且僅包含于前P個質(zhì)數(shù)。給定k和p,求Σphi(AkFpP)。

?

首先要知道歐拉函數(shù)這幾個性質(zhì):

  • φ(p)=p-1(p是質(zhì)數(shù))
  • φ(p*a)=(p-1)*φ(a)(p是質(zhì)數(shù)且p不能整除a)
  • φ(p*a)=p*φ(a)(p是質(zhì)數(shù)且p|a)
  • 然后,可以考慮用DP來解,利用上面的性質(zhì)去轉(zhuǎn)移。

    ?

    一開始我這么想的,首先由于k個質(zhì)因子中那p個是一定要有的,先把它們固定下來,即∏prime[1...p],其phi值為∏(prime[1...p]-1)。然后還剩下k-p個質(zhì)因子要確定,有p個質(zhì)因子可以選擇,這其實就是完全背包問題了:p種物品體積都為1選了之后價值*prime[i],背包容量k-p,問所有選擇方案的價值和。

    不過這樣會TLE的,數(shù)據(jù)有10000組,通過可以枚舉p把所有情況預(yù)處理出來,O(P2K)的時間復(fù)雜度,可能會超時,不過已經(jīng)爆內(nèi)存了。

    ?

    事實上還有更直接的預(yù)處理方式:

    • dp[i][j]表示Σphi(Almost-i-First-j-Prime)

    考慮這么轉(zhuǎn)移:

    • 如果prime[j]只出現(xiàn)一次,那么就是從dp[i-1][j-1](這個狀態(tài)prime[j]不會出現(xiàn),出現(xiàn)的是前j-1個質(zhì)數(shù))通過第i個質(zhì)因子選擇prime[j]轉(zhuǎn)移:dp[i][j]+=dp[i-1][j-1]*(prime[j]-1)
    • 如果prime[j]出現(xiàn)多于一次,那么就是從dp[i-1][j](這個狀態(tài)prime[j]至少出現(xiàn)一次,再加上一次就大于1次了)轉(zhuǎn)移了:dp[i][j]+=dp[i-1][j]*prime[j]

    時間復(fù)雜度就是O(PK)

    其實這種轉(zhuǎn)移的分析方式覺得挺強的,分成等于1、大于等于1,這兩個能分別求出來且互補的子問題。和排隊購票那個經(jīng)典題的轉(zhuǎn)移一樣。

    ?

    下面是那兩個算法的代碼實現(xiàn)。

    ?

    代碼

    O(P2K)

    #include<cstdio> #include<cstring> using namespace std;int prime[555];bool is_prime(int n){for(long long i=2; i*i<=n; ++i){if(n%i==0) return 0;}return 1; }long long d[501][501][500];void init(){int n=0;for(int i=2; n!=500; ++i){if(is_prime(i)){prime[++n]=i;}}for(int p=1; p<=500; ++p){d[p][0][0]=1;for(int i=1; i<=p; ++i){d[p][0][0]*=prime[i]-1;d[p][0][0]%=1000000007;}for(int i=1; i<=p; ++i){for(int j=0; j<500; ++j){if(j) d[p][i][j]=d[p][i-1][j]+d[p][i][j-1]*prime[i];else d[p][i][j]=d[p][i-1][j];d[p][i][j]%=1000000007;}}} }int main(){init();int t,k,p;scanf("%d",&t);for(int cse=1; cse<=t; ++cse){scanf("%d%d",&k,&p);printf("Case %d: %lld\n",cse,d[p][p][k-p]);}return 0; }

    ?

    O(PK)

    #include<cstdio> #include<cstring> using namespace std;int prime[555];bool is_prime(int n){for(long long i=2; i*i<=n; ++i){if(n%i==0) return 0;}return 1; }long long d[555][555];void init(){int n=0;for(int i=2; n!=500; ++i){if(is_prime(i)){prime[++n]=i;}}d[0][0]=1;for(int i=1; i<=500; ++i){for(int j=1; j<=i; ++j){d[i][j]+=d[i-1][j-1]*(prime[j]-1);if(i-1>=j) d[i][j]+=d[i-1][j]*prime[j];d[i][j]%=1000000007;}} }int main(){init();int t,k,p;scanf("%d",&t);for(int cse=1; cse<=t; ++cse){scanf("%d%d",&k,&p);printf("Case %d: %lld\n",cse,d[k][p]);}return 0; }

    ?

    轉(zhuǎn)載于:https://www.cnblogs.com/WABoss/p/5767332.html

    總結(jié)

    以上是生活随笔為你收集整理的LightOJ1298 One Theorem, One Year(DP + 欧拉函数性质)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

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