1. 程式人生 > 其它 >Solution -「LOJ #150」挑戰多項式 ||「模板」多項式全家桶

Solution -「LOJ #150」挑戰多項式 ||「模板」多項式全家桶

B. Plus and Multiply

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

題意:

\(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 $ 的話,那麼\(1*2*\cdots*(x-1)\)

一定是\(i\) 的因數 那麼我們可以將\(lcm(1\cdots(x-1))\) 作為x的週期.

程式碼:

// 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;
}