Codeforces Round #729 (Div. 2)

简介: 【6月更文挑战第4天】在这两个编程问题中,B. Plus and Multiply 要求判断通过加法和乘法操作数组是否能形成目标数 `n`。思路是形如 `x^a + yb = n` 的表达式,如果满足则能构造。C. Strange Function 关注的是找到最小正整数 `x` 使得 `x` 不是 `i` 的因子,计算这些值的和模 `10^9+7`。对于每个 `i`,偶数时 `f(i)` 是 3,奇数时是 2,利用因子与最大公约数计算周期性求和。

B. Plus and Multiply

http://codeforces.com/contest/1542/problem/B

http://codeforces.com/contest/1542/problem/B

题意:

一开始数组中只有一个$1$,你可以往数组里加入$数组中的数+b$或者加入$数组中的数*a$.问你$n$ 会出现在数组中吗?

思路:

我们随机匹配一下,最终的样式就是$x^a+yb=n$ 如果满足这种格式就一定能够构成

代码:

// Problem: B. Plus and Multiply
// Contest: Codeforces - Codeforces Round #729 (Div. 2)
// URL: http://codeforces.com/contest/1542/problem/B
// Memory Limit: 512 MB
// Time Limit: 3000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef unsigned long long ull;

#define x first
#define y second
#define sf scanf
#define pf printf
#define PI acos(-1)
#define inf 0x3f3f3f3f
#define lowbit(x) ((-x)&x)
#define mem(a,x) memset(a,x,sizeof(a))
#define rep(i,n) for(int i=0;i<(n);++i)
#define repi(i,a,b) for(int i=int(a);i<=(b);++i)
#define repr(i,b,a) for(int i=int(b);i>=(a);--i)
#define debug(x) cout << #x << ": " << x << endl;

const int MOD = 998244353;
const int mod = 1e9 + 7;
const int maxn = 2e5 + 10;
const int dx[] = {0, 1, -1, 0, 0};
const int dy[] = {0, 0, 0, 1, -1};
const int dz[] = {1, -1, 0, 0, 0, 0 };
int day[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

ll n,x,y;
priority_queue < ll> q;
map<ll,ll> mp;
ll qpow(ll a, ll b){
  ll ans=1;
  while(b){
    if(b&1) ans=ans*a;
    a=a*a;
    b>>=1;
  }
  return ans;
}
void solve()
{
  cin>>n>>x>>y;
  if(x==1){
    if((n-1)%y==0){
      puts("Yes");
    }else puts("No");
    return ;
  }
  for(ll i=0;;i++){
    ll num=qpow(x,i);
    ll yu=n-num;
    if(yu<0) break;
    if(yu%y==0) {
      puts("Yes");
      return ;
    }

  }
  puts("No");
}
int main()
{
    ll t = 1;
    scanf("%lld",&t);
    while(t--)
    {
        solve();
    }
    return 0;
}

C. Strange Function

http://codeforces.com/contest/1542/problem/C

题意:

记 $f(i)$ 为最小的正整数数 $x$ 满足 $x$ 不是 $i$ 的因子。

现给出正整数 $n$,请计算出 $\sum_{i=1}^n f(i)$ 模$ 10^9+7$ 的值。上述式子等价于 $f(1)+f(2)+\cdots+f(n)$。

本题含多组数据。令数据组数为 $t$,那么有 $1 \leq t \leq 10^4,1 \leq n \leq 10^{16}$

思路:

如果$i$ 是奇数那么$f(i)$ 一定是2

如果$i$ 是偶数那么$f(i)$ 最小是3

然后我们会发现 $f(i)=x $ 的话,那么$12\cdots*(x-1)$ 一定是$i$ 的因数 那么我们可以将$lcm(1\cdots(x-1))$ 作为x的周期.

这就是桥面的利用了这个周期,也就是题中给的性质最小一个不是他的因子,那么$1,2,\cdots,(i-1)$ 一定就是这个都是这个数的因子 那么直接就是他们的lcm就是周期即可.

代码:

// Problem: C. Strange Function
// Contest: Codeforces - Codeforces Round #729 (Div. 2)
// URL: http://codeforces.com/contest/1542/problem/C
// Memory Limit: 256 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef unsigned long long ull;

#define x first
#define y second
#define sf scanf
#define pf printf
#define PI acos(-1)
#define inf 0x3f3f3f3f
#define lowbit(x) ((-x)&x)
#define mem(a,x) memset(a,x,sizeof(a))
#define rep(i,n) for(int i=0;i<(n);++i)
#define repi(i,a,b) for(int i=int(a);i<=(b);++i)
#define repr(i,b,a) for(int i=int(b);i>=(a);--i)
#define debug(x) cout << #x << ": " << x << endl;

const int MOD = 998244353;
const int mod = 1e9 + 7;
const int maxn = 2e5 + 10;
const int dx[] = {0, 1, -1, 0, 0};
const int dy[] = {0, 0, 0, 1, -1};
const int dz[] = {1, -1, 0, 0, 0, 0 };
int day[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

ll gcd(ll a,ll b){
  while(b){
    ll tmp=a%b;
    a=b;
    b=tmp;
  }
  return a;
}
ll a[50];
ll n;
void solve()
{
  cin>>n;
  ll ans=n*2%mod;
  ans=(ans+n/2)%mod;
  for(int i=4;a[i]<=n;i++){
    ans=(ans+n/a[i])%mod;
  }
  printf("%lld\n",ans);
}

int main()
{
  for(ll i=4,cnt=2;cnt<=1e16;i++){
    ll g=gcd(cnt,i-1);
    cnt = cnt/g*(i-1);
    a[i]=cnt;
  }
    ll t = 1;
    scanf("%lld",&t);
    while(t--)
    {
        solve();
    }
    return 0;
}
目录
相关文章
|
9月前
|
人工智能 算法 BI
Codeforces Round #179 (Div. 2)A、B、C、D
我们每次加进来的点相当于k,首先需要进行一个双重循环找到k点和所有点之间的最短路径;然后就以k点位判断节点更新之前的k-1个点,时间复杂度降到O(n^3),而暴力解法每次都要进行floyd,时间复杂度为O(n^4);相比之下前述解法考虑到了floyd算法的性质,更好了运用了算法的内质。
40 0
|
9月前
Codeforces Round #192 (Div. 2) (330A) A. Cakeminator
如果某一行没有草莓,就可以吃掉这一行,某一列没有也可以吃点这一列,求最多会被吃掉多少块蛋糕。
30 0
|
9月前
Codeforces Round #192 (Div. 2) (329A)C.Purification
Codeforces Round #192 (Div. 2) (329A)C.Purification
26 0
|
11月前
|
机器学习/深度学习 Go
codeforces round 885 (div. 2)
codeforces round 885 (div. 2)
70 0
|
人工智能 算法
|
人工智能 算法