[JZOJ5773]【NOIP2008模拟】简单数学题
233考场打的大暴力拿了65分。
既没有判断素数又没有打表,竟然暴力拿这么多分真是不敢相信。
放上考场yy代码:
#include <iostream> #include <cstdio> #include <cstring> #include <queue> #include <cmath> #include <algorithm> using namespace std; #define int long long inline int read() { int res=0;char c=getchar();bool f=0; while(!isdigit(c)) {if(c=='-')f=1;c=getchar();} while(isdigit(c))res=(res<<3)+(res<<1)+(c^48),c=getchar(); return f?-res:res; } int tmp[10000005], cnt; signed main(){ freopen("math.in", "r", stdin); freopen("math.out", "w", stdout); int n = read(); if (n == 1) return puts("0"), 0; int ans = 0; if (n <= 6e7) { for (register int i = 1 ; i < n ; i ++) if ((n - i / 2) % (n - i) == 0 and (i % 2) == 0) tmp[++ans] = i; printf("%lld ", ans); for (register int j = 1 ; j <= ans ; j ++) printf("%lld ", tmp[j]); return 0; } int tt = n - 3 * sqrt(n); tt = max(tt, (int)0); if (tt % 2 == 1) tt--; for (register int i = tt ; i < n ; i += 2) if ((n - i / 2) % (n - i) == 0) tmp[++ans] = i; printf("%lld ", ans); for (register int i = 1 ; i <= ans ; i ++) printf("%lld ", tmp[i]); return 0; }
其实它给的暴力分是40分(逃。。
然后聊聊正解233.
设...算了不写了233.
放个网址就跑题解
然后有不少细节要注意,比如枚举i的时候一定要判断是不是奇数,和最后答案是不是奇数。
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace std; #define int long long inline int read() { int res=0;char c=getchar();bool f=0; while(!isdigit(c)) {if(c=='-')f=1;c=getchar();} while(isdigit(c))res=(res<<3)+(res<<1)+(c^48),c=getchar(); return f?-res:res; } int tmp[2*10000005], cnt; signed main() { int n = read(); n <<= 1; int ans = 0; for (register int i = 2 ; i <= sqrt(n) ; i ++) { if (n % i == 0) { if (i % 2 == 1) tmp[++ans] = n / i * max(((i - 1) / 2), (int)1); int t = n / i; if (i != t and t % 2 == 1) tmp[++ans] = n / t * max(((t - 1) / 2), (int)1); } } int tt = ans; sort(tmp + 1, tmp + 1 + ans); for (register int i = 1 ; i <= tt ; i ++) if (tmp[i] % 2 == 1) tmp[i] = 0, ans--; printf("%lld ", ans); for (register int i = 1 ; i <= tt ; i ++) if(tmp[i]) printf("%lld ", tmp[i]); return 0; }