U-Resistors in Parallel

U-Resistors in Parallel
In this physics problem, what we are concerned about are only resistors. If you are poor at physics, do not worry, since solving this problem does not require you to have advanced abilities in physics.

Resistors are said to be connected together in parallel when both of their terminals are respectively connected to each terminal of the other resistors.

We have the following parallel resistor equation for k resistors with resistances R1, R2, ..., Rk in parallel and their combined resistance R:

Now you have n resistors, the i-th of which has a resistance of ri ohms with the equation

You also have n selections, the i-th of which is a set of resistors Si such that

Please find a selection in which the resistors form a parallel resistor with the minimum resistance and output the reduced fraction  of its resistance.

Input

The input contains several test cases, and the first line contains a positive integer T indicating the number of test cases which is up to 100.

For each test case, the only one line contains an integer n, where 1 ≤ n ≤ 10100.

Output

For each test case, output a line containing a reduced fraction of the form p/q indicating the minimum possible resistance, where p and qshould be positive numbers that are coprime.

Example
input
 
3
10
100
1000
output
1/2
5/12
35/96

题目链接:http://codeforces.com/gym/102028/problem/E

题目解析:

 

找规律。

怎么找呢?(百度,反正我是找不出来。。。。)

我们先求连续质数数组【2,3,5,7,11.......】的前缀积【2,6,30,210,2310........】

1         1/1
2-5       2/3
6-29      6/12
30-209    30/72
210-2309  210/576

我们会发现分子就是刚刚的前缀积,分母则是对应的质数+1的前缀积。

求完前缀积后就简单了,由于此题的数据有10100,我们需要用到大数,作为一个苦逼的不怎么会用java,py的人,我用了c(其实这也是百度的,不要怀疑,我菜的真实)。

#include<bits/stdc++.h>
using namespace std;
#define ll long long 
 
bool flag[1005];//标记数组
int Ans_p[1005],tot;//素数表,总素数个数,注意(Ans_p[tot])内有素数
 
void eulgp(int n)//2~n 内的素数
{
    tot=0;                         //初始化
    memset(flag,-1,sizeof(flag));
    for(int i=2;i<=n;++i){
        if(flag[i]) Ans_p[++tot]=i;         //存入素数
        for(int j=1;(j<=tot)&&(i*Ans_p[j]<=n);++j){
            flag[i*Ans_p[j]]=0;
            if(i%Ans_p[j]==0)       //避免重复赋0及时跳出
                 break;
        }
    }
}
 
string div(string a,int b)
{
    string c;
    int len=a.length(),ans=0;
    char s;
    for(int i=0; i<len; i++){
        ans=ans*10+a[i]-'0';
        s=ans/b+'0';
        ans%=b;
        c+=s;
    }
    int pos=0;
    while(pos<len && c[pos]=='0') pos++;
    if(pos==len) return "0";
    return c.substr(pos);
}
 
string mul(string a,int b)
{
    string c;
    char s;
    int len=a.length(),ok=0;
    for(int i=len-1; i>=0; i--){
        int temp=(a[i]-'0')*b+ok;
        ok=temp/10;
        s=temp%10+'0';
        c=s+c;
    }
    while(ok){
        s=ok%10+'0';
        c=s+c;
        ok/=10;
    }
    return c;
}
 
int MOD(string str,int mod)
{
    int rem=0;
    for(int i=0; i<str.length(); i++){
        rem=rem*10+str[i]-'0';
        rem=rem%mod;
    }
    return rem;
}
 
string up[65],down[65];
 
int main()
{
    eulgp(1000);
    up[0] = down[0] = "1"; 
    for(int i=1;i<=60;i++){
        up[i] = mul(up[i-1],Ans_p[i]);
        down[i] = mul(down[i-1],Ans_p[i]+1);
    }
    int t;scanf("%d",&t);
    while(t--){
        string n;
        cin>>n;
        for(int i=1;;i++){
            if(up[i].size()>n.size()||(up[i].size()==n.size()&&up[i]>n)){
                string ansu=up[i-1],ansd=down[i-1];
                for(int j=1;j<=60;j++){
                    if(!MOD(ansu,Ans_p[j])&&!MOD(ansd,Ans_p[j])) {
                        ansu=div(ansu,Ans_p[j]);
                        ansd=div(ansd,Ans_p[j]);
                    }
                }
                cout<<ansu<<"/"<<ansd<<endl;
                break;
            }
        }
    }
    return 0;
}

这是我抄代码的博客,写的很好我也不想改了:https://blog.csdn.net/LeBron_Yang/article/details/102243990

 

 

posted @ 2019-12-05 17:54  北区以北  阅读(151)  评论(0)    收藏  举报