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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

Codeforces Round #450 (Div. 2)D. Unusual Sequences[数论][组合数学][dp II]

發(fā)布時(shí)間:2023/12/6 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Codeforces Round #450 (Div. 2)D. Unusual Sequences[数论][组合数学][dp II] 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

題目:http://codeforces.com/contest/900/problem/D

題意:找到加和為m的且gcd為n的數(shù)列種類數(shù)

分析:可以轉(zhuǎn)化為求gcd為1的加和為m/n的種類數(shù),假設(shè)有m/n個(gè)1,則除了第一個(gè)以外的每個(gè)1可以選擇和前面一項(xiàng)合并,也可以獨(dú)立存在,故不考慮gcd總情況有$2^{(m/n-1)}$。

考慮過加和后,可以刪除gcd為2,3,4……的情況,gcd為2的情況則為2個(gè)相同的gcd為1且加和為n/2的序列組合,3同理。所以找出除1以外的所有x的因子,減去相應(yīng)的情況,使用記憶化搜索。

代碼:

1 #define _CRT_SECURE_NO_DEPRECATE 2 #pragma comment(linker, "/STACK:102400000,102400000") 3 #include<iostream> 4 #include<cstdio> 5 #include<fstream> 6 #include<iomanip> 7 #include<algorithm> 8 #include<cmath> 9 #include<deque> 10 #include<vector> 11 #include<bitset> 12 #include<queue> 13 #include<string> 14 #include<cstring> 15 #include<map> 16 #include<stack> 17 #include<set> 18 #include<functional> 19 #define pii pair<int, int> 20 #define mod 1000000007 21 #define mp make_pair 22 #define pi acos(-1) 23 #define eps 0.00000001 24 #define mst(a,i) memset(a,i,sizeof(a)) 25 #define all(n) n.begin(),n.end() 26 #define lson(x) ((x<<1)) 27 #define rson(x) ((x<<1)|1) 28 #define inf 0x3f3f3f3f 29 typedef long long ll; 30 typedef unsigned long long ull; 31 using namespace std; 32 const int maxn = 1e5 + 5; 33 map<int, ll>dp; 34 ll poww(ll m, int n) 35 { 36 ll ans = 1; 37 ll temp = m%mod; 38 while (n) 39 { 40 if (n & 1) 41 ans *= temp; 42 ans %= mod; 43 temp *= temp; 44 temp %= mod; 45 n >>= 1; 46 } 47 return ans%mod; 48 } 49 ll getans(ll x) 50 { 51 if (dp.count(x))return dp[x]; 52 ll tempans = poww(2, x - 1); 53 for (int i = 2; i*i <= x; ++i) 54 { 55 if (x%i == 0)tempans = (tempans - getans(x / i) + mod) % mod; 56 if (x%i==0&&i*i != x)tempans = (tempans - getans(i) + mod) % mod; 57 } 58 tempans = (tempans - getans(1) + mod) % mod; 59 return dp[x]=tempans; 60 } 61 62 int main() 63 { 64 ios::sync_with_stdio(false); 65 cin.tie(0); cout.tie(0); 66 int i, j, k, m, n; 67 cin >> n >> m; 68 if (m%n) { cout << "0" << endl; return 0; } 69 dp[1] = 1; 70 m /= n; 71 ll ans = getans(m); 72 cout << ans << endl; 73 return 0; 74 }

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

創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)

總結(jié)

以上是生活随笔為你收集整理的Codeforces Round #450 (Div. 2)D. Unusual Sequences[数论][组合数学][dp II]的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

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