2017西安交大ACM小学期数论 [等差数列]
生活随笔
收集整理的這篇文章主要介紹了
2017西安交大ACM小学期数论 [等差数列]
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
等差數列
發布時間: 2017年6月25日 13:42?? 最后更新: 2017年7月3日 09:27?? 時間限制: 1000ms?? 內存限制: 128M
描述給定正整數n,試問存在多少個和為n的等差數列?
當然,等差數列中每一項要為非負整數,且不考慮降序的等差數列。
多組輸入數據(不超過1000組)。
每組數據一個正整數n,1≤n≤109。
每組數據輸出一個數表示答案。
樣例輸入1 4 5 樣例輸出1 5 5 提示對于第一組數據:
4=4
0+4=4
1+3=4
2+2=4
1+1+1+1=4
共存在5個等差數列
題解:這道題目是真的麻煩,公式推導特別多
代碼:
#include <iostream> #include <cmath> #include <cstdio> #include <cstring> #include <map> typedef long long LL; using namespace std; const int MAX = 1e6; map<int,int> vis; using namespace std; LL n; int dfs(int k){if(vis[k] || k > n){return 0;}vis[k] = 1;if(k == 1){return 1;}if(k == 2){return 0;}LL t = 2*n/k;LL r = t / (k-1);if(t % 2 == 0){if(k%2){return r + 1;}else{return r/2 + 1;}}else{if(k%2){return 0;}else{return (r+1)/2;}} } int main(){while(~scanf("%lld",&n)){vis.clear();if(n == 1){puts("2");continue;}if(n == 2){puts("3");continue;}LL s = sqrt(2*n);LL ans = 0;for(LL i = 1;i <= s;i++){if(2*n%i == 0){ans += dfs(i);if(i*i != 2*n)ans += dfs(2*n/i);}} ans += n/2+1;printf("%lld\n",ans);}return 0; }
總結
以上是生活随笔為你收集整理的2017西安交大ACM小学期数论 [等差数列]的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 万字推荐:一站式购齐Switch必备配件
- 下一篇: 海南联合腾讯健康升级“一卡一档”应用 开