Authored by CFhM_R, 2011 - 2017, All Rights Reserved.
[[email protected]](my mail)
#include
#include
#include
using namespace std;
typedef long long LL;
const LL mod = 1e9 + 7;
const int maxn = 446;
LL C[maxn][maxn];
struct Matrix
{
LL m[maxn][maxn];
Matrix(){memset(m, 0, sizeof(m));}
void E(){memset(m, 0, sizeof(m)); for(int i = 0; i < maxn; i++) m[i][i] = 1;}
};
Matrix M_mul(Matrix a, Matrix b, int r)
{
Matrix ret;
for(int i = 0; i <= r; i++)
for(int k = 0; k <= r; k++)
if(a.m[i][k])
for(int j = 0; j <= r; j++)
if(b.m[k][j])
ret.m[i][j] = ( ret.m[i][j] + a.m[i][k] * b.m[k][j]) % mod;
return ret;
}
Matrix M_qpow(Matrix P, LL n, int r)
{
Matrix ret;
ret.E();
while(n)
{
if(n & 1LL) ret = M_mul(ret, P, r);
n >>= 1LL;
P = M_mul(P, P, r);
}
return ret;
}
int main(void)
{
for(int i = 0; i < maxn; i++) C[i][0] = C[i][i] = 1;
for(int i = 2; i < maxn; i++)
for(int j = 1; j <= i; j++)
C[i][j] = (C[i-1][j-1] + C[i-1][j]) % mod;
int T;
scanf("%d", &T);
while(T--)
{
int N, a, r;
scanf("%d %d %d", &N, &a, &r);
Matrix M;
for(int i = 0; i <= r; i++)
for(int j = 0; j <= i; j++)
M.m[i][j] = C[i][j] * a % mod;
M.m[r+1][r] = M.m[r+1][r+1] = 1;
M = M_qpow(M, N, r + 1);
LL ans = 0LL;
for(int i = 0; i <= r; i++) ans = (ans + M.m[r+1][i] * a) % mod;
printf("%lld\n", ans);
}
return 0;
}
int main() {
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
long _begin_time = clock();
#endif
ll a, b, w, x, c;
scan(a, b, w); scan(x, c);
ll ans = 0LL;
if(c > a) ans = ceil(1.0 * ((c - a) * x - b) / (w - x)) + c - a;
dbg(ans);
#ifndef ONLINE_JUDGE
long _end_time = clock();
printf("time = %ld ms\n", _end_time - _begin_time);
#endif
return 0;
}
void gao(vi &vec, int pos, ll &ans) {
vi vec1, vec2;
for(auto v : vec) {
if(v & (1 << pos)) vec1.pb(v);
else vec2.pb(v);
}
ans += (ll)(1LL << pos) * vec1.size() * vec2.size() % MOD;
if(pos >= 28) return;
if(vec1.size() > 0) gao(vec1, pos + 1, ans); //剪枝很重要
if(vec2.size() > 0) gao(vec2, pos + 1, ans);
}
int main() {
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
long _begin_time = clock();
#endif
int T; scan(T);
rep(i, 0, T)
{
vi vec;
int n, x; scan(n);
while(n--) {
scan(x);
vec.pb(x);
}
ll ans = 0LL;
gao(vec, 0, ans);
ans = ans * 2LL % MOD;
printf("Case #%d: %lld\n", i + 1, ans);
}
#ifndef ONLINE_JUDGE
long _end_time = clock();
printf("time = %ld ms\n", _end_time - _begin_time);
#endif
return 0;
}
#include
#include
#include
#include
#include
#include
#include
using namespace std;
typedef long long ll;
const int MAXQ=10000000;
const int MAXL=14;
int vis[MAXQ],num[MAXL];
int change(int t)
{
int now=0;
while(t)
{
num[now++]=t%10;
t/=10;
}
return now;
}
unordered_mapint >mp[2][10];
void solve_right(ll n,ll &k)
{
for(int i=1;iint t=change(i),ok=1;
if(t==2)ok&=(num[0]!=num[1]);
for(int j=1;j1;j++)
ok&=((num[j]>num[j-1] && num[j]>num[j+1])
||(num[j]1] && num[j]1]));
if(!ok)continue;
vis[i]=1;
k-=(i%n==0);
if(k==0)
{
printf("%d",i);
exit(0);
}
if(t==6 && num[t-1]>num[t-2])mp[0][0][i%n]++;
else if(t==7)mp[num[t-1]>num[t-2]][num[t-1]][i%n]++;
}
}
void get_res(ll n,ll &k,ll Mod,int lef)
{
int len=7;
while(lef)
{
num[len++]=lef%10;
lef/=10;
}
for(int i=1;iif(!vis[i])continue;
int t=change(i),ok=1;
while(t<7)num[t++]=0;
t=len;
for(int j=1;j1;j++)
ok&=((num[j]>num[j-1] && num[j]>num[j+1])
||(num[j]1] && num[j]1]));
if(!ok)continue;
k-=(i%n==Mod);
if(k==0)
{
printf("%07d",i);
exit(0);
}
}
}
void solve_left(ll n,ll &k)
{
for(int i=1;iif(!vis[i])continue;
int t=change(i),ok=1;
if(t==2)ok&=(num[0]!=num[1]);
for(int j=1;j1;j++)
ok&=((num[j]>num[j-1] && num[j]>num[j+1])
||(num[j]1] && num[j]1]));
if(!ok)continue;
ll m=(n-1LL*i*MAXQ%n)%n,cnt=0;
if(t==1)
{
for(int j=0;j0];j++)
if(mp[0][j].find(m)!=mp[0][j].end())
cnt+=mp[0][j][m];
for(int j=num[0]+1;j<10;j++)
if(mp[1][j].find(m)!=mp[1][j].end())
cnt+=mp[1][j][m];
}
else
{
int go=(num[1]>num[0]);
if(go==0)for(int j=0;j0];j++)
if(mp[0][j].find(m)!=mp[0][j].end())
cnt+=mp[0][j][m];
if(go==1)for(int j=num[0]+1;j<10;j++)
if(mp[1][j].find(m)!=mp[1][j].end())
cnt+=mp[1][j][m];
}
if(k<=cnt)
{
printf("%d",i);
get_res(n,k,m,i);
}
else k-=cnt;
}
}
int main()
{
ll n,k;
scanf("%lld%lld",&n,&k);
solve_right(n,k);
solve_left(n,k);
return 0*printf("-1");
}
double l, r;
struct point {
double x, y, ang;
point() {}
point(double _x, double _y, double _ang) {
x = _x - l;
y = fabs(_y);
ang = _ang * pi / 180.0;
}
}p[maxn];
double dp[maxn];
int main() {
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
long _begin_time = clock();
#endif
int n; scan(n); scan(l, r);
r -= l;
rep(i, 0, n) {
double x, y, ang;
scan(x, y, ang);
p[i] = point(x, y, ang);
}
rep(i, 0, (1 << n)) {
rep(j, 0, n) {
if((i & (1 << j)) == 0) {
double tmp = atan((r - p[j].x) / p[j].y);
tmp = min(tmp, atan((dp[i] - p[j].x) / p[j].y) + p[j].ang);
dp[i | (1 << j)] = max(dp[i | (1 << j)], p[j].x + p[j].y * tan(tmp));
}
}
}
printf("%.9f\n", dp[(1 << n) - 1]);
#ifndef ONLINE_JUDGE
long _end_time = clock();
printf("time = %ld ms\n", _end_time - _begin_time);
#endif
return 0;
}
struct point {
int id;
ll x;
bool operator<(const point &b) const {
return x < b.x;
}
}p[maxn];
ll sum[maxn], dp[maxn];
int main() {
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
long _begin_time = clock();
#endif
int n, k; scan(n);
rep(i, 0, n) {
scan(p[i].x);
p[i].id = i;
}
sort(p, p + n);
sum[0] = p[0].x;
rep(i, 1, n) sum[i] = sum[i - 1] + p[i].x;
scan(k);
rep(i, 1, k) dp[i] = dp[i - 1] + p[i].x * i - sum[i - 1];
dp[0] = dp[k - 1];
ll ans = dp[0], l = 0;
rep(i, 1, n - k + 1) {
dp[i] = dp[i - 1] - (sum[i + k - 2] - sum[i - 1] - p[i - 1].x * (k - 1)) + (p[i + k - 1].x * (k - 1) - (sum[i + k - 2] - sum[i - 1]));
if(ans > dp[i]) {
ans = dp[i];
l = i;
}
}
rep(i, l, l + k) printf("%d%c", p[i].id + 1, i == l + k - 1 ? '\n' : ' ');
#ifndef ONLINE_JUDGE
long _end_time = clock();
printf("time = %ld ms\n", _end_time - _begin_time);
#endif
return 0;
}
int n, K;
struct Matrix{
double x[128][128];
}A,B,ans;
Matrix operator * (const Matrix &k1, const Matrix &k2) {
mem(B.x, 0);
rep(i, 0, n + 1)
rep(j, 0, n + 1)
rep(k, 0, n + 1)
B.x[i][j] += k1.x[i][k] * k2.x[k][j];
return B;
}
int main() {
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
long _begin_time = clock();
#endif
scan(K, n);
rep(i, 0, n + 1) {
double k1; scan(k1);
rep(j, 0, 128) A.x[j][i^j]=k1;
}
n = 127;
ans=A; K--;
while (K) {
if (K & 1) ans = ans * A; A = A * A; K >>= 1;
}
printf("%.11lf\n",1 - ans.x[0][0]);
#ifndef ONLINE_JUDGE
long _end_time = clock();
printf("time = %ld ms\n", _end_time - _begin_time);
#endif
return 0;
}
ll gao(ll n) { return n * (n - 1) / 2LL; }
char s[maxn];
int main() {
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
long _begin_time = clock();
#endif
scan(s);
int n = strlen(s);
ll oa = 0LL, ea = 0LL, ob = 0LL, eb = 0LL;
rep(i, 0, n) {
if(i & 1) {
if(s[i] == 'a') oa++;
else ob++;
} else {
if(s[i] == 'a') ea++;
else eb++;
}
}
dbg(oa * ea + ob * eb);
dbg(gao(oa) + gao(ob) + gao(ea) + gao(eb) + n);
#ifndef ONLINE_JUDGE
long _end_time = clock();
printf("time = %ld ms\n", _end_time - _begin_time);
#endif
return 0;
}
int cnt = 0, mnt = 0;
ll fac[maxn];
void gao(ll x, ll k) {
if(cnt > 99999) return;
if(x == 1LL || k == 0LL) { dbg(x); cnt++; return; }
rep(i, 0, mnt) {
if(x < fac[i]) break;
if(x % fac[i] == 0) gao(fac[i], k - 1);
}
}
int main() {
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
long _begin_time = clock();
#endif
ll x, k; scan(x, k);
for(ll i = 1LL; i * i <= x; i++) {
if(x % i == 0) {
fac[mnt++] = i;
fac[mnt++] = x / i;
}
}
mnt = unique(fac, fac + mnt) - fac;
sort(fac, fac + mnt);
gao(x, k);
#ifndef ONLINE_JUDGE
long _end_time = clock();
printf("time = %ld ms\n", _end_time - _begin_time);
#endif
return 0;
}
int a[maxn];
int gao(int l, int r, int minn) {
if(l > r) return 0;
if(l == r) return a[l] > minn;
int m = min_element(a + l, a + r + 1) - a;
return min(r - l + 1, gao(l, m - 1, a[m]) + gao(m + 1, r, a[m]) + a[m] - minn);
}
int main() {
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
long _begin_time = clock();
#endif
int n; scan(n);
rep(i, 1, n + 1) scan(a[i]);
dbg(gao(1, n, 0));
#ifndef ONLINE_JUDGE
long _end_time = clock();
printf("time = %ld ms\n", _end_time - _begin_time);
#endif
return 0;
}
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include
using namespace std;
#define fi first
#define se second
#define MP(A, B) make_pair(A, B)
#define pb push_back
#define gcd __gcd
#define foreach(it,a) for(__typeof((a).begin()) it=(a).begin();it!=(a).end();it++)
#define rep(i, a, b) for(int i = a; i < b; i++)
#define per(i, a, b) for(int i = a; i > b; i--)
typedef long long ll;
typedef unsigned long long ulls;
typedef unsigned int uint;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector vii;
typedef map<int, int> mii;
typedef map<string, int> msi;
typedef mapint > mpi;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fLL;
const ll MOD = 998244353;
const double pi = acos(-1.0);
const double eps = 1e-6;
const int maxn = 1e6 + 5;
const int maxm = 1e6 + 5;
const int dx[] = {-1, 0, 1, 0, -1, -1, 1, 1};
const int dy[] = {0, 1, 0, -1, 1, -1, 1, -1};
inline int scan(int &a) { return scanf("%d", &a); }
inline int scan(int &a, int &b) { return scanf("%d%d", &a, &b); }
inline int scan(int &a, int &b, int &c) { return scanf("%d%d%d", &a, &b, &c); }
inline int scan(ll &a) { return scanf("%I64d", &a); }
inline int scan(ll &a, ll &b) { return scanf("%I64d%I64d", &a, &b); }
inline int scan(ll &a, ll &b, ll &c) { return scanf("%I64d%I64d%I64d", &a, &b, &c); }
inline int scan(double &a) { return scanf("%lf", &a); }
inline int scan(double &a, double &b) { return scanf("%lf%lf", &a, &b); }
inline int scan(double &a, double &b, double &c) { return scanf("%lf%lf%lf", &a, &b, &c); }
inline int scan(char &a) { return scanf("%c", &a); }
inline int scan(char *a) { return scanf("%s", a); }
template<class T> inline void mem(T &A, int x) { memset(A, x, sizeof(A)); }
template<class T0, class T1> inline void mem(T0 &A0, T1 &A1, int x) { mem(A0, x), mem(A1, x); }
template<class T0, class T1, class T2> inline void mem(T0 &A0, T1 &A1, T2 &A2, int x) { mem(A0, x), mem(A1, x), mem(A2, x); }
template<class T0, class T1, class T2, class T3> inline void mem(T0 &A0, T1 &A1, T2 &A2, T3 &A3, int x) { mem(A0, x), mem(A1, x), mem(A2, x), mem(A3, x); }
template<class T0, class T1, class T2, class T3, class T4> inline void mem(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, int x) { mem(A0, x), mem(A1, x), mem(A2, x), mem(A3, x), mem(A4, x); }
template<class T0, class T1, class T2, class T3, class T4, class T5> inline void mem(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5, int x) { mem(A0, x), mem(A1, x), mem(A2, x), mem(A3, x), mem(A4, x), mem(A5, x); }
template<class T0, class T1, class T2, class T3, class T4, class T5, class T6> inline void mem(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5, T6 &A6, int x) { mem(A0, x), mem(A1, x), mem(A2, x), mem(A3, x), mem(A4, x), mem(A5, x), mem(A6, x); }
template<class T> inline T min(T a, T b, T c) { return min(min(a, b), c); }
template<class T> inline T max(T a, T b, T c) { return max(max(a, b), c); }
template<class T> inline T min(T a, T b, T c, T d) { return min(min(a, b), min(c, d)); }
template<class T> inline T max(T a, T b, T c, T d) { return max(max(a, b), max(c, d)); }
template<class T> inline T min(T a, T b, T c, T d, T e) { return min(min(min(a,b),min(c,d)),e); }
template<class T> inline T max(T a, T b, T c, T d, T e) { return max(max(max(a,b),max(c,d)),e); }
template<class T> inline void dbg(T a[], int n) { rep(i, 0, n) cout << a[i] << (i == n - 1 ? "\n" : " ");}
template<class T> inline void dbg(T a) { cout << a << " "; }
template<class T> inline void dbg(T a[][maxn], int n, int m) { rep(i, 0, n) rep(j, 0, m) cout << a[i][j] << (j == m - 1 ? "\n" : " "); }